1 /* Target-dependent code for UltraSPARC.
3 Copyright 2003 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "arch-utils.h"
24 #include "floatformat.h"
26 #include "frame-base.h"
27 #include "frame-unwind.h"
35 #include "gdb_assert.h"
36 #include "gdb_string.h"
38 #include "sparc64-tdep.h"
40 /* This file implements the The SPARC 64-bit ABI as defined by the
41 section "Low-Level System Information" of the SPARC Compliance
42 Definition (SCD) 2.4.1, which is the 64-bit System V psABI for
45 /* Please use the sparc32_-prefix for 32-bit specific code, the
46 sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
47 code can handle both. */
49 /* The stack pointer is offset from the stack frame by a BIAS of 2047
50 (0x7ff) for 64-bit code. BIAS is likely to be defined on SPARC
51 hosts, so undefine it first. */
55 /* Macros to extract fields from SPARC instructions. */
56 #define X_OP(i) (((i) >> 30) & 0x3)
57 #define X_A(i) (((i) >> 29) & 1)
58 #define X_COND(i) (((i) >> 25) & 0xf)
59 #define X_OP2(i) (((i) >> 22) & 0x7)
60 #define X_IMM22(i) ((i) & 0x3fffff)
61 #define X_OP3(i) (((i) >> 19) & 0x3f)
62 /* Sign extension macros. */
63 #define X_DISP22(i) ((X_IMM22 (i) ^ 0x200000) - 0x200000)
64 #define X_DISP19(i) ((((i) & 0x7ffff) ^ 0x40000) - 0x40000)
66 /* Fetch the instruction at PC. Instructions are always big-endian
67 even if the processor operates in little-endian mode. */
70 sparc_fetch_instruction (CORE_ADDR pc)
76 read_memory (pc, buf, sizeof (buf));
79 for (i = 0; i < sizeof (buf); i++)
80 insn = (insn << 8) | buf[i];
84 /* The functions on this page are intended to be used to classify
85 function arguments. */
87 /* Return the contents if register REGNUM as an address. */
90 sparc_address_from_register (int regnum)
94 regcache_cooked_read_unsigned (current_regcache, regnum, &addr);
98 /* Check whether TYPE is "Integral or Pointer". */
101 sparc64_integral_or_pointer_p (const struct type *type)
103 switch (TYPE_CODE (type))
109 case TYPE_CODE_RANGE:
111 int len = TYPE_LENGTH (type);
112 gdb_assert (len == 1 || len == 2 || len == 4 || len == 8);
118 int len = TYPE_LENGTH (type);
119 gdb_assert (len == 8);
129 /* Check whether TYPE is "Floating". */
132 sparc64_floating_p (const struct type *type)
134 switch (TYPE_CODE (type))
138 int len = TYPE_LENGTH (type);
139 gdb_assert (len == 4 || len == 8 || len == 16);
149 /* Check whether TYPE is "Structure or Union". */
152 sparc64_structure_or_union_p (const struct type *type)
154 switch (TYPE_CODE (type))
156 case TYPE_CODE_STRUCT:
157 case TYPE_CODE_UNION:
166 /* UltraSPARC architecture specific information. */
170 /* Offset of saved PC in jmp_buf. */
174 /* Register information. */
176 struct sparc64_register_info
182 static struct sparc64_register_info sparc64_register_info[] =
184 { "g0", &builtin_type_int64 },
185 { "g1", &builtin_type_int64 },
186 { "g2", &builtin_type_int64 },
187 { "g3", &builtin_type_int64 },
188 { "g4", &builtin_type_int64 },
189 { "g5", &builtin_type_int64 },
190 { "g6", &builtin_type_int64 },
191 { "g7", &builtin_type_int64 },
193 { "o0", &builtin_type_int64 },
194 { "o1", &builtin_type_int64 },
195 { "o2", &builtin_type_int64 },
196 { "o3", &builtin_type_int64 },
197 { "o4", &builtin_type_int64 },
198 { "o5", &builtin_type_int64 },
199 { "sp", &builtin_type_void_data_ptr },
200 { "o7", &builtin_type_int64 },
202 { "l0", &builtin_type_int64 },
203 { "l1", &builtin_type_int64 },
204 { "l2", &builtin_type_int64 },
205 { "l3", &builtin_type_int64 },
206 { "l4", &builtin_type_int64 },
207 { "l5", &builtin_type_int64 },
208 { "l6", &builtin_type_int64 },
209 { "l7", &builtin_type_int64 },
211 { "i0", &builtin_type_int64 },
212 { "i1", &builtin_type_int64 },
213 { "i2", &builtin_type_int64 },
214 { "i3", &builtin_type_int64 },
215 { "i4", &builtin_type_int64 },
216 { "i5", &builtin_type_int64 },
217 { "fp", &builtin_type_void_data_ptr },
218 { "i7", &builtin_type_int64 },
220 { "f0", &builtin_type_float },
221 { "f1", &builtin_type_float },
222 { "f2", &builtin_type_float },
223 { "f3", &builtin_type_float },
224 { "f4", &builtin_type_float },
225 { "f5", &builtin_type_float },
226 { "f6", &builtin_type_float },
227 { "f7", &builtin_type_float },
228 { "f8", &builtin_type_float },
229 { "f9", &builtin_type_float },
230 { "f10", &builtin_type_float },
231 { "f11", &builtin_type_float },
232 { "f12", &builtin_type_float },
233 { "f13", &builtin_type_float },
234 { "f14", &builtin_type_float },
235 { "f15", &builtin_type_float },
236 { "f16", &builtin_type_float },
237 { "f17", &builtin_type_float },
238 { "f18", &builtin_type_float },
239 { "f19", &builtin_type_float },
240 { "f20", &builtin_type_float },
241 { "f21", &builtin_type_float },
242 { "f22", &builtin_type_float },
243 { "f23", &builtin_type_float },
244 { "f24", &builtin_type_float },
245 { "f25", &builtin_type_float },
246 { "f26", &builtin_type_float },
247 { "f27", &builtin_type_float },
248 { "f28", &builtin_type_float },
249 { "f29", &builtin_type_float },
250 { "f30", &builtin_type_float },
251 { "f31", &builtin_type_float },
252 { "f32", &builtin_type_double },
253 { "f34", &builtin_type_double },
254 { "f36", &builtin_type_double },
255 { "f38", &builtin_type_double },
256 { "f40", &builtin_type_double },
257 { "f42", &builtin_type_double },
258 { "f44", &builtin_type_double },
259 { "f46", &builtin_type_double },
260 { "f48", &builtin_type_double },
261 { "f50", &builtin_type_double },
262 { "f52", &builtin_type_double },
263 { "f54", &builtin_type_double },
264 { "f56", &builtin_type_double },
265 { "f58", &builtin_type_double },
266 { "f60", &builtin_type_double },
267 { "f62", &builtin_type_double },
269 { "pc", &builtin_type_void_func_ptr },
270 { "npc", &builtin_type_void_func_ptr },
272 /* This raw register contains the contents of %cwp, %pstate, %asi
273 and %ccr as laid out in a %tstate register. */
274 /* FIXME: Give it a name until we start using register groups. */
275 { "state", &builtin_type_int64 },
277 { "fsr", &builtin_type_int64 },
278 { "fprs", &builtin_type_int64 },
280 /* "Although Y is a 64-bit register, its high-order 32 bits are
281 reserved and always read as 0." */
282 { "y", &builtin_type_int64 }
285 /* Total number of registers. */
286 #define SPARC64_NUM_REGS \
287 (sizeof (sparc64_register_info) / sizeof (sparc64_register_info[0]))
289 /* We provide the aliases %d0..%d62 and %q0..%q60 for the floating
290 registers as "psuedo" registers. */
292 static struct sparc64_register_info sparc64_pseudo_register_info[] =
294 { "cwp", &builtin_type_int64 },
295 { "pstate", &builtin_type_int64 },
296 { "asi", &builtin_type_int64 },
297 { "ccr", &builtin_type_int64 },
299 { "d0", &builtin_type_double },
300 { "d2", &builtin_type_double },
301 { "d4", &builtin_type_double },
302 { "d6", &builtin_type_double },
303 { "d8", &builtin_type_double },
304 { "d10", &builtin_type_double },
305 { "d12", &builtin_type_double },
306 { "d14", &builtin_type_double },
307 { "d16", &builtin_type_double },
308 { "d18", &builtin_type_double },
309 { "d20", &builtin_type_double },
310 { "d22", &builtin_type_double },
311 { "d24", &builtin_type_double },
312 { "d26", &builtin_type_double },
313 { "d28", &builtin_type_double },
314 { "d30", &builtin_type_double },
315 { "d32", &builtin_type_double },
316 { "d34", &builtin_type_double },
317 { "d36", &builtin_type_double },
318 { "d38", &builtin_type_double },
319 { "d40", &builtin_type_double },
320 { "d42", &builtin_type_double },
321 { "d44", &builtin_type_double },
322 { "d46", &builtin_type_double },
323 { "d48", &builtin_type_double },
324 { "d50", &builtin_type_double },
325 { "d52", &builtin_type_double },
326 { "d54", &builtin_type_double },
327 { "d56", &builtin_type_double },
328 { "d58", &builtin_type_double },
329 { "d60", &builtin_type_double },
330 { "d62", &builtin_type_double },
332 { "q0", &builtin_type_long_double },
333 { "q4", &builtin_type_long_double },
334 { "q8", &builtin_type_long_double },
335 { "q12", &builtin_type_long_double },
336 { "q16", &builtin_type_long_double },
337 { "q20", &builtin_type_long_double },
338 { "q24", &builtin_type_long_double },
339 { "q28", &builtin_type_long_double },
340 { "q32", &builtin_type_long_double },
341 { "q36", &builtin_type_long_double },
342 { "q40", &builtin_type_long_double },
343 { "q44", &builtin_type_long_double },
344 { "q48", &builtin_type_long_double },
345 { "q52", &builtin_type_long_double },
346 { "q56", &builtin_type_long_double },
347 { "q60", &builtin_type_long_double }
350 /* Total number of pseudo registers. */
351 #define SPARC64_NUM_PSEUDO_REGS \
352 (sizeof (sparc64_pseudo_register_info) \
353 / sizeof (sparc64_pseudo_register_info[0]))
355 /* Return the name of register REGNUM. */
358 sparc64_register_name (int regnum)
360 if (regnum >= 0 && regnum < SPARC64_NUM_REGS)
361 return sparc64_register_info[regnum].name;
363 if (regnum >= SPARC64_NUM_REGS
364 && regnum < SPARC64_NUM_REGS + SPARC64_NUM_PSEUDO_REGS)
365 return sparc64_pseudo_register_info[regnum - SPARC64_NUM_REGS].name;
370 /* Return the GDB type object for the "standard" data type of data in
374 sparc64_register_type (struct gdbarch *gdbarch, int regnum)
376 if (regnum >= SPARC64_NUM_REGS
377 && regnum < SPARC64_NUM_REGS + SPARC64_NUM_PSEUDO_REGS)
378 return *sparc64_pseudo_register_info[regnum - SPARC64_NUM_REGS].type;
380 gdb_assert (regnum >= 0 && regnum < SPARC64_NUM_REGS);
381 return *sparc64_register_info[regnum].type;
385 sparc64_pseudo_register_read (struct gdbarch *gdbarch,
386 struct regcache *regcache,
387 int regnum, void *buf)
389 gdb_assert (regnum >= SPARC64_NUM_REGS);
391 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
393 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
394 regcache_raw_read (regcache, regnum, buf);
395 regcache_raw_read (regcache, regnum + 1, ((char *)buf) + 4);
397 else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
399 regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
400 regcache_raw_read (regcache, regnum, buf);
402 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
404 regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
405 regcache_raw_read (regcache, regnum, buf);
406 regcache_raw_read (regcache, regnum + 1, ((char *)buf) + 4);
407 regcache_raw_read (regcache, regnum + 2, ((char *)buf) + 8);
408 regcache_raw_read (regcache, regnum + 3, ((char *)buf) + 12);
410 else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
412 regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
413 regcache_raw_read (regcache, regnum, buf);
414 regcache_raw_read (regcache, regnum + 1, ((char *)buf) + 8);
416 else if (regnum == SPARC64_CWP_REGNUM
417 || regnum == SPARC64_PSTATE_REGNUM
418 || regnum == SPARC64_ASI_REGNUM
419 || regnum == SPARC64_CCR_REGNUM)
423 regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
426 case SPARC64_CWP_REGNUM:
427 state = (state >> 0) & ((1 << 5) - 1);
429 case SPARC64_PSTATE_REGNUM:
430 state = (state >> 8) & ((1 << 12) - 1);
432 case SPARC64_ASI_REGNUM:
433 state = (state >> 24) & ((1 << 8) - 1);
435 case SPARC64_CCR_REGNUM:
436 state = (state >> 32) & ((1 << 8) - 1);
439 store_unsigned_integer (buf, 8, state);
444 sparc64_pseudo_register_write (struct gdbarch *gdbarch,
445 struct regcache *regcache,
446 int regnum, const void *buf)
448 gdb_assert (regnum >= SPARC64_NUM_REGS);
450 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
452 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
453 regcache_raw_write (regcache, regnum, buf);
454 regcache_raw_write (regcache, regnum + 1, ((const char *)buf) + 4);
456 else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
458 regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
459 regcache_raw_write (regcache, regnum, buf);
461 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
463 regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
464 regcache_raw_write (regcache, regnum, buf);
465 regcache_raw_write (regcache, regnum + 1, ((const char *)buf) + 4);
466 regcache_raw_write (regcache, regnum + 2, ((const char *)buf) + 8);
467 regcache_raw_write (regcache, regnum + 3, ((const char *)buf) + 12);
469 else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
471 regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
472 regcache_raw_write (regcache, regnum, buf);
473 regcache_raw_write (regcache, regnum + 1, ((const char *)buf) + 8);
475 else if (regnum == SPARC64_CWP_REGNUM
476 || regnum == SPARC64_PSTATE_REGNUM
477 || regnum == SPARC64_ASI_REGNUM
478 || regnum == SPARC64_CCR_REGNUM)
480 ULONGEST state, bits;
482 regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
483 bits = extract_unsigned_integer (buf, 8);
486 case SPARC64_CWP_REGNUM:
487 state |= ((bits & ((1 << 5) - 1)) << 0);
489 case SPARC64_PSTATE_REGNUM:
490 state |= ((bits & ((1 << 12) - 1)) << 8);
492 case SPARC64_ASI_REGNUM:
493 state |= ((bits & ((1 << 8) - 1)) << 24);
495 case SPARC64_CCR_REGNUM:
496 state |= ((bits & ((1 << 8) - 1)) << 32);
499 regcache_raw_write_unsigned (regcache, SPARC64_STATE_REGNUM, state);
503 /* Use the program counter to determine the contents and size of a
504 breakpoint instruction. Return a pointer to a string of bytes that
505 encode a breakpoint instruction, store the length of the string in
506 *LEN and optionally adjust *PC to point to the correct memory
507 location for inserting the breakpoint. */
509 static const unsigned char *
510 sparc_breakpoint_from_pc (CORE_ADDR *pc, int *len)
512 static unsigned char break_insn[] = { 0x91, 0xd0, 0x20, 0x01 };
514 *len = sizeof (break_insn);
519 struct sparc64_frame_cache
525 /* Do we have a frame? */
529 /* Allocate and initialize a frame cache. */
531 static struct sparc64_frame_cache *
532 sparc64_alloc_frame_cache (void)
534 struct sparc64_frame_cache *cache;
537 cache = FRAME_OBSTACK_ZALLOC (struct sparc64_frame_cache);
543 /* Frameless until proven otherwise. */
544 cache->frameless_p = 1;
550 sparc64_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
551 struct sparc64_frame_cache *cache)
555 if (current_pc <= pc)
558 /* Check whether the function starts with a SAVE instruction. */
559 insn = sparc_fetch_instruction (pc);
560 if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3c)
562 cache->frameless_p = 0;
570 sparc64_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
572 return frame_unwind_register_unsigned (next_frame, SPARC64_PC_REGNUM);
575 /* Return PC of first real instruction of the function starting at
579 sparc64_skip_prologue (CORE_ADDR start_pc)
581 struct symtab_and_line sal;
582 CORE_ADDR func_start, func_end;
583 struct sparc64_frame_cache cache;
585 /* This is the preferred method, find the end of the prologue by
586 using the debugging information. */
587 if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
589 sal = find_pc_line (func_start, 0);
591 if (sal.end < func_end
592 && start_pc <= sal.end)
596 return sparc64_analyze_prologue (start_pc, 0xffffffffffffffffUL, &cache);
601 static struct sparc64_frame_cache *
602 sparc64_frame_cache (struct frame_info *next_frame, void **this_cache)
604 struct sparc64_frame_cache *cache;
609 cache = sparc64_alloc_frame_cache ();
612 /* In priciple, for normal frames, %fp (%i6) holds the frame
613 pointer, which holds the base address for the current stack
616 cache->base = frame_unwind_register_unsigned (next_frame, SPARC_FP_REGNUM);
617 if (cache->base == 0)
620 cache->pc = frame_func_unwind (next_frame);
622 sparc64_analyze_prologue (cache->pc, frame_pc_unwind (next_frame), cache);
624 if (cache->frameless_p)
626 /* We didn't find a valid frame, which means that CACHE->base
627 currently holds the frame pointer for our calling frame. */
628 cache->base = frame_unwind_register_unsigned (next_frame,
636 sparc64_frame_this_id (struct frame_info *next_frame, void **this_cache,
637 struct frame_id *this_id)
639 struct sparc64_frame_cache *cache =
640 sparc64_frame_cache (next_frame, this_cache);
642 /* This marks the outermost frame. */
643 if (cache->base == 0)
646 (*this_id) = frame_id_build (cache->base, cache->pc);
650 sparc64_frame_prev_register (struct frame_info *next_frame, void **this_cache,
651 int regnum, int *optimizedp,
652 enum lval_type *lvalp, CORE_ADDR *addrp,
653 int *realnump, void *valuep)
655 struct sparc64_frame_cache *cache =
656 sparc64_frame_cache (next_frame, this_cache);
658 if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
666 CORE_ADDR pc = (regnum == SPARC64_NPC_REGNUM) ? 4 : 0;
668 regnum = cache->frameless_p ? SPARC_O7_REGNUM : SPARC_I7_REGNUM;
669 pc += frame_unwind_register_unsigned (next_frame, regnum) + 8;
670 store_unsigned_integer (valuep, 8, pc);
675 /* The previous frame's `local' and `in' registers have been saved
676 in the register save area. */
677 if (!cache->frameless_p
678 && regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM)
681 *lvalp = lval_memory;
682 *addrp = cache->base + BIAS + (regnum - SPARC_L0_REGNUM) * 8;
686 struct gdbarch *gdbarch = get_frame_arch (next_frame);
688 /* Read the value in from memory. */
689 read_memory (*addrp, valuep, register_size (gdbarch, regnum));
694 /* The previous frame's `out' registers are accessable as the
695 current frame's `in' registers. */
696 if (!cache->frameless_p
697 && regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM)
698 regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
700 frame_register_unwind (next_frame, regnum,
701 optimizedp, lvalp, addrp, realnump, valuep);
704 static const struct frame_unwind sparc64_frame_unwind =
707 sparc64_frame_this_id,
708 sparc64_frame_prev_register
711 static const struct frame_unwind *
712 sparc64_frame_sniffer (struct frame_info *next_frame)
714 return &sparc64_frame_unwind;
719 sparc64_frame_base_address (struct frame_info *next_frame, void **this_cache)
721 struct sparc64_frame_cache *cache =
722 sparc64_frame_cache (next_frame, this_cache);
724 /* ??? Should we take BIAS into account here? */
728 static const struct frame_base sparc64_frame_base =
730 &sparc64_frame_unwind,
731 sparc64_frame_base_address,
732 sparc64_frame_base_address,
733 sparc64_frame_base_address
736 static struct frame_id
737 sparc_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
741 sp = frame_unwind_register_unsigned (next_frame, SPARC_SP_REGNUM);
742 return frame_id_build (sp, frame_pc_unwind (next_frame));
745 /* Check whether TYPE must be 16-byte aligned. */
748 sparc64_16_byte_align_p (struct type *type)
750 if (sparc64_floating_p (type) && TYPE_LENGTH (type) == 16)
753 if (sparc64_structure_or_union_p (type))
757 for (i = 0; i < TYPE_NFIELDS (type); i++)
758 if (sparc64_16_byte_align_p (TYPE_FIELD_TYPE (type, i)))
765 /* Store floating fields of element ELEMENT of an "parameter array"
766 that has type TYPE and is stored at BITPOS in VALBUF in the
767 apropriate registers of REGCACHE. This function can be called
768 recursively and therefore handles floating types in addition to
772 sparc64_store_floating_fields (struct regcache *regcache, struct type *type,
773 char *valbuf, int element, int bitpos)
775 gdb_assert (element < 16);
777 if (sparc64_floating_p (type))
779 int len = TYPE_LENGTH (type);
784 gdb_assert (bitpos == 0);
785 gdb_assert ((element % 2) == 0);
787 regnum = SPARC64_Q0_REGNUM + element / 2;
788 regcache_cooked_write (regcache, regnum, valbuf);
792 gdb_assert (bitpos == 0 || bitpos == 64);
794 regnum = SPARC64_D0_REGNUM + element + bitpos / 64;
795 regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
799 gdb_assert (len == 4);
800 gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 128);
802 regnum = SPARC_F0_REGNUM + element * 2 + bitpos / 32;
803 regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
806 else if (sparc64_structure_or_union_p (type))
810 for (i = 0; i < TYPE_NFIELDS (type); i++)
811 sparc64_store_floating_fields (regcache, TYPE_FIELD_TYPE (type, i),
813 bitpos + TYPE_FIELD_BITPOS (type, i));
817 /* Fetch floating fields from a variable of type TYPE from the
818 appropriate registers for BITPOS in REGCACHE and store it at BITPOS
819 in VALBUF. This function can be called recursively and therefore
820 handles floating types in addition to structures. */
823 sparc64_extract_floating_fields (struct regcache *regcache, struct type *type,
824 char *valbuf, int bitpos)
826 if (sparc64_floating_p (type))
828 int len = TYPE_LENGTH (type);
833 gdb_assert (bitpos == 0 || bitpos == 128);
835 regnum = SPARC64_Q0_REGNUM + bitpos / 128;
836 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
840 gdb_assert (bitpos % 64 == 0 && bitpos >= 0 && bitpos < 256);
842 regnum = SPARC64_D0_REGNUM + bitpos / 64;
843 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
847 gdb_assert (len == 4);
848 gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 256);
850 regnum = SPARC_F0_REGNUM + bitpos / 32;
851 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
854 else if (sparc64_structure_or_union_p (type))
858 for (i = 0; i < TYPE_NFIELDS (type); i++)
859 sparc64_extract_floating_fields (regcache, TYPE_FIELD_TYPE (type, i),
861 bitpos + TYPE_FIELD_BITPOS (type, i));
865 /* Store the NARGS arguments ARGS and STRUCT_ADDR (if STRUCT_RETURN is
866 non-zero) in REGCACHE and on the stack (starting from address SP). */
869 sparc64_store_arguments (struct regcache *regcache, int nargs,
870 struct value **args, CORE_ADDR sp,
871 int struct_return, CORE_ADDR struct_addr)
873 /* Number of extended words in the "parameter array". */
874 int num_elements = 0;
878 /* Take BIAS into account. */
881 /* First we calculate the number of extended words in the "parameter
882 array". While doing so we also convert some of the arguments. */
887 for (i = 0; i < nargs; i++)
889 struct type *type = VALUE_TYPE (args[i]);
890 int len = TYPE_LENGTH (type);
892 if (sparc64_structure_or_union_p (type))
894 /* Structure or Union arguments. */
897 if (num_elements % 2 && sparc64_16_byte_align_p (type))
899 num_elements += ((len + 7) / 8);
903 /* The psABI says that "Structures or unions larger than
904 sixteen bytes are copied by the caller and passed
905 indirectly; the caller will pass the address of a
906 correctly aligned structure value. This sixty-four
907 bit address will occupy one word in the parameter
908 array, and may be promoted to an %o register like any
909 other pointer value." Allocate memory for these
910 values on the stack. */
913 /* Use 16-byte alignment for these values. That's
914 always correct, and wasting a few bytes shouldn't be
918 write_memory (sp, VALUE_CONTENTS (args[i]), len);
919 args[i] = value_from_pointer (lookup_pointer_type (type), sp);
923 else if (sparc64_floating_p (type))
925 /* Floating arguments. */
929 /* The psABI says that "Each quad-precision parameter
930 value will be assigned to two extended words in the
934 /* The psABI says that "Long doubles must be
935 quad-aligned, and thus a hole might be introduced
936 into the parameter array to force alignment." Skip
937 an element if necessary. */
938 if (num_elements % 2)
946 /* Integral and pointer arguments. */
947 gdb_assert (sparc64_integral_or_pointer_p (type));
949 /* The psABI says that "Each argument value of integral type
950 smaller than an extended word will be widened by the
951 caller to an extended word according to the signed-ness
952 of the argument type." */
954 args[i] = value_cast (builtin_type_int64, args[i]);
959 /* Allocate the "parameter array". */
960 sp -= num_elements * 8;
962 /* The psABI says that "Every stack frame must be 16-byte aligned." */
965 /* Now we store the arguments in to the "paramater array". Some
966 Integer or Pointer arguments and Structure or Union arguments
967 will be passed in %o registers. Some Floating arguments and
968 floating members of structures are passed in floating-point
969 registers. However, for functions with variable arguments,
970 floating arguments are stored in an %0 register, and for
971 functions without a prototype floating arguments are stored in
972 both a floating-point and an %o registers, or a floating-point
973 register and memory. To simplify the logic here we always pass
974 arguments in memory, an %o register, and a floating-point
975 register if appropriate. This should be no problem since the
976 contents of any unused memory or registers in the "parameter
977 array" are undefined. */
981 regcache_cooked_write_unsigned (regcache, SPARC_O0_REGNUM, struct_addr);
985 for (i = 0; i < nargs; i++)
987 char *valbuf = VALUE_CONTENTS (args[i]);
988 struct type *type = VALUE_TYPE (args[i]);
989 int len = TYPE_LENGTH (type);
993 if (sparc64_structure_or_union_p (type))
995 /* Structure or Union arguments. */
996 gdb_assert (len <= 16);
997 memset (buf, 0, sizeof (buf));
998 valbuf = memcpy (buf, valbuf, len);
1000 if (element % 2 && sparc64_16_byte_align_p (type))
1005 regnum = SPARC_O0_REGNUM + element;
1006 if (len > 8 && element < 5)
1007 regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
1011 sparc64_store_floating_fields (regcache, type, valbuf, element, 0);
1013 else if (sparc64_floating_p (type))
1015 /* Floating arguments. */
1021 regnum = SPARC64_Q0_REGNUM + element / 2;
1026 regnum = SPARC64_D0_REGNUM + element;
1030 /* The psABI says "Each single-precision parameter value
1031 will be assigned to one extended word in the
1032 parameter array, and right-justified within that
1033 word; the left half (even floatregister) is
1034 undefined." Even though the psABI says that "the
1035 left half is undefined", set it to zero here. */
1037 valbuf = memcpy (buf + 4, valbuf, 4);
1040 regnum = SPARC64_D0_REGNUM;
1045 /* Integral and pointer arguments. */
1046 gdb_assert (len == 8);
1048 regnum = SPARC_O0_REGNUM + element;
1053 regcache_cooked_write (regcache, regnum, valbuf);
1055 /* If we're storing the value in a floating-point register,
1056 also store it in the corresponding %0 register(s). */
1057 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D10_REGNUM)
1059 gdb_assert (element < 6);
1060 regnum = SPARC_O0_REGNUM + element;
1061 regcache_cooked_write (regcache, regnum, valbuf);
1063 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q8_REGNUM)
1065 gdb_assert (element < 6);
1066 regnum = SPARC_O0_REGNUM + element;
1067 regcache_cooked_write (regcache, regnum, valbuf);
1068 regcache_cooked_write (regcache, regnum + 1, valbuf);
1072 /* Always store the argument in memeory. */
1073 write_memory (sp + element * 8, valbuf, len);
1074 element += ((len + 7) / 8);
1077 gdb_assert (element == num_elements);
1079 /* Take BIAS into account. */
1085 sparc64_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
1086 struct regcache *regcache, CORE_ADDR bp_addr,
1087 int nargs, struct value **args, CORE_ADDR sp,
1088 int struct_return, CORE_ADDR struct_addr)
1090 /* Set return address. */
1091 regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, bp_addr - 8);
1093 /* Set up function arguments. */
1094 sp = sparc64_store_arguments (regcache, nargs, args, sp,
1095 struct_return, struct_addr);
1097 /* Allocate the register save area. */
1100 /* Stack should be 16-byte aligned at this point. */
1101 gdb_assert ((sp + BIAS) % 16 == 0);
1103 /* Finally, update the stack pointer. */
1104 regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
1110 /* Extract from an array REGBUF containing the (raw) register state, a
1111 function return value of TYPE, and copy that into VALBUF. */
1114 sparc64_extract_return_value (struct type *type, struct regcache *regcache,
1117 int len = TYPE_LENGTH (type);
1121 if (sparc64_structure_or_union_p (type))
1123 /* Structure or Union return values. */
1124 gdb_assert (len <= 32);
1126 for (i = 0; i < ((len + 7) / 8); i++)
1127 regcache_cooked_read (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1128 if (TYPE_CODE (type) != TYPE_CODE_UNION)
1129 sparc64_extract_floating_fields (regcache, type, buf, 0);
1130 memcpy (valbuf, buf, len);
1132 else if (sparc64_floating_p (type))
1134 /* Floating return values. */
1135 for (i = 0; i < len / 4; i++)
1136 regcache_cooked_read (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
1137 memcpy (valbuf, buf, len);
1141 /* Integral and pointer return values. */
1142 gdb_assert (sparc64_integral_or_pointer_p (type));
1144 /* Just stripping off any unused bytes should preserve the
1145 signed-ness just fine. */
1146 regcache_cooked_read (regcache, SPARC_O0_REGNUM, buf);
1147 memcpy (valbuf, buf + 8 - len, len);
1151 /* Write into the appropriate registers a function return value stored
1152 in VALBUF of type TYPE. */
1155 sparc64_store_return_value (struct type *type, struct regcache *regcache,
1158 int len = TYPE_LENGTH (type);
1162 if (sparc64_structure_or_union_p (type))
1164 /* Structure or Union return values. */
1165 gdb_assert (len <= 32);
1167 /* Simplify matters by storing the complete value (including
1168 floating members) into %o0 and %o1. Floating members are
1169 also store in the appropriate floating-point registers. */
1170 memset (buf, 0, sizeof (buf));
1171 memcpy (buf, valbuf, len);
1172 for (i = 0; i < ((len + 7) / 8); i++)
1173 regcache_cooked_write (regcache, SPARC_O0_REGNUM + i, buf + i * 4);
1174 if (TYPE_CODE (type) != TYPE_CODE_UNION)
1175 sparc64_store_floating_fields (regcache, type, buf, 0, 0);
1177 else if (sparc64_floating_p (type))
1179 /* Floating return values. */
1180 memcpy (buf, valbuf, len);
1181 for (i = 0; i < len / 4; i++)
1182 regcache_cooked_write (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
1186 /* Integral and pointer return values. */
1187 gdb_assert (sparc64_integral_or_pointer_p (type));
1189 /* ??? Do we need to do any sign-extension here? */
1191 memcpy (buf + 8 - len, valbuf, len);
1192 regcache_cooked_write (regcache, SPARC_O0_REGNUM, buf);
1196 /* Extract from REGCACHE, which contains the (raw) register state, the
1197 address in which a function should return its structure value, as a
1201 sparc_extract_struct_value_address (struct regcache *regcache)
1205 regcache_cooked_read_unsigned (regcache, SPARC_O0_REGNUM, &addr);
1210 sparc64_use_struct_convention (int gcc_p, struct type *type)
1212 /* Structure and union types up to 32 bytes in size are returned in
1214 return (TYPE_LENGTH (type) > 32);
1218 /* The SPARC Architecture doesn't have hardware single-step support,
1219 and most operating systems don't implement it either, so we provide
1220 software single-step mechanism. */
1223 sparc_analyze_control_transfer (CORE_ADDR pc, CORE_ADDR *npc)
1225 unsigned long insn = sparc_fetch_instruction (pc);
1226 int conditional_p = X_COND (insn) & 0x7;
1228 long offset = 0; /* Must be signed for sign-extend. */
1230 if (X_OP (insn) == 0 && X_OP2 (insn) == 3 && (insn & 0x1000000) == 0)
1232 /* Branch on Integer Register with Prediction (BPr). */
1236 else if (X_OP (insn) == 0 && X_OP2 (insn) == 6)
1238 /* Branch on Floating-Point Condition Codes (FBfcc). */
1240 offset = 4 * X_DISP22 (insn);
1242 else if (X_OP (insn) == 0 && X_OP2 (insn) == 5)
1244 /* Branch on Floating-Point Condition Codes with Prediction
1247 offset = 4 * X_DISP19 (insn);
1249 else if (X_OP (insn) == 0 && X_OP2 (insn) == 2)
1251 /* Branch on Integer Condition Codes (Bicc). */
1253 offset = 4 * X_DISP22 (insn);
1255 else if (X_OP (insn) == 0 && X_OP2 (insn) == 1)
1257 /* Branch on Integer Condition Codes with Prediction (BPcc). */
1259 offset = 4 * X_DISP19 (insn);
1262 /* FIXME: Handle DONE and RETRY instructions. */
1264 /* FIXME: Handle the Trap instruction. */
1270 /* For conditional branches, return nPC + 4 iff the annul
1272 return (X_A (insn) ? *npc + 4 : 0);
1276 /* For unconditional branches, return the target if its
1277 specified condition is "always" and return nPC + 4 if the
1278 condition is "never". If the annul bit is 1, set *NPC to
1280 if (X_COND (insn) == 0x0)
1281 pc = *npc, offset = 4;
1285 gdb_assert (offset != 0);
1294 sparc_software_single_step (enum target_signal sig, int insert_breakpoints_p)
1296 static CORE_ADDR npc, nnpc;
1297 static char npc_save[4], nnpc_save[4];
1299 if (insert_breakpoints_p)
1303 pc = sparc_address_from_register (SPARC64_PC_REGNUM);
1304 npc = sparc_address_from_register (SPARC64_NPC_REGNUM);
1306 /* Analyze the instruction at PC. */
1307 nnpc = sparc_analyze_control_transfer (pc, &npc);
1309 target_insert_breakpoint (npc, npc_save);
1311 target_insert_breakpoint (nnpc, nnpc_save);
1313 /* Assert that we have set at least one breakpoint. */
1314 gdb_assert (npc != 0 || nnpc != 0);
1319 target_remove_breakpoint (npc, npc_save);
1321 target_remove_breakpoint (nnpc, nnpc_save);
1329 static struct gdbarch *
1330 sparc64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1332 struct gdbarch_tdep *tdep;
1333 struct gdbarch *gdbarch;
1335 /* If there is already a candidate, use it. */
1336 arches = gdbarch_list_lookup_by_info (arches, &info);
1338 return arches->gdbarch;
1340 /* Allocate space for the new architecture. */
1341 tdep = XMALLOC (struct gdbarch_tdep);
1342 gdbarch = gdbarch_alloc (&info, tdep);
1344 set_gdbarch_long_bit (gdbarch, 64);
1345 set_gdbarch_long_long_bit (gdbarch, 64);
1346 set_gdbarch_ptr_bit (gdbarch, 64);
1347 set_gdbarch_long_double_bit (gdbarch, 128);
1349 set_gdbarch_num_regs (gdbarch, SPARC64_NUM_REGS);
1350 set_gdbarch_register_name (gdbarch, sparc64_register_name);
1351 set_gdbarch_register_type (gdbarch, sparc64_register_type);
1352 set_gdbarch_num_pseudo_regs (gdbarch, SPARC64_NUM_PSEUDO_REGS);
1353 set_gdbarch_pseudo_register_read (gdbarch, sparc64_pseudo_register_read);
1354 set_gdbarch_pseudo_register_write (gdbarch, sparc64_pseudo_register_write);
1356 /* Register numbers of various important registers. */
1357 set_gdbarch_sp_regnum (gdbarch, SPARC_SP_REGNUM); /* %sp */
1358 set_gdbarch_pc_regnum (gdbarch, SPARC64_PC_REGNUM); /* %pc */
1359 set_gdbarch_deprecated_npc_regnum (gdbarch, SPARC64_NPC_REGNUM);
1360 set_gdbarch_fp0_regnum (gdbarch, SPARC_F0_REGNUM); /* %f0 */
1362 /* Call dummy code. */
1363 set_gdbarch_push_dummy_call (gdbarch, sparc64_push_dummy_call);
1365 set_gdbarch_extract_return_value (gdbarch, sparc64_extract_return_value);
1366 set_gdbarch_store_return_value (gdbarch, sparc64_store_return_value);
1367 set_gdbarch_extract_struct_value_address
1368 (gdbarch, sparc_extract_struct_value_address);
1369 set_gdbarch_use_struct_convention (gdbarch, sparc64_use_struct_convention);
1371 set_gdbarch_skip_prologue (gdbarch, sparc64_skip_prologue);
1373 /* Stack grows downward. */
1374 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1376 set_gdbarch_breakpoint_from_pc (gdbarch, sparc_breakpoint_from_pc);
1377 set_gdbarch_decr_pc_after_break (gdbarch, 0);
1378 set_gdbarch_function_start_offset (gdbarch, 0);
1380 set_gdbarch_frame_args_skip (gdbarch, 8);
1382 set_gdbarch_print_insn (gdbarch, print_insn_sparc);
1384 set_gdbarch_software_single_step (gdbarch, sparc_software_single_step);
1386 set_gdbarch_unwind_dummy_id (gdbarch, sparc_unwind_dummy_id);
1388 set_gdbarch_unwind_pc (gdbarch, sparc64_unwind_pc);
1390 frame_base_set_default (gdbarch, &sparc64_frame_base);
1392 /* Hook in ABI-specific overrides, if they have been registered. */
1393 gdbarch_init_osabi (info, gdbarch);
1395 frame_unwind_append_sniffer (gdbarch, sparc64_frame_sniffer);
1400 /* Helper functions for dealing with register windows. */
1403 sparc_supply_rwindow (CORE_ADDR sp, int regnum)
1411 /* Registers are 64-bit. */
1414 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1416 if (regnum == i || regnum == -1)
1418 target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
1419 supply_register (i, buf);
1425 /* Registers are 32-bit. Toss any sign-extension of the stack
1429 /* Clear out the top half of the temporary buffer, and put the
1430 register value in the bottom half if we're in 64-bit mode. */
1431 if (gdbarch_ptr_bit (current_gdbarch) == 64)
1437 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1439 if (regnum == i || regnum == -1)
1441 target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
1443 supply_register (i, buf);
1450 sparc_fill_rwindow (CORE_ADDR sp, int regnum)
1458 /* Registers are 64-bit. */
1461 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1463 if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
1465 regcache_collect (i, buf);
1466 target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
1472 /* Registers are 32-bit. Toss any sign-extension of the stack
1476 /* Only use the bottom half if we're in 64-bit mode. */
1477 if (gdbarch_ptr_bit (current_gdbarch) == 64)
1480 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1482 if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
1484 regcache_collect (i, buf);
1485 target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
1493 /* Provide a prototype to silence -Wmissing-prototypes. */
1494 void _initialize_sparc64_tdep (void);
1497 _initialize_sparc64_tdep (void)
1499 register_gdbarch_init (bfd_arch_sparc, sparc64_gdbarch_init);