gdb/
[deliverable/binutils-gdb.git] / gdb / sparc64-tdep.c
CommitLineData
8b39fe56
MK
1/* Target-dependent code for UltraSPARC.
2
7b6bb8da 3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
9b254dd1 4 Free Software Foundation, Inc.
8b39fe56
MK
5
6 This file is part of GDB.
7
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
8b39fe56
MK
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
8b39fe56
MK
20
21#include "defs.h"
22#include "arch-utils.h"
02a71ae8 23#include "dwarf2-frame.h"
8b39fe56
MK
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"
386c036b
MK
30#include "inferior.h"
31#include "symtab.h"
32#include "objfiles.h"
8b39fe56
MK
33#include "osabi.h"
34#include "regcache.h"
35#include "target.h"
36#include "value.h"
37
38#include "gdb_assert.h"
39#include "gdb_string.h"
40
41#include "sparc64-tdep.h"
42
b021a221 43/* This file implements the SPARC 64-bit ABI as defined by the
8b39fe56
MK
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
46 SPARC. */
47
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. */
8b39fe56
MK
51\f
52/* The functions on this page are intended to be used to classify
53 function arguments. */
54
8b39fe56
MK
55/* Check whether TYPE is "Integral or Pointer". */
56
57static int
58sparc64_integral_or_pointer_p (const struct type *type)
59{
60 switch (TYPE_CODE (type))
61 {
62 case TYPE_CODE_INT:
63 case TYPE_CODE_BOOL:
64 case TYPE_CODE_CHAR:
65 case TYPE_CODE_ENUM:
66 case TYPE_CODE_RANGE:
67 {
68 int len = TYPE_LENGTH (type);
69 gdb_assert (len == 1 || len == 2 || len == 4 || len == 8);
70 }
71 return 1;
72 case TYPE_CODE_PTR:
73 case TYPE_CODE_REF:
74 {
75 int len = TYPE_LENGTH (type);
76 gdb_assert (len == 8);
77 }
78 return 1;
79 default:
80 break;
81 }
82
83 return 0;
84}
85
86/* Check whether TYPE is "Floating". */
87
88static int
89sparc64_floating_p (const struct type *type)
90{
91 switch (TYPE_CODE (type))
92 {
93 case TYPE_CODE_FLT:
94 {
95 int len = TYPE_LENGTH (type);
96 gdb_assert (len == 4 || len == 8 || len == 16);
97 }
98 return 1;
99 default:
100 break;
101 }
102
103 return 0;
104}
105
0497f5b0
JB
106/* Check whether TYPE is "Structure or Union".
107
108 In terms of Ada subprogram calls, arrays are treated the same as
109 struct and union types. So this function also returns non-zero
110 for array types. */
8b39fe56
MK
111
112static int
113sparc64_structure_or_union_p (const struct type *type)
114{
115 switch (TYPE_CODE (type))
116 {
117 case TYPE_CODE_STRUCT:
118 case TYPE_CODE_UNION:
0497f5b0 119 case TYPE_CODE_ARRAY:
8b39fe56
MK
120 return 1;
121 default:
122 break;
123 }
124
125 return 0;
126}
fd936806
MK
127\f
128
209bd28e 129/* Construct types for ISA-specific registers. */
fd936806 130
209bd28e
UW
131static struct type *
132sparc64_pstate_type (struct gdbarch *gdbarch)
133{
134 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
fd936806 135
209bd28e
UW
136 if (!tdep->sparc64_pstate_type)
137 {
138 struct type *type;
139
e9bb382b 140 type = arch_flags_type (gdbarch, "builtin_type_sparc64_pstate", 8);
209bd28e
UW
141 append_flags_type_flag (type, 0, "AG");
142 append_flags_type_flag (type, 1, "IE");
143 append_flags_type_flag (type, 2, "PRIV");
144 append_flags_type_flag (type, 3, "AM");
145 append_flags_type_flag (type, 4, "PEF");
146 append_flags_type_flag (type, 5, "RED");
147 append_flags_type_flag (type, 8, "TLE");
148 append_flags_type_flag (type, 9, "CLE");
149 append_flags_type_flag (type, 10, "PID0");
150 append_flags_type_flag (type, 11, "PID1");
151
152 tdep->sparc64_pstate_type = type;
153 }
fd936806 154
209bd28e
UW
155 return tdep->sparc64_pstate_type;
156}
fd936806 157
209bd28e
UW
158static struct type *
159sparc64_fsr_type (struct gdbarch *gdbarch)
160{
161 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
162
163 if (!tdep->sparc64_fsr_type)
164 {
165 struct type *type;
166
e9bb382b 167 type = arch_flags_type (gdbarch, "builtin_type_sparc64_fsr", 8);
209bd28e
UW
168 append_flags_type_flag (type, 0, "NXA");
169 append_flags_type_flag (type, 1, "DZA");
170 append_flags_type_flag (type, 2, "UFA");
171 append_flags_type_flag (type, 3, "OFA");
172 append_flags_type_flag (type, 4, "NVA");
173 append_flags_type_flag (type, 5, "NXC");
174 append_flags_type_flag (type, 6, "DZC");
175 append_flags_type_flag (type, 7, "UFC");
176 append_flags_type_flag (type, 8, "OFC");
177 append_flags_type_flag (type, 9, "NVC");
178 append_flags_type_flag (type, 22, "NS");
179 append_flags_type_flag (type, 23, "NXM");
180 append_flags_type_flag (type, 24, "DZM");
181 append_flags_type_flag (type, 25, "UFM");
182 append_flags_type_flag (type, 26, "OFM");
183 append_flags_type_flag (type, 27, "NVM");
184
185 tdep->sparc64_fsr_type = type;
186 }
187
188 return tdep->sparc64_fsr_type;
189}
190
191static struct type *
192sparc64_fprs_type (struct gdbarch *gdbarch)
fd936806 193{
209bd28e
UW
194 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
195
196 if (!tdep->sparc64_fprs_type)
197 {
198 struct type *type;
199
e9bb382b 200 type = arch_flags_type (gdbarch, "builtin_type_sparc64_fprs", 8);
209bd28e
UW
201 append_flags_type_flag (type, 0, "DL");
202 append_flags_type_flag (type, 1, "DU");
203 append_flags_type_flag (type, 2, "FEF");
204
205 tdep->sparc64_fprs_type = type;
206 }
207
208 return tdep->sparc64_fprs_type;
fd936806 209}
8b39fe56 210
209bd28e 211
8b39fe56
MK
212/* Register information. */
213
6707b003 214static const char *sparc64_register_names[] =
8b39fe56 215{
6707b003
UW
216 "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
217 "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
218 "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
219 "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",
220
221 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
222 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
223 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
224 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
225 "f32", "f34", "f36", "f38", "f40", "f42", "f44", "f46",
226 "f48", "f50", "f52", "f54", "f56", "f58", "f60", "f62",
227
228 "pc", "npc",
8b39fe56 229
6707b003
UW
230 /* FIXME: Give "state" a name until we start using register groups. */
231 "state",
232 "fsr",
233 "fprs",
234 "y",
8b39fe56
MK
235};
236
237/* Total number of registers. */
6707b003 238#define SPARC64_NUM_REGS ARRAY_SIZE (sparc64_register_names)
8b39fe56
MK
239
240/* We provide the aliases %d0..%d62 and %q0..%q60 for the floating
241 registers as "psuedo" registers. */
242
6707b003 243static const char *sparc64_pseudo_register_names[] =
8b39fe56 244{
6707b003
UW
245 "cwp", "pstate", "asi", "ccr",
246
247 "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
248 "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30",
249 "d32", "d34", "d36", "d38", "d40", "d42", "d44", "d46",
250 "d48", "d50", "d52", "d54", "d56", "d58", "d60", "d62",
251
252 "q0", "q4", "q8", "q12", "q16", "q20", "q24", "q28",
253 "q32", "q36", "q40", "q44", "q48", "q52", "q56", "q60",
8b39fe56
MK
254};
255
256/* Total number of pseudo registers. */
6707b003 257#define SPARC64_NUM_PSEUDO_REGS ARRAY_SIZE (sparc64_pseudo_register_names)
8b39fe56
MK
258
259/* Return the name of register REGNUM. */
260
261static const char *
d93859e2 262sparc64_register_name (struct gdbarch *gdbarch, int regnum)
8b39fe56
MK
263{
264 if (regnum >= 0 && regnum < SPARC64_NUM_REGS)
6707b003 265 return sparc64_register_names[regnum];
8b39fe56
MK
266
267 if (regnum >= SPARC64_NUM_REGS
268 && regnum < SPARC64_NUM_REGS + SPARC64_NUM_PSEUDO_REGS)
6707b003 269 return sparc64_pseudo_register_names[regnum - SPARC64_NUM_REGS];
8b39fe56
MK
270
271 return NULL;
272}
273
274/* Return the GDB type object for the "standard" data type of data in
c378eb4e 275 register REGNUM. */
8b39fe56
MK
276
277static struct type *
278sparc64_register_type (struct gdbarch *gdbarch, int regnum)
279{
6707b003
UW
280 /* Raw registers. */
281
282 if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
0dfff4cb 283 return builtin_type (gdbarch)->builtin_data_ptr;
6707b003 284 if (regnum >= SPARC_G0_REGNUM && regnum <= SPARC_I7_REGNUM)
df4df182 285 return builtin_type (gdbarch)->builtin_int64;
6707b003 286 if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
0dfff4cb 287 return builtin_type (gdbarch)->builtin_float;
6707b003 288 if (regnum >= SPARC64_F32_REGNUM && regnum <= SPARC64_F62_REGNUM)
0dfff4cb 289 return builtin_type (gdbarch)->builtin_double;
6707b003 290 if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
0dfff4cb 291 return builtin_type (gdbarch)->builtin_func_ptr;
6707b003
UW
292 /* This raw register contains the contents of %cwp, %pstate, %asi
293 and %ccr as laid out in a %tstate register. */
294 if (regnum == SPARC64_STATE_REGNUM)
df4df182 295 return builtin_type (gdbarch)->builtin_int64;
6707b003 296 if (regnum == SPARC64_FSR_REGNUM)
209bd28e 297 return sparc64_fsr_type (gdbarch);
6707b003 298 if (regnum == SPARC64_FPRS_REGNUM)
209bd28e 299 return sparc64_fprs_type (gdbarch);
6707b003
UW
300 /* "Although Y is a 64-bit register, its high-order 32 bits are
301 reserved and always read as 0." */
302 if (regnum == SPARC64_Y_REGNUM)
df4df182 303 return builtin_type (gdbarch)->builtin_int64;
6707b003
UW
304
305 /* Pseudo registers. */
306
307 if (regnum == SPARC64_CWP_REGNUM)
df4df182 308 return builtin_type (gdbarch)->builtin_int64;
6707b003 309 if (regnum == SPARC64_PSTATE_REGNUM)
209bd28e 310 return sparc64_pstate_type (gdbarch);
6707b003 311 if (regnum == SPARC64_ASI_REGNUM)
df4df182 312 return builtin_type (gdbarch)->builtin_int64;
6707b003 313 if (regnum == SPARC64_CCR_REGNUM)
df4df182 314 return builtin_type (gdbarch)->builtin_int64;
6707b003 315 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D62_REGNUM)
0dfff4cb 316 return builtin_type (gdbarch)->builtin_double;
6707b003 317 if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q60_REGNUM)
0dfff4cb 318 return builtin_type (gdbarch)->builtin_long_double;
6707b003
UW
319
320 internal_error (__FILE__, __LINE__, _("invalid regnum"));
8b39fe56
MK
321}
322
05d1431c 323static enum register_status
8b39fe56
MK
324sparc64_pseudo_register_read (struct gdbarch *gdbarch,
325 struct regcache *regcache,
e1613aba 326 int regnum, gdb_byte *buf)
8b39fe56 327{
e17a4113 328 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
05d1431c
PA
329 enum register_status status;
330
8b39fe56
MK
331 gdb_assert (regnum >= SPARC64_NUM_REGS);
332
333 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
334 {
335 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
05d1431c
PA
336 status = regcache_raw_read (regcache, regnum, buf);
337 if (status == REG_VALID)
338 status = regcache_raw_read (regcache, regnum + 1, buf + 4);
339 return status;
8b39fe56
MK
340 }
341 else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
342 {
343 regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
05d1431c 344 return regcache_raw_read (regcache, regnum, buf);
8b39fe56
MK
345 }
346 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
347 {
348 regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
05d1431c
PA
349
350 status = regcache_raw_read (regcache, regnum, buf);
351 if (status == REG_VALID)
352 status = regcache_raw_read (regcache, regnum + 1, buf + 4);
353 if (status == REG_VALID)
354 status = regcache_raw_read (regcache, regnum + 2, buf + 8);
355 if (status == REG_VALID)
356 status = regcache_raw_read (regcache, regnum + 3, buf + 12);
357
358 return status;
8b39fe56
MK
359 }
360 else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
361 {
362 regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
05d1431c
PA
363
364 status = regcache_raw_read (regcache, regnum, buf);
365 if (status == REG_VALID)
366 status = regcache_raw_read (regcache, regnum + 1, buf + 8);
367
368 return status;
8b39fe56
MK
369 }
370 else if (regnum == SPARC64_CWP_REGNUM
371 || regnum == SPARC64_PSTATE_REGNUM
372 || regnum == SPARC64_ASI_REGNUM
373 || regnum == SPARC64_CCR_REGNUM)
374 {
375 ULONGEST state;
376
05d1431c
PA
377 status = regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
378 if (status != REG_VALID)
379 return status;
380
8b39fe56
MK
381 switch (regnum)
382 {
3567a8ea 383 case SPARC64_CWP_REGNUM:
8b39fe56
MK
384 state = (state >> 0) & ((1 << 5) - 1);
385 break;
3567a8ea 386 case SPARC64_PSTATE_REGNUM:
8b39fe56
MK
387 state = (state >> 8) & ((1 << 12) - 1);
388 break;
3567a8ea 389 case SPARC64_ASI_REGNUM:
8b39fe56
MK
390 state = (state >> 24) & ((1 << 8) - 1);
391 break;
3567a8ea 392 case SPARC64_CCR_REGNUM:
8b39fe56
MK
393 state = (state >> 32) & ((1 << 8) - 1);
394 break;
395 }
e17a4113 396 store_unsigned_integer (buf, 8, byte_order, state);
8b39fe56 397 }
05d1431c
PA
398
399 return REG_VALID;
8b39fe56
MK
400}
401
402static void
403sparc64_pseudo_register_write (struct gdbarch *gdbarch,
404 struct regcache *regcache,
e1613aba 405 int regnum, const gdb_byte *buf)
8b39fe56 406{
e17a4113 407 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8b39fe56
MK
408 gdb_assert (regnum >= SPARC64_NUM_REGS);
409
410 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
411 {
412 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
413 regcache_raw_write (regcache, regnum, buf);
e1613aba 414 regcache_raw_write (regcache, regnum + 1, buf + 4);
8b39fe56
MK
415 }
416 else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
417 {
418 regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
419 regcache_raw_write (regcache, regnum, buf);
420 }
421 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
422 {
423 regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
424 regcache_raw_write (regcache, regnum, buf);
e1613aba
MK
425 regcache_raw_write (regcache, regnum + 1, buf + 4);
426 regcache_raw_write (regcache, regnum + 2, buf + 8);
427 regcache_raw_write (regcache, regnum + 3, buf + 12);
8b39fe56
MK
428 }
429 else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
430 {
431 regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
432 regcache_raw_write (regcache, regnum, buf);
e1613aba 433 regcache_raw_write (regcache, regnum + 1, buf + 8);
8b39fe56 434 }
3567a8ea
MK
435 else if (regnum == SPARC64_CWP_REGNUM
436 || regnum == SPARC64_PSTATE_REGNUM
437 || regnum == SPARC64_ASI_REGNUM
438 || regnum == SPARC64_CCR_REGNUM)
439 {
440 ULONGEST state, bits;
441
442 regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
e17a4113 443 bits = extract_unsigned_integer (buf, 8, byte_order);
3567a8ea
MK
444 switch (regnum)
445 {
446 case SPARC64_CWP_REGNUM:
447 state |= ((bits & ((1 << 5) - 1)) << 0);
448 break;
449 case SPARC64_PSTATE_REGNUM:
450 state |= ((bits & ((1 << 12) - 1)) << 8);
451 break;
452 case SPARC64_ASI_REGNUM:
453 state |= ((bits & ((1 << 8) - 1)) << 24);
454 break;
455 case SPARC64_CCR_REGNUM:
456 state |= ((bits & ((1 << 8) - 1)) << 32);
457 break;
458 }
459 regcache_raw_write_unsigned (regcache, SPARC64_STATE_REGNUM, state);
460 }
8b39fe56 461}
8b39fe56
MK
462\f
463
8b39fe56
MK
464/* Return PC of first real instruction of the function starting at
465 START_PC. */
466
467static CORE_ADDR
6093d2eb 468sparc64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
8b39fe56
MK
469{
470 struct symtab_and_line sal;
471 CORE_ADDR func_start, func_end;
386c036b 472 struct sparc_frame_cache cache;
8b39fe56
MK
473
474 /* This is the preferred method, find the end of the prologue by
475 using the debugging information. */
476 if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
477 {
478 sal = find_pc_line (func_start, 0);
479
480 if (sal.end < func_end
481 && start_pc <= sal.end)
482 return sal.end;
483 }
484
be8626e0
MD
485 return sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffULL,
486 &cache);
8b39fe56
MK
487}
488
489/* Normal frames. */
490
386c036b 491static struct sparc_frame_cache *
236369e7 492sparc64_frame_cache (struct frame_info *this_frame, void **this_cache)
8b39fe56 493{
236369e7 494 return sparc_frame_cache (this_frame, this_cache);
8b39fe56
MK
495}
496
497static void
236369e7 498sparc64_frame_this_id (struct frame_info *this_frame, void **this_cache,
8b39fe56
MK
499 struct frame_id *this_id)
500{
386c036b 501 struct sparc_frame_cache *cache =
236369e7 502 sparc64_frame_cache (this_frame, this_cache);
8b39fe56
MK
503
504 /* This marks the outermost frame. */
505 if (cache->base == 0)
506 return;
507
508 (*this_id) = frame_id_build (cache->base, cache->pc);
509}
510
236369e7
JB
511static struct value *
512sparc64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
513 int regnum)
8b39fe56 514{
e17a4113 515 struct gdbarch *gdbarch = get_frame_arch (this_frame);
386c036b 516 struct sparc_frame_cache *cache =
236369e7 517 sparc64_frame_cache (this_frame, this_cache);
8b39fe56
MK
518
519 if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
520 {
236369e7 521 CORE_ADDR pc = (regnum == SPARC64_NPC_REGNUM) ? 4 : 0;
8b39fe56 522
369c397b
JB
523 regnum =
524 (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
236369e7
JB
525 pc += get_frame_register_unsigned (this_frame, regnum) + 8;
526 return frame_unwind_got_constant (this_frame, regnum, pc);
8b39fe56
MK
527 }
528
f700a364
MK
529 /* Handle StackGhost. */
530 {
e17a4113 531 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
f700a364
MK
532
533 if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
534 {
236369e7
JB
535 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
536 ULONGEST i7;
537
538 /* Read the value in from memory. */
539 i7 = get_frame_memory_unsigned (this_frame, addr, 8);
540 return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
f700a364
MK
541 }
542 }
543
369c397b 544 /* The previous frame's `local' and `in' registers may have been saved
8b39fe56 545 in the register save area. */
369c397b
JB
546 if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
547 && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
8b39fe56 548 {
236369e7 549 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
8b39fe56 550
236369e7 551 return frame_unwind_got_memory (this_frame, regnum, addr);
8b39fe56
MK
552 }
553
369c397b
JB
554 /* The previous frame's `out' registers may be accessible as the current
555 frame's `in' registers. */
556 if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
557 && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
8b39fe56
MK
558 regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
559
236369e7 560 return frame_unwind_got_register (this_frame, regnum, regnum);
8b39fe56
MK
561}
562
563static const struct frame_unwind sparc64_frame_unwind =
564{
565 NORMAL_FRAME,
8fbca658 566 default_frame_unwind_stop_reason,
8b39fe56 567 sparc64_frame_this_id,
236369e7
JB
568 sparc64_frame_prev_register,
569 NULL,
570 default_frame_sniffer
8b39fe56 571};
8b39fe56
MK
572\f
573
574static CORE_ADDR
236369e7 575sparc64_frame_base_address (struct frame_info *this_frame, void **this_cache)
8b39fe56 576{
386c036b 577 struct sparc_frame_cache *cache =
236369e7 578 sparc64_frame_cache (this_frame, this_cache);
8b39fe56 579
5b2d44a0 580 return cache->base;
8b39fe56
MK
581}
582
583static const struct frame_base sparc64_frame_base =
584{
585 &sparc64_frame_unwind,
586 sparc64_frame_base_address,
587 sparc64_frame_base_address,
588 sparc64_frame_base_address
589};
8b39fe56
MK
590\f
591/* Check whether TYPE must be 16-byte aligned. */
592
593static int
594sparc64_16_byte_align_p (struct type *type)
595{
596 if (sparc64_floating_p (type) && TYPE_LENGTH (type) == 16)
597 return 1;
598
599 if (sparc64_structure_or_union_p (type))
600 {
601 int i;
602
603 for (i = 0; i < TYPE_NFIELDS (type); i++)
60af1db2
MK
604 {
605 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
606
607 if (sparc64_16_byte_align_p (subtype))
608 return 1;
609 }
8b39fe56
MK
610 }
611
612 return 0;
613}
614
615/* Store floating fields of element ELEMENT of an "parameter array"
616 that has type TYPE and is stored at BITPOS in VALBUF in the
617 apropriate registers of REGCACHE. This function can be called
618 recursively and therefore handles floating types in addition to
619 structures. */
620
621static void
622sparc64_store_floating_fields (struct regcache *regcache, struct type *type,
e1613aba 623 const gdb_byte *valbuf, int element, int bitpos)
8b39fe56
MK
624{
625 gdb_assert (element < 16);
626
627 if (sparc64_floating_p (type))
628 {
629 int len = TYPE_LENGTH (type);
630 int regnum;
631
632 if (len == 16)
633 {
634 gdb_assert (bitpos == 0);
635 gdb_assert ((element % 2) == 0);
636
637 regnum = SPARC64_Q0_REGNUM + element / 2;
638 regcache_cooked_write (regcache, regnum, valbuf);
639 }
640 else if (len == 8)
641 {
642 gdb_assert (bitpos == 0 || bitpos == 64);
643
644 regnum = SPARC64_D0_REGNUM + element + bitpos / 64;
645 regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
646 }
647 else
648 {
649 gdb_assert (len == 4);
650 gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 128);
651
652 regnum = SPARC_F0_REGNUM + element * 2 + bitpos / 32;
653 regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
654 }
655 }
656 else if (sparc64_structure_or_union_p (type))
657 {
658 int i;
659
660 for (i = 0; i < TYPE_NFIELDS (type); i++)
60af1db2
MK
661 {
662 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
663 int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);
664
665 sparc64_store_floating_fields (regcache, subtype, valbuf,
666 element, subpos);
667 }
200cc553
MK
668
669 /* GCC has an interesting bug. If TYPE is a structure that has
670 a single `float' member, GCC doesn't treat it as a structure
671 at all, but rather as an ordinary `float' argument. This
672 argument will be stored in %f1, as required by the psABI.
673 However, as a member of a structure the psABI requires it to
5154b0cd
MK
674 be stored in %f0. This bug is present in GCC 3.3.2, but
675 probably in older releases to. To appease GCC, if a
676 structure has only a single `float' member, we store its
677 value in %f1 too (we already have stored in %f0). */
200cc553
MK
678 if (TYPE_NFIELDS (type) == 1)
679 {
680 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, 0));
681
682 if (sparc64_floating_p (subtype) && TYPE_LENGTH (subtype) == 4)
683 regcache_cooked_write (regcache, SPARC_F1_REGNUM, valbuf);
684 }
8b39fe56
MK
685 }
686}
687
688/* Fetch floating fields from a variable of type TYPE from the
689 appropriate registers for BITPOS in REGCACHE and store it at BITPOS
690 in VALBUF. This function can be called recursively and therefore
691 handles floating types in addition to structures. */
692
693static void
694sparc64_extract_floating_fields (struct regcache *regcache, struct type *type,
e1613aba 695 gdb_byte *valbuf, int bitpos)
8b39fe56
MK
696{
697 if (sparc64_floating_p (type))
698 {
699 int len = TYPE_LENGTH (type);
700 int regnum;
701
702 if (len == 16)
703 {
704 gdb_assert (bitpos == 0 || bitpos == 128);
705
706 regnum = SPARC64_Q0_REGNUM + bitpos / 128;
707 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
708 }
709 else if (len == 8)
710 {
711 gdb_assert (bitpos % 64 == 0 && bitpos >= 0 && bitpos < 256);
712
713 regnum = SPARC64_D0_REGNUM + bitpos / 64;
714 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
715 }
716 else
717 {
718 gdb_assert (len == 4);
719 gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 256);
720
721 regnum = SPARC_F0_REGNUM + bitpos / 32;
722 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
723 }
724 }
725 else if (sparc64_structure_or_union_p (type))
726 {
727 int i;
728
729 for (i = 0; i < TYPE_NFIELDS (type); i++)
60af1db2
MK
730 {
731 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
732 int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);
733
734 sparc64_extract_floating_fields (regcache, subtype, valbuf, subpos);
735 }
8b39fe56
MK
736 }
737}
738
739/* Store the NARGS arguments ARGS and STRUCT_ADDR (if STRUCT_RETURN is
740 non-zero) in REGCACHE and on the stack (starting from address SP). */
741
742static CORE_ADDR
743sparc64_store_arguments (struct regcache *regcache, int nargs,
744 struct value **args, CORE_ADDR sp,
745 int struct_return, CORE_ADDR struct_addr)
746{
df4df182 747 struct gdbarch *gdbarch = get_regcache_arch (regcache);
8b39fe56
MK
748 /* Number of extended words in the "parameter array". */
749 int num_elements = 0;
750 int element = 0;
751 int i;
752
753 /* Take BIAS into account. */
754 sp += BIAS;
755
756 /* First we calculate the number of extended words in the "parameter
757 array". While doing so we also convert some of the arguments. */
758
759 if (struct_return)
760 num_elements++;
761
762 for (i = 0; i < nargs; i++)
763 {
4991999e 764 struct type *type = value_type (args[i]);
8b39fe56
MK
765 int len = TYPE_LENGTH (type);
766
767 if (sparc64_structure_or_union_p (type))
768 {
769 /* Structure or Union arguments. */
770 if (len <= 16)
771 {
772 if (num_elements % 2 && sparc64_16_byte_align_p (type))
773 num_elements++;
774 num_elements += ((len + 7) / 8);
775 }
776 else
777 {
778 /* The psABI says that "Structures or unions larger than
779 sixteen bytes are copied by the caller and passed
780 indirectly; the caller will pass the address of a
781 correctly aligned structure value. This sixty-four
782 bit address will occupy one word in the parameter
783 array, and may be promoted to an %o register like any
784 other pointer value." Allocate memory for these
785 values on the stack. */
786 sp -= len;
787
788 /* Use 16-byte alignment for these values. That's
789 always correct, and wasting a few bytes shouldn't be
790 a problem. */
791 sp &= ~0xf;
792
0fd88904 793 write_memory (sp, value_contents (args[i]), len);
8b39fe56
MK
794 args[i] = value_from_pointer (lookup_pointer_type (type), sp);
795 num_elements++;
796 }
797 }
798 else if (sparc64_floating_p (type))
799 {
800 /* Floating arguments. */
801
802 if (len == 16)
803 {
804 /* The psABI says that "Each quad-precision parameter
805 value will be assigned to two extended words in the
806 parameter array. */
807 num_elements += 2;
808
809 /* The psABI says that "Long doubles must be
810 quad-aligned, and thus a hole might be introduced
811 into the parameter array to force alignment." Skip
812 an element if necessary. */
813 if (num_elements % 2)
814 num_elements++;
815 }
816 else
817 num_elements++;
818 }
819 else
820 {
821 /* Integral and pointer arguments. */
822 gdb_assert (sparc64_integral_or_pointer_p (type));
823
824 /* The psABI says that "Each argument value of integral type
825 smaller than an extended word will be widened by the
826 caller to an extended word according to the signed-ness
827 of the argument type." */
828 if (len < 8)
df4df182
UW
829 args[i] = value_cast (builtin_type (gdbarch)->builtin_int64,
830 args[i]);
8b39fe56
MK
831 num_elements++;
832 }
833 }
834
835 /* Allocate the "parameter array". */
836 sp -= num_elements * 8;
837
838 /* The psABI says that "Every stack frame must be 16-byte aligned." */
839 sp &= ~0xf;
840
841 /* Now we store the arguments in to the "paramater array". Some
842 Integer or Pointer arguments and Structure or Union arguments
843 will be passed in %o registers. Some Floating arguments and
844 floating members of structures are passed in floating-point
845 registers. However, for functions with variable arguments,
846 floating arguments are stored in an %0 register, and for
847 functions without a prototype floating arguments are stored in
848 both a floating-point and an %o registers, or a floating-point
849 register and memory. To simplify the logic here we always pass
850 arguments in memory, an %o register, and a floating-point
851 register if appropriate. This should be no problem since the
852 contents of any unused memory or registers in the "parameter
853 array" are undefined. */
854
855 if (struct_return)
856 {
857 regcache_cooked_write_unsigned (regcache, SPARC_O0_REGNUM, struct_addr);
858 element++;
859 }
860
861 for (i = 0; i < nargs; i++)
862 {
e1613aba 863 const gdb_byte *valbuf = value_contents (args[i]);
4991999e 864 struct type *type = value_type (args[i]);
8b39fe56
MK
865 int len = TYPE_LENGTH (type);
866 int regnum = -1;
e1613aba 867 gdb_byte buf[16];
8b39fe56
MK
868
869 if (sparc64_structure_or_union_p (type))
870 {
871 /* Structure or Union arguments. */
872 gdb_assert (len <= 16);
873 memset (buf, 0, sizeof (buf));
874 valbuf = memcpy (buf, valbuf, len);
875
876 if (element % 2 && sparc64_16_byte_align_p (type))
877 element++;
878
879 if (element < 6)
880 {
881 regnum = SPARC_O0_REGNUM + element;
882 if (len > 8 && element < 5)
883 regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
884 }
885
886 if (element < 16)
887 sparc64_store_floating_fields (regcache, type, valbuf, element, 0);
888 }
889 else if (sparc64_floating_p (type))
890 {
891 /* Floating arguments. */
892 if (len == 16)
893 {
894 if (element % 2)
895 element++;
896 if (element < 16)
897 regnum = SPARC64_Q0_REGNUM + element / 2;
898 }
899 else if (len == 8)
900 {
901 if (element < 16)
902 regnum = SPARC64_D0_REGNUM + element;
903 }
904 else
905 {
906 /* The psABI says "Each single-precision parameter value
907 will be assigned to one extended word in the
908 parameter array, and right-justified within that
909 word; the left half (even floatregister) is
910 undefined." Even though the psABI says that "the
911 left half is undefined", set it to zero here. */
912 memset (buf, 0, 4);
8ada74e3
MK
913 memcpy (buf + 4, valbuf, 4);
914 valbuf = buf;
8b39fe56
MK
915 len = 8;
916 if (element < 16)
8ada74e3 917 regnum = SPARC64_D0_REGNUM + element;
8b39fe56
MK
918 }
919 }
920 else
921 {
922 /* Integral and pointer arguments. */
923 gdb_assert (len == 8);
924 if (element < 6)
925 regnum = SPARC_O0_REGNUM + element;
926 }
927
928 if (regnum != -1)
929 {
930 regcache_cooked_write (regcache, regnum, valbuf);
931
932 /* If we're storing the value in a floating-point register,
933 also store it in the corresponding %0 register(s). */
934 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D10_REGNUM)
935 {
936 gdb_assert (element < 6);
937 regnum = SPARC_O0_REGNUM + element;
938 regcache_cooked_write (regcache, regnum, valbuf);
939 }
940 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q8_REGNUM)
941 {
942 gdb_assert (element < 6);
943 regnum = SPARC_O0_REGNUM + element;
944 regcache_cooked_write (regcache, regnum, valbuf);
d47079be 945 regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
8b39fe56
MK
946 }
947 }
948
c4f2d4d7 949 /* Always store the argument in memory. */
8b39fe56
MK
950 write_memory (sp + element * 8, valbuf, len);
951 element += ((len + 7) / 8);
952 }
953
954 gdb_assert (element == num_elements);
955
956 /* Take BIAS into account. */
957 sp -= BIAS;
958 return sp;
959}
960
49a45ecf
JB
961static CORE_ADDR
962sparc64_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
963{
964 /* The ABI requires 16-byte alignment. */
965 return address & ~0xf;
966}
967
8b39fe56 968static CORE_ADDR
7d9b040b 969sparc64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
8b39fe56
MK
970 struct regcache *regcache, CORE_ADDR bp_addr,
971 int nargs, struct value **args, CORE_ADDR sp,
972 int struct_return, CORE_ADDR struct_addr)
973{
974 /* Set return address. */
975 regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, bp_addr - 8);
976
977 /* Set up function arguments. */
978 sp = sparc64_store_arguments (regcache, nargs, args, sp,
979 struct_return, struct_addr);
980
981 /* Allocate the register save area. */
982 sp -= 16 * 8;
983
984 /* Stack should be 16-byte aligned at this point. */
3567a8ea 985 gdb_assert ((sp + BIAS) % 16 == 0);
8b39fe56
MK
986
987 /* Finally, update the stack pointer. */
988 regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
989
5b2d44a0 990 return sp + BIAS;
8b39fe56
MK
991}
992\f
993
994/* Extract from an array REGBUF containing the (raw) register state, a
995 function return value of TYPE, and copy that into VALBUF. */
996
997static void
998sparc64_extract_return_value (struct type *type, struct regcache *regcache,
e1613aba 999 gdb_byte *valbuf)
8b39fe56
MK
1000{
1001 int len = TYPE_LENGTH (type);
e1613aba 1002 gdb_byte buf[32];
8b39fe56
MK
1003 int i;
1004
1005 if (sparc64_structure_or_union_p (type))
1006 {
1007 /* Structure or Union return values. */
1008 gdb_assert (len <= 32);
1009
1010 for (i = 0; i < ((len + 7) / 8); i++)
1011 regcache_cooked_read (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1012 if (TYPE_CODE (type) != TYPE_CODE_UNION)
1013 sparc64_extract_floating_fields (regcache, type, buf, 0);
1014 memcpy (valbuf, buf, len);
1015 }
1016 else if (sparc64_floating_p (type))
1017 {
1018 /* Floating return values. */
1019 for (i = 0; i < len / 4; i++)
1020 regcache_cooked_read (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
1021 memcpy (valbuf, buf, len);
1022 }
4bd87714
JB
1023 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1024 {
1025 /* Small arrays are returned the same way as small structures. */
1026 gdb_assert (len <= 32);
1027
1028 for (i = 0; i < ((len + 7) / 8); i++)
1029 regcache_cooked_read (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1030 memcpy (valbuf, buf, len);
1031 }
8b39fe56
MK
1032 else
1033 {
1034 /* Integral and pointer return values. */
1035 gdb_assert (sparc64_integral_or_pointer_p (type));
1036
1037 /* Just stripping off any unused bytes should preserve the
1038 signed-ness just fine. */
1039 regcache_cooked_read (regcache, SPARC_O0_REGNUM, buf);
1040 memcpy (valbuf, buf + 8 - len, len);
1041 }
1042}
1043
1044/* Write into the appropriate registers a function return value stored
1045 in VALBUF of type TYPE. */
1046
1047static void
1048sparc64_store_return_value (struct type *type, struct regcache *regcache,
e1613aba 1049 const gdb_byte *valbuf)
8b39fe56
MK
1050{
1051 int len = TYPE_LENGTH (type);
e1613aba 1052 gdb_byte buf[16];
8b39fe56
MK
1053 int i;
1054
1055 if (sparc64_structure_or_union_p (type))
1056 {
1057 /* Structure or Union return values. */
1058 gdb_assert (len <= 32);
1059
1060 /* Simplify matters by storing the complete value (including
1061 floating members) into %o0 and %o1. Floating members are
1062 also store in the appropriate floating-point registers. */
1063 memset (buf, 0, sizeof (buf));
1064 memcpy (buf, valbuf, len);
1065 for (i = 0; i < ((len + 7) / 8); i++)
60af1db2 1066 regcache_cooked_write (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
8b39fe56
MK
1067 if (TYPE_CODE (type) != TYPE_CODE_UNION)
1068 sparc64_store_floating_fields (regcache, type, buf, 0, 0);
1069 }
1070 else if (sparc64_floating_p (type))
1071 {
1072 /* Floating return values. */
1073 memcpy (buf, valbuf, len);
1074 for (i = 0; i < len / 4; i++)
1075 regcache_cooked_write (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
1076 }
4bd87714
JB
1077 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1078 {
1079 /* Small arrays are returned the same way as small structures. */
1080 gdb_assert (len <= 32);
1081
1082 memset (buf, 0, sizeof (buf));
1083 memcpy (buf, valbuf, len);
1084 for (i = 0; i < ((len + 7) / 8); i++)
1085 regcache_cooked_write (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1086 }
8b39fe56
MK
1087 else
1088 {
1089 /* Integral and pointer return values. */
1090 gdb_assert (sparc64_integral_or_pointer_p (type));
1091
1092 /* ??? Do we need to do any sign-extension here? */
1093 memset (buf, 0, 8);
1094 memcpy (buf + 8 - len, valbuf, len);
1095 regcache_cooked_write (regcache, SPARC_O0_REGNUM, buf);
1096 }
1097}
1098
60af1db2 1099static enum return_value_convention
c055b101
CV
1100sparc64_return_value (struct gdbarch *gdbarch, struct type *func_type,
1101 struct type *type, struct regcache *regcache,
1102 gdb_byte *readbuf, const gdb_byte *writebuf)
8b39fe56 1103{
60af1db2
MK
1104 if (TYPE_LENGTH (type) > 32)
1105 return RETURN_VALUE_STRUCT_CONVENTION;
1106
1107 if (readbuf)
1108 sparc64_extract_return_value (type, regcache, readbuf);
1109 if (writebuf)
1110 sparc64_store_return_value (type, regcache, writebuf);
1111
1112 return RETURN_VALUE_REGISTER_CONVENTION;
8b39fe56 1113}
8b39fe56 1114\f
8b39fe56 1115
02a71ae8
MK
1116static void
1117sparc64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
aff37fc1 1118 struct dwarf2_frame_state_reg *reg,
4a4e5149 1119 struct frame_info *this_frame)
02a71ae8
MK
1120{
1121 switch (regnum)
1122 {
1123 case SPARC_G0_REGNUM:
1124 /* Since %g0 is always zero, there is no point in saving it, and
1125 people will be inclined omit it from the CFI. Make sure we
1126 don't warn about that. */
1127 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1128 break;
1129 case SPARC_SP_REGNUM:
1130 reg->how = DWARF2_FRAME_REG_CFA;
1131 break;
1132 case SPARC64_PC_REGNUM:
1133 reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1134 reg->loc.offset = 8;
1135 break;
1136 case SPARC64_NPC_REGNUM:
1137 reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1138 reg->loc.offset = 12;
1139 break;
1140 }
1141}
1142
8b39fe56 1143void
386c036b 1144sparc64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
8b39fe56 1145{
386c036b 1146 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8b39fe56 1147
386c036b
MK
1148 tdep->pc_regnum = SPARC64_PC_REGNUM;
1149 tdep->npc_regnum = SPARC64_NPC_REGNUM;
8b39fe56 1150
386c036b 1151 /* This is what all the fuss is about. */
8b39fe56
MK
1152 set_gdbarch_long_bit (gdbarch, 64);
1153 set_gdbarch_long_long_bit (gdbarch, 64);
1154 set_gdbarch_ptr_bit (gdbarch, 64);
8b39fe56
MK
1155
1156 set_gdbarch_num_regs (gdbarch, SPARC64_NUM_REGS);
1157 set_gdbarch_register_name (gdbarch, sparc64_register_name);
1158 set_gdbarch_register_type (gdbarch, sparc64_register_type);
1159 set_gdbarch_num_pseudo_regs (gdbarch, SPARC64_NUM_PSEUDO_REGS);
1160 set_gdbarch_pseudo_register_read (gdbarch, sparc64_pseudo_register_read);
1161 set_gdbarch_pseudo_register_write (gdbarch, sparc64_pseudo_register_write);
1162
1163 /* Register numbers of various important registers. */
8b39fe56 1164 set_gdbarch_pc_regnum (gdbarch, SPARC64_PC_REGNUM); /* %pc */
8b39fe56
MK
1165
1166 /* Call dummy code. */
49a45ecf 1167 set_gdbarch_frame_align (gdbarch, sparc64_frame_align);
386c036b
MK
1168 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1169 set_gdbarch_push_dummy_code (gdbarch, NULL);
8b39fe56
MK
1170 set_gdbarch_push_dummy_call (gdbarch, sparc64_push_dummy_call);
1171
60af1db2 1172 set_gdbarch_return_value (gdbarch, sparc64_return_value);
386c036b
MK
1173 set_gdbarch_stabs_argument_has_addr
1174 (gdbarch, default_stabs_argument_has_addr);
8b39fe56
MK
1175
1176 set_gdbarch_skip_prologue (gdbarch, sparc64_skip_prologue);
1177
02a71ae8
MK
1178 /* Hook in the DWARF CFI frame unwinder. */
1179 dwarf2_frame_set_init_reg (gdbarch, sparc64_dwarf2_frame_init_reg);
1180 /* FIXME: kettenis/20050423: Don't enable the unwinder until the
1181 StackGhost issues have been resolved. */
1182
236369e7 1183 frame_unwind_append_unwinder (gdbarch, &sparc64_frame_unwind);
8b39fe56 1184 frame_base_set_default (gdbarch, &sparc64_frame_base);
386c036b
MK
1185}
1186\f
8b39fe56 1187
386c036b 1188/* Helper functions for dealing with register sets. */
8b39fe56 1189
386c036b
MK
1190#define TSTATE_CWP 0x000000000000001fULL
1191#define TSTATE_ICC 0x0000000f00000000ULL
1192#define TSTATE_XCC 0x000000f000000000ULL
8b39fe56 1193
386c036b
MK
1194#define PSR_S 0x00000080
1195#define PSR_ICC 0x00f00000
1196#define PSR_VERS 0x0f000000
1197#define PSR_IMPL 0xf0000000
1198#define PSR_V8PLUS 0xff000000
1199#define PSR_XCC 0x000f0000
8b39fe56 1200
3567a8ea 1201void
386c036b
MK
1202sparc64_supply_gregset (const struct sparc_gregset *gregset,
1203 struct regcache *regcache,
1204 int regnum, const void *gregs)
8b39fe56 1205{
e17a4113
UW
1206 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1207 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1208 int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
e1613aba 1209 const gdb_byte *regs = gregs;
22e74ef9 1210 gdb_byte zero[8] = { 0 };
8b39fe56
MK
1211 int i;
1212
386c036b 1213 if (sparc32)
8b39fe56 1214 {
386c036b
MK
1215 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1216 {
1217 int offset = gregset->r_tstate_offset;
1218 ULONGEST tstate, psr;
e1613aba 1219 gdb_byte buf[4];
386c036b 1220
e17a4113 1221 tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
386c036b
MK
1222 psr = ((tstate & TSTATE_CWP) | PSR_S | ((tstate & TSTATE_ICC) >> 12)
1223 | ((tstate & TSTATE_XCC) >> 20) | PSR_V8PLUS);
e17a4113 1224 store_unsigned_integer (buf, 4, byte_order, psr);
386c036b
MK
1225 regcache_raw_supply (regcache, SPARC32_PSR_REGNUM, buf);
1226 }
1227
1228 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1229 regcache_raw_supply (regcache, SPARC32_PC_REGNUM,
1230 regs + gregset->r_pc_offset + 4);
1231
1232 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1233 regcache_raw_supply (regcache, SPARC32_NPC_REGNUM,
1234 regs + gregset->r_npc_offset + 4);
8b39fe56 1235
386c036b 1236 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
8b39fe56 1237 {
386c036b
MK
1238 int offset = gregset->r_y_offset + 8 - gregset->r_y_size;
1239 regcache_raw_supply (regcache, SPARC32_Y_REGNUM, regs + offset);
8b39fe56
MK
1240 }
1241 }
1242 else
1243 {
386c036b
MK
1244 if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
1245 regcache_raw_supply (regcache, SPARC64_STATE_REGNUM,
1246 regs + gregset->r_tstate_offset);
8b39fe56 1247
386c036b
MK
1248 if (regnum == SPARC64_PC_REGNUM || regnum == -1)
1249 regcache_raw_supply (regcache, SPARC64_PC_REGNUM,
1250 regs + gregset->r_pc_offset);
1251
1252 if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
1253 regcache_raw_supply (regcache, SPARC64_NPC_REGNUM,
1254 regs + gregset->r_npc_offset);
1255
1256 if (regnum == SPARC64_Y_REGNUM || regnum == -1)
3567a8ea 1257 {
e1613aba 1258 gdb_byte buf[8];
386c036b
MK
1259
1260 memset (buf, 0, 8);
1261 memcpy (buf + 8 - gregset->r_y_size,
1262 regs + gregset->r_y_offset, gregset->r_y_size);
1263 regcache_raw_supply (regcache, SPARC64_Y_REGNUM, buf);
3567a8ea 1264 }
8b39fe56 1265
386c036b
MK
1266 if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
1267 && gregset->r_fprs_offset != -1)
1268 regcache_raw_supply (regcache, SPARC64_FPRS_REGNUM,
1269 regs + gregset->r_fprs_offset);
1270 }
1271
1272 if (regnum == SPARC_G0_REGNUM || regnum == -1)
22e74ef9 1273 regcache_raw_supply (regcache, SPARC_G0_REGNUM, &zero);
386c036b
MK
1274
1275 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1276 {
1277 int offset = gregset->r_g1_offset;
1278
1279 if (sparc32)
1280 offset += 4;
1281
1282 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
8b39fe56 1283 {
3567a8ea 1284 if (regnum == i || regnum == -1)
386c036b
MK
1285 regcache_raw_supply (regcache, i, regs + offset);
1286 offset += 8;
1287 }
1288 }
1289
1290 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1291 {
1292 /* Not all of the register set variants include Locals and
1293 Inputs. For those that don't, we read them off the stack. */
1294 if (gregset->r_l0_offset == -1)
1295 {
1296 ULONGEST sp;
1297
1298 regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1299 sparc_supply_rwindow (regcache, sp, regnum);
1300 }
1301 else
1302 {
1303 int offset = gregset->r_l0_offset;
1304
1305 if (sparc32)
1306 offset += 4;
1307
1308 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
3567a8ea 1309 {
386c036b
MK
1310 if (regnum == i || regnum == -1)
1311 regcache_raw_supply (regcache, i, regs + offset);
1312 offset += 8;
3567a8ea 1313 }
8b39fe56
MK
1314 }
1315 }
1316}
1317
1318void
386c036b
MK
1319sparc64_collect_gregset (const struct sparc_gregset *gregset,
1320 const struct regcache *regcache,
1321 int regnum, void *gregs)
8b39fe56 1322{
e17a4113
UW
1323 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1324 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1325 int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
e1613aba 1326 gdb_byte *regs = gregs;
3567a8ea
MK
1327 int i;
1328
386c036b 1329 if (sparc32)
8b39fe56 1330 {
386c036b
MK
1331 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1332 {
1333 int offset = gregset->r_tstate_offset;
1334 ULONGEST tstate, psr;
e1613aba 1335 gdb_byte buf[8];
386c036b 1336
e17a4113 1337 tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
386c036b 1338 regcache_raw_collect (regcache, SPARC32_PSR_REGNUM, buf);
e17a4113 1339 psr = extract_unsigned_integer (buf, 4, byte_order);
386c036b
MK
1340 tstate |= (psr & PSR_ICC) << 12;
1341 if ((psr & (PSR_VERS | PSR_IMPL)) == PSR_V8PLUS)
1342 tstate |= (psr & PSR_XCC) << 20;
e17a4113 1343 store_unsigned_integer (buf, 8, byte_order, tstate);
386c036b
MK
1344 memcpy (regs + offset, buf, 8);
1345 }
8b39fe56 1346
386c036b
MK
1347 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1348 regcache_raw_collect (regcache, SPARC32_PC_REGNUM,
1349 regs + gregset->r_pc_offset + 4);
1350
1351 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1352 regcache_raw_collect (regcache, SPARC32_NPC_REGNUM,
1353 regs + gregset->r_npc_offset + 4);
1354
1355 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
8b39fe56 1356 {
386c036b
MK
1357 int offset = gregset->r_y_offset + 8 - gregset->r_y_size;
1358 regcache_raw_collect (regcache, SPARC32_Y_REGNUM, regs + offset);
8b39fe56
MK
1359 }
1360 }
1361 else
1362 {
386c036b
MK
1363 if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
1364 regcache_raw_collect (regcache, SPARC64_STATE_REGNUM,
1365 regs + gregset->r_tstate_offset);
1366
1367 if (regnum == SPARC64_PC_REGNUM || regnum == -1)
1368 regcache_raw_collect (regcache, SPARC64_PC_REGNUM,
1369 regs + gregset->r_pc_offset);
3567a8ea 1370
386c036b
MK
1371 if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
1372 regcache_raw_collect (regcache, SPARC64_NPC_REGNUM,
1373 regs + gregset->r_npc_offset);
3567a8ea 1374
386c036b 1375 if (regnum == SPARC64_Y_REGNUM || regnum == -1)
3567a8ea 1376 {
e1613aba 1377 gdb_byte buf[8];
386c036b
MK
1378
1379 regcache_raw_collect (regcache, SPARC64_Y_REGNUM, buf);
1380 memcpy (regs + gregset->r_y_offset,
1381 buf + 8 - gregset->r_y_size, gregset->r_y_size);
1382 }
1383
1384 if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
1385 && gregset->r_fprs_offset != -1)
1386 regcache_raw_collect (regcache, SPARC64_FPRS_REGNUM,
1387 regs + gregset->r_fprs_offset);
1388
1389 }
1390
1391 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1392 {
1393 int offset = gregset->r_g1_offset;
1394
1395 if (sparc32)
1396 offset += 4;
1397
1398 /* %g0 is always zero. */
1399 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
1400 {
1401 if (regnum == i || regnum == -1)
1402 regcache_raw_collect (regcache, i, regs + offset);
1403 offset += 8;
1404 }
1405 }
1406
1407 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1408 {
1409 /* Not all of the register set variants include Locals and
1410 Inputs. For those that don't, we read them off the stack. */
1411 if (gregset->r_l0_offset != -1)
1412 {
1413 int offset = gregset->r_l0_offset;
1414
1415 if (sparc32)
1416 offset += 4;
1417
1418 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
3567a8ea 1419 {
386c036b
MK
1420 if (regnum == i || regnum == -1)
1421 regcache_raw_collect (regcache, i, regs + offset);
1422 offset += 8;
3567a8ea
MK
1423 }
1424 }
8b39fe56
MK
1425 }
1426}
8b39fe56 1427
386c036b
MK
1428void
1429sparc64_supply_fpregset (struct regcache *regcache,
1430 int regnum, const void *fpregs)
1431{
e6d4f032 1432 int sparc32 = (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32);
e1613aba 1433 const gdb_byte *regs = fpregs;
386c036b
MK
1434 int i;
1435
1436 for (i = 0; i < 32; i++)
1437 {
1438 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
1439 regcache_raw_supply (regcache, SPARC_F0_REGNUM + i, regs + (i * 4));
1440 }
1441
1442 if (sparc32)
1443 {
1444 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
1445 regcache_raw_supply (regcache, SPARC32_FSR_REGNUM,
1446 regs + (32 * 4) + (16 * 8) + 4);
1447 }
1448 else
1449 {
1450 for (i = 0; i < 16; i++)
1451 {
1452 if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
1453 regcache_raw_supply (regcache, SPARC64_F32_REGNUM + i,
1454 regs + (32 * 4) + (i * 8));
1455 }
1456
1457 if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
1458 regcache_raw_supply (regcache, SPARC64_FSR_REGNUM,
1459 regs + (32 * 4) + (16 * 8));
1460 }
1461}
8b39fe56
MK
1462
1463void
386c036b
MK
1464sparc64_collect_fpregset (const struct regcache *regcache,
1465 int regnum, void *fpregs)
8b39fe56 1466{
e6d4f032 1467 int sparc32 = (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32);
e1613aba 1468 gdb_byte *regs = fpregs;
386c036b
MK
1469 int i;
1470
1471 for (i = 0; i < 32; i++)
1472 {
1473 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
1474 regcache_raw_collect (regcache, SPARC_F0_REGNUM + i, regs + (i * 4));
1475 }
1476
1477 if (sparc32)
1478 {
1479 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
1480 regcache_raw_collect (regcache, SPARC32_FSR_REGNUM,
1481 regs + (32 * 4) + (16 * 8) + 4);
1482 }
1483 else
1484 {
1485 for (i = 0; i < 16; i++)
1486 {
1487 if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
1488 regcache_raw_collect (regcache, SPARC64_F32_REGNUM + i,
1489 regs + (32 * 4) + (i * 8));
1490 }
1491
1492 if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
1493 regcache_raw_collect (regcache, SPARC64_FSR_REGNUM,
1494 regs + (32 * 4) + (16 * 8));
1495 }
8b39fe56 1496}
fd936806 1497
This page took 0.860996 seconds and 4 git commands to generate.