SPARC: Rename register maps from "*regset" to "*regmap"
[external/binutils.git] / gdb / sparc-tdep.c
1 /* Target-dependent code for SPARC.
2
3    Copyright (C) 2003-2014 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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.
11
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.
16
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/>.  */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include "dis-asm.h"
23 #include "dwarf2-frame.h"
24 #include "floatformat.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 "value.h"
37
38 #include "gdb_assert.h"
39 #include <string.h>
40
41 #include "sparc-tdep.h"
42 #include "sparc-ravenscar-thread.h"
43
44 struct regset;
45
46 /* This file implements the SPARC 32-bit ABI as defined by the section
47    "Low-Level System Information" of the SPARC Compliance Definition
48    (SCD) 2.4.1, which is the 32-bit System V psABI for SPARC.  The SCD
49    lists changes with respect to the original 32-bit psABI as defined
50    in the "System V ABI, SPARC Processor Supplement".
51
52    Note that if we talk about SunOS, we mean SunOS 4.x, which was
53    BSD-based, which is sometimes (retroactively?) referred to as
54    Solaris 1.x.  If we talk about Solaris we mean Solaris 2.x and
55    above (Solaris 7, 8 and 9 are nothing but Solaris 2.7, 2.8 and 2.9
56    suffering from severe version number inflation).  Solaris 2.x is
57    also known as SunOS 5.x, since that's what uname(1) says.  Solaris
58    2.x is SVR4-based.  */
59
60 /* Please use the sparc32_-prefix for 32-bit specific code, the
61    sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
62    code that can handle both.  The 64-bit specific code lives in
63    sparc64-tdep.c; don't add any here.  */
64
65 /* The SPARC Floating-Point Quad-Precision format is similar to
66    big-endian IA-64 Quad-Precision format.  */
67 #define floatformats_sparc_quad floatformats_ia64_quad
68
69 /* The stack pointer is offset from the stack frame by a BIAS of 2047
70    (0x7ff) for 64-bit code.  BIAS is likely to be defined on SPARC
71    hosts, so undefine it first.  */
72 #undef BIAS
73 #define BIAS 2047
74
75 /* Macros to extract fields from SPARC instructions.  */
76 #define X_OP(i) (((i) >> 30) & 0x3)
77 #define X_RD(i) (((i) >> 25) & 0x1f)
78 #define X_A(i) (((i) >> 29) & 1)
79 #define X_COND(i) (((i) >> 25) & 0xf)
80 #define X_OP2(i) (((i) >> 22) & 0x7)
81 #define X_IMM22(i) ((i) & 0x3fffff)
82 #define X_OP3(i) (((i) >> 19) & 0x3f)
83 #define X_RS1(i) (((i) >> 14) & 0x1f)
84 #define X_RS2(i) ((i) & 0x1f)
85 #define X_I(i) (((i) >> 13) & 1)
86 /* Sign extension macros.  */
87 #define X_DISP22(i) ((X_IMM22 (i) ^ 0x200000) - 0x200000)
88 #define X_DISP19(i) ((((i) & 0x7ffff) ^ 0x40000) - 0x40000)
89 #define X_DISP10(i) ((((((i) >> 11) && 0x300) | (((i) >> 5) & 0xff)) ^ 0x200) - 0x200)
90 #define X_SIMM13(i) ((((i) & 0x1fff) ^ 0x1000) - 0x1000)
91 /* Macros to identify some instructions.  */
92 /* RETURN (RETT in V8) */
93 #define X_RETTURN(i) ((X_OP (i) == 0x2) && (X_OP3 (i) == 0x39))
94
95 /* Fetch the instruction at PC.  Instructions are always big-endian
96    even if the processor operates in little-endian mode.  */
97
98 unsigned long
99 sparc_fetch_instruction (CORE_ADDR pc)
100 {
101   gdb_byte buf[4];
102   unsigned long insn;
103   int i;
104
105   /* If we can't read the instruction at PC, return zero.  */
106   if (target_read_memory (pc, buf, sizeof (buf)))
107     return 0;
108
109   insn = 0;
110   for (i = 0; i < sizeof (buf); i++)
111     insn = (insn << 8) | buf[i];
112   return insn;
113 }
114 \f
115
116 /* Return non-zero if the instruction corresponding to PC is an "unimp"
117    instruction.  */
118
119 static int
120 sparc_is_unimp_insn (CORE_ADDR pc)
121 {
122   const unsigned long insn = sparc_fetch_instruction (pc);
123   
124   return ((insn & 0xc1c00000) == 0);
125 }
126
127 /* Return non-zero if the instruction corresponding to PC is an
128    "annulled" branch, i.e. the annul bit is set.  */
129
130 int
131 sparc_is_annulled_branch_insn (CORE_ADDR pc)
132 {
133   /* The branch instructions featuring an annul bit can be identified
134      by the following bit patterns:
135
136      OP=0
137       OP2=1: Branch on Integer Condition Codes with Prediction (BPcc).
138       OP2=2: Branch on Integer Condition Codes (Bcc).
139       OP2=5: Branch on FP Condition Codes with Prediction (FBfcc).
140       OP2=6: Branch on FP Condition Codes (FBcc).
141       OP2=3 && Bit28=0:
142              Branch on Integer Register with Prediction (BPr).
143
144      This leaves out ILLTRAP (OP2=0), SETHI/NOP (OP2=4) and the V8
145      coprocessor branch instructions (Op2=7).  */
146
147   const unsigned long insn = sparc_fetch_instruction (pc);
148   const unsigned op2 = X_OP2 (insn);
149
150   if ((X_OP (insn) == 0)
151       && ((op2 == 1) || (op2 == 2) || (op2 == 5) || (op2 == 6)
152           || ((op2 == 3) && ((insn & 0x10000000) == 0))))
153     return X_A (insn);
154   else
155     return 0;
156 }
157
158 /* OpenBSD/sparc includes StackGhost, which according to the author's
159    website http://stackghost.cerias.purdue.edu "... transparently and
160    automatically protects applications' stack frames; more
161    specifically, it guards the return pointers.  The protection
162    mechanisms require no application source or binary modification and
163    imposes only a negligible performance penalty."
164
165    The same website provides the following description of how
166    StackGhost works:
167
168    "StackGhost interfaces with the kernel trap handler that would
169    normally write out registers to the stack and the handler that
170    would read them back in.  By XORing a cookie into the
171    return-address saved in the user stack when it is actually written
172    to the stack, and then XOR it out when the return-address is pulled
173    from the stack, StackGhost can cause attacker corrupted return
174    pointers to behave in a manner the attacker cannot predict.
175    StackGhost can also use several unused bits in the return pointer
176    to detect a smashed return pointer and abort the process."
177
178    For GDB this means that whenever we're reading %i7 from a stack
179    frame's window save area, we'll have to XOR the cookie.
180
181    More information on StackGuard can be found on in:
182
183    Mike Frantzen and Mike Shuey.  "StackGhost: Hardware Facilitated
184    Stack Protection."  2001.  Published in USENIX Security Symposium
185    '01.  */
186
187 /* Fetch StackGhost Per-Process XOR cookie.  */
188
189 ULONGEST
190 sparc_fetch_wcookie (struct gdbarch *gdbarch)
191 {
192   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
193   struct target_ops *ops = &current_target;
194   gdb_byte buf[8];
195   int len;
196
197   len = target_read (ops, TARGET_OBJECT_WCOOKIE, NULL, buf, 0, 8);
198   if (len == -1)
199     return 0;
200
201   /* We should have either an 32-bit or an 64-bit cookie.  */
202   gdb_assert (len == 4 || len == 8);
203
204   return extract_unsigned_integer (buf, len, byte_order);
205 }
206 \f
207
208 /* The functions on this page are intended to be used to classify
209    function arguments.  */
210
211 /* Check whether TYPE is "Integral or Pointer".  */
212
213 static int
214 sparc_integral_or_pointer_p (const struct type *type)
215 {
216   int len = TYPE_LENGTH (type);
217
218   switch (TYPE_CODE (type))
219     {
220     case TYPE_CODE_INT:
221     case TYPE_CODE_BOOL:
222     case TYPE_CODE_CHAR:
223     case TYPE_CODE_ENUM:
224     case TYPE_CODE_RANGE:
225       /* We have byte, half-word, word and extended-word/doubleword
226          integral types.  The doubleword is an extension to the
227          original 32-bit ABI by the SCD 2.4.x.  */
228       return (len == 1 || len == 2 || len == 4 || len == 8);
229     case TYPE_CODE_PTR:
230     case TYPE_CODE_REF:
231       /* Allow either 32-bit or 64-bit pointers.  */
232       return (len == 4 || len == 8);
233     default:
234       break;
235     }
236
237   return 0;
238 }
239
240 /* Check whether TYPE is "Floating".  */
241
242 static int
243 sparc_floating_p (const struct type *type)
244 {
245   switch (TYPE_CODE (type))
246     {
247     case TYPE_CODE_FLT:
248       {
249         int len = TYPE_LENGTH (type);
250         return (len == 4 || len == 8 || len == 16);
251       }
252     default:
253       break;
254     }
255
256   return 0;
257 }
258
259 /* Check whether TYPE is "Complex Floating".  */
260
261 static int
262 sparc_complex_floating_p (const struct type *type)
263 {
264   switch (TYPE_CODE (type))
265     {
266     case TYPE_CODE_COMPLEX:
267       {
268         int len = TYPE_LENGTH (type);
269         return (len == 8 || len == 16 || len == 32);
270       }
271     default:
272       break;
273     }
274
275   return 0;
276 }
277
278 /* Check whether TYPE is "Structure or Union".
279
280    In terms of Ada subprogram calls, arrays are treated the same as
281    struct and union types.  So this function also returns non-zero
282    for array types.  */
283
284 static int
285 sparc_structure_or_union_p (const struct type *type)
286 {
287   switch (TYPE_CODE (type))
288     {
289     case TYPE_CODE_STRUCT:
290     case TYPE_CODE_UNION:
291     case TYPE_CODE_ARRAY:
292       return 1;
293     default:
294       break;
295     }
296
297   return 0;
298 }
299
300 /* Register information.  */
301
302 static const char *sparc32_register_names[] =
303 {
304   "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
305   "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
306   "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
307   "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",
308
309   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
310   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
311   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
312   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
313
314   "y", "psr", "wim", "tbr", "pc", "npc", "fsr", "csr"
315 };
316
317 /* Total number of registers.  */
318 #define SPARC32_NUM_REGS ARRAY_SIZE (sparc32_register_names)
319
320 /* We provide the aliases %d0..%d30 for the floating registers as
321    "psuedo" registers.  */
322
323 static const char *sparc32_pseudo_register_names[] =
324 {
325   "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
326   "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30"
327 };
328
329 /* Total number of pseudo registers.  */
330 #define SPARC32_NUM_PSEUDO_REGS ARRAY_SIZE (sparc32_pseudo_register_names)
331
332 /* Return the name of register REGNUM.  */
333
334 static const char *
335 sparc32_register_name (struct gdbarch *gdbarch, int regnum)
336 {
337   if (regnum >= 0 && regnum < SPARC32_NUM_REGS)
338     return sparc32_register_names[regnum];
339
340   if (regnum < SPARC32_NUM_REGS + SPARC32_NUM_PSEUDO_REGS)
341     return sparc32_pseudo_register_names[regnum - SPARC32_NUM_REGS];
342
343   return NULL;
344 }
345 \f
346 /* Construct types for ISA-specific registers.  */
347
348 static struct type *
349 sparc_psr_type (struct gdbarch *gdbarch)
350 {
351   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
352
353   if (!tdep->sparc_psr_type)
354     {
355       struct type *type;
356
357       type = arch_flags_type (gdbarch, "builtin_type_sparc_psr", 4);
358       append_flags_type_flag (type, 5, "ET");
359       append_flags_type_flag (type, 6, "PS");
360       append_flags_type_flag (type, 7, "S");
361       append_flags_type_flag (type, 12, "EF");
362       append_flags_type_flag (type, 13, "EC");
363
364       tdep->sparc_psr_type = type;
365     }
366
367   return tdep->sparc_psr_type;
368 }
369
370 static struct type *
371 sparc_fsr_type (struct gdbarch *gdbarch)
372 {
373   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
374
375   if (!tdep->sparc_fsr_type)
376     {
377       struct type *type;
378
379       type = arch_flags_type (gdbarch, "builtin_type_sparc_fsr", 4);
380       append_flags_type_flag (type, 0, "NXA");
381       append_flags_type_flag (type, 1, "DZA");
382       append_flags_type_flag (type, 2, "UFA");
383       append_flags_type_flag (type, 3, "OFA");
384       append_flags_type_flag (type, 4, "NVA");
385       append_flags_type_flag (type, 5, "NXC");
386       append_flags_type_flag (type, 6, "DZC");
387       append_flags_type_flag (type, 7, "UFC");
388       append_flags_type_flag (type, 8, "OFC");
389       append_flags_type_flag (type, 9, "NVC");
390       append_flags_type_flag (type, 22, "NS");
391       append_flags_type_flag (type, 23, "NXM");
392       append_flags_type_flag (type, 24, "DZM");
393       append_flags_type_flag (type, 25, "UFM");
394       append_flags_type_flag (type, 26, "OFM");
395       append_flags_type_flag (type, 27, "NVM");
396
397       tdep->sparc_fsr_type = type;
398     }
399
400   return tdep->sparc_fsr_type;
401 }
402
403 /* Return the GDB type object for the "standard" data type of data in
404    register REGNUM.  */
405
406 static struct type *
407 sparc32_register_type (struct gdbarch *gdbarch, int regnum)
408 {
409   if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
410     return builtin_type (gdbarch)->builtin_float;
411
412   if (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM)
413     return builtin_type (gdbarch)->builtin_double;
414
415   if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
416     return builtin_type (gdbarch)->builtin_data_ptr;
417
418   if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
419     return builtin_type (gdbarch)->builtin_func_ptr;
420
421   if (regnum == SPARC32_PSR_REGNUM)
422     return sparc_psr_type (gdbarch);
423
424   if (regnum == SPARC32_FSR_REGNUM)
425     return sparc_fsr_type (gdbarch);
426
427   return builtin_type (gdbarch)->builtin_int32;
428 }
429
430 static enum register_status
431 sparc32_pseudo_register_read (struct gdbarch *gdbarch,
432                               struct regcache *regcache,
433                               int regnum, gdb_byte *buf)
434 {
435   enum register_status status;
436
437   gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
438
439   regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
440   status = regcache_raw_read (regcache, regnum, buf);
441   if (status == REG_VALID)
442     status = regcache_raw_read (regcache, regnum + 1, buf + 4);
443   return status;
444 }
445
446 static void
447 sparc32_pseudo_register_write (struct gdbarch *gdbarch,
448                                struct regcache *regcache,
449                                int regnum, const gdb_byte *buf)
450 {
451   gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
452
453   regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
454   regcache_raw_write (regcache, regnum, buf);
455   regcache_raw_write (regcache, regnum + 1, buf + 4);
456 }
457 \f
458 /* Implement "in_function_epilogue_p".  */
459
460 int
461 sparc_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
462 {
463   /* This function must return true if we are one instruction after an
464      instruction that destroyed the stack frame of the current
465      function.  The SPARC instructions used to restore the callers
466      stack frame are RESTORE and RETURN/RETT.
467
468      Of these RETURN/RETT is a branch instruction and thus we return
469      true if we are in its delay slot.
470
471      RESTORE is almost always found in the delay slot of a branch
472      instruction that transfers control to the caller, such as JMPL.
473      Thus the next instruction is in the caller frame and we don't
474      need to do anything about it.  */
475
476   unsigned int insn = sparc_fetch_instruction (pc - 4);
477
478   return X_RETTURN (insn);
479 }
480 \f
481
482 static CORE_ADDR
483 sparc32_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
484 {
485   /* The ABI requires double-word alignment.  */
486   return address & ~0x7;
487 }
488
489 static CORE_ADDR
490 sparc32_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
491                          CORE_ADDR funcaddr,
492                          struct value **args, int nargs,
493                          struct type *value_type,
494                          CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
495                          struct regcache *regcache)
496 {
497   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
498
499   *bp_addr = sp - 4;
500   *real_pc = funcaddr;
501
502   if (using_struct_return (gdbarch, NULL, value_type))
503     {
504       gdb_byte buf[4];
505
506       /* This is an UNIMP instruction.  */
507       store_unsigned_integer (buf, 4, byte_order,
508                               TYPE_LENGTH (value_type) & 0x1fff);
509       write_memory (sp - 8, buf, 4);
510       return sp - 8;
511     }
512
513   return sp - 4;
514 }
515
516 static CORE_ADDR
517 sparc32_store_arguments (struct regcache *regcache, int nargs,
518                          struct value **args, CORE_ADDR sp,
519                          int struct_return, CORE_ADDR struct_addr)
520 {
521   struct gdbarch *gdbarch = get_regcache_arch (regcache);
522   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
523   /* Number of words in the "parameter array".  */
524   int num_elements = 0;
525   int element = 0;
526   int i;
527
528   for (i = 0; i < nargs; i++)
529     {
530       struct type *type = value_type (args[i]);
531       int len = TYPE_LENGTH (type);
532
533       if (sparc_structure_or_union_p (type)
534           || (sparc_floating_p (type) && len == 16)
535           || sparc_complex_floating_p (type))
536         {
537           /* Structure, Union and Quad-Precision Arguments.  */
538           sp -= len;
539
540           /* Use doubleword alignment for these values.  That's always
541              correct, and wasting a few bytes shouldn't be a problem.  */
542           sp &= ~0x7;
543
544           write_memory (sp, value_contents (args[i]), len);
545           args[i] = value_from_pointer (lookup_pointer_type (type), sp);
546           num_elements++;
547         }
548       else if (sparc_floating_p (type))
549         {
550           /* Floating arguments.  */
551           gdb_assert (len == 4 || len == 8);
552           num_elements += (len / 4);
553         }
554       else
555         {
556           /* Integral and pointer arguments.  */
557           gdb_assert (sparc_integral_or_pointer_p (type));
558
559           if (len < 4)
560             args[i] = value_cast (builtin_type (gdbarch)->builtin_int32,
561                                   args[i]);
562           num_elements += ((len + 3) / 4);
563         }
564     }
565
566   /* Always allocate at least six words.  */
567   sp -= max (6, num_elements) * 4;
568
569   /* The psABI says that "Software convention requires space for the
570      struct/union return value pointer, even if the word is unused."  */
571   sp -= 4;
572
573   /* The psABI says that "Although software convention and the
574      operating system require every stack frame to be doubleword
575      aligned."  */
576   sp &= ~0x7;
577
578   for (i = 0; i < nargs; i++)
579     {
580       const bfd_byte *valbuf = value_contents (args[i]);
581       struct type *type = value_type (args[i]);
582       int len = TYPE_LENGTH (type);
583
584       gdb_assert (len == 4 || len == 8);
585
586       if (element < 6)
587         {
588           int regnum = SPARC_O0_REGNUM + element;
589
590           regcache_cooked_write (regcache, regnum, valbuf);
591           if (len > 4 && element < 5)
592             regcache_cooked_write (regcache, regnum + 1, valbuf + 4);
593         }
594
595       /* Always store the argument in memory.  */
596       write_memory (sp + 4 + element * 4, valbuf, len);
597       element += len / 4;
598     }
599
600   gdb_assert (element == num_elements);
601
602   if (struct_return)
603     {
604       gdb_byte buf[4];
605
606       store_unsigned_integer (buf, 4, byte_order, struct_addr);
607       write_memory (sp, buf, 4);
608     }
609
610   return sp;
611 }
612
613 static CORE_ADDR
614 sparc32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
615                          struct regcache *regcache, CORE_ADDR bp_addr,
616                          int nargs, struct value **args, CORE_ADDR sp,
617                          int struct_return, CORE_ADDR struct_addr)
618 {
619   CORE_ADDR call_pc = (struct_return ? (bp_addr - 12) : (bp_addr - 8));
620
621   /* Set return address.  */
622   regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, call_pc);
623
624   /* Set up function arguments.  */
625   sp = sparc32_store_arguments (regcache, nargs, args, sp,
626                                 struct_return, struct_addr);
627
628   /* Allocate the 16-word window save area.  */
629   sp -= 16 * 4;
630
631   /* Stack should be doubleword aligned at this point.  */
632   gdb_assert (sp % 8 == 0);
633
634   /* Finally, update the stack pointer.  */
635   regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
636
637   return sp;
638 }
639 \f
640
641 /* Use the program counter to determine the contents and size of a
642    breakpoint instruction.  Return a pointer to a string of bytes that
643    encode a breakpoint instruction, store the length of the string in
644    *LEN and optionally adjust *PC to point to the correct memory
645    location for inserting the breakpoint.  */
646    
647 static const gdb_byte *
648 sparc_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
649 {
650   static const gdb_byte break_insn[] = { 0x91, 0xd0, 0x20, 0x01 };
651
652   *len = sizeof (break_insn);
653   return break_insn;
654 }
655 \f
656
657 /* Allocate and initialize a frame cache.  */
658
659 static struct sparc_frame_cache *
660 sparc_alloc_frame_cache (void)
661 {
662   struct sparc_frame_cache *cache;
663
664   cache = FRAME_OBSTACK_ZALLOC (struct sparc_frame_cache);
665
666   /* Base address.  */
667   cache->base = 0;
668   cache->pc = 0;
669
670   /* Frameless until proven otherwise.  */
671   cache->frameless_p = 1;
672   cache->frame_offset = 0;
673   cache->saved_regs_mask = 0;
674   cache->copied_regs_mask = 0;
675   cache->struct_return_p = 0;
676
677   return cache;
678 }
679
680 /* GCC generates several well-known sequences of instructions at the begining
681    of each function prologue when compiling with -fstack-check.  If one of
682    such sequences starts at START_PC, then return the address of the
683    instruction immediately past this sequence.  Otherwise, return START_PC.  */
684    
685 static CORE_ADDR
686 sparc_skip_stack_check (const CORE_ADDR start_pc)
687 {
688   CORE_ADDR pc = start_pc;
689   unsigned long insn;
690   int offset_stack_checking_sequence = 0;
691   int probing_loop = 0;
692
693   /* With GCC, all stack checking sequences begin with the same two
694      instructions, plus an optional one in the case of a probing loop:
695
696          sethi <some immediate>, %g1
697          sub %sp, %g1, %g1
698
699      or:
700
701          sethi <some immediate>, %g1
702          sethi <some immediate>, %g4
703          sub %sp, %g1, %g1
704
705      or:
706
707          sethi <some immediate>, %g1
708          sub %sp, %g1, %g1
709          sethi <some immediate>, %g4
710
711      If the optional instruction is found (setting g4), assume that a
712      probing loop will follow.  */
713
714   /* sethi <some immediate>, %g1 */
715   insn = sparc_fetch_instruction (pc);
716   pc = pc + 4;
717   if (!(X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 1))
718     return start_pc;
719
720   /* optional: sethi <some immediate>, %g4 */
721   insn = sparc_fetch_instruction (pc);
722   pc = pc + 4;
723   if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
724     {
725       probing_loop = 1;
726       insn = sparc_fetch_instruction (pc);
727       pc = pc + 4;
728     }
729
730   /* sub %sp, %g1, %g1 */
731   if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
732         && X_RD (insn) == 1 && X_RS1 (insn) == 14 && X_RS2 (insn) == 1))
733     return start_pc;
734
735   insn = sparc_fetch_instruction (pc);
736   pc = pc + 4;
737
738   /* optional: sethi <some immediate>, %g4 */
739   if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
740     {
741       probing_loop = 1;
742       insn = sparc_fetch_instruction (pc);
743       pc = pc + 4;
744     }
745
746   /* First possible sequence:
747          [first two instructions above]
748          clr [%g1 - some immediate]  */
749
750   /* clr [%g1 - some immediate]  */
751   if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
752       && X_RS1 (insn) == 1 && X_RD (insn) == 0)
753     {
754       /* Valid stack-check sequence, return the new PC.  */
755       return pc;
756     }
757
758   /* Second possible sequence: A small number of probes.
759          [first two instructions above]
760          clr [%g1]
761          add   %g1, -<some immediate>, %g1
762          clr [%g1]
763          [repeat the two instructions above any (small) number of times]
764          clr [%g1 - some immediate]  */
765
766   /* clr [%g1] */
767   else if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
768       && X_RS1 (insn) == 1 && X_RD (insn) == 0)
769     {
770       while (1)
771         {
772           /* add %g1, -<some immediate>, %g1 */
773           insn = sparc_fetch_instruction (pc);
774           pc = pc + 4;
775           if (!(X_OP (insn) == 2  && X_OP3(insn) == 0 && X_I(insn)
776                 && X_RS1 (insn) == 1 && X_RD (insn) == 1))
777             break;
778
779           /* clr [%g1] */
780           insn = sparc_fetch_instruction (pc);
781           pc = pc + 4;
782           if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
783                 && X_RD (insn) == 0 && X_RS1 (insn) == 1))
784             return start_pc;
785         }
786
787       /* clr [%g1 - some immediate] */
788       if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
789             && X_RS1 (insn) == 1 && X_RD (insn) == 0))
790         return start_pc;
791
792       /* We found a valid stack-check sequence, return the new PC.  */
793       return pc;
794     }
795   
796   /* Third sequence: A probing loop.
797          [first three instructions above]
798          sub  %g1, %g4, %g4
799          cmp  %g1, %g4
800          be  <disp>
801          add  %g1, -<some immediate>, %g1
802          ba  <disp>
803          clr  [%g1]
804
805      And an optional last probe for the remainder:
806
807          clr [%g4 - some immediate]  */
808
809   if (probing_loop)
810     {
811       /* sub  %g1, %g4, %g4 */
812       if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
813             && X_RD (insn) == 4 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
814         return start_pc;
815
816       /* cmp  %g1, %g4 */
817       insn = sparc_fetch_instruction (pc);
818       pc = pc + 4;
819       if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x14 && !X_I(insn)
820             && X_RD (insn) == 0 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
821         return start_pc;
822
823       /* be  <disp> */
824       insn = sparc_fetch_instruction (pc);
825       pc = pc + 4;
826       if (!(X_OP (insn) == 0 && X_COND (insn) == 0x1))
827         return start_pc;
828
829       /* add  %g1, -<some immediate>, %g1 */
830       insn = sparc_fetch_instruction (pc);
831       pc = pc + 4;
832       if (!(X_OP (insn) == 2  && X_OP3(insn) == 0 && X_I(insn)
833             && X_RS1 (insn) == 1 && X_RD (insn) == 1))
834         return start_pc;
835
836       /* ba  <disp> */
837       insn = sparc_fetch_instruction (pc);
838       pc = pc + 4;
839       if (!(X_OP (insn) == 0 && X_COND (insn) == 0x8))
840         return start_pc;
841
842       /* clr  [%g1] (st %g0, [%g1] or st %g0, [%g1+0]) */
843       insn = sparc_fetch_instruction (pc);
844       pc = pc + 4;
845       if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4
846             && X_RD (insn) == 0 && X_RS1 (insn) == 1
847             && (!X_I(insn) || X_SIMM13 (insn) == 0)))
848         return start_pc;
849
850       /* We found a valid stack-check sequence, return the new PC.  */
851
852       /* optional: clr [%g4 - some immediate]  */
853       insn = sparc_fetch_instruction (pc);
854       pc = pc + 4;
855       if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
856             && X_RS1 (insn) == 4 && X_RD (insn) == 0))
857         return pc - 4;
858       else
859         return pc;
860     }
861
862   /* No stack check code in our prologue, return the start_pc.  */
863   return start_pc;
864 }
865
866 /* Record the effect of a SAVE instruction on CACHE.  */
867
868 void
869 sparc_record_save_insn (struct sparc_frame_cache *cache)
870 {
871   /* The frame is set up.  */
872   cache->frameless_p = 0;
873
874   /* The frame pointer contains the CFA.  */
875   cache->frame_offset = 0;
876
877   /* The `local' and `in' registers are all saved.  */
878   cache->saved_regs_mask = 0xffff;
879
880   /* The `out' registers are all renamed.  */
881   cache->copied_regs_mask = 0xff;
882 }
883
884 /* Do a full analysis of the prologue at PC and update CACHE accordingly.
885    Bail out early if CURRENT_PC is reached.  Return the address where
886    the analysis stopped.
887
888    We handle both the traditional register window model and the single
889    register window (aka flat) model.  */
890
891 CORE_ADDR
892 sparc_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
893                         CORE_ADDR current_pc, struct sparc_frame_cache *cache)
894 {
895   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
896   unsigned long insn;
897   int offset = 0;
898   int dest = -1;
899
900   pc = sparc_skip_stack_check (pc);
901
902   if (current_pc <= pc)
903     return current_pc;
904
905   /* We have to handle to "Procedure Linkage Table" (PLT) special.  On
906      SPARC the linker usually defines a symbol (typically
907      _PROCEDURE_LINKAGE_TABLE_) at the start of the .plt section.
908      This symbol makes us end up here with PC pointing at the start of
909      the PLT and CURRENT_PC probably pointing at a PLT entry.  If we
910      would do our normal prologue analysis, we would probably conclude
911      that we've got a frame when in reality we don't, since the
912      dynamic linker patches up the first PLT with some code that
913      starts with a SAVE instruction.  Patch up PC such that it points
914      at the start of our PLT entry.  */
915   if (tdep->plt_entry_size > 0 && in_plt_section (current_pc))
916     pc = current_pc - ((current_pc - pc) % tdep->plt_entry_size);
917
918   insn = sparc_fetch_instruction (pc);
919
920   /* Recognize store insns and record their sources.  */
921   while (X_OP (insn) == 3
922          && (X_OP3 (insn) == 0x4     /* stw */
923              || X_OP3 (insn) == 0x7  /* std */
924              || X_OP3 (insn) == 0xe) /* stx */
925          && X_RS1 (insn) == SPARC_SP_REGNUM)
926     {
927       int regnum = X_RD (insn);
928
929       /* Recognize stores into the corresponding stack slots.  */
930       if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
931           && ((X_I (insn)
932                && X_SIMM13 (insn) == (X_OP3 (insn) == 0xe
933                                       ? (regnum - SPARC_L0_REGNUM) * 8 + BIAS
934                                       : (regnum - SPARC_L0_REGNUM) * 4))
935               || (!X_I (insn) && regnum == SPARC_L0_REGNUM)))
936         {
937           cache->saved_regs_mask |= (1 << (regnum - SPARC_L0_REGNUM));
938           if (X_OP3 (insn) == 0x7)
939             cache->saved_regs_mask |= (1 << (regnum + 1 - SPARC_L0_REGNUM));
940         }
941
942       offset += 4;
943
944       insn = sparc_fetch_instruction (pc + offset);
945     }
946
947   /* Recognize a SETHI insn and record its destination.  */
948   if (X_OP (insn) == 0 && X_OP2 (insn) == 0x04)
949     {
950       dest = X_RD (insn);
951       offset += 4;
952
953       insn = sparc_fetch_instruction (pc + offset);
954     }
955
956   /* Allow for an arithmetic operation on DEST or %g1.  */
957   if (X_OP (insn) == 2 && X_I (insn)
958       && (X_RD (insn) == 1 || X_RD (insn) == dest))
959     {
960       offset += 4;
961
962       insn = sparc_fetch_instruction (pc + offset);
963     }
964
965   /* Check for the SAVE instruction that sets up the frame.  */
966   if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3c)
967     {
968       sparc_record_save_insn (cache);
969       offset += 4;
970       return pc + offset;
971     }
972
973   /* Check for an arithmetic operation on %sp.  */
974   if (X_OP (insn) == 2
975       && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
976       && X_RS1 (insn) == SPARC_SP_REGNUM
977       && X_RD (insn) == SPARC_SP_REGNUM)
978     {
979       if (X_I (insn))
980         {
981           cache->frame_offset = X_SIMM13 (insn);
982           if (X_OP3 (insn) == 0)
983             cache->frame_offset = -cache->frame_offset;
984         }
985       offset += 4;
986
987       insn = sparc_fetch_instruction (pc + offset);
988
989       /* Check for an arithmetic operation that sets up the frame.  */
990       if (X_OP (insn) == 2
991           && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
992           && X_RS1 (insn) == SPARC_SP_REGNUM
993           && X_RD (insn) == SPARC_FP_REGNUM)
994         {
995           cache->frameless_p = 0;
996           cache->frame_offset = 0;
997           /* We could check that the amount subtracted to %sp above is the
998              same as the one added here, but this seems superfluous.  */
999           cache->copied_regs_mask |= 0x40;
1000           offset += 4;
1001
1002           insn = sparc_fetch_instruction (pc + offset);
1003         }
1004
1005       /* Check for a move (or) operation that copies the return register.  */
1006       if (X_OP (insn) == 2
1007           && X_OP3 (insn) == 0x2
1008           && !X_I (insn)
1009           && X_RS1 (insn) == SPARC_G0_REGNUM
1010           && X_RS2 (insn) == SPARC_O7_REGNUM
1011           && X_RD (insn) == SPARC_I7_REGNUM)
1012         {
1013            cache->copied_regs_mask |= 0x80;
1014            offset += 4;
1015         }
1016
1017       return pc + offset;
1018     }
1019
1020   return pc;
1021 }
1022
1023 static CORE_ADDR
1024 sparc_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
1025 {
1026   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1027   return frame_unwind_register_unsigned (this_frame, tdep->pc_regnum);
1028 }
1029
1030 /* Return PC of first real instruction of the function starting at
1031    START_PC.  */
1032
1033 static CORE_ADDR
1034 sparc32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
1035 {
1036   struct symtab_and_line sal;
1037   CORE_ADDR func_start, func_end;
1038   struct sparc_frame_cache cache;
1039
1040   /* This is the preferred method, find the end of the prologue by
1041      using the debugging information.  */
1042   if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
1043     {
1044       sal = find_pc_line (func_start, 0);
1045
1046       if (sal.end < func_end
1047           && start_pc <= sal.end)
1048         return sal.end;
1049     }
1050
1051   start_pc = sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffUL, &cache);
1052
1053   /* The psABI says that "Although the first 6 words of arguments
1054      reside in registers, the standard stack frame reserves space for
1055      them.".  It also suggests that a function may use that space to
1056      "write incoming arguments 0 to 5" into that space, and that's
1057      indeed what GCC seems to be doing.  In that case GCC will
1058      generate debug information that points to the stack slots instead
1059      of the registers, so we should consider the instructions that
1060      write out these incoming arguments onto the stack.  */
1061
1062   while (1)
1063     {
1064       unsigned long insn = sparc_fetch_instruction (start_pc);
1065
1066       /* Recognize instructions that store incoming arguments into the
1067          corresponding stack slots.  */
1068       if (X_OP (insn) == 3 && (X_OP3 (insn) & 0x3c) == 0x04
1069           && X_I (insn) && X_RS1 (insn) == SPARC_FP_REGNUM)
1070         {
1071           int regnum = X_RD (insn);
1072
1073           /* Case of arguments still in %o[0..5].  */
1074           if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O5_REGNUM
1075               && !(cache.copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM)))
1076               && X_SIMM13 (insn) == 68 + (regnum - SPARC_O0_REGNUM) * 4)
1077             {
1078               start_pc += 4;
1079               continue;
1080             }
1081
1082           /* Case of arguments copied into %i[0..5].  */
1083           if (regnum >= SPARC_I0_REGNUM && regnum <= SPARC_I5_REGNUM
1084               && (cache.copied_regs_mask & (1 << (regnum - SPARC_I0_REGNUM)))
1085               && X_SIMM13 (insn) == 68 + (regnum - SPARC_I0_REGNUM) * 4)
1086             {
1087               start_pc += 4;
1088               continue;
1089             }
1090         }
1091
1092       break;
1093     }
1094
1095   return start_pc;
1096 }
1097
1098 /* Normal frames.  */
1099
1100 struct sparc_frame_cache *
1101 sparc_frame_cache (struct frame_info *this_frame, void **this_cache)
1102 {
1103   struct sparc_frame_cache *cache;
1104
1105   if (*this_cache)
1106     return *this_cache;
1107
1108   cache = sparc_alloc_frame_cache ();
1109   *this_cache = cache;
1110
1111   cache->pc = get_frame_func (this_frame);
1112   if (cache->pc != 0)
1113     sparc_analyze_prologue (get_frame_arch (this_frame), cache->pc,
1114                             get_frame_pc (this_frame), cache);
1115
1116   if (cache->frameless_p)
1117     {
1118       /* This function is frameless, so %fp (%i6) holds the frame
1119          pointer for our calling frame.  Use %sp (%o6) as this frame's
1120          base address.  */
1121       cache->base =
1122         get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
1123     }
1124   else
1125     {
1126       /* For normal frames, %fp (%i6) holds the frame pointer, the
1127          base address for the current stack frame.  */
1128       cache->base =
1129         get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
1130     }
1131
1132   cache->base += cache->frame_offset;
1133
1134   if (cache->base & 1)
1135     cache->base += BIAS;
1136
1137   return cache;
1138 }
1139
1140 static int
1141 sparc32_struct_return_from_sym (struct symbol *sym)
1142 {
1143   struct type *type = check_typedef (SYMBOL_TYPE (sym));
1144   enum type_code code = TYPE_CODE (type);
1145
1146   if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
1147     {
1148       type = check_typedef (TYPE_TARGET_TYPE (type));
1149       if (sparc_structure_or_union_p (type)
1150           || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16))
1151         return 1;
1152     }
1153
1154   return 0;
1155 }
1156
1157 struct sparc_frame_cache *
1158 sparc32_frame_cache (struct frame_info *this_frame, void **this_cache)
1159 {
1160   struct sparc_frame_cache *cache;
1161   struct symbol *sym;
1162
1163   if (*this_cache)
1164     return *this_cache;
1165
1166   cache = sparc_frame_cache (this_frame, this_cache);
1167
1168   sym = find_pc_function (cache->pc);
1169   if (sym)
1170     {
1171       cache->struct_return_p = sparc32_struct_return_from_sym (sym);
1172     }
1173   else
1174     {
1175       /* There is no debugging information for this function to
1176          help us determine whether this function returns a struct
1177          or not.  So we rely on another heuristic which is to check
1178          the instruction at the return address and see if this is
1179          an "unimp" instruction.  If it is, then it is a struct-return
1180          function.  */
1181       CORE_ADDR pc;
1182       int regnum =
1183         (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
1184
1185       pc = get_frame_register_unsigned (this_frame, regnum) + 8;
1186       if (sparc_is_unimp_insn (pc))
1187         cache->struct_return_p = 1;
1188     }
1189
1190   return cache;
1191 }
1192
1193 static void
1194 sparc32_frame_this_id (struct frame_info *this_frame, void **this_cache,
1195                        struct frame_id *this_id)
1196 {
1197   struct sparc_frame_cache *cache =
1198     sparc32_frame_cache (this_frame, this_cache);
1199
1200   /* This marks the outermost frame.  */
1201   if (cache->base == 0)
1202     return;
1203
1204   (*this_id) = frame_id_build (cache->base, cache->pc);
1205 }
1206
1207 static struct value *
1208 sparc32_frame_prev_register (struct frame_info *this_frame,
1209                              void **this_cache, int regnum)
1210 {
1211   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1212   struct sparc_frame_cache *cache =
1213     sparc32_frame_cache (this_frame, this_cache);
1214
1215   if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
1216     {
1217       CORE_ADDR pc = (regnum == SPARC32_NPC_REGNUM) ? 4 : 0;
1218
1219       /* If this functions has a Structure, Union or Quad-Precision
1220          return value, we have to skip the UNIMP instruction that encodes
1221          the size of the structure.  */
1222       if (cache->struct_return_p)
1223         pc += 4;
1224
1225       regnum =
1226         (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
1227       pc += get_frame_register_unsigned (this_frame, regnum) + 8;
1228       return frame_unwind_got_constant (this_frame, regnum, pc);
1229     }
1230
1231   /* Handle StackGhost.  */
1232   {
1233     ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1234
1235     if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
1236       {
1237         CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
1238         ULONGEST i7;
1239
1240         /* Read the value in from memory.  */
1241         i7 = get_frame_memory_unsigned (this_frame, addr, 4);
1242         return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
1243       }
1244   }
1245
1246   /* The previous frame's `local' and `in' registers may have been saved
1247      in the register save area.  */
1248   if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
1249       && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
1250     {
1251       CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
1252
1253       return frame_unwind_got_memory (this_frame, regnum, addr);
1254     }
1255
1256   /* The previous frame's `out' registers may be accessible as the current
1257      frame's `in' registers.  */
1258   if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
1259       && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
1260     regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
1261
1262   return frame_unwind_got_register (this_frame, regnum, regnum);
1263 }
1264
1265 static const struct frame_unwind sparc32_frame_unwind =
1266 {
1267   NORMAL_FRAME,
1268   default_frame_unwind_stop_reason,
1269   sparc32_frame_this_id,
1270   sparc32_frame_prev_register,
1271   NULL,
1272   default_frame_sniffer
1273 };
1274 \f
1275
1276 static CORE_ADDR
1277 sparc32_frame_base_address (struct frame_info *this_frame, void **this_cache)
1278 {
1279   struct sparc_frame_cache *cache =
1280     sparc32_frame_cache (this_frame, this_cache);
1281
1282   return cache->base;
1283 }
1284
1285 static const struct frame_base sparc32_frame_base =
1286 {
1287   &sparc32_frame_unwind,
1288   sparc32_frame_base_address,
1289   sparc32_frame_base_address,
1290   sparc32_frame_base_address
1291 };
1292
1293 static struct frame_id
1294 sparc_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1295 {
1296   CORE_ADDR sp;
1297
1298   sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
1299   if (sp & 1)
1300     sp += BIAS;
1301   return frame_id_build (sp, get_frame_pc (this_frame));
1302 }
1303 \f
1304
1305 /* Extract a function return value of TYPE from REGCACHE, and copy
1306    that into VALBUF.  */
1307
1308 static void
1309 sparc32_extract_return_value (struct type *type, struct regcache *regcache,
1310                               gdb_byte *valbuf)
1311 {
1312   int len = TYPE_LENGTH (type);
1313   gdb_byte buf[32];
1314
1315   gdb_assert (!sparc_structure_or_union_p (type));
1316   gdb_assert (!(sparc_floating_p (type) && len == 16));
1317
1318   if (sparc_floating_p (type) || sparc_complex_floating_p (type))
1319     {
1320       /* Floating return values.  */
1321       regcache_cooked_read (regcache, SPARC_F0_REGNUM, buf);
1322       if (len > 4)
1323         regcache_cooked_read (regcache, SPARC_F1_REGNUM, buf + 4);
1324       if (len > 8)
1325         {
1326           regcache_cooked_read (regcache, SPARC_F2_REGNUM, buf + 8);
1327           regcache_cooked_read (regcache, SPARC_F3_REGNUM, buf + 12);
1328         }
1329       if (len > 16)
1330         {
1331           regcache_cooked_read (regcache, SPARC_F4_REGNUM, buf + 16);
1332           regcache_cooked_read (regcache, SPARC_F5_REGNUM, buf + 20);
1333           regcache_cooked_read (regcache, SPARC_F6_REGNUM, buf + 24);
1334           regcache_cooked_read (regcache, SPARC_F7_REGNUM, buf + 28);
1335         }
1336       memcpy (valbuf, buf, len);
1337     }
1338   else
1339     {
1340       /* Integral and pointer return values.  */
1341       gdb_assert (sparc_integral_or_pointer_p (type));
1342
1343       regcache_cooked_read (regcache, SPARC_O0_REGNUM, buf);
1344       if (len > 4)
1345         {
1346           regcache_cooked_read (regcache, SPARC_O1_REGNUM, buf + 4);
1347           gdb_assert (len == 8);
1348           memcpy (valbuf, buf, 8);
1349         }
1350       else
1351         {
1352           /* Just stripping off any unused bytes should preserve the
1353              signed-ness just fine.  */
1354           memcpy (valbuf, buf + 4 - len, len);
1355         }
1356     }
1357 }
1358
1359 /* Store the function return value of type TYPE from VALBUF into
1360    REGCACHE.  */
1361
1362 static void
1363 sparc32_store_return_value (struct type *type, struct regcache *regcache,
1364                             const gdb_byte *valbuf)
1365 {
1366   int len = TYPE_LENGTH (type);
1367   gdb_byte buf[8];
1368
1369   gdb_assert (!sparc_structure_or_union_p (type));
1370   gdb_assert (!(sparc_floating_p (type) && len == 16));
1371   gdb_assert (len <= 8);
1372
1373   if (sparc_floating_p (type) || sparc_complex_floating_p (type))
1374     {
1375       /* Floating return values.  */
1376       memcpy (buf, valbuf, len);
1377       regcache_cooked_write (regcache, SPARC_F0_REGNUM, buf);
1378       if (len > 4)
1379         regcache_cooked_write (regcache, SPARC_F1_REGNUM, buf + 4);
1380       if (len > 8)
1381         {
1382           regcache_cooked_write (regcache, SPARC_F2_REGNUM, buf + 8);
1383           regcache_cooked_write (regcache, SPARC_F3_REGNUM, buf + 12);
1384         }
1385       if (len > 16)
1386         {
1387           regcache_cooked_write (regcache, SPARC_F4_REGNUM, buf + 16);
1388           regcache_cooked_write (regcache, SPARC_F5_REGNUM, buf + 20);
1389           regcache_cooked_write (regcache, SPARC_F6_REGNUM, buf + 24);
1390           regcache_cooked_write (regcache, SPARC_F7_REGNUM, buf + 28);
1391         }
1392     }
1393   else
1394     {
1395       /* Integral and pointer return values.  */
1396       gdb_assert (sparc_integral_or_pointer_p (type));
1397
1398       if (len > 4)
1399         {
1400           gdb_assert (len == 8);
1401           memcpy (buf, valbuf, 8);
1402           regcache_cooked_write (regcache, SPARC_O1_REGNUM, buf + 4);
1403         }
1404       else
1405         {
1406           /* ??? Do we need to do any sign-extension here?  */
1407           memcpy (buf + 4 - len, valbuf, len);
1408         }
1409       regcache_cooked_write (regcache, SPARC_O0_REGNUM, buf);
1410     }
1411 }
1412
1413 static enum return_value_convention
1414 sparc32_return_value (struct gdbarch *gdbarch, struct value *function,
1415                       struct type *type, struct regcache *regcache,
1416                       gdb_byte *readbuf, const gdb_byte *writebuf)
1417 {
1418   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1419
1420   /* The psABI says that "...every stack frame reserves the word at
1421      %fp+64.  If a function returns a structure, union, or
1422      quad-precision value, this word should hold the address of the
1423      object into which the return value should be copied."  This
1424      guarantees that we can always find the return value, not just
1425      before the function returns.  */
1426
1427   if (sparc_structure_or_union_p (type)
1428       || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16))
1429     {
1430       ULONGEST sp;
1431       CORE_ADDR addr;
1432
1433       if (readbuf)
1434         {
1435           regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1436           addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
1437           read_memory (addr, readbuf, TYPE_LENGTH (type));
1438         }
1439       if (writebuf)
1440         {
1441           regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1442           addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
1443           write_memory (addr, writebuf, TYPE_LENGTH (type));
1444         }
1445
1446       return RETURN_VALUE_ABI_PRESERVES_ADDRESS;
1447     }
1448
1449   if (readbuf)
1450     sparc32_extract_return_value (type, regcache, readbuf);
1451   if (writebuf)
1452     sparc32_store_return_value (type, regcache, writebuf);
1453
1454   return RETURN_VALUE_REGISTER_CONVENTION;
1455 }
1456
1457 static int
1458 sparc32_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
1459 {
1460   return (sparc_structure_or_union_p (type)
1461           || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16)
1462           || sparc_complex_floating_p (type));
1463 }
1464
1465 static int
1466 sparc32_dwarf2_struct_return_p (struct frame_info *this_frame)
1467 {
1468   CORE_ADDR pc = get_frame_address_in_block (this_frame);
1469   struct symbol *sym = find_pc_function (pc);
1470
1471   if (sym)
1472     return sparc32_struct_return_from_sym (sym);
1473   return 0;
1474 }
1475
1476 static void
1477 sparc32_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1478                                struct dwarf2_frame_state_reg *reg,
1479                                struct frame_info *this_frame)
1480 {
1481   int off;
1482
1483   switch (regnum)
1484     {
1485     case SPARC_G0_REGNUM:
1486       /* Since %g0 is always zero, there is no point in saving it, and
1487          people will be inclined omit it from the CFI.  Make sure we
1488          don't warn about that.  */
1489       reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1490       break;
1491     case SPARC_SP_REGNUM:
1492       reg->how = DWARF2_FRAME_REG_CFA;
1493       break;
1494     case SPARC32_PC_REGNUM:
1495     case SPARC32_NPC_REGNUM:
1496       reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1497       off = 8;
1498       if (sparc32_dwarf2_struct_return_p (this_frame))
1499         off += 4;
1500       if (regnum == SPARC32_NPC_REGNUM)
1501         off += 4;
1502       reg->loc.offset = off;
1503       break;
1504     }
1505 }
1506
1507 \f
1508 /* The SPARC Architecture doesn't have hardware single-step support,
1509    and most operating systems don't implement it either, so we provide
1510    software single-step mechanism.  */
1511
1512 static CORE_ADDR
1513 sparc_analyze_control_transfer (struct frame_info *frame,
1514                                 CORE_ADDR pc, CORE_ADDR *npc)
1515 {
1516   unsigned long insn = sparc_fetch_instruction (pc);
1517   int conditional_p = X_COND (insn) & 0x7;
1518   int branch_p = 0, fused_p = 0;
1519   long offset = 0;                      /* Must be signed for sign-extend.  */
1520
1521   if (X_OP (insn) == 0 && X_OP2 (insn) == 3)
1522     {
1523       if ((insn & 0x10000000) == 0)
1524         {
1525           /* Branch on Integer Register with Prediction (BPr).  */
1526           branch_p = 1;
1527           conditional_p = 1;
1528         }
1529       else
1530         {
1531           /* Compare and Branch  */
1532           branch_p = 1;
1533           fused_p = 1;
1534           offset = 4 * X_DISP10 (insn);
1535         }
1536     }
1537   else if (X_OP (insn) == 0 && X_OP2 (insn) == 6)
1538     {
1539       /* Branch on Floating-Point Condition Codes (FBfcc).  */
1540       branch_p = 1;
1541       offset = 4 * X_DISP22 (insn);
1542     }
1543   else if (X_OP (insn) == 0 && X_OP2 (insn) == 5)
1544     {
1545       /* Branch on Floating-Point Condition Codes with Prediction
1546          (FBPfcc).  */
1547       branch_p = 1;
1548       offset = 4 * X_DISP19 (insn);
1549     }
1550   else if (X_OP (insn) == 0 && X_OP2 (insn) == 2)
1551     {
1552       /* Branch on Integer Condition Codes (Bicc).  */
1553       branch_p = 1;
1554       offset = 4 * X_DISP22 (insn);
1555     }
1556   else if (X_OP (insn) == 0 && X_OP2 (insn) == 1)
1557     {
1558       /* Branch on Integer Condition Codes with Prediction (BPcc).  */
1559       branch_p = 1;
1560       offset = 4 * X_DISP19 (insn);
1561     }
1562   else if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3a)
1563     {
1564       /* Trap instruction (TRAP).  */
1565       return gdbarch_tdep (get_frame_arch (frame))->step_trap (frame, insn);
1566     }
1567
1568   /* FIXME: Handle DONE and RETRY instructions.  */
1569
1570   if (branch_p)
1571     {
1572       if (fused_p)
1573         {
1574           /* Fused compare-and-branch instructions are non-delayed,
1575              and do not have an annuling capability.  So we need to
1576              always set a breakpoint on both the NPC and the branch
1577              target address.  */
1578           gdb_assert (offset != 0);
1579           return pc + offset;
1580         }
1581       else if (conditional_p)
1582         {
1583           /* For conditional branches, return nPC + 4 iff the annul
1584              bit is 1.  */
1585           return (X_A (insn) ? *npc + 4 : 0);
1586         }
1587       else
1588         {
1589           /* For unconditional branches, return the target if its
1590              specified condition is "always" and return nPC + 4 if the
1591              condition is "never".  If the annul bit is 1, set *NPC to
1592              zero.  */
1593           if (X_COND (insn) == 0x0)
1594             pc = *npc, offset = 4;
1595           if (X_A (insn))
1596             *npc = 0;
1597
1598           return pc + offset;
1599         }
1600     }
1601
1602   return 0;
1603 }
1604
1605 static CORE_ADDR
1606 sparc_step_trap (struct frame_info *frame, unsigned long insn)
1607 {
1608   return 0;
1609 }
1610
1611 int
1612 sparc_software_single_step (struct frame_info *frame)
1613 {
1614   struct gdbarch *arch = get_frame_arch (frame);
1615   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
1616   struct address_space *aspace = get_frame_address_space (frame);
1617   CORE_ADDR npc, nnpc;
1618
1619   CORE_ADDR pc, orig_npc;
1620
1621   pc = get_frame_register_unsigned (frame, tdep->pc_regnum);
1622   orig_npc = npc = get_frame_register_unsigned (frame, tdep->npc_regnum);
1623
1624   /* Analyze the instruction at PC.  */
1625   nnpc = sparc_analyze_control_transfer (frame, pc, &npc);
1626   if (npc != 0)
1627     insert_single_step_breakpoint (arch, aspace, npc);
1628
1629   if (nnpc != 0)
1630     insert_single_step_breakpoint (arch, aspace, nnpc);
1631
1632   /* Assert that we have set at least one breakpoint, and that
1633      they're not set at the same spot - unless we're going
1634      from here straight to NULL, i.e. a call or jump to 0.  */
1635   gdb_assert (npc != 0 || nnpc != 0 || orig_npc == 0);
1636   gdb_assert (nnpc != npc || orig_npc == 0);
1637
1638   return 1;
1639 }
1640
1641 static void
1642 sparc_write_pc (struct regcache *regcache, CORE_ADDR pc)
1643 {
1644   struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
1645
1646   regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc);
1647   regcache_cooked_write_unsigned (regcache, tdep->npc_regnum, pc + 4);
1648 }
1649 \f
1650
1651 /* Return the appropriate register set for the core section identified
1652    by SECT_NAME and SECT_SIZE.  */
1653
1654 static const struct regset *
1655 sparc_regset_from_core_section (struct gdbarch *gdbarch,
1656                                 const char *sect_name, size_t sect_size)
1657 {
1658   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1659
1660   if (strcmp (sect_name, ".reg") == 0 && sect_size >= tdep->sizeof_gregset)
1661     return tdep->gregset;
1662
1663   if (strcmp (sect_name, ".reg2") == 0 && sect_size >= tdep->sizeof_fpregset)
1664     return tdep->fpregset;
1665
1666   return NULL;
1667 }
1668 \f
1669
1670 static struct gdbarch *
1671 sparc32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1672 {
1673   struct gdbarch_tdep *tdep;
1674   struct gdbarch *gdbarch;
1675
1676   /* If there is already a candidate, use it.  */
1677   arches = gdbarch_list_lookup_by_info (arches, &info);
1678   if (arches != NULL)
1679     return arches->gdbarch;
1680
1681   /* Allocate space for the new architecture.  */
1682   tdep = XCNEW (struct gdbarch_tdep);
1683   gdbarch = gdbarch_alloc (&info, tdep);
1684
1685   tdep->pc_regnum = SPARC32_PC_REGNUM;
1686   tdep->npc_regnum = SPARC32_NPC_REGNUM;
1687   tdep->step_trap = sparc_step_trap;
1688
1689   set_gdbarch_long_double_bit (gdbarch, 128);
1690   set_gdbarch_long_double_format (gdbarch, floatformats_sparc_quad);
1691
1692   set_gdbarch_num_regs (gdbarch, SPARC32_NUM_REGS);
1693   set_gdbarch_register_name (gdbarch, sparc32_register_name);
1694   set_gdbarch_register_type (gdbarch, sparc32_register_type);
1695   set_gdbarch_num_pseudo_regs (gdbarch, SPARC32_NUM_PSEUDO_REGS);
1696   set_gdbarch_pseudo_register_read (gdbarch, sparc32_pseudo_register_read);
1697   set_gdbarch_pseudo_register_write (gdbarch, sparc32_pseudo_register_write);
1698
1699   /* Register numbers of various important registers.  */
1700   set_gdbarch_sp_regnum (gdbarch, SPARC_SP_REGNUM); /* %sp */
1701   set_gdbarch_pc_regnum (gdbarch, SPARC32_PC_REGNUM); /* %pc */
1702   set_gdbarch_fp0_regnum (gdbarch, SPARC_F0_REGNUM); /* %f0 */
1703
1704   /* Call dummy code.  */
1705   set_gdbarch_frame_align (gdbarch, sparc32_frame_align);
1706   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1707   set_gdbarch_push_dummy_code (gdbarch, sparc32_push_dummy_code);
1708   set_gdbarch_push_dummy_call (gdbarch, sparc32_push_dummy_call);
1709
1710   set_gdbarch_return_value (gdbarch, sparc32_return_value);
1711   set_gdbarch_stabs_argument_has_addr
1712     (gdbarch, sparc32_stabs_argument_has_addr);
1713
1714   set_gdbarch_skip_prologue (gdbarch, sparc32_skip_prologue);
1715
1716   /* Stack grows downward.  */
1717   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1718
1719   set_gdbarch_breakpoint_from_pc (gdbarch, sparc_breakpoint_from_pc);
1720
1721   set_gdbarch_frame_args_skip (gdbarch, 8);
1722
1723   set_gdbarch_print_insn (gdbarch, print_insn_sparc);
1724
1725   set_gdbarch_software_single_step (gdbarch, sparc_software_single_step);
1726   set_gdbarch_write_pc (gdbarch, sparc_write_pc);
1727
1728   set_gdbarch_dummy_id (gdbarch, sparc_dummy_id);
1729
1730   set_gdbarch_unwind_pc (gdbarch, sparc_unwind_pc);
1731
1732   frame_base_set_default (gdbarch, &sparc32_frame_base);
1733
1734   /* Hook in the DWARF CFI frame unwinder.  */
1735   dwarf2_frame_set_init_reg (gdbarch, sparc32_dwarf2_frame_init_reg);
1736   /* FIXME: kettenis/20050423: Don't enable the unwinder until the
1737      StackGhost issues have been resolved.  */
1738
1739   /* Hook in ABI-specific overrides, if they have been registered.  */
1740   gdbarch_init_osabi (info, gdbarch);
1741
1742   frame_unwind_append_unwinder (gdbarch, &sparc32_frame_unwind);
1743
1744   /* If we have register sets, enable the generic core file support.  */
1745   if (tdep->gregset)
1746     set_gdbarch_regset_from_core_section (gdbarch,
1747                                           sparc_regset_from_core_section);
1748
1749   register_sparc_ravenscar_ops (gdbarch);
1750
1751   return gdbarch;
1752 }
1753 \f
1754 /* Helper functions for dealing with register windows.  */
1755
1756 void
1757 sparc_supply_rwindow (struct regcache *regcache, CORE_ADDR sp, int regnum)
1758 {
1759   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1760   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1761   int offset = 0;
1762   gdb_byte buf[8];
1763   int i;
1764
1765   if (sp & 1)
1766     {
1767       /* Registers are 64-bit.  */
1768       sp += BIAS;
1769
1770       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1771         {
1772           if (regnum == i || regnum == -1)
1773             {
1774               target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
1775
1776               /* Handle StackGhost.  */
1777               if (i == SPARC_I7_REGNUM)
1778                 {
1779                   ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1780                   ULONGEST i7;
1781
1782                   i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
1783                   store_unsigned_integer (buf + offset, 8, byte_order,
1784                                           i7 ^ wcookie);
1785                 }
1786
1787               regcache_raw_supply (regcache, i, buf);
1788             }
1789         }
1790     }
1791   else
1792     {
1793       /* Registers are 32-bit.  Toss any sign-extension of the stack
1794          pointer.  */
1795       sp &= 0xffffffffUL;
1796
1797       /* Clear out the top half of the temporary buffer, and put the
1798          register value in the bottom half if we're in 64-bit mode.  */
1799       if (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 64)
1800         {
1801           memset (buf, 0, 4);
1802           offset = 4;
1803         }
1804
1805       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1806         {
1807           if (regnum == i || regnum == -1)
1808             {
1809               target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
1810                                   buf + offset, 4);
1811
1812               /* Handle StackGhost.  */
1813               if (i == SPARC_I7_REGNUM)
1814                 {
1815                   ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1816                   ULONGEST i7;
1817
1818                   i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
1819                   store_unsigned_integer (buf + offset, 4, byte_order,
1820                                           i7 ^ wcookie);
1821                 }
1822
1823               regcache_raw_supply (regcache, i, buf);
1824             }
1825         }
1826     }
1827 }
1828
1829 void
1830 sparc_collect_rwindow (const struct regcache *regcache,
1831                        CORE_ADDR sp, int regnum)
1832 {
1833   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1834   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1835   int offset = 0;
1836   gdb_byte buf[8];
1837   int i;
1838
1839   if (sp & 1)
1840     {
1841       /* Registers are 64-bit.  */
1842       sp += BIAS;
1843
1844       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1845         {
1846           if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
1847             {
1848               regcache_raw_collect (regcache, i, buf);
1849
1850               /* Handle StackGhost.  */
1851               if (i == SPARC_I7_REGNUM)
1852                 {
1853                   ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1854                   ULONGEST i7;
1855
1856                   i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
1857                   store_unsigned_integer (buf, 8, byte_order, i7 ^ wcookie);
1858                 }
1859
1860               target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
1861             }
1862         }
1863     }
1864   else
1865     {
1866       /* Registers are 32-bit.  Toss any sign-extension of the stack
1867          pointer.  */
1868       sp &= 0xffffffffUL;
1869
1870       /* Only use the bottom half if we're in 64-bit mode.  */
1871       if (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 64)
1872         offset = 4;
1873
1874       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1875         {
1876           if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
1877             {
1878               regcache_raw_collect (regcache, i, buf);
1879
1880               /* Handle StackGhost.  */
1881               if (i == SPARC_I7_REGNUM)
1882                 {
1883                   ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1884                   ULONGEST i7;
1885
1886                   i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
1887                   store_unsigned_integer (buf + offset, 4, byte_order,
1888                                           i7 ^ wcookie);
1889                 }
1890
1891               target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
1892                                    buf + offset, 4);
1893             }
1894         }
1895     }
1896 }
1897
1898 /* Helper functions for dealing with register sets.  */
1899
1900 void
1901 sparc32_supply_gregset (const struct sparc_gregmap *gregmap,
1902                         struct regcache *regcache,
1903                         int regnum, const void *gregs)
1904 {
1905   const gdb_byte *regs = gregs;
1906   gdb_byte zero[4] = { 0 };
1907   int i;
1908
1909   if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1910     regcache_raw_supply (regcache, SPARC32_PSR_REGNUM,
1911                          regs + gregmap->r_psr_offset);
1912
1913   if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1914     regcache_raw_supply (regcache, SPARC32_PC_REGNUM,
1915                          regs + gregmap->r_pc_offset);
1916
1917   if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1918     regcache_raw_supply (regcache, SPARC32_NPC_REGNUM,
1919                          regs + gregmap->r_npc_offset);
1920
1921   if (regnum == SPARC32_Y_REGNUM || regnum == -1)
1922     regcache_raw_supply (regcache, SPARC32_Y_REGNUM,
1923                          regs + gregmap->r_y_offset);
1924
1925   if (regnum == SPARC_G0_REGNUM || regnum == -1)
1926     regcache_raw_supply (regcache, SPARC_G0_REGNUM, &zero);
1927
1928   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1929     {
1930       int offset = gregmap->r_g1_offset;
1931
1932       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
1933         {
1934           if (regnum == i || regnum == -1)
1935             regcache_raw_supply (regcache, i, regs + offset);
1936           offset += 4;
1937         }
1938     }
1939
1940   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1941     {
1942       /* Not all of the register set variants include Locals and
1943          Inputs.  For those that don't, we read them off the stack.  */
1944       if (gregmap->r_l0_offset == -1)
1945         {
1946           ULONGEST sp;
1947
1948           regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1949           sparc_supply_rwindow (regcache, sp, regnum);
1950         }
1951       else
1952         {
1953           int offset = gregmap->r_l0_offset;
1954
1955           for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1956             {
1957               if (regnum == i || regnum == -1)
1958                 regcache_raw_supply (regcache, i, regs + offset);
1959               offset += 4;
1960             }
1961         }
1962     }
1963 }
1964
1965 void
1966 sparc32_collect_gregset (const struct sparc_gregmap *gregmap,
1967                          const struct regcache *regcache,
1968                          int regnum, void *gregs)
1969 {
1970   gdb_byte *regs = gregs;
1971   int i;
1972
1973   if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1974     regcache_raw_collect (regcache, SPARC32_PSR_REGNUM,
1975                           regs + gregmap->r_psr_offset);
1976
1977   if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1978     regcache_raw_collect (regcache, SPARC32_PC_REGNUM,
1979                           regs + gregmap->r_pc_offset);
1980
1981   if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1982     regcache_raw_collect (regcache, SPARC32_NPC_REGNUM,
1983                           regs + gregmap->r_npc_offset);
1984
1985   if (regnum == SPARC32_Y_REGNUM || regnum == -1)
1986     regcache_raw_collect (regcache, SPARC32_Y_REGNUM,
1987                           regs + gregmap->r_y_offset);
1988
1989   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1990     {
1991       int offset = gregmap->r_g1_offset;
1992
1993       /* %g0 is always zero.  */
1994       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
1995         {
1996           if (regnum == i || regnum == -1)
1997             regcache_raw_collect (regcache, i, regs + offset);
1998           offset += 4;
1999         }
2000     }
2001
2002   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
2003     {
2004       /* Not all of the register set variants include Locals and
2005          Inputs.  For those that don't, we read them off the stack.  */
2006       if (gregmap->r_l0_offset != -1)
2007         {
2008           int offset = gregmap->r_l0_offset;
2009
2010           for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
2011             {
2012               if (regnum == i || regnum == -1)
2013                 regcache_raw_collect (regcache, i, regs + offset);
2014               offset += 4;
2015             }
2016         }
2017     }
2018 }
2019
2020 void
2021 sparc32_supply_fpregset (const struct sparc_fpregmap *fpregmap,
2022                          struct regcache *regcache,
2023                          int regnum, const void *fpregs)
2024 {
2025   const gdb_byte *regs = fpregs;
2026   int i;
2027
2028   for (i = 0; i < 32; i++)
2029     {
2030       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
2031         regcache_raw_supply (regcache, SPARC_F0_REGNUM + i,
2032                              regs + fpregmap->r_f0_offset + (i * 4));
2033     }
2034
2035   if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
2036     regcache_raw_supply (regcache, SPARC32_FSR_REGNUM,
2037                          regs + fpregmap->r_fsr_offset);
2038 }
2039
2040 void
2041 sparc32_collect_fpregset (const struct sparc_fpregmap *fpregmap,
2042                           const struct regcache *regcache,
2043                           int regnum, void *fpregs)
2044 {
2045   gdb_byte *regs = fpregs;
2046   int i;
2047
2048   for (i = 0; i < 32; i++)
2049     {
2050       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
2051         regcache_raw_collect (regcache, SPARC_F0_REGNUM + i,
2052                               regs + fpregmap->r_f0_offset + (i * 4));
2053     }
2054
2055   if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
2056     regcache_raw_collect (regcache, SPARC32_FSR_REGNUM,
2057                           regs + fpregmap->r_fsr_offset);
2058 }
2059 \f
2060
2061 /* SunOS 4.  */
2062
2063 /* From <machine/reg.h>.  */
2064 const struct sparc_gregmap sparc32_sunos4_gregmap =
2065 {
2066   0 * 4,                        /* %psr */
2067   1 * 4,                        /* %pc */
2068   2 * 4,                        /* %npc */
2069   3 * 4,                        /* %y */
2070   -1,                           /* %wim */
2071   -1,                           /* %tbr */
2072   4 * 4,                        /* %g1 */
2073   -1                            /* %l0 */
2074 };
2075
2076 const struct sparc_fpregmap sparc32_sunos4_fpregmap =
2077 {
2078   0 * 4,                        /* %f0 */
2079   33 * 4,                       /* %fsr */
2080 };
2081
2082 const struct sparc_fpregmap sparc32_bsd_fpregmap =
2083 {
2084   0 * 4,                        /* %f0 */
2085   32 * 4,                       /* %fsr */
2086 };
2087 \f
2088
2089 /* Provide a prototype to silence -Wmissing-prototypes.  */
2090 void _initialize_sparc_tdep (void);
2091
2092 void
2093 _initialize_sparc_tdep (void)
2094 {
2095   register_gdbarch_init (bfd_arch_sparc, sparc32_gdbarch_init);
2096 }