2011-02-25 Rafael Ávila de Espíndola <respindola@mozilla.com>
[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
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
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
323static void
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);
8b39fe56
MK
329 gdb_assert (regnum >= SPARC64_NUM_REGS);
330
331 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
332 {
333 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
334 regcache_raw_read (regcache, regnum, buf);
e1613aba 335 regcache_raw_read (regcache, regnum + 1, buf + 4);
8b39fe56
MK
336 }
337 else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
338 {
339 regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
340 regcache_raw_read (regcache, regnum, buf);
341 }
342 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
343 {
344 regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
345 regcache_raw_read (regcache, regnum, buf);
e1613aba
MK
346 regcache_raw_read (regcache, regnum + 1, buf + 4);
347 regcache_raw_read (regcache, regnum + 2, buf + 8);
348 regcache_raw_read (regcache, regnum + 3, buf + 12);
8b39fe56
MK
349 }
350 else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
351 {
352 regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
353 regcache_raw_read (regcache, regnum, buf);
e1613aba 354 regcache_raw_read (regcache, regnum + 1, buf + 8);
8b39fe56
MK
355 }
356 else if (regnum == SPARC64_CWP_REGNUM
357 || regnum == SPARC64_PSTATE_REGNUM
358 || regnum == SPARC64_ASI_REGNUM
359 || regnum == SPARC64_CCR_REGNUM)
360 {
361 ULONGEST state;
362
363 regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
364 switch (regnum)
365 {
3567a8ea 366 case SPARC64_CWP_REGNUM:
8b39fe56
MK
367 state = (state >> 0) & ((1 << 5) - 1);
368 break;
3567a8ea 369 case SPARC64_PSTATE_REGNUM:
8b39fe56
MK
370 state = (state >> 8) & ((1 << 12) - 1);
371 break;
3567a8ea 372 case SPARC64_ASI_REGNUM:
8b39fe56
MK
373 state = (state >> 24) & ((1 << 8) - 1);
374 break;
3567a8ea 375 case SPARC64_CCR_REGNUM:
8b39fe56
MK
376 state = (state >> 32) & ((1 << 8) - 1);
377 break;
378 }
e17a4113 379 store_unsigned_integer (buf, 8, byte_order, state);
8b39fe56
MK
380 }
381}
382
383static void
384sparc64_pseudo_register_write (struct gdbarch *gdbarch,
385 struct regcache *regcache,
e1613aba 386 int regnum, const gdb_byte *buf)
8b39fe56 387{
e17a4113 388 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8b39fe56
MK
389 gdb_assert (regnum >= SPARC64_NUM_REGS);
390
391 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
392 {
393 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
394 regcache_raw_write (regcache, regnum, buf);
e1613aba 395 regcache_raw_write (regcache, regnum + 1, buf + 4);
8b39fe56
MK
396 }
397 else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
398 {
399 regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
400 regcache_raw_write (regcache, regnum, buf);
401 }
402 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
403 {
404 regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
405 regcache_raw_write (regcache, regnum, buf);
e1613aba
MK
406 regcache_raw_write (regcache, regnum + 1, buf + 4);
407 regcache_raw_write (regcache, regnum + 2, buf + 8);
408 regcache_raw_write (regcache, regnum + 3, buf + 12);
8b39fe56
MK
409 }
410 else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
411 {
412 regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
413 regcache_raw_write (regcache, regnum, buf);
e1613aba 414 regcache_raw_write (regcache, regnum + 1, buf + 8);
8b39fe56 415 }
3567a8ea
MK
416 else if (regnum == SPARC64_CWP_REGNUM
417 || regnum == SPARC64_PSTATE_REGNUM
418 || regnum == SPARC64_ASI_REGNUM
419 || regnum == SPARC64_CCR_REGNUM)
420 {
421 ULONGEST state, bits;
422
423 regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
e17a4113 424 bits = extract_unsigned_integer (buf, 8, byte_order);
3567a8ea
MK
425 switch (regnum)
426 {
427 case SPARC64_CWP_REGNUM:
428 state |= ((bits & ((1 << 5) - 1)) << 0);
429 break;
430 case SPARC64_PSTATE_REGNUM:
431 state |= ((bits & ((1 << 12) - 1)) << 8);
432 break;
433 case SPARC64_ASI_REGNUM:
434 state |= ((bits & ((1 << 8) - 1)) << 24);
435 break;
436 case SPARC64_CCR_REGNUM:
437 state |= ((bits & ((1 << 8) - 1)) << 32);
438 break;
439 }
440 regcache_raw_write_unsigned (regcache, SPARC64_STATE_REGNUM, state);
441 }
8b39fe56 442}
8b39fe56
MK
443\f
444
8b39fe56
MK
445/* Return PC of first real instruction of the function starting at
446 START_PC. */
447
448static CORE_ADDR
6093d2eb 449sparc64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
8b39fe56
MK
450{
451 struct symtab_and_line sal;
452 CORE_ADDR func_start, func_end;
386c036b 453 struct sparc_frame_cache cache;
8b39fe56
MK
454
455 /* This is the preferred method, find the end of the prologue by
456 using the debugging information. */
457 if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
458 {
459 sal = find_pc_line (func_start, 0);
460
461 if (sal.end < func_end
462 && start_pc <= sal.end)
463 return sal.end;
464 }
465
be8626e0
MD
466 return sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffULL,
467 &cache);
8b39fe56
MK
468}
469
470/* Normal frames. */
471
386c036b 472static struct sparc_frame_cache *
236369e7 473sparc64_frame_cache (struct frame_info *this_frame, void **this_cache)
8b39fe56 474{
236369e7 475 return sparc_frame_cache (this_frame, this_cache);
8b39fe56
MK
476}
477
478static void
236369e7 479sparc64_frame_this_id (struct frame_info *this_frame, void **this_cache,
8b39fe56
MK
480 struct frame_id *this_id)
481{
386c036b 482 struct sparc_frame_cache *cache =
236369e7 483 sparc64_frame_cache (this_frame, this_cache);
8b39fe56
MK
484
485 /* This marks the outermost frame. */
486 if (cache->base == 0)
487 return;
488
489 (*this_id) = frame_id_build (cache->base, cache->pc);
490}
491
236369e7
JB
492static struct value *
493sparc64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
494 int regnum)
8b39fe56 495{
e17a4113 496 struct gdbarch *gdbarch = get_frame_arch (this_frame);
386c036b 497 struct sparc_frame_cache *cache =
236369e7 498 sparc64_frame_cache (this_frame, this_cache);
8b39fe56
MK
499
500 if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
501 {
236369e7 502 CORE_ADDR pc = (regnum == SPARC64_NPC_REGNUM) ? 4 : 0;
8b39fe56 503
236369e7
JB
504 regnum = cache->frameless_p ? SPARC_O7_REGNUM : SPARC_I7_REGNUM;
505 pc += get_frame_register_unsigned (this_frame, regnum) + 8;
506 return frame_unwind_got_constant (this_frame, regnum, pc);
8b39fe56
MK
507 }
508
f700a364
MK
509 /* Handle StackGhost. */
510 {
e17a4113 511 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
f700a364
MK
512
513 if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
514 {
236369e7
JB
515 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
516 ULONGEST i7;
517
518 /* Read the value in from memory. */
519 i7 = get_frame_memory_unsigned (this_frame, addr, 8);
520 return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
f700a364
MK
521 }
522 }
523
8b39fe56
MK
524 /* The previous frame's `local' and `in' registers have been saved
525 in the register save area. */
526 if (!cache->frameless_p
527 && regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM)
528 {
236369e7 529 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
8b39fe56 530
236369e7 531 return frame_unwind_got_memory (this_frame, regnum, addr);
8b39fe56
MK
532 }
533
534 /* The previous frame's `out' registers are accessable as the
535 current frame's `in' registers. */
536 if (!cache->frameless_p
537 && regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM)
538 regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
539
236369e7 540 return frame_unwind_got_register (this_frame, regnum, regnum);
8b39fe56
MK
541}
542
543static const struct frame_unwind sparc64_frame_unwind =
544{
545 NORMAL_FRAME,
546 sparc64_frame_this_id,
236369e7
JB
547 sparc64_frame_prev_register,
548 NULL,
549 default_frame_sniffer
8b39fe56 550};
8b39fe56
MK
551\f
552
553static CORE_ADDR
236369e7 554sparc64_frame_base_address (struct frame_info *this_frame, void **this_cache)
8b39fe56 555{
386c036b 556 struct sparc_frame_cache *cache =
236369e7 557 sparc64_frame_cache (this_frame, this_cache);
8b39fe56 558
5b2d44a0 559 return cache->base;
8b39fe56
MK
560}
561
562static const struct frame_base sparc64_frame_base =
563{
564 &sparc64_frame_unwind,
565 sparc64_frame_base_address,
566 sparc64_frame_base_address,
567 sparc64_frame_base_address
568};
8b39fe56
MK
569\f
570/* Check whether TYPE must be 16-byte aligned. */
571
572static int
573sparc64_16_byte_align_p (struct type *type)
574{
575 if (sparc64_floating_p (type) && TYPE_LENGTH (type) == 16)
576 return 1;
577
578 if (sparc64_structure_or_union_p (type))
579 {
580 int i;
581
582 for (i = 0; i < TYPE_NFIELDS (type); i++)
60af1db2
MK
583 {
584 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
585
586 if (sparc64_16_byte_align_p (subtype))
587 return 1;
588 }
8b39fe56
MK
589 }
590
591 return 0;
592}
593
594/* Store floating fields of element ELEMENT of an "parameter array"
595 that has type TYPE and is stored at BITPOS in VALBUF in the
596 apropriate registers of REGCACHE. This function can be called
597 recursively and therefore handles floating types in addition to
598 structures. */
599
600static void
601sparc64_store_floating_fields (struct regcache *regcache, struct type *type,
e1613aba 602 const gdb_byte *valbuf, int element, int bitpos)
8b39fe56
MK
603{
604 gdb_assert (element < 16);
605
606 if (sparc64_floating_p (type))
607 {
608 int len = TYPE_LENGTH (type);
609 int regnum;
610
611 if (len == 16)
612 {
613 gdb_assert (bitpos == 0);
614 gdb_assert ((element % 2) == 0);
615
616 regnum = SPARC64_Q0_REGNUM + element / 2;
617 regcache_cooked_write (regcache, regnum, valbuf);
618 }
619 else if (len == 8)
620 {
621 gdb_assert (bitpos == 0 || bitpos == 64);
622
623 regnum = SPARC64_D0_REGNUM + element + bitpos / 64;
624 regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
625 }
626 else
627 {
628 gdb_assert (len == 4);
629 gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 128);
630
631 regnum = SPARC_F0_REGNUM + element * 2 + bitpos / 32;
632 regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
633 }
634 }
635 else if (sparc64_structure_or_union_p (type))
636 {
637 int i;
638
639 for (i = 0; i < TYPE_NFIELDS (type); i++)
60af1db2
MK
640 {
641 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
642 int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);
643
644 sparc64_store_floating_fields (regcache, subtype, valbuf,
645 element, subpos);
646 }
200cc553
MK
647
648 /* GCC has an interesting bug. If TYPE is a structure that has
649 a single `float' member, GCC doesn't treat it as a structure
650 at all, but rather as an ordinary `float' argument. This
651 argument will be stored in %f1, as required by the psABI.
652 However, as a member of a structure the psABI requires it to
5154b0cd
MK
653 be stored in %f0. This bug is present in GCC 3.3.2, but
654 probably in older releases to. To appease GCC, if a
655 structure has only a single `float' member, we store its
656 value in %f1 too (we already have stored in %f0). */
200cc553
MK
657 if (TYPE_NFIELDS (type) == 1)
658 {
659 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, 0));
660
661 if (sparc64_floating_p (subtype) && TYPE_LENGTH (subtype) == 4)
662 regcache_cooked_write (regcache, SPARC_F1_REGNUM, valbuf);
663 }
8b39fe56
MK
664 }
665}
666
667/* Fetch floating fields from a variable of type TYPE from the
668 appropriate registers for BITPOS in REGCACHE and store it at BITPOS
669 in VALBUF. This function can be called recursively and therefore
670 handles floating types in addition to structures. */
671
672static void
673sparc64_extract_floating_fields (struct regcache *regcache, struct type *type,
e1613aba 674 gdb_byte *valbuf, int bitpos)
8b39fe56
MK
675{
676 if (sparc64_floating_p (type))
677 {
678 int len = TYPE_LENGTH (type);
679 int regnum;
680
681 if (len == 16)
682 {
683 gdb_assert (bitpos == 0 || bitpos == 128);
684
685 regnum = SPARC64_Q0_REGNUM + bitpos / 128;
686 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
687 }
688 else if (len == 8)
689 {
690 gdb_assert (bitpos % 64 == 0 && bitpos >= 0 && bitpos < 256);
691
692 regnum = SPARC64_D0_REGNUM + bitpos / 64;
693 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
694 }
695 else
696 {
697 gdb_assert (len == 4);
698 gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 256);
699
700 regnum = SPARC_F0_REGNUM + bitpos / 32;
701 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
702 }
703 }
704 else if (sparc64_structure_or_union_p (type))
705 {
706 int i;
707
708 for (i = 0; i < TYPE_NFIELDS (type); i++)
60af1db2
MK
709 {
710 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
711 int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);
712
713 sparc64_extract_floating_fields (regcache, subtype, valbuf, subpos);
714 }
8b39fe56
MK
715 }
716}
717
718/* Store the NARGS arguments ARGS and STRUCT_ADDR (if STRUCT_RETURN is
719 non-zero) in REGCACHE and on the stack (starting from address SP). */
720
721static CORE_ADDR
722sparc64_store_arguments (struct regcache *regcache, int nargs,
723 struct value **args, CORE_ADDR sp,
724 int struct_return, CORE_ADDR struct_addr)
725{
df4df182 726 struct gdbarch *gdbarch = get_regcache_arch (regcache);
8b39fe56
MK
727 /* Number of extended words in the "parameter array". */
728 int num_elements = 0;
729 int element = 0;
730 int i;
731
732 /* Take BIAS into account. */
733 sp += BIAS;
734
735 /* First we calculate the number of extended words in the "parameter
736 array". While doing so we also convert some of the arguments. */
737
738 if (struct_return)
739 num_elements++;
740
741 for (i = 0; i < nargs; i++)
742 {
4991999e 743 struct type *type = value_type (args[i]);
8b39fe56
MK
744 int len = TYPE_LENGTH (type);
745
746 if (sparc64_structure_or_union_p (type))
747 {
748 /* Structure or Union arguments. */
749 if (len <= 16)
750 {
751 if (num_elements % 2 && sparc64_16_byte_align_p (type))
752 num_elements++;
753 num_elements += ((len + 7) / 8);
754 }
755 else
756 {
757 /* The psABI says that "Structures or unions larger than
758 sixteen bytes are copied by the caller and passed
759 indirectly; the caller will pass the address of a
760 correctly aligned structure value. This sixty-four
761 bit address will occupy one word in the parameter
762 array, and may be promoted to an %o register like any
763 other pointer value." Allocate memory for these
764 values on the stack. */
765 sp -= len;
766
767 /* Use 16-byte alignment for these values. That's
768 always correct, and wasting a few bytes shouldn't be
769 a problem. */
770 sp &= ~0xf;
771
0fd88904 772 write_memory (sp, value_contents (args[i]), len);
8b39fe56
MK
773 args[i] = value_from_pointer (lookup_pointer_type (type), sp);
774 num_elements++;
775 }
776 }
777 else if (sparc64_floating_p (type))
778 {
779 /* Floating arguments. */
780
781 if (len == 16)
782 {
783 /* The psABI says that "Each quad-precision parameter
784 value will be assigned to two extended words in the
785 parameter array. */
786 num_elements += 2;
787
788 /* The psABI says that "Long doubles must be
789 quad-aligned, and thus a hole might be introduced
790 into the parameter array to force alignment." Skip
791 an element if necessary. */
792 if (num_elements % 2)
793 num_elements++;
794 }
795 else
796 num_elements++;
797 }
798 else
799 {
800 /* Integral and pointer arguments. */
801 gdb_assert (sparc64_integral_or_pointer_p (type));
802
803 /* The psABI says that "Each argument value of integral type
804 smaller than an extended word will be widened by the
805 caller to an extended word according to the signed-ness
806 of the argument type." */
807 if (len < 8)
df4df182
UW
808 args[i] = value_cast (builtin_type (gdbarch)->builtin_int64,
809 args[i]);
8b39fe56
MK
810 num_elements++;
811 }
812 }
813
814 /* Allocate the "parameter array". */
815 sp -= num_elements * 8;
816
817 /* The psABI says that "Every stack frame must be 16-byte aligned." */
818 sp &= ~0xf;
819
820 /* Now we store the arguments in to the "paramater array". Some
821 Integer or Pointer arguments and Structure or Union arguments
822 will be passed in %o registers. Some Floating arguments and
823 floating members of structures are passed in floating-point
824 registers. However, for functions with variable arguments,
825 floating arguments are stored in an %0 register, and for
826 functions without a prototype floating arguments are stored in
827 both a floating-point and an %o registers, or a floating-point
828 register and memory. To simplify the logic here we always pass
829 arguments in memory, an %o register, and a floating-point
830 register if appropriate. This should be no problem since the
831 contents of any unused memory or registers in the "parameter
832 array" are undefined. */
833
834 if (struct_return)
835 {
836 regcache_cooked_write_unsigned (regcache, SPARC_O0_REGNUM, struct_addr);
837 element++;
838 }
839
840 for (i = 0; i < nargs; i++)
841 {
e1613aba 842 const gdb_byte *valbuf = value_contents (args[i]);
4991999e 843 struct type *type = value_type (args[i]);
8b39fe56
MK
844 int len = TYPE_LENGTH (type);
845 int regnum = -1;
e1613aba 846 gdb_byte buf[16];
8b39fe56
MK
847
848 if (sparc64_structure_or_union_p (type))
849 {
850 /* Structure or Union arguments. */
851 gdb_assert (len <= 16);
852 memset (buf, 0, sizeof (buf));
853 valbuf = memcpy (buf, valbuf, len);
854
855 if (element % 2 && sparc64_16_byte_align_p (type))
856 element++;
857
858 if (element < 6)
859 {
860 regnum = SPARC_O0_REGNUM + element;
861 if (len > 8 && element < 5)
862 regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
863 }
864
865 if (element < 16)
866 sparc64_store_floating_fields (regcache, type, valbuf, element, 0);
867 }
868 else if (sparc64_floating_p (type))
869 {
870 /* Floating arguments. */
871 if (len == 16)
872 {
873 if (element % 2)
874 element++;
875 if (element < 16)
876 regnum = SPARC64_Q0_REGNUM + element / 2;
877 }
878 else if (len == 8)
879 {
880 if (element < 16)
881 regnum = SPARC64_D0_REGNUM + element;
882 }
883 else
884 {
885 /* The psABI says "Each single-precision parameter value
886 will be assigned to one extended word in the
887 parameter array, and right-justified within that
888 word; the left half (even floatregister) is
889 undefined." Even though the psABI says that "the
890 left half is undefined", set it to zero here. */
891 memset (buf, 0, 4);
8ada74e3
MK
892 memcpy (buf + 4, valbuf, 4);
893 valbuf = buf;
8b39fe56
MK
894 len = 8;
895 if (element < 16)
8ada74e3 896 regnum = SPARC64_D0_REGNUM + element;
8b39fe56
MK
897 }
898 }
899 else
900 {
901 /* Integral and pointer arguments. */
902 gdb_assert (len == 8);
903 if (element < 6)
904 regnum = SPARC_O0_REGNUM + element;
905 }
906
907 if (regnum != -1)
908 {
909 regcache_cooked_write (regcache, regnum, valbuf);
910
911 /* If we're storing the value in a floating-point register,
912 also store it in the corresponding %0 register(s). */
913 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D10_REGNUM)
914 {
915 gdb_assert (element < 6);
916 regnum = SPARC_O0_REGNUM + element;
917 regcache_cooked_write (regcache, regnum, valbuf);
918 }
919 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q8_REGNUM)
920 {
921 gdb_assert (element < 6);
922 regnum = SPARC_O0_REGNUM + element;
923 regcache_cooked_write (regcache, regnum, valbuf);
d47079be 924 regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
8b39fe56
MK
925 }
926 }
927
c4f2d4d7 928 /* Always store the argument in memory. */
8b39fe56
MK
929 write_memory (sp + element * 8, valbuf, len);
930 element += ((len + 7) / 8);
931 }
932
933 gdb_assert (element == num_elements);
934
935 /* Take BIAS into account. */
936 sp -= BIAS;
937 return sp;
938}
939
49a45ecf
JB
940static CORE_ADDR
941sparc64_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
942{
943 /* The ABI requires 16-byte alignment. */
944 return address & ~0xf;
945}
946
8b39fe56 947static CORE_ADDR
7d9b040b 948sparc64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
8b39fe56
MK
949 struct regcache *regcache, CORE_ADDR bp_addr,
950 int nargs, struct value **args, CORE_ADDR sp,
951 int struct_return, CORE_ADDR struct_addr)
952{
953 /* Set return address. */
954 regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, bp_addr - 8);
955
956 /* Set up function arguments. */
957 sp = sparc64_store_arguments (regcache, nargs, args, sp,
958 struct_return, struct_addr);
959
960 /* Allocate the register save area. */
961 sp -= 16 * 8;
962
963 /* Stack should be 16-byte aligned at this point. */
3567a8ea 964 gdb_assert ((sp + BIAS) % 16 == 0);
8b39fe56
MK
965
966 /* Finally, update the stack pointer. */
967 regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
968
5b2d44a0 969 return sp + BIAS;
8b39fe56
MK
970}
971\f
972
973/* Extract from an array REGBUF containing the (raw) register state, a
974 function return value of TYPE, and copy that into VALBUF. */
975
976static void
977sparc64_extract_return_value (struct type *type, struct regcache *regcache,
e1613aba 978 gdb_byte *valbuf)
8b39fe56
MK
979{
980 int len = TYPE_LENGTH (type);
e1613aba 981 gdb_byte buf[32];
8b39fe56
MK
982 int i;
983
984 if (sparc64_structure_or_union_p (type))
985 {
986 /* Structure or Union return values. */
987 gdb_assert (len <= 32);
988
989 for (i = 0; i < ((len + 7) / 8); i++)
990 regcache_cooked_read (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
991 if (TYPE_CODE (type) != TYPE_CODE_UNION)
992 sparc64_extract_floating_fields (regcache, type, buf, 0);
993 memcpy (valbuf, buf, len);
994 }
995 else if (sparc64_floating_p (type))
996 {
997 /* Floating return values. */
998 for (i = 0; i < len / 4; i++)
999 regcache_cooked_read (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
1000 memcpy (valbuf, buf, len);
1001 }
4bd87714
JB
1002 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1003 {
1004 /* Small arrays are returned the same way as small structures. */
1005 gdb_assert (len <= 32);
1006
1007 for (i = 0; i < ((len + 7) / 8); i++)
1008 regcache_cooked_read (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1009 memcpy (valbuf, buf, len);
1010 }
8b39fe56
MK
1011 else
1012 {
1013 /* Integral and pointer return values. */
1014 gdb_assert (sparc64_integral_or_pointer_p (type));
1015
1016 /* Just stripping off any unused bytes should preserve the
1017 signed-ness just fine. */
1018 regcache_cooked_read (regcache, SPARC_O0_REGNUM, buf);
1019 memcpy (valbuf, buf + 8 - len, len);
1020 }
1021}
1022
1023/* Write into the appropriate registers a function return value stored
1024 in VALBUF of type TYPE. */
1025
1026static void
1027sparc64_store_return_value (struct type *type, struct regcache *regcache,
e1613aba 1028 const gdb_byte *valbuf)
8b39fe56
MK
1029{
1030 int len = TYPE_LENGTH (type);
e1613aba 1031 gdb_byte buf[16];
8b39fe56
MK
1032 int i;
1033
1034 if (sparc64_structure_or_union_p (type))
1035 {
1036 /* Structure or Union return values. */
1037 gdb_assert (len <= 32);
1038
1039 /* Simplify matters by storing the complete value (including
1040 floating members) into %o0 and %o1. Floating members are
1041 also store in the appropriate floating-point registers. */
1042 memset (buf, 0, sizeof (buf));
1043 memcpy (buf, valbuf, len);
1044 for (i = 0; i < ((len + 7) / 8); i++)
60af1db2 1045 regcache_cooked_write (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
8b39fe56
MK
1046 if (TYPE_CODE (type) != TYPE_CODE_UNION)
1047 sparc64_store_floating_fields (regcache, type, buf, 0, 0);
1048 }
1049 else if (sparc64_floating_p (type))
1050 {
1051 /* Floating return values. */
1052 memcpy (buf, valbuf, len);
1053 for (i = 0; i < len / 4; i++)
1054 regcache_cooked_write (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
1055 }
4bd87714
JB
1056 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1057 {
1058 /* Small arrays are returned the same way as small structures. */
1059 gdb_assert (len <= 32);
1060
1061 memset (buf, 0, sizeof (buf));
1062 memcpy (buf, valbuf, len);
1063 for (i = 0; i < ((len + 7) / 8); i++)
1064 regcache_cooked_write (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1065 }
8b39fe56
MK
1066 else
1067 {
1068 /* Integral and pointer return values. */
1069 gdb_assert (sparc64_integral_or_pointer_p (type));
1070
1071 /* ??? Do we need to do any sign-extension here? */
1072 memset (buf, 0, 8);
1073 memcpy (buf + 8 - len, valbuf, len);
1074 regcache_cooked_write (regcache, SPARC_O0_REGNUM, buf);
1075 }
1076}
1077
60af1db2 1078static enum return_value_convention
c055b101
CV
1079sparc64_return_value (struct gdbarch *gdbarch, struct type *func_type,
1080 struct type *type, struct regcache *regcache,
1081 gdb_byte *readbuf, const gdb_byte *writebuf)
8b39fe56 1082{
60af1db2
MK
1083 if (TYPE_LENGTH (type) > 32)
1084 return RETURN_VALUE_STRUCT_CONVENTION;
1085
1086 if (readbuf)
1087 sparc64_extract_return_value (type, regcache, readbuf);
1088 if (writebuf)
1089 sparc64_store_return_value (type, regcache, writebuf);
1090
1091 return RETURN_VALUE_REGISTER_CONVENTION;
8b39fe56 1092}
8b39fe56 1093\f
8b39fe56 1094
02a71ae8
MK
1095static void
1096sparc64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
aff37fc1 1097 struct dwarf2_frame_state_reg *reg,
4a4e5149 1098 struct frame_info *this_frame)
02a71ae8
MK
1099{
1100 switch (regnum)
1101 {
1102 case SPARC_G0_REGNUM:
1103 /* Since %g0 is always zero, there is no point in saving it, and
1104 people will be inclined omit it from the CFI. Make sure we
1105 don't warn about that. */
1106 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1107 break;
1108 case SPARC_SP_REGNUM:
1109 reg->how = DWARF2_FRAME_REG_CFA;
1110 break;
1111 case SPARC64_PC_REGNUM:
1112 reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1113 reg->loc.offset = 8;
1114 break;
1115 case SPARC64_NPC_REGNUM:
1116 reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1117 reg->loc.offset = 12;
1118 break;
1119 }
1120}
1121
8b39fe56 1122void
386c036b 1123sparc64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
8b39fe56 1124{
386c036b 1125 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8b39fe56 1126
386c036b
MK
1127 tdep->pc_regnum = SPARC64_PC_REGNUM;
1128 tdep->npc_regnum = SPARC64_NPC_REGNUM;
8b39fe56 1129
386c036b 1130 /* This is what all the fuss is about. */
8b39fe56
MK
1131 set_gdbarch_long_bit (gdbarch, 64);
1132 set_gdbarch_long_long_bit (gdbarch, 64);
1133 set_gdbarch_ptr_bit (gdbarch, 64);
8b39fe56
MK
1134
1135 set_gdbarch_num_regs (gdbarch, SPARC64_NUM_REGS);
1136 set_gdbarch_register_name (gdbarch, sparc64_register_name);
1137 set_gdbarch_register_type (gdbarch, sparc64_register_type);
1138 set_gdbarch_num_pseudo_regs (gdbarch, SPARC64_NUM_PSEUDO_REGS);
1139 set_gdbarch_pseudo_register_read (gdbarch, sparc64_pseudo_register_read);
1140 set_gdbarch_pseudo_register_write (gdbarch, sparc64_pseudo_register_write);
1141
1142 /* Register numbers of various important registers. */
8b39fe56 1143 set_gdbarch_pc_regnum (gdbarch, SPARC64_PC_REGNUM); /* %pc */
8b39fe56
MK
1144
1145 /* Call dummy code. */
49a45ecf 1146 set_gdbarch_frame_align (gdbarch, sparc64_frame_align);
386c036b
MK
1147 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1148 set_gdbarch_push_dummy_code (gdbarch, NULL);
8b39fe56
MK
1149 set_gdbarch_push_dummy_call (gdbarch, sparc64_push_dummy_call);
1150
60af1db2 1151 set_gdbarch_return_value (gdbarch, sparc64_return_value);
386c036b
MK
1152 set_gdbarch_stabs_argument_has_addr
1153 (gdbarch, default_stabs_argument_has_addr);
8b39fe56
MK
1154
1155 set_gdbarch_skip_prologue (gdbarch, sparc64_skip_prologue);
1156
02a71ae8
MK
1157 /* Hook in the DWARF CFI frame unwinder. */
1158 dwarf2_frame_set_init_reg (gdbarch, sparc64_dwarf2_frame_init_reg);
1159 /* FIXME: kettenis/20050423: Don't enable the unwinder until the
1160 StackGhost issues have been resolved. */
1161
236369e7 1162 frame_unwind_append_unwinder (gdbarch, &sparc64_frame_unwind);
8b39fe56 1163 frame_base_set_default (gdbarch, &sparc64_frame_base);
386c036b
MK
1164}
1165\f
8b39fe56 1166
386c036b 1167/* Helper functions for dealing with register sets. */
8b39fe56 1168
386c036b
MK
1169#define TSTATE_CWP 0x000000000000001fULL
1170#define TSTATE_ICC 0x0000000f00000000ULL
1171#define TSTATE_XCC 0x000000f000000000ULL
8b39fe56 1172
386c036b
MK
1173#define PSR_S 0x00000080
1174#define PSR_ICC 0x00f00000
1175#define PSR_VERS 0x0f000000
1176#define PSR_IMPL 0xf0000000
1177#define PSR_V8PLUS 0xff000000
1178#define PSR_XCC 0x000f0000
8b39fe56 1179
3567a8ea 1180void
386c036b
MK
1181sparc64_supply_gregset (const struct sparc_gregset *gregset,
1182 struct regcache *regcache,
1183 int regnum, const void *gregs)
8b39fe56 1184{
e17a4113
UW
1185 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1186 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1187 int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
e1613aba 1188 const gdb_byte *regs = gregs;
8b39fe56
MK
1189 int i;
1190
386c036b 1191 if (sparc32)
8b39fe56 1192 {
386c036b
MK
1193 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1194 {
1195 int offset = gregset->r_tstate_offset;
1196 ULONGEST tstate, psr;
e1613aba 1197 gdb_byte buf[4];
386c036b 1198
e17a4113 1199 tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
386c036b
MK
1200 psr = ((tstate & TSTATE_CWP) | PSR_S | ((tstate & TSTATE_ICC) >> 12)
1201 | ((tstate & TSTATE_XCC) >> 20) | PSR_V8PLUS);
e17a4113 1202 store_unsigned_integer (buf, 4, byte_order, psr);
386c036b
MK
1203 regcache_raw_supply (regcache, SPARC32_PSR_REGNUM, buf);
1204 }
1205
1206 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1207 regcache_raw_supply (regcache, SPARC32_PC_REGNUM,
1208 regs + gregset->r_pc_offset + 4);
1209
1210 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1211 regcache_raw_supply (regcache, SPARC32_NPC_REGNUM,
1212 regs + gregset->r_npc_offset + 4);
8b39fe56 1213
386c036b 1214 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
8b39fe56 1215 {
386c036b
MK
1216 int offset = gregset->r_y_offset + 8 - gregset->r_y_size;
1217 regcache_raw_supply (regcache, SPARC32_Y_REGNUM, regs + offset);
8b39fe56
MK
1218 }
1219 }
1220 else
1221 {
386c036b
MK
1222 if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
1223 regcache_raw_supply (regcache, SPARC64_STATE_REGNUM,
1224 regs + gregset->r_tstate_offset);
8b39fe56 1225
386c036b
MK
1226 if (regnum == SPARC64_PC_REGNUM || regnum == -1)
1227 regcache_raw_supply (regcache, SPARC64_PC_REGNUM,
1228 regs + gregset->r_pc_offset);
1229
1230 if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
1231 regcache_raw_supply (regcache, SPARC64_NPC_REGNUM,
1232 regs + gregset->r_npc_offset);
1233
1234 if (regnum == SPARC64_Y_REGNUM || regnum == -1)
3567a8ea 1235 {
e1613aba 1236 gdb_byte buf[8];
386c036b
MK
1237
1238 memset (buf, 0, 8);
1239 memcpy (buf + 8 - gregset->r_y_size,
1240 regs + gregset->r_y_offset, gregset->r_y_size);
1241 regcache_raw_supply (regcache, SPARC64_Y_REGNUM, buf);
3567a8ea 1242 }
8b39fe56 1243
386c036b
MK
1244 if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
1245 && gregset->r_fprs_offset != -1)
1246 regcache_raw_supply (regcache, SPARC64_FPRS_REGNUM,
1247 regs + gregset->r_fprs_offset);
1248 }
1249
1250 if (regnum == SPARC_G0_REGNUM || regnum == -1)
1251 regcache_raw_supply (regcache, SPARC_G0_REGNUM, NULL);
1252
1253 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1254 {
1255 int offset = gregset->r_g1_offset;
1256
1257 if (sparc32)
1258 offset += 4;
1259
1260 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
8b39fe56 1261 {
3567a8ea 1262 if (regnum == i || regnum == -1)
386c036b
MK
1263 regcache_raw_supply (regcache, i, regs + offset);
1264 offset += 8;
1265 }
1266 }
1267
1268 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1269 {
1270 /* Not all of the register set variants include Locals and
1271 Inputs. For those that don't, we read them off the stack. */
1272 if (gregset->r_l0_offset == -1)
1273 {
1274 ULONGEST sp;
1275
1276 regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1277 sparc_supply_rwindow (regcache, sp, regnum);
1278 }
1279 else
1280 {
1281 int offset = gregset->r_l0_offset;
1282
1283 if (sparc32)
1284 offset += 4;
1285
1286 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
3567a8ea 1287 {
386c036b
MK
1288 if (regnum == i || regnum == -1)
1289 regcache_raw_supply (regcache, i, regs + offset);
1290 offset += 8;
3567a8ea 1291 }
8b39fe56
MK
1292 }
1293 }
1294}
1295
1296void
386c036b
MK
1297sparc64_collect_gregset (const struct sparc_gregset *gregset,
1298 const struct regcache *regcache,
1299 int regnum, void *gregs)
8b39fe56 1300{
e17a4113
UW
1301 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1302 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1303 int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
e1613aba 1304 gdb_byte *regs = gregs;
3567a8ea
MK
1305 int i;
1306
386c036b 1307 if (sparc32)
8b39fe56 1308 {
386c036b
MK
1309 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1310 {
1311 int offset = gregset->r_tstate_offset;
1312 ULONGEST tstate, psr;
e1613aba 1313 gdb_byte buf[8];
386c036b 1314
e17a4113 1315 tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
386c036b 1316 regcache_raw_collect (regcache, SPARC32_PSR_REGNUM, buf);
e17a4113 1317 psr = extract_unsigned_integer (buf, 4, byte_order);
386c036b
MK
1318 tstate |= (psr & PSR_ICC) << 12;
1319 if ((psr & (PSR_VERS | PSR_IMPL)) == PSR_V8PLUS)
1320 tstate |= (psr & PSR_XCC) << 20;
e17a4113 1321 store_unsigned_integer (buf, 8, byte_order, tstate);
386c036b
MK
1322 memcpy (regs + offset, buf, 8);
1323 }
8b39fe56 1324
386c036b
MK
1325 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1326 regcache_raw_collect (regcache, SPARC32_PC_REGNUM,
1327 regs + gregset->r_pc_offset + 4);
1328
1329 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1330 regcache_raw_collect (regcache, SPARC32_NPC_REGNUM,
1331 regs + gregset->r_npc_offset + 4);
1332
1333 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
8b39fe56 1334 {
386c036b
MK
1335 int offset = gregset->r_y_offset + 8 - gregset->r_y_size;
1336 regcache_raw_collect (regcache, SPARC32_Y_REGNUM, regs + offset);
8b39fe56
MK
1337 }
1338 }
1339 else
1340 {
386c036b
MK
1341 if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
1342 regcache_raw_collect (regcache, SPARC64_STATE_REGNUM,
1343 regs + gregset->r_tstate_offset);
1344
1345 if (regnum == SPARC64_PC_REGNUM || regnum == -1)
1346 regcache_raw_collect (regcache, SPARC64_PC_REGNUM,
1347 regs + gregset->r_pc_offset);
3567a8ea 1348
386c036b
MK
1349 if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
1350 regcache_raw_collect (regcache, SPARC64_NPC_REGNUM,
1351 regs + gregset->r_npc_offset);
3567a8ea 1352
386c036b 1353 if (regnum == SPARC64_Y_REGNUM || regnum == -1)
3567a8ea 1354 {
e1613aba 1355 gdb_byte buf[8];
386c036b
MK
1356
1357 regcache_raw_collect (regcache, SPARC64_Y_REGNUM, buf);
1358 memcpy (regs + gregset->r_y_offset,
1359 buf + 8 - gregset->r_y_size, gregset->r_y_size);
1360 }
1361
1362 if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
1363 && gregset->r_fprs_offset != -1)
1364 regcache_raw_collect (regcache, SPARC64_FPRS_REGNUM,
1365 regs + gregset->r_fprs_offset);
1366
1367 }
1368
1369 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1370 {
1371 int offset = gregset->r_g1_offset;
1372
1373 if (sparc32)
1374 offset += 4;
1375
1376 /* %g0 is always zero. */
1377 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
1378 {
1379 if (regnum == i || regnum == -1)
1380 regcache_raw_collect (regcache, i, regs + offset);
1381 offset += 8;
1382 }
1383 }
1384
1385 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1386 {
1387 /* Not all of the register set variants include Locals and
1388 Inputs. For those that don't, we read them off the stack. */
1389 if (gregset->r_l0_offset != -1)
1390 {
1391 int offset = gregset->r_l0_offset;
1392
1393 if (sparc32)
1394 offset += 4;
1395
1396 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
3567a8ea 1397 {
386c036b
MK
1398 if (regnum == i || regnum == -1)
1399 regcache_raw_collect (regcache, i, regs + offset);
1400 offset += 8;
3567a8ea
MK
1401 }
1402 }
8b39fe56
MK
1403 }
1404}
8b39fe56 1405
386c036b
MK
1406void
1407sparc64_supply_fpregset (struct regcache *regcache,
1408 int regnum, const void *fpregs)
1409{
e6d4f032 1410 int sparc32 = (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32);
e1613aba 1411 const gdb_byte *regs = fpregs;
386c036b
MK
1412 int i;
1413
1414 for (i = 0; i < 32; i++)
1415 {
1416 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
1417 regcache_raw_supply (regcache, SPARC_F0_REGNUM + i, regs + (i * 4));
1418 }
1419
1420 if (sparc32)
1421 {
1422 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
1423 regcache_raw_supply (regcache, SPARC32_FSR_REGNUM,
1424 regs + (32 * 4) + (16 * 8) + 4);
1425 }
1426 else
1427 {
1428 for (i = 0; i < 16; i++)
1429 {
1430 if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
1431 regcache_raw_supply (regcache, SPARC64_F32_REGNUM + i,
1432 regs + (32 * 4) + (i * 8));
1433 }
1434
1435 if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
1436 regcache_raw_supply (regcache, SPARC64_FSR_REGNUM,
1437 regs + (32 * 4) + (16 * 8));
1438 }
1439}
8b39fe56
MK
1440
1441void
386c036b
MK
1442sparc64_collect_fpregset (const struct regcache *regcache,
1443 int regnum, void *fpregs)
8b39fe56 1444{
e6d4f032 1445 int sparc32 = (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32);
e1613aba 1446 gdb_byte *regs = fpregs;
386c036b
MK
1447 int i;
1448
1449 for (i = 0; i < 32; i++)
1450 {
1451 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
1452 regcache_raw_collect (regcache, SPARC_F0_REGNUM + i, regs + (i * 4));
1453 }
1454
1455 if (sparc32)
1456 {
1457 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
1458 regcache_raw_collect (regcache, SPARC32_FSR_REGNUM,
1459 regs + (32 * 4) + (16 * 8) + 4);
1460 }
1461 else
1462 {
1463 for (i = 0; i < 16; i++)
1464 {
1465 if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
1466 regcache_raw_collect (regcache, SPARC64_F32_REGNUM + i,
1467 regs + (32 * 4) + (i * 8));
1468 }
1469
1470 if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
1471 regcache_raw_collect (regcache, SPARC64_FSR_REGNUM,
1472 regs + (32 * 4) + (16 * 8));
1473 }
8b39fe56 1474}
fd936806 1475
This page took 1.042985 seconds and 4 git commands to generate.