1 /* Target-dependent code for UltraSPARC.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "arch-utils.h"
23 #include "dwarf2-frame.h"
24 #include "floatformat.h"
26 #include "frame-base.h"
27 #include "frame-unwind.h"
38 #include "gdb_assert.h"
39 #include "gdb_string.h"
41 #include "sparc64-tdep.h"
43 /* This file implements the The SPARC 64-bit ABI as defined by the
44 section "Low-Level System Information" of the SPARC Compliance
45 Definition (SCD) 2.4.1, which is the 64-bit System V psABI for
48 /* Please use the sparc32_-prefix for 32-bit specific code, the
49 sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
50 code can handle both. */
52 /* The functions on this page are intended to be used to classify
53 function arguments. */
55 /* Check whether TYPE is "Integral or Pointer". */
58 sparc64_integral_or_pointer_p (const struct type *type)
60 switch (TYPE_CODE (type))
68 int len = TYPE_LENGTH (type);
69 gdb_assert (len == 1 || len == 2 || len == 4 || len == 8);
75 int len = TYPE_LENGTH (type);
76 gdb_assert (len == 8);
86 /* Check whether TYPE is "Floating". */
89 sparc64_floating_p (const struct type *type)
91 switch (TYPE_CODE (type))
95 int len = TYPE_LENGTH (type);
96 gdb_assert (len == 4 || len == 8 || len == 16);
106 /* Check whether TYPE is "Structure or Union". */
109 sparc64_structure_or_union_p (const struct type *type)
111 switch (TYPE_CODE (type))
113 case TYPE_CODE_STRUCT:
114 case TYPE_CODE_UNION:
124 /* Construct types for ISA-specific registers. */
127 sparc64_pstate_type (struct gdbarch *gdbarch)
129 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
131 if (!tdep->sparc64_pstate_type)
135 type = arch_flags_type (gdbarch, "builtin_type_sparc64_pstate", 8);
136 append_flags_type_flag (type, 0, "AG");
137 append_flags_type_flag (type, 1, "IE");
138 append_flags_type_flag (type, 2, "PRIV");
139 append_flags_type_flag (type, 3, "AM");
140 append_flags_type_flag (type, 4, "PEF");
141 append_flags_type_flag (type, 5, "RED");
142 append_flags_type_flag (type, 8, "TLE");
143 append_flags_type_flag (type, 9, "CLE");
144 append_flags_type_flag (type, 10, "PID0");
145 append_flags_type_flag (type, 11, "PID1");
147 tdep->sparc64_pstate_type = type;
150 return tdep->sparc64_pstate_type;
154 sparc64_fsr_type (struct gdbarch *gdbarch)
156 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
158 if (!tdep->sparc64_fsr_type)
162 type = arch_flags_type (gdbarch, "builtin_type_sparc64_fsr", 8);
163 append_flags_type_flag (type, 0, "NXA");
164 append_flags_type_flag (type, 1, "DZA");
165 append_flags_type_flag (type, 2, "UFA");
166 append_flags_type_flag (type, 3, "OFA");
167 append_flags_type_flag (type, 4, "NVA");
168 append_flags_type_flag (type, 5, "NXC");
169 append_flags_type_flag (type, 6, "DZC");
170 append_flags_type_flag (type, 7, "UFC");
171 append_flags_type_flag (type, 8, "OFC");
172 append_flags_type_flag (type, 9, "NVC");
173 append_flags_type_flag (type, 22, "NS");
174 append_flags_type_flag (type, 23, "NXM");
175 append_flags_type_flag (type, 24, "DZM");
176 append_flags_type_flag (type, 25, "UFM");
177 append_flags_type_flag (type, 26, "OFM");
178 append_flags_type_flag (type, 27, "NVM");
180 tdep->sparc64_fsr_type = type;
183 return tdep->sparc64_fsr_type;
187 sparc64_fprs_type (struct gdbarch *gdbarch)
189 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
191 if (!tdep->sparc64_fprs_type)
195 type = arch_flags_type (gdbarch, "builtin_type_sparc64_fprs", 8);
196 append_flags_type_flag (type, 0, "DL");
197 append_flags_type_flag (type, 1, "DU");
198 append_flags_type_flag (type, 2, "FEF");
200 tdep->sparc64_fprs_type = type;
203 return tdep->sparc64_fprs_type;
207 /* Register information. */
209 static const char *sparc64_register_names[] =
211 "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
212 "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
213 "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
214 "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",
216 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
217 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
218 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
219 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
220 "f32", "f34", "f36", "f38", "f40", "f42", "f44", "f46",
221 "f48", "f50", "f52", "f54", "f56", "f58", "f60", "f62",
225 /* FIXME: Give "state" a name until we start using register groups. */
232 /* Total number of registers. */
233 #define SPARC64_NUM_REGS ARRAY_SIZE (sparc64_register_names)
235 /* We provide the aliases %d0..%d62 and %q0..%q60 for the floating
236 registers as "psuedo" registers. */
238 static const char *sparc64_pseudo_register_names[] =
240 "cwp", "pstate", "asi", "ccr",
242 "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
243 "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30",
244 "d32", "d34", "d36", "d38", "d40", "d42", "d44", "d46",
245 "d48", "d50", "d52", "d54", "d56", "d58", "d60", "d62",
247 "q0", "q4", "q8", "q12", "q16", "q20", "q24", "q28",
248 "q32", "q36", "q40", "q44", "q48", "q52", "q56", "q60",
251 /* Total number of pseudo registers. */
252 #define SPARC64_NUM_PSEUDO_REGS ARRAY_SIZE (sparc64_pseudo_register_names)
254 /* Return the name of register REGNUM. */
257 sparc64_register_name (struct gdbarch *gdbarch, int regnum)
259 if (regnum >= 0 && regnum < SPARC64_NUM_REGS)
260 return sparc64_register_names[regnum];
262 if (regnum >= SPARC64_NUM_REGS
263 && regnum < SPARC64_NUM_REGS + SPARC64_NUM_PSEUDO_REGS)
264 return sparc64_pseudo_register_names[regnum - SPARC64_NUM_REGS];
269 /* Return the GDB type object for the "standard" data type of data in
273 sparc64_register_type (struct gdbarch *gdbarch, int regnum)
277 if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
278 return builtin_type (gdbarch)->builtin_data_ptr;
279 if (regnum >= SPARC_G0_REGNUM && regnum <= SPARC_I7_REGNUM)
280 return builtin_type (gdbarch)->builtin_int64;
281 if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
282 return builtin_type (gdbarch)->builtin_float;
283 if (regnum >= SPARC64_F32_REGNUM && regnum <= SPARC64_F62_REGNUM)
284 return builtin_type (gdbarch)->builtin_double;
285 if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
286 return builtin_type (gdbarch)->builtin_func_ptr;
287 /* This raw register contains the contents of %cwp, %pstate, %asi
288 and %ccr as laid out in a %tstate register. */
289 if (regnum == SPARC64_STATE_REGNUM)
290 return builtin_type (gdbarch)->builtin_int64;
291 if (regnum == SPARC64_FSR_REGNUM)
292 return sparc64_fsr_type (gdbarch);
293 if (regnum == SPARC64_FPRS_REGNUM)
294 return sparc64_fprs_type (gdbarch);
295 /* "Although Y is a 64-bit register, its high-order 32 bits are
296 reserved and always read as 0." */
297 if (regnum == SPARC64_Y_REGNUM)
298 return builtin_type (gdbarch)->builtin_int64;
300 /* Pseudo registers. */
302 if (regnum == SPARC64_CWP_REGNUM)
303 return builtin_type (gdbarch)->builtin_int64;
304 if (regnum == SPARC64_PSTATE_REGNUM)
305 return sparc64_pstate_type (gdbarch);
306 if (regnum == SPARC64_ASI_REGNUM)
307 return builtin_type (gdbarch)->builtin_int64;
308 if (regnum == SPARC64_CCR_REGNUM)
309 return builtin_type (gdbarch)->builtin_int64;
310 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D62_REGNUM)
311 return builtin_type (gdbarch)->builtin_double;
312 if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q60_REGNUM)
313 return builtin_type (gdbarch)->builtin_long_double;
315 internal_error (__FILE__, __LINE__, _("invalid regnum"));
319 sparc64_pseudo_register_read (struct gdbarch *gdbarch,
320 struct regcache *regcache,
321 int regnum, gdb_byte *buf)
323 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
324 gdb_assert (regnum >= SPARC64_NUM_REGS);
326 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
328 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
329 regcache_raw_read (regcache, regnum, buf);
330 regcache_raw_read (regcache, regnum + 1, buf + 4);
332 else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
334 regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
335 regcache_raw_read (regcache, regnum, buf);
337 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
339 regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
340 regcache_raw_read (regcache, regnum, buf);
341 regcache_raw_read (regcache, regnum + 1, buf + 4);
342 regcache_raw_read (regcache, regnum + 2, buf + 8);
343 regcache_raw_read (regcache, regnum + 3, buf + 12);
345 else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
347 regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
348 regcache_raw_read (regcache, regnum, buf);
349 regcache_raw_read (regcache, regnum + 1, buf + 8);
351 else if (regnum == SPARC64_CWP_REGNUM
352 || regnum == SPARC64_PSTATE_REGNUM
353 || regnum == SPARC64_ASI_REGNUM
354 || regnum == SPARC64_CCR_REGNUM)
358 regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
361 case SPARC64_CWP_REGNUM:
362 state = (state >> 0) & ((1 << 5) - 1);
364 case SPARC64_PSTATE_REGNUM:
365 state = (state >> 8) & ((1 << 12) - 1);
367 case SPARC64_ASI_REGNUM:
368 state = (state >> 24) & ((1 << 8) - 1);
370 case SPARC64_CCR_REGNUM:
371 state = (state >> 32) & ((1 << 8) - 1);
374 store_unsigned_integer (buf, 8, byte_order, state);
379 sparc64_pseudo_register_write (struct gdbarch *gdbarch,
380 struct regcache *regcache,
381 int regnum, const gdb_byte *buf)
383 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
384 gdb_assert (regnum >= SPARC64_NUM_REGS);
386 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
388 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
389 regcache_raw_write (regcache, regnum, buf);
390 regcache_raw_write (regcache, regnum + 1, buf + 4);
392 else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
394 regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
395 regcache_raw_write (regcache, regnum, buf);
397 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
399 regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
400 regcache_raw_write (regcache, regnum, buf);
401 regcache_raw_write (regcache, regnum + 1, buf + 4);
402 regcache_raw_write (regcache, regnum + 2, buf + 8);
403 regcache_raw_write (regcache, regnum + 3, buf + 12);
405 else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
407 regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
408 regcache_raw_write (regcache, regnum, buf);
409 regcache_raw_write (regcache, regnum + 1, buf + 8);
411 else if (regnum == SPARC64_CWP_REGNUM
412 || regnum == SPARC64_PSTATE_REGNUM
413 || regnum == SPARC64_ASI_REGNUM
414 || regnum == SPARC64_CCR_REGNUM)
416 ULONGEST state, bits;
418 regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
419 bits = extract_unsigned_integer (buf, 8, byte_order);
422 case SPARC64_CWP_REGNUM:
423 state |= ((bits & ((1 << 5) - 1)) << 0);
425 case SPARC64_PSTATE_REGNUM:
426 state |= ((bits & ((1 << 12) - 1)) << 8);
428 case SPARC64_ASI_REGNUM:
429 state |= ((bits & ((1 << 8) - 1)) << 24);
431 case SPARC64_CCR_REGNUM:
432 state |= ((bits & ((1 << 8) - 1)) << 32);
435 regcache_raw_write_unsigned (regcache, SPARC64_STATE_REGNUM, state);
440 /* Return PC of first real instruction of the function starting at
444 sparc64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
446 struct symtab_and_line sal;
447 CORE_ADDR func_start, func_end;
448 struct sparc_frame_cache cache;
450 /* This is the preferred method, find the end of the prologue by
451 using the debugging information. */
452 if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
454 sal = find_pc_line (func_start, 0);
456 if (sal.end < func_end
457 && start_pc <= sal.end)
461 return sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffULL,
467 static struct sparc_frame_cache *
468 sparc64_frame_cache (struct frame_info *this_frame, void **this_cache)
470 return sparc_frame_cache (this_frame, this_cache);
474 sparc64_frame_this_id (struct frame_info *this_frame, void **this_cache,
475 struct frame_id *this_id)
477 struct sparc_frame_cache *cache =
478 sparc64_frame_cache (this_frame, this_cache);
480 /* This marks the outermost frame. */
481 if (cache->base == 0)
484 (*this_id) = frame_id_build (cache->base, cache->pc);
487 static struct value *
488 sparc64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
491 struct gdbarch *gdbarch = get_frame_arch (this_frame);
492 struct sparc_frame_cache *cache =
493 sparc64_frame_cache (this_frame, this_cache);
495 if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
497 CORE_ADDR pc = (regnum == SPARC64_NPC_REGNUM) ? 4 : 0;
499 regnum = cache->frameless_p ? SPARC_O7_REGNUM : SPARC_I7_REGNUM;
500 pc += get_frame_register_unsigned (this_frame, regnum) + 8;
501 return frame_unwind_got_constant (this_frame, regnum, pc);
504 /* Handle StackGhost. */
506 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
508 if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
510 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
513 /* Read the value in from memory. */
514 i7 = get_frame_memory_unsigned (this_frame, addr, 8);
515 return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
519 /* The previous frame's `local' and `in' registers have been saved
520 in the register save area. */
521 if (!cache->frameless_p
522 && regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM)
524 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
526 return frame_unwind_got_memory (this_frame, regnum, addr);
529 /* The previous frame's `out' registers are accessable as the
530 current frame's `in' registers. */
531 if (!cache->frameless_p
532 && regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM)
533 regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
535 return frame_unwind_got_register (this_frame, regnum, regnum);
538 static const struct frame_unwind sparc64_frame_unwind =
541 sparc64_frame_this_id,
542 sparc64_frame_prev_register,
544 default_frame_sniffer
549 sparc64_frame_base_address (struct frame_info *this_frame, void **this_cache)
551 struct sparc_frame_cache *cache =
552 sparc64_frame_cache (this_frame, this_cache);
557 static const struct frame_base sparc64_frame_base =
559 &sparc64_frame_unwind,
560 sparc64_frame_base_address,
561 sparc64_frame_base_address,
562 sparc64_frame_base_address
565 /* Check whether TYPE must be 16-byte aligned. */
568 sparc64_16_byte_align_p (struct type *type)
570 if (sparc64_floating_p (type) && TYPE_LENGTH (type) == 16)
573 if (sparc64_structure_or_union_p (type))
577 for (i = 0; i < TYPE_NFIELDS (type); i++)
579 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
581 if (sparc64_16_byte_align_p (subtype))
589 /* Store floating fields of element ELEMENT of an "parameter array"
590 that has type TYPE and is stored at BITPOS in VALBUF in the
591 apropriate registers of REGCACHE. This function can be called
592 recursively and therefore handles floating types in addition to
596 sparc64_store_floating_fields (struct regcache *regcache, struct type *type,
597 const gdb_byte *valbuf, int element, int bitpos)
599 gdb_assert (element < 16);
601 if (sparc64_floating_p (type))
603 int len = TYPE_LENGTH (type);
608 gdb_assert (bitpos == 0);
609 gdb_assert ((element % 2) == 0);
611 regnum = SPARC64_Q0_REGNUM + element / 2;
612 regcache_cooked_write (regcache, regnum, valbuf);
616 gdb_assert (bitpos == 0 || bitpos == 64);
618 regnum = SPARC64_D0_REGNUM + element + bitpos / 64;
619 regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
623 gdb_assert (len == 4);
624 gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 128);
626 regnum = SPARC_F0_REGNUM + element * 2 + bitpos / 32;
627 regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
630 else if (sparc64_structure_or_union_p (type))
634 for (i = 0; i < TYPE_NFIELDS (type); i++)
636 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
637 int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);
639 sparc64_store_floating_fields (regcache, subtype, valbuf,
643 /* GCC has an interesting bug. If TYPE is a structure that has
644 a single `float' member, GCC doesn't treat it as a structure
645 at all, but rather as an ordinary `float' argument. This
646 argument will be stored in %f1, as required by the psABI.
647 However, as a member of a structure the psABI requires it to
648 be stored in %f0. This bug is present in GCC 3.3.2, but
649 probably in older releases to. To appease GCC, if a
650 structure has only a single `float' member, we store its
651 value in %f1 too (we already have stored in %f0). */
652 if (TYPE_NFIELDS (type) == 1)
654 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, 0));
656 if (sparc64_floating_p (subtype) && TYPE_LENGTH (subtype) == 4)
657 regcache_cooked_write (regcache, SPARC_F1_REGNUM, valbuf);
662 /* Fetch floating fields from a variable of type TYPE from the
663 appropriate registers for BITPOS in REGCACHE and store it at BITPOS
664 in VALBUF. This function can be called recursively and therefore
665 handles floating types in addition to structures. */
668 sparc64_extract_floating_fields (struct regcache *regcache, struct type *type,
669 gdb_byte *valbuf, int bitpos)
671 if (sparc64_floating_p (type))
673 int len = TYPE_LENGTH (type);
678 gdb_assert (bitpos == 0 || bitpos == 128);
680 regnum = SPARC64_Q0_REGNUM + bitpos / 128;
681 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
685 gdb_assert (bitpos % 64 == 0 && bitpos >= 0 && bitpos < 256);
687 regnum = SPARC64_D0_REGNUM + bitpos / 64;
688 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
692 gdb_assert (len == 4);
693 gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 256);
695 regnum = SPARC_F0_REGNUM + bitpos / 32;
696 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
699 else if (sparc64_structure_or_union_p (type))
703 for (i = 0; i < TYPE_NFIELDS (type); i++)
705 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
706 int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);
708 sparc64_extract_floating_fields (regcache, subtype, valbuf, subpos);
713 /* Store the NARGS arguments ARGS and STRUCT_ADDR (if STRUCT_RETURN is
714 non-zero) in REGCACHE and on the stack (starting from address SP). */
717 sparc64_store_arguments (struct regcache *regcache, int nargs,
718 struct value **args, CORE_ADDR sp,
719 int struct_return, CORE_ADDR struct_addr)
721 struct gdbarch *gdbarch = get_regcache_arch (regcache);
722 /* Number of extended words in the "parameter array". */
723 int num_elements = 0;
727 /* Take BIAS into account. */
730 /* First we calculate the number of extended words in the "parameter
731 array". While doing so we also convert some of the arguments. */
736 for (i = 0; i < nargs; i++)
738 struct type *type = value_type (args[i]);
739 int len = TYPE_LENGTH (type);
741 if (sparc64_structure_or_union_p (type))
743 /* Structure or Union arguments. */
746 if (num_elements % 2 && sparc64_16_byte_align_p (type))
748 num_elements += ((len + 7) / 8);
752 /* The psABI says that "Structures or unions larger than
753 sixteen bytes are copied by the caller and passed
754 indirectly; the caller will pass the address of a
755 correctly aligned structure value. This sixty-four
756 bit address will occupy one word in the parameter
757 array, and may be promoted to an %o register like any
758 other pointer value." Allocate memory for these
759 values on the stack. */
762 /* Use 16-byte alignment for these values. That's
763 always correct, and wasting a few bytes shouldn't be
767 write_memory (sp, value_contents (args[i]), len);
768 args[i] = value_from_pointer (lookup_pointer_type (type), sp);
772 else if (sparc64_floating_p (type))
774 /* Floating arguments. */
778 /* The psABI says that "Each quad-precision parameter
779 value will be assigned to two extended words in the
783 /* The psABI says that "Long doubles must be
784 quad-aligned, and thus a hole might be introduced
785 into the parameter array to force alignment." Skip
786 an element if necessary. */
787 if (num_elements % 2)
795 /* Integral and pointer arguments. */
796 gdb_assert (sparc64_integral_or_pointer_p (type));
798 /* The psABI says that "Each argument value of integral type
799 smaller than an extended word will be widened by the
800 caller to an extended word according to the signed-ness
801 of the argument type." */
803 args[i] = value_cast (builtin_type (gdbarch)->builtin_int64,
809 /* Allocate the "parameter array". */
810 sp -= num_elements * 8;
812 /* The psABI says that "Every stack frame must be 16-byte aligned." */
815 /* Now we store the arguments in to the "paramater array". Some
816 Integer or Pointer arguments and Structure or Union arguments
817 will be passed in %o registers. Some Floating arguments and
818 floating members of structures are passed in floating-point
819 registers. However, for functions with variable arguments,
820 floating arguments are stored in an %0 register, and for
821 functions without a prototype floating arguments are stored in
822 both a floating-point and an %o registers, or a floating-point
823 register and memory. To simplify the logic here we always pass
824 arguments in memory, an %o register, and a floating-point
825 register if appropriate. This should be no problem since the
826 contents of any unused memory or registers in the "parameter
827 array" are undefined. */
831 regcache_cooked_write_unsigned (regcache, SPARC_O0_REGNUM, struct_addr);
835 for (i = 0; i < nargs; i++)
837 const gdb_byte *valbuf = value_contents (args[i]);
838 struct type *type = value_type (args[i]);
839 int len = TYPE_LENGTH (type);
843 if (sparc64_structure_or_union_p (type))
845 /* Structure or Union arguments. */
846 gdb_assert (len <= 16);
847 memset (buf, 0, sizeof (buf));
848 valbuf = memcpy (buf, valbuf, len);
850 if (element % 2 && sparc64_16_byte_align_p (type))
855 regnum = SPARC_O0_REGNUM + element;
856 if (len > 8 && element < 5)
857 regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
861 sparc64_store_floating_fields (regcache, type, valbuf, element, 0);
863 else if (sparc64_floating_p (type))
865 /* Floating arguments. */
871 regnum = SPARC64_Q0_REGNUM + element / 2;
876 regnum = SPARC64_D0_REGNUM + element;
880 /* The psABI says "Each single-precision parameter value
881 will be assigned to one extended word in the
882 parameter array, and right-justified within that
883 word; the left half (even floatregister) is
884 undefined." Even though the psABI says that "the
885 left half is undefined", set it to zero here. */
887 memcpy (buf + 4, valbuf, 4);
891 regnum = SPARC64_D0_REGNUM + element;
896 /* Integral and pointer arguments. */
897 gdb_assert (len == 8);
899 regnum = SPARC_O0_REGNUM + element;
904 regcache_cooked_write (regcache, regnum, valbuf);
906 /* If we're storing the value in a floating-point register,
907 also store it in the corresponding %0 register(s). */
908 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D10_REGNUM)
910 gdb_assert (element < 6);
911 regnum = SPARC_O0_REGNUM + element;
912 regcache_cooked_write (regcache, regnum, valbuf);
914 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q8_REGNUM)
916 gdb_assert (element < 6);
917 regnum = SPARC_O0_REGNUM + element;
918 regcache_cooked_write (regcache, regnum, valbuf);
919 regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
923 /* Always store the argument in memory. */
924 write_memory (sp + element * 8, valbuf, len);
925 element += ((len + 7) / 8);
928 gdb_assert (element == num_elements);
930 /* Take BIAS into account. */
936 sparc64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
937 struct regcache *regcache, CORE_ADDR bp_addr,
938 int nargs, struct value **args, CORE_ADDR sp,
939 int struct_return, CORE_ADDR struct_addr)
941 /* Set return address. */
942 regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, bp_addr - 8);
944 /* Set up function arguments. */
945 sp = sparc64_store_arguments (regcache, nargs, args, sp,
946 struct_return, struct_addr);
948 /* Allocate the register save area. */
951 /* Stack should be 16-byte aligned at this point. */
952 gdb_assert ((sp + BIAS) % 16 == 0);
954 /* Finally, update the stack pointer. */
955 regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
961 /* Extract from an array REGBUF containing the (raw) register state, a
962 function return value of TYPE, and copy that into VALBUF. */
965 sparc64_extract_return_value (struct type *type, struct regcache *regcache,
968 int len = TYPE_LENGTH (type);
972 if (sparc64_structure_or_union_p (type))
974 /* Structure or Union return values. */
975 gdb_assert (len <= 32);
977 for (i = 0; i < ((len + 7) / 8); i++)
978 regcache_cooked_read (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
979 if (TYPE_CODE (type) != TYPE_CODE_UNION)
980 sparc64_extract_floating_fields (regcache, type, buf, 0);
981 memcpy (valbuf, buf, len);
983 else if (sparc64_floating_p (type))
985 /* Floating return values. */
986 for (i = 0; i < len / 4; i++)
987 regcache_cooked_read (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
988 memcpy (valbuf, buf, len);
990 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
992 /* Small arrays are returned the same way as small structures. */
993 gdb_assert (len <= 32);
995 for (i = 0; i < ((len + 7) / 8); i++)
996 regcache_cooked_read (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
997 memcpy (valbuf, buf, len);
1001 /* Integral and pointer return values. */
1002 gdb_assert (sparc64_integral_or_pointer_p (type));
1004 /* Just stripping off any unused bytes should preserve the
1005 signed-ness just fine. */
1006 regcache_cooked_read (regcache, SPARC_O0_REGNUM, buf);
1007 memcpy (valbuf, buf + 8 - len, len);
1011 /* Write into the appropriate registers a function return value stored
1012 in VALBUF of type TYPE. */
1015 sparc64_store_return_value (struct type *type, struct regcache *regcache,
1016 const gdb_byte *valbuf)
1018 int len = TYPE_LENGTH (type);
1022 if (sparc64_structure_or_union_p (type))
1024 /* Structure or Union return values. */
1025 gdb_assert (len <= 32);
1027 /* Simplify matters by storing the complete value (including
1028 floating members) into %o0 and %o1. Floating members are
1029 also store in the appropriate floating-point registers. */
1030 memset (buf, 0, sizeof (buf));
1031 memcpy (buf, valbuf, len);
1032 for (i = 0; i < ((len + 7) / 8); i++)
1033 regcache_cooked_write (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1034 if (TYPE_CODE (type) != TYPE_CODE_UNION)
1035 sparc64_store_floating_fields (regcache, type, buf, 0, 0);
1037 else if (sparc64_floating_p (type))
1039 /* Floating return values. */
1040 memcpy (buf, valbuf, len);
1041 for (i = 0; i < len / 4; i++)
1042 regcache_cooked_write (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
1044 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1046 /* Small arrays are returned the same way as small structures. */
1047 gdb_assert (len <= 32);
1049 memset (buf, 0, sizeof (buf));
1050 memcpy (buf, valbuf, len);
1051 for (i = 0; i < ((len + 7) / 8); i++)
1052 regcache_cooked_write (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1056 /* Integral and pointer return values. */
1057 gdb_assert (sparc64_integral_or_pointer_p (type));
1059 /* ??? Do we need to do any sign-extension here? */
1061 memcpy (buf + 8 - len, valbuf, len);
1062 regcache_cooked_write (regcache, SPARC_O0_REGNUM, buf);
1066 static enum return_value_convention
1067 sparc64_return_value (struct gdbarch *gdbarch, struct type *func_type,
1068 struct type *type, struct regcache *regcache,
1069 gdb_byte *readbuf, const gdb_byte *writebuf)
1071 if (TYPE_LENGTH (type) > 32)
1072 return RETURN_VALUE_STRUCT_CONVENTION;
1075 sparc64_extract_return_value (type, regcache, readbuf);
1077 sparc64_store_return_value (type, regcache, writebuf);
1079 return RETURN_VALUE_REGISTER_CONVENTION;
1084 sparc64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1085 struct dwarf2_frame_state_reg *reg,
1086 struct frame_info *this_frame)
1090 case SPARC_G0_REGNUM:
1091 /* Since %g0 is always zero, there is no point in saving it, and
1092 people will be inclined omit it from the CFI. Make sure we
1093 don't warn about that. */
1094 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1096 case SPARC_SP_REGNUM:
1097 reg->how = DWARF2_FRAME_REG_CFA;
1099 case SPARC64_PC_REGNUM:
1100 reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1101 reg->loc.offset = 8;
1103 case SPARC64_NPC_REGNUM:
1104 reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1105 reg->loc.offset = 12;
1111 sparc64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1113 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1115 tdep->pc_regnum = SPARC64_PC_REGNUM;
1116 tdep->npc_regnum = SPARC64_NPC_REGNUM;
1118 /* This is what all the fuss is about. */
1119 set_gdbarch_long_bit (gdbarch, 64);
1120 set_gdbarch_long_long_bit (gdbarch, 64);
1121 set_gdbarch_ptr_bit (gdbarch, 64);
1123 set_gdbarch_num_regs (gdbarch, SPARC64_NUM_REGS);
1124 set_gdbarch_register_name (gdbarch, sparc64_register_name);
1125 set_gdbarch_register_type (gdbarch, sparc64_register_type);
1126 set_gdbarch_num_pseudo_regs (gdbarch, SPARC64_NUM_PSEUDO_REGS);
1127 set_gdbarch_pseudo_register_read (gdbarch, sparc64_pseudo_register_read);
1128 set_gdbarch_pseudo_register_write (gdbarch, sparc64_pseudo_register_write);
1130 /* Register numbers of various important registers. */
1131 set_gdbarch_pc_regnum (gdbarch, SPARC64_PC_REGNUM); /* %pc */
1133 /* Call dummy code. */
1134 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1135 set_gdbarch_push_dummy_code (gdbarch, NULL);
1136 set_gdbarch_push_dummy_call (gdbarch, sparc64_push_dummy_call);
1138 set_gdbarch_return_value (gdbarch, sparc64_return_value);
1139 set_gdbarch_stabs_argument_has_addr
1140 (gdbarch, default_stabs_argument_has_addr);
1142 set_gdbarch_skip_prologue (gdbarch, sparc64_skip_prologue);
1144 /* Hook in the DWARF CFI frame unwinder. */
1145 dwarf2_frame_set_init_reg (gdbarch, sparc64_dwarf2_frame_init_reg);
1146 /* FIXME: kettenis/20050423: Don't enable the unwinder until the
1147 StackGhost issues have been resolved. */
1149 frame_unwind_append_unwinder (gdbarch, &sparc64_frame_unwind);
1150 frame_base_set_default (gdbarch, &sparc64_frame_base);
1154 /* Helper functions for dealing with register sets. */
1156 #define TSTATE_CWP 0x000000000000001fULL
1157 #define TSTATE_ICC 0x0000000f00000000ULL
1158 #define TSTATE_XCC 0x000000f000000000ULL
1160 #define PSR_S 0x00000080
1161 #define PSR_ICC 0x00f00000
1162 #define PSR_VERS 0x0f000000
1163 #define PSR_IMPL 0xf0000000
1164 #define PSR_V8PLUS 0xff000000
1165 #define PSR_XCC 0x000f0000
1168 sparc64_supply_gregset (const struct sparc_gregset *gregset,
1169 struct regcache *regcache,
1170 int regnum, const void *gregs)
1172 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1173 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1174 int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
1175 const gdb_byte *regs = gregs;
1180 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1182 int offset = gregset->r_tstate_offset;
1183 ULONGEST tstate, psr;
1186 tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
1187 psr = ((tstate & TSTATE_CWP) | PSR_S | ((tstate & TSTATE_ICC) >> 12)
1188 | ((tstate & TSTATE_XCC) >> 20) | PSR_V8PLUS);
1189 store_unsigned_integer (buf, 4, byte_order, psr);
1190 regcache_raw_supply (regcache, SPARC32_PSR_REGNUM, buf);
1193 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1194 regcache_raw_supply (regcache, SPARC32_PC_REGNUM,
1195 regs + gregset->r_pc_offset + 4);
1197 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1198 regcache_raw_supply (regcache, SPARC32_NPC_REGNUM,
1199 regs + gregset->r_npc_offset + 4);
1201 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
1203 int offset = gregset->r_y_offset + 8 - gregset->r_y_size;
1204 regcache_raw_supply (regcache, SPARC32_Y_REGNUM, regs + offset);
1209 if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
1210 regcache_raw_supply (regcache, SPARC64_STATE_REGNUM,
1211 regs + gregset->r_tstate_offset);
1213 if (regnum == SPARC64_PC_REGNUM || regnum == -1)
1214 regcache_raw_supply (regcache, SPARC64_PC_REGNUM,
1215 regs + gregset->r_pc_offset);
1217 if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
1218 regcache_raw_supply (regcache, SPARC64_NPC_REGNUM,
1219 regs + gregset->r_npc_offset);
1221 if (regnum == SPARC64_Y_REGNUM || regnum == -1)
1226 memcpy (buf + 8 - gregset->r_y_size,
1227 regs + gregset->r_y_offset, gregset->r_y_size);
1228 regcache_raw_supply (regcache, SPARC64_Y_REGNUM, buf);
1231 if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
1232 && gregset->r_fprs_offset != -1)
1233 regcache_raw_supply (regcache, SPARC64_FPRS_REGNUM,
1234 regs + gregset->r_fprs_offset);
1237 if (regnum == SPARC_G0_REGNUM || regnum == -1)
1238 regcache_raw_supply (regcache, SPARC_G0_REGNUM, NULL);
1240 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1242 int offset = gregset->r_g1_offset;
1247 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
1249 if (regnum == i || regnum == -1)
1250 regcache_raw_supply (regcache, i, regs + offset);
1255 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1257 /* Not all of the register set variants include Locals and
1258 Inputs. For those that don't, we read them off the stack. */
1259 if (gregset->r_l0_offset == -1)
1263 regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1264 sparc_supply_rwindow (regcache, sp, regnum);
1268 int offset = gregset->r_l0_offset;
1273 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1275 if (regnum == i || regnum == -1)
1276 regcache_raw_supply (regcache, i, regs + offset);
1284 sparc64_collect_gregset (const struct sparc_gregset *gregset,
1285 const struct regcache *regcache,
1286 int regnum, void *gregs)
1288 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1289 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1290 int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
1291 gdb_byte *regs = gregs;
1296 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1298 int offset = gregset->r_tstate_offset;
1299 ULONGEST tstate, psr;
1302 tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
1303 regcache_raw_collect (regcache, SPARC32_PSR_REGNUM, buf);
1304 psr = extract_unsigned_integer (buf, 4, byte_order);
1305 tstate |= (psr & PSR_ICC) << 12;
1306 if ((psr & (PSR_VERS | PSR_IMPL)) == PSR_V8PLUS)
1307 tstate |= (psr & PSR_XCC) << 20;
1308 store_unsigned_integer (buf, 8, byte_order, tstate);
1309 memcpy (regs + offset, buf, 8);
1312 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1313 regcache_raw_collect (regcache, SPARC32_PC_REGNUM,
1314 regs + gregset->r_pc_offset + 4);
1316 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1317 regcache_raw_collect (regcache, SPARC32_NPC_REGNUM,
1318 regs + gregset->r_npc_offset + 4);
1320 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
1322 int offset = gregset->r_y_offset + 8 - gregset->r_y_size;
1323 regcache_raw_collect (regcache, SPARC32_Y_REGNUM, regs + offset);
1328 if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
1329 regcache_raw_collect (regcache, SPARC64_STATE_REGNUM,
1330 regs + gregset->r_tstate_offset);
1332 if (regnum == SPARC64_PC_REGNUM || regnum == -1)
1333 regcache_raw_collect (regcache, SPARC64_PC_REGNUM,
1334 regs + gregset->r_pc_offset);
1336 if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
1337 regcache_raw_collect (regcache, SPARC64_NPC_REGNUM,
1338 regs + gregset->r_npc_offset);
1340 if (regnum == SPARC64_Y_REGNUM || regnum == -1)
1344 regcache_raw_collect (regcache, SPARC64_Y_REGNUM, buf);
1345 memcpy (regs + gregset->r_y_offset,
1346 buf + 8 - gregset->r_y_size, gregset->r_y_size);
1349 if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
1350 && gregset->r_fprs_offset != -1)
1351 regcache_raw_collect (regcache, SPARC64_FPRS_REGNUM,
1352 regs + gregset->r_fprs_offset);
1356 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1358 int offset = gregset->r_g1_offset;
1363 /* %g0 is always zero. */
1364 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
1366 if (regnum == i || regnum == -1)
1367 regcache_raw_collect (regcache, i, regs + offset);
1372 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1374 /* Not all of the register set variants include Locals and
1375 Inputs. For those that don't, we read them off the stack. */
1376 if (gregset->r_l0_offset != -1)
1378 int offset = gregset->r_l0_offset;
1383 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1385 if (regnum == i || regnum == -1)
1386 regcache_raw_collect (regcache, i, regs + offset);
1394 sparc64_supply_fpregset (struct regcache *regcache,
1395 int regnum, const void *fpregs)
1397 int sparc32 = (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32);
1398 const gdb_byte *regs = fpregs;
1401 for (i = 0; i < 32; i++)
1403 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
1404 regcache_raw_supply (regcache, SPARC_F0_REGNUM + i, regs + (i * 4));
1409 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
1410 regcache_raw_supply (regcache, SPARC32_FSR_REGNUM,
1411 regs + (32 * 4) + (16 * 8) + 4);
1415 for (i = 0; i < 16; i++)
1417 if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
1418 regcache_raw_supply (regcache, SPARC64_F32_REGNUM + i,
1419 regs + (32 * 4) + (i * 8));
1422 if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
1423 regcache_raw_supply (regcache, SPARC64_FSR_REGNUM,
1424 regs + (32 * 4) + (16 * 8));
1429 sparc64_collect_fpregset (const struct regcache *regcache,
1430 int regnum, void *fpregs)
1432 int sparc32 = (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32);
1433 gdb_byte *regs = fpregs;
1436 for (i = 0; i < 32; i++)
1438 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
1439 regcache_raw_collect (regcache, SPARC_F0_REGNUM + i, regs + (i * 4));
1444 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
1445 regcache_raw_collect (regcache, SPARC32_FSR_REGNUM,
1446 regs + (32 * 4) + (16 * 8) + 4);
1450 for (i = 0; i < 16; i++)
1452 if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
1453 regcache_raw_collect (regcache, SPARC64_F32_REGNUM + i,
1454 regs + (32 * 4) + (i * 8));
1457 if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
1458 regcache_raw_collect (regcache, SPARC64_FSR_REGNUM,
1459 regs + (32 * 4) + (16 * 8));