Tue Oct 29 16:56:01 1996 Geoffrey Noer <noer@cygnus.com>
[deliverable/binutils-gdb.git] / gdb / sparc-tdep.c
CommitLineData
f9e3b3cc 1/* Target-dependent code for the SPARC for GDB, the GNU debugger.
012be3ce 2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996
94d4b713 3 Free Software Foundation, Inc.
bd5635a1
RP
4
5This file is part of GDB.
6
5259796b 7This program is free software; you can redistribute it and/or modify
bd5635a1 8it under the terms of the GNU General Public License as published by
5259796b
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
bd5635a1 11
5259796b 12This program is distributed in the hope that it will be useful,
bd5635a1
RP
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
5259796b 18along with this program; if not, write to the Free Software
673e1bab 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
bd5635a1 20
8b0f5a9d
DE
21/* ??? Support for calling functions from gdb in sparc64 is unfinished. */
22
bd5635a1 23#include "defs.h"
bd5635a1
RP
24#include "frame.h"
25#include "inferior.h"
26#include "obstack.h"
bd5635a1 27#include "target.h"
48792545 28#include "value.h"
91550191 29#include "bfd.h"
4365c36c 30
8f86a4e4
JG
31#ifdef USE_PROC_FS
32#include <sys/procfs.h>
8f86a4e4 33#endif
bd5635a1 34
bd5635a1
RP
35#include "gdbcore.h"
36
8b0f5a9d 37#ifdef GDB_TARGET_IS_SPARC64
012be3ce 38#define FP_REGISTER_BYTES (64 * 4)
8b0f5a9d 39#else
012be3ce
DP
40#define FP_REGISTER_BYTES (32 * 4)
41#endif
42
43/* If not defined, assume 32 bit sparc. */
44#ifndef FP_MAX_REGNUM
45#define FP_MAX_REGNUM (FP0_REGNUM + 32)
8b0f5a9d
DE
46#endif
47
48#define SPARC_INTREG_SIZE (REGISTER_RAW_SIZE (G0_REGNUM))
49
bd5635a1
RP
50/* From infrun.c */
51extern int stop_after_trap;
52
b38f304c
JG
53/* We don't store all registers immediately when requested, since they
54 get sent over in large chunks anyway. Instead, we accumulate most
55 of the changes and send them over once. "deferred_stores" keeps
56 track of which sets of registers we have locally-changed copies of,
57 so we only need send the groups that have changed. */
58
59int deferred_stores = 0; /* Cumulates stores we want to do eventually. */
60
8b0f5a9d
DE
61/* Branches with prediction are treated like their non-predicting cousins. */
62/* FIXME: What about floating point branches? */
63
ee7b9e92
JK
64/* Macros to extract fields from sparc instructions. */
65#define X_OP(i) (((i) >> 30) & 0x3)
66#define X_RD(i) (((i) >> 25) & 0x1f)
67#define X_A(i) (((i) >> 29) & 1)
68#define X_COND(i) (((i) >> 25) & 0xf)
69#define X_OP2(i) (((i) >> 22) & 0x7)
70#define X_IMM22(i) ((i) & 0x3fffff)
71#define X_OP3(i) (((i) >> 19) & 0x3f)
72#define X_RS1(i) (((i) >> 14) & 0x1f)
73#define X_I(i) (((i) >> 13) & 1)
74#define X_IMM13(i) ((i) & 0x1fff)
75/* Sign extension macros. */
76#define X_SIMM13(i) ((X_IMM13 (i) ^ 0x1000) - 0x1000)
77#define X_DISP22(i) ((X_IMM22 (i) ^ 0x200000) - 0x200000)
8b0f5a9d
DE
78#ifdef GDB_TARGET_IS_SPARC64
79#define X_CC(i) (((i) >> 20) & 3)
80#define X_P(i) (((i) >> 19) & 1)
81#define X_DISP19(i) ((((i) & 0x7ffff) ^ 0x40000) - 0x40000)
82#define X_RCOND(i) (((i) >> 25) & 7)
83#define X_DISP16(i) ((((((i) >> 6) && 0xc000) | ((i) & 0x3fff)) ^ 0x8000) - 0x8000)
84#define X_FCN(i) (((i) >> 25) & 31)
85#endif
ee7b9e92 86
bd5635a1
RP
87typedef enum
88{
8b0f5a9d
DE
89 Error, not_branch, bicc, bicca, ba, baa, ticc, ta,
90#ifdef GDB_TARGET_IS_SPARC64
91 done_retry
92#endif
bd5635a1
RP
93} branch_type;
94
95/* Simulate single-step ptrace call for sun4. Code written by Gary
96 Beihl (beihl@mcc.com). */
97
98/* npc4 and next_pc describe the situation at the time that the
99 step-breakpoint was set, not necessary the current value of NPC_REGNUM. */
100static CORE_ADDR next_pc, npc4, target;
101static int brknpc4, brktrg;
102typedef char binsn_quantum[BREAKPOINT_MAX];
103static binsn_quantum break_mem[3];
104
105/* Non-zero if we just simulated a single-step ptrace call. This is
106 needed because we cannot remove the breakpoints in the inferior
107 process until after the `wait' in `wait_for_inferior'. Used for
108 sun4. */
109
110int one_stepped;
111
d11c44f1
JG
112/* single_step() is called just before we want to resume the inferior,
113 if we want to single-step it but there is no hardware or kernel single-step
114 support (as on all SPARCs). We find all the possible targets of the
115 coming instruction and breakpoint them.
116
117 single_step is also called just after the inferior stops. If we had
118 set up a simulated single-step, we undo our damage. */
119
bd5635a1 120void
8f86a4e4
JG
121single_step (ignore)
122 int ignore; /* pid, but we don't need it */
bd5635a1 123{
8b0f5a9d 124 branch_type br, isbranch();
bd5635a1
RP
125 CORE_ADDR pc;
126 long pc_instruction;
127
128 if (!one_stepped)
129 {
130 /* Always set breakpoint for NPC. */
131 next_pc = read_register (NPC_REGNUM);
132 npc4 = next_pc + 4; /* branch not taken */
133
134 target_insert_breakpoint (next_pc, break_mem[0]);
f1de67d3 135 /* printf_unfiltered ("set break at %x\n",next_pc); */
bd5635a1
RP
136
137 pc = read_register (PC_REGNUM);
ee7b9e92 138 pc_instruction = read_memory_integer (pc, 4);
8b0f5a9d 139 br = isbranch (pc_instruction, pc, &target);
bd5635a1
RP
140 brknpc4 = brktrg = 0;
141
142 if (br == bicca)
143 {
144 /* Conditional annulled branch will either end up at
145 npc (if taken) or at npc+4 (if not taken).
146 Trap npc+4. */
147 brknpc4 = 1;
148 target_insert_breakpoint (npc4, break_mem[1]);
149 }
150 else if (br == baa && target != next_pc)
151 {
152 /* Unconditional annulled branch will always end up at
153 the target. */
154 brktrg = 1;
155 target_insert_breakpoint (target, break_mem[2]);
156 }
8b0f5a9d
DE
157#ifdef GDB_TARGET_IS_SPARC64
158 else if (br == done_retry)
159 {
160 brktrg = 1;
161 target_insert_breakpoint (target, break_mem[2]);
162 }
163#endif
bd5635a1 164
d11c44f1 165 /* We are ready to let it go */
bd5635a1
RP
166 one_stepped = 1;
167 return;
168 }
169 else
170 {
171 /* Remove breakpoints */
172 target_remove_breakpoint (next_pc, break_mem[0]);
173
174 if (brknpc4)
175 target_remove_breakpoint (npc4, break_mem[1]);
176
177 if (brktrg)
178 target_remove_breakpoint (target, break_mem[2]);
179
180 one_stepped = 0;
181 }
182}
183\f
84bdfea6
PS
184/* Call this for each newly created frame. For SPARC, we need to calculate
185 the bottom of the frame, and do some extra work if the prologue
186 has been generated via the -mflat option to GCC. In particular,
187 we need to know where the previous fp and the pc have been stashed,
188 since their exact position within the frame may vary. */
189
190void
191sparc_init_extra_frame_info (fromleaf, fi)
192 int fromleaf;
193 struct frame_info *fi;
194{
195 char *name;
196 CORE_ADDR addr;
ee7b9e92 197 int insn;
84bdfea6
PS
198
199 fi->bottom =
200 (fi->next ?
201 (fi->frame == fi->next->frame ? fi->next->bottom : fi->next->frame) :
202 read_register (SP_REGNUM));
203
ee7b9e92
JK
204 /* If fi->next is NULL, then we already set ->frame by passing read_fp()
205 to create_new_frame. */
206 if (fi->next)
207 {
208 char buf[MAX_REGISTER_RAW_SIZE];
209 int err;
210
211 /* Compute ->frame as if not flat. If it is flat, we'll change
212 it later. */
213 /* FIXME: If error reading memory, should just stop backtracing, rather
214 than error(). */
215 get_saved_register (buf, 0, 0, fi, FP_REGNUM, 0);
216 fi->frame = extract_address (buf, REGISTER_RAW_SIZE (FP_REGNUM));
217 }
218
84bdfea6
PS
219 /* Decide whether this is a function with a ``flat register window''
220 frame. For such functions, the frame pointer is actually in %i7. */
221 fi->flat = 0;
222 if (find_pc_partial_function (fi->pc, &name, &addr, NULL))
223 {
224 /* See if the function starts with an add (which will be of a
ee7b9e92
JK
225 negative number if a flat frame) to the sp. FIXME: Does not
226 handle large frames which will need more than one instruction
227 to adjust the sp. */
228 insn = read_memory_integer (addr, 4);
229 if (X_OP (insn) == 2 && X_RD (insn) == 14 && X_OP3 (insn) == 0
230 && X_I (insn) && X_SIMM13 (insn) < 0)
84bdfea6 231 {
ee7b9e92
JK
232 int offset = X_SIMM13 (insn);
233
84bdfea6 234 /* Then look for a save of %i7 into the frame. */
ee7b9e92
JK
235 insn = read_memory_integer (addr + 4, 4);
236 if (X_OP (insn) == 3
237 && X_RD (insn) == 31
238 && X_OP3 (insn) == 4
239 && X_RS1 (insn) == 14)
84bdfea6 240 {
ee7b9e92
JK
241 char buf[MAX_REGISTER_RAW_SIZE];
242
84bdfea6
PS
243 /* We definitely have a flat frame now. */
244 fi->flat = 1;
ee7b9e92
JK
245
246 fi->sp_offset = offset;
247
84bdfea6 248 /* Overwrite the frame's address with the value in %i7. */
ee7b9e92
JK
249 get_saved_register (buf, 0, 0, fi, I7_REGNUM, 0);
250 fi->frame = extract_address (buf, REGISTER_RAW_SIZE (I7_REGNUM));
251
84bdfea6 252 /* Record where the fp got saved. */
ee7b9e92
JK
253 fi->fp_addr = fi->frame + fi->sp_offset + X_SIMM13 (insn);
254
84bdfea6
PS
255 /* Also try to collect where the pc got saved to. */
256 fi->pc_addr = 0;
ee7b9e92
JK
257 insn = read_memory_integer (addr + 12, 4);
258 if (X_OP (insn) == 3
259 && X_RD (insn) == 15
260 && X_OP3 (insn) == 4
261 && X_RS1 (insn) == 14)
262 fi->pc_addr = fi->frame + fi->sp_offset + X_SIMM13 (insn);
84bdfea6
PS
263 }
264 }
265 }
ee7b9e92
JK
266 if (fi->next && fi->frame == 0)
267 {
268 /* Kludge to cause init_prev_frame_info to destroy the new frame. */
269 fi->frame = fi->next->frame;
270 fi->pc = fi->next->pc;
271 }
84bdfea6
PS
272}
273
d11c44f1 274CORE_ADDR
84bdfea6
PS
275sparc_frame_chain (frame)
276 struct frame_info *frame;
d11c44f1 277{
ee7b9e92
JK
278 /* Value that will cause FRAME_CHAIN_VALID to not worry about the chain
279 value. If it realy is zero, we detect it later in
280 sparc_init_prev_frame. */
281 return (CORE_ADDR)1;
d11c44f1
JG
282}
283
284CORE_ADDR
285sparc_extract_struct_value_address (regbuf)
286 char regbuf[REGISTER_BYTES];
287{
b9e58503
PS
288#ifdef GDB_TARGET_IS_SPARC64
289 return extract_address (regbuf + REGISTER_BYTE (O0_REGNUM),
290 REGISTER_RAW_SIZE (O0_REGNUM));
291#else
8b0f5a9d 292 return read_memory_integer (((int *)(regbuf)) [SP_REGNUM] + (16 * SPARC_INTREG_SIZE),
f1de67d3 293 TARGET_PTR_BIT / TARGET_CHAR_BIT);
b9e58503 294#endif
d11c44f1
JG
295}
296
f9e3b3cc
JG
297/* Find the pc saved in frame FRAME. */
298
bd5635a1 299CORE_ADDR
8bf94f44 300sparc_frame_saved_pc (frame)
84bdfea6 301 struct frame_info *frame;
bd5635a1 302{
f1de67d3 303 char buf[MAX_REGISTER_RAW_SIZE];
34df79fc 304 CORE_ADDR addr;
f9e3b3cc 305
48792545
JK
306 if (frame->signal_handler_caller)
307 {
308 /* This is the signal trampoline frame.
309 Get the saved PC from the sigcontext structure. */
310
311#ifndef SIGCONTEXT_PC_OFFSET
312#define SIGCONTEXT_PC_OFFSET 12
313#endif
314
315 CORE_ADDR sigcontext_addr;
316 char scbuf[TARGET_PTR_BIT / HOST_CHAR_BIT];
94d4b713
JK
317 int saved_pc_offset = SIGCONTEXT_PC_OFFSET;
318 char *name = NULL;
319
320 /* Solaris2 ucbsigvechandler passes a pointer to a sigcontext
321 as the third parameter. The offset to the saved pc is 12. */
322 find_pc_partial_function (frame->pc, &name,
323 (CORE_ADDR *)NULL,(CORE_ADDR *)NULL);
324 if (name && STREQ (name, "ucbsigvechandler"))
325 saved_pc_offset = 12;
48792545
JK
326
327 /* The sigcontext address is contained in register O2. */
328 get_saved_register (buf, (int *)NULL, (CORE_ADDR *)NULL,
329 frame, O0_REGNUM + 2, (enum lval_type *)NULL);
8b0f5a9d 330 sigcontext_addr = extract_address (buf, REGISTER_RAW_SIZE (O0_REGNUM + 2));
48792545
JK
331
332 /* Don't cause a memory_error when accessing sigcontext in case the
333 stack layout has changed or the stack is corrupt. */
94d4b713 334 target_read_memory (sigcontext_addr + saved_pc_offset,
48792545
JK
335 scbuf, sizeof (scbuf));
336 return extract_address (scbuf, sizeof (scbuf));
337 }
84bdfea6
PS
338 if (frame->flat)
339 addr = frame->pc_addr;
340 else
341 addr = frame->bottom + FRAME_SAVED_I0 +
8b0f5a9d 342 SPARC_INTREG_SIZE * (I7_REGNUM - I0_REGNUM);
ee7b9e92
JK
343
344 if (addr == 0)
345 /* A flat frame leaf function might not save the PC anywhere,
346 just leave it in %o7. */
347 return PC_ADJUST (read_register (O7_REGNUM));
348
8b0f5a9d
DE
349 read_memory (addr, buf, SPARC_INTREG_SIZE);
350 return PC_ADJUST (extract_address (buf, SPARC_INTREG_SIZE));
bd5635a1
RP
351}
352
84bdfea6
PS
353/* Since an individual frame in the frame cache is defined by two
354 arguments (a frame pointer and a stack pointer), we need two
355 arguments to get info for an arbitrary stack frame. This routine
356 takes two arguments and makes the cached frames look as if these
357 two arguments defined a frame on the cache. This allows the rest
358 of info frame to extract the important arguments without
359 difficulty. */
360
361struct frame_info *
2093fe68
RP
362setup_arbitrary_frame (argc, argv)
363 int argc;
84bdfea6 364 CORE_ADDR *argv;
bd5635a1 365{
84bdfea6 366 struct frame_info *frame;
2093fe68
RP
367
368 if (argc != 2)
369 error ("Sparc frame specifications require two arguments: fp and sp");
370
84bdfea6 371 frame = create_new_frame (argv[0], 0);
bd5635a1 372
84bdfea6
PS
373 if (!frame)
374 fatal ("internal: create_new_frame returned invalid frame");
bd5635a1 375
84bdfea6
PS
376 frame->bottom = argv[1];
377 frame->pc = FRAME_SAVED_PC (frame);
378 return frame;
bd5635a1
RP
379}
380
5259796b
JG
381/* Given a pc value, skip it forward past the function prologue by
382 disassembling instructions that appear to be a prologue.
383
384 If FRAMELESS_P is set, we are only testing to see if the function
385 is frameless. This allows a quicker answer.
386
387 This routine should be more specific in its actions; making sure
192cbba9 388 that it uses the same register in the initial prologue section. */
84bdfea6 389
ee7b9e92
JK
390static CORE_ADDR examine_prologue PARAMS ((CORE_ADDR, int, struct frame_info *,
391 struct frame_saved_regs *));
392
393static CORE_ADDR
394examine_prologue (start_pc, frameless_p, fi, saved_regs)
192cbba9 395 CORE_ADDR start_pc;
5259796b 396 int frameless_p;
ee7b9e92
JK
397 struct frame_info *fi;
398 struct frame_saved_regs *saved_regs;
bd5635a1 399{
ee7b9e92 400 int insn;
bd5635a1 401 int dest = -1;
192cbba9 402 CORE_ADDR pc = start_pc;
84bdfea6 403 int is_flat = 0;
bd5635a1 404
ee7b9e92 405 insn = read_memory_integer (pc, 4);
bd5635a1
RP
406
407 /* Recognize the `sethi' insn and record its destination. */
ee7b9e92 408 if (X_OP (insn) == 0 && X_OP2 (insn) == 4)
bd5635a1 409 {
ee7b9e92 410 dest = X_RD (insn);
bd5635a1 411 pc += 4;
ee7b9e92 412 insn = read_memory_integer (pc, 4);
bd5635a1
RP
413 }
414
415 /* Recognize an add immediate value to register to either %g1 or
416 the destination register recorded above. Actually, this might
192cbba9
JK
417 well recognize several different arithmetic operations.
418 It doesn't check that rs1 == rd because in theory "sub %g0, 5, %g1"
419 followed by "save %sp, %g1, %sp" is a valid prologue (Not that
420 I imagine any compiler really does that, however). */
ee7b9e92
JK
421 if (X_OP (insn) == 2
422 && X_I (insn)
423 && (X_RD (insn) == 1 || X_RD (insn) == dest))
bd5635a1
RP
424 {
425 pc += 4;
ee7b9e92 426 insn = read_memory_integer (pc, 4);
bd5635a1
RP
427 }
428
ee7b9e92
JK
429 /* Recognize any SAVE insn. */
430 if (X_OP (insn) == 2 && X_OP3 (insn) == 60)
bd5635a1
RP
431 {
432 pc += 4;
5259796b
JG
433 if (frameless_p) /* If the save is all we care about, */
434 return pc; /* return before doing more work */
ee7b9e92 435 insn = read_memory_integer (pc, 4);
bd5635a1 436 }
ee7b9e92
JK
437 /* Recognize add to %sp. */
438 else if (X_OP (insn) == 2 && X_RD (insn) == 14 && X_OP3 (insn) == 0)
5259796b 439 {
84bdfea6
PS
440 pc += 4;
441 if (frameless_p) /* If the add is all we care about, */
442 return pc; /* return before doing more work */
ee7b9e92
JK
443 is_flat = 1;
444 insn = read_memory_integer (pc, 4);
445 /* Recognize store of frame pointer (i7). */
446 if (X_OP (insn) == 3
447 && X_RD (insn) == 31
448 && X_OP3 (insn) == 4
449 && X_RS1 (insn) == 14)
450 {
451 pc += 4;
452 insn = read_memory_integer (pc, 4);
453
454 /* Recognize sub %sp, <anything>, %i7. */
455 if (X_OP (insn) == 2
456 && X_OP3 (insn) == 4
457 && X_RS1 (insn) == 14
458 && X_RD (insn) == 31)
459 {
460 pc += 4;
461 insn = read_memory_integer (pc, 4);
462 }
463 else
464 return pc;
465 }
466 else
467 return pc;
5259796b 468 }
84bdfea6
PS
469 else
470 /* Without a save or add instruction, it's not a prologue. */
471 return start_pc;
bd5635a1 472
ee7b9e92 473 while (1)
bd5635a1 474 {
ee7b9e92
JK
475 /* Recognize stores into the frame from the input registers.
476 This recognizes all non alternate stores of input register,
477 into a location offset from the frame pointer. */
478 if ((X_OP (insn) == 3
479 && (X_OP3 (insn) & 0x3c) == 4 /* Store, non-alternate. */
480 && (X_RD (insn) & 0x18) == 0x18 /* Input register. */
481 && X_I (insn) /* Immediate mode. */
482 && X_RS1 (insn) == 30 /* Off of frame pointer. */
483 /* Into reserved stack space. */
484 && X_SIMM13 (insn) >= 0x44
485 && X_SIMM13 (insn) < 0x5b))
486 ;
487 else if (is_flat
488 && X_OP (insn) == 3
489 && X_OP3 (insn) == 4
490 && X_RS1 (insn) == 14
491 )
492 {
493 if (saved_regs && X_I (insn))
494 saved_regs->regs[X_RD (insn)] =
495 fi->frame + fi->sp_offset + X_SIMM13 (insn);
496 }
497 else
498 break;
bd5635a1 499 pc += 4;
ee7b9e92 500 insn = read_memory_integer (pc, 4);
bd5635a1 501 }
84bdfea6 502
5259796b 503 return pc;
bd5635a1
RP
504}
505
ee7b9e92
JK
506CORE_ADDR
507skip_prologue (start_pc, frameless_p)
508 CORE_ADDR start_pc;
509 int frameless_p;
510{
511 return examine_prologue (start_pc, frameless_p, NULL, NULL);
512}
513
8b0f5a9d
DE
514/* Check instruction at ADDR to see if it is a branch.
515 All non-annulled instructions will go to NPC or will trap.
516 Set *TARGET if we find a candidate branch; set to zero if not.
517
518 This isn't static as it's used by remote-sa.sparc.c. */
519
bd5635a1 520branch_type
8b0f5a9d 521isbranch (instruction, addr, target)
bd5635a1
RP
522 long instruction;
523 CORE_ADDR addr, *target;
524{
525 branch_type val = not_branch;
526 long int offset; /* Must be signed for sign-extend. */
bd5635a1
RP
527
528 *target = 0;
bd5635a1 529
ee7b9e92
JK
530 if (X_OP (instruction) == 0
531 && (X_OP2 (instruction) == 2
532 || X_OP2 (instruction) == 6
8b0f5a9d
DE
533#ifdef GDB_TARGET_IS_SPARC64
534 || X_OP2 (instruction) == 1
535 || X_OP2 (instruction) == 3
536 || X_OP2 (instruction) == 5
537#else
538 || X_OP2 (instruction) == 7
539#endif
540 ))
bd5635a1 541 {
ee7b9e92
JK
542 if (X_COND (instruction) == 8)
543 val = X_A (instruction) ? baa : ba;
bd5635a1 544 else
ee7b9e92 545 val = X_A (instruction) ? bicca : bicc;
673e1bab 546 switch (X_OP2 (instruction))
8b0f5a9d
DE
547 {
548 case 2:
549 case 6:
550#ifndef GDB_TARGET_IS_SPARC64
551 case 7:
552#endif
553 offset = 4 * X_DISP22 (instruction);
554 break;
555#ifdef GDB_TARGET_IS_SPARC64
556 case 1:
557 case 5:
558 offset = 4 * X_DISP19 (instruction);
559 break;
560 case 3:
561 offset = 4 * X_DISP16 (instruction);
562 break;
563#endif
564 }
bd5635a1
RP
565 *target = addr + offset;
566 }
8b0f5a9d
DE
567#ifdef GDB_TARGET_IS_SPARC64
568 else if (X_OP (instruction) == 2
569 && X_OP3 (instruction) == 62)
570 {
571 if (X_FCN (instruction) == 0)
572 {
573 /* done */
574 *target = read_register (TNPC_REGNUM);
575 val = done_retry;
576 }
577 else if (X_FCN (instruction) == 1)
578 {
579 /* retry */
580 *target = read_register (TPC_REGNUM);
581 val = done_retry;
582 }
583 }
584#endif
bd5635a1
RP
585
586 return val;
587}
ee7b9e92
JK
588\f
589/* Find register number REGNUM relative to FRAME and put its
590 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
591 was optimized out (and thus can't be fetched). If the variable
592 was fetched from memory, set *ADDRP to where it was fetched from,
593 otherwise it was fetched from a register.
594
595 The argument RAW_BUFFER must point to aligned memory. */
596
597void
598get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
599 char *raw_buffer;
600 int *optimized;
601 CORE_ADDR *addrp;
602 struct frame_info *frame;
603 int regnum;
604 enum lval_type *lval;
605{
606 struct frame_info *frame1;
607 CORE_ADDR addr;
608
888a18ee
JK
609 if (!target_has_registers)
610 error ("No registers.");
611
ee7b9e92
JK
612 if (optimized)
613 *optimized = 0;
614
615 addr = 0;
012be3ce
DP
616
617 /* FIXME This code extracted from infcmd.c; should put elsewhere! */
618 if (frame == NULL)
619 {
620 /* error ("No selected frame."); */
621 if (!target_has_registers)
622 error ("The program has no registers now.");
623 if (selected_frame == NULL)
624 error ("No selected frame.");
625 /* Try to use selected frame */
626 frame = get_prev_frame (selected_frame);
627 if (frame == 0)
628 error ("Cmd not meaningful in the outermost frame.");
629 }
630
631
ee7b9e92
JK
632 frame1 = frame->next;
633 while (frame1 != NULL)
634 {
635 if (frame1->pc >= (frame1->bottom ? frame1->bottom :
636 read_register (SP_REGNUM))
637 && frame1->pc <= FRAME_FP (frame1))
638 {
012be3ce
DP
639 /* Dummy frame. All but the window regs are in there somewhere.
640 The window registers are saved on the stack, just like in a
641 normal frame. */
ee7b9e92 642 if (regnum >= G1_REGNUM && regnum < G1_REGNUM + 7)
8b0f5a9d 643 addr = frame1->frame + (regnum - G0_REGNUM) * SPARC_INTREG_SIZE
012be3ce 644 - (FP_REGISTER_BYTES + 8 * SPARC_INTREG_SIZE);
ee7b9e92 645 else if (regnum >= I0_REGNUM && regnum < I0_REGNUM + 8)
012be3ce
DP
646 addr = (frame1->prev->bottom
647 + (regnum - I0_REGNUM) * SPARC_INTREG_SIZE
648 + FRAME_SAVED_I0);
649 else if (regnum >= L0_REGNUM && regnum < L0_REGNUM + 8)
650 addr = (frame1->prev->bottom
651 + (regnum - L0_REGNUM) * SPARC_INTREG_SIZE
652 + FRAME_SAVED_L0);
653 else if (regnum >= O0_REGNUM && regnum < O0_REGNUM + 8)
654 addr = frame1->frame + (regnum - O0_REGNUM) * SPARC_INTREG_SIZE
655 - (FP_REGISTER_BYTES + 16 * SPARC_INTREG_SIZE);
656#ifdef FP0_REGNUM
657 else if (regnum >= FP0_REGNUM && regnum < FP0_REGNUM + 32)
8b0f5a9d 658 addr = frame1->frame + (regnum - FP0_REGNUM) * 4
012be3ce
DP
659 - (FP_REGISTER_BYTES);
660#ifdef GDB_TARGET_IS_SPARC64
661 else if (regnum >= FP0_REGNUM + 32 && regnum < FP_MAX_REGNUM)
662 addr = frame1->frame + 32 * 4 + (regnum - FP0_REGNUM - 32) * 8
663 - (FP_REGISTER_BYTES);
664#endif
665#endif /* FP0_REGNUM */
ee7b9e92 666 else if (regnum >= Y_REGNUM && regnum < NUM_REGS)
8b0f5a9d 667 addr = frame1->frame + (regnum - Y_REGNUM) * SPARC_INTREG_SIZE
012be3ce 668 - (FP_REGISTER_BYTES + 24 * SPARC_INTREG_SIZE);
ee7b9e92
JK
669 }
670 else if (frame1->flat)
671 {
672
673 if (regnum == RP_REGNUM)
674 addr = frame1->pc_addr;
675 else if (regnum == I7_REGNUM)
676 addr = frame1->fp_addr;
677 else
678 {
679 CORE_ADDR func_start;
680 struct frame_saved_regs regs;
681 memset (&regs, 0, sizeof (regs));
682
683 find_pc_partial_function (frame1->pc, NULL, &func_start, NULL);
684 examine_prologue (func_start, 0, frame1, &regs);
685 addr = regs.regs[regnum];
686 }
687 }
688 else
689 {
690 /* Normal frame. Local and In registers are saved on stack. */
691 if (regnum >= I0_REGNUM && regnum < I0_REGNUM + 8)
692 addr = (frame1->prev->bottom
8b0f5a9d 693 + (regnum - I0_REGNUM) * SPARC_INTREG_SIZE
ee7b9e92
JK
694 + FRAME_SAVED_I0);
695 else if (regnum >= L0_REGNUM && regnum < L0_REGNUM + 8)
696 addr = (frame1->prev->bottom
8b0f5a9d 697 + (regnum - L0_REGNUM) * SPARC_INTREG_SIZE
ee7b9e92
JK
698 + FRAME_SAVED_L0);
699 else if (regnum >= O0_REGNUM && regnum < O0_REGNUM + 8)
700 {
701 /* Outs become ins. */
702 get_saved_register (raw_buffer, optimized, addrp, frame1,
703 (regnum - O0_REGNUM + I0_REGNUM), lval);
704 return;
705 }
706 }
707 if (addr != 0)
708 break;
709 frame1 = frame1->next;
710 }
711 if (addr != 0)
712 {
713 if (lval != NULL)
714 *lval = lval_memory;
715 if (regnum == SP_REGNUM)
716 {
717 if (raw_buffer != NULL)
718 {
719 /* Put it back in target format. */
720 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), addr);
721 }
722 if (addrp != NULL)
723 *addrp = 0;
724 return;
725 }
726 if (raw_buffer != NULL)
727 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
728 }
729 else
730 {
731 if (lval != NULL)
732 *lval = lval_register;
733 addr = REGISTER_BYTE (regnum);
734 if (raw_buffer != NULL)
735 read_register_gen (regnum, raw_buffer);
736 }
737 if (addrp != NULL)
738 *addrp = addr;
739}
740
741/* Push an empty stack frame, and record in it the current PC, regs, etc.
742
743 We save the non-windowed registers and the ins. The locals and outs
744 are new; they don't need to be saved. The i's and l's of
745 the last frame were already saved on the stack. */
746
747/* Definitely see tm-sparc.h for more doc of the frame format here. */
748
8b0f5a9d
DE
749#ifdef GDB_TARGET_IS_SPARC64
750#define DUMMY_REG_SAVE_OFFSET (128 + 16)
751#else
752#define DUMMY_REG_SAVE_OFFSET 0x60
753#endif
754
755/* See tm-sparc.h for how this is calculated. */
012be3ce 756#ifdef FP0_REGNUM
8b0f5a9d
DE
757#define DUMMY_STACK_REG_BUF_SIZE \
758(((8+8+8) * SPARC_INTREG_SIZE) + (32 * REGISTER_RAW_SIZE (FP0_REGNUM)))
012be3ce
DP
759#else
760#define DUMMY_STACK_REG_BUF_SIZE \
761(((8+8+8) * SPARC_INTREG_SIZE) )
762#endif /* FP0_REGNUM */
8b0f5a9d
DE
763#define DUMMY_STACK_SIZE (DUMMY_STACK_REG_BUF_SIZE + DUMMY_REG_SAVE_OFFSET)
764
ee7b9e92
JK
765void
766sparc_push_dummy_frame ()
767{
768 CORE_ADDR sp, old_sp;
8b0f5a9d 769 char register_temp[DUMMY_STACK_SIZE];
ee7b9e92
JK
770
771 old_sp = sp = read_register (SP_REGNUM);
772
8b0f5a9d
DE
773#ifdef GDB_TARGET_IS_SPARC64
774 /* FIXME: not sure what needs to be saved here. */
775#else
ee7b9e92
JK
776 /* Y, PS, WIM, TBR, PC, NPC, FPS, CPS regs */
777 read_register_bytes (REGISTER_BYTE (Y_REGNUM), &register_temp[0],
778 REGISTER_RAW_SIZE (Y_REGNUM) * 8);
8b0f5a9d 779#endif
ee7b9e92 780
8b0f5a9d
DE
781 read_register_bytes (REGISTER_BYTE (O0_REGNUM),
782 &register_temp[8 * SPARC_INTREG_SIZE],
783 SPARC_INTREG_SIZE * 8);
ee7b9e92 784
8b0f5a9d
DE
785 read_register_bytes (REGISTER_BYTE (G0_REGNUM),
786 &register_temp[16 * SPARC_INTREG_SIZE],
787 SPARC_INTREG_SIZE * 8);
ee7b9e92 788
012be3ce 789#ifdef FP0_REGNUM
8b0f5a9d
DE
790 read_register_bytes (REGISTER_BYTE (FP0_REGNUM),
791 &register_temp[24 * SPARC_INTREG_SIZE],
012be3ce
DP
792 FP_REGISTER_BYTES);
793#endif /* FP0_REGNUM */
ee7b9e92 794
8b0f5a9d 795 sp -= DUMMY_STACK_SIZE;
ee7b9e92
JK
796
797 write_register (SP_REGNUM, sp);
798
8b0f5a9d
DE
799 write_memory (sp + DUMMY_REG_SAVE_OFFSET, &register_temp[0],
800 DUMMY_STACK_REG_BUF_SIZE);
ee7b9e92
JK
801
802 write_register (FP_REGNUM, old_sp);
803
804 /* Set return address register for the call dummy to the current PC. */
805 write_register (I7_REGNUM, read_pc() - 8);
806}
bd5635a1 807
ee7b9e92
JK
808/* sparc_frame_find_saved_regs (). This function is here only because
809 pop_frame uses it. Note there is an interesting corner case which
810 I think few ports of GDB get right--if you are popping a frame
811 which does not save some register that *is* saved by a more inner
812 frame (such a frame will never be a dummy frame because dummy
813 frames save all registers). Rewriting pop_frame to use
814 get_saved_register would solve this problem and also get rid of the
815 ugly duplication between sparc_frame_find_saved_regs and
816 get_saved_register.
bd5635a1
RP
817
818 Stores, into a struct frame_saved_regs,
819 the addresses of the saved registers of frame described by FRAME_INFO.
820 This includes special registers such as pc and fp saved in special
821 ways in the stack frame. sp is even more special:
822 the address we return for it IS the sp for the next frame.
823
824 Note that on register window machines, we are currently making the
825 assumption that window registers are being saved somewhere in the
826 frame in which they are being used. If they are stored in an
827 inferior frame, find_saved_register will break.
828
829 On the Sun 4, the only time all registers are saved is when
830 a dummy frame is involved. Otherwise, the only saved registers
831 are the LOCAL and IN registers which are saved as a result
832 of the "save/restore" opcodes. This condition is determined
833 by address rather than by value.
834
835 The "pc" is not stored in a frame on the SPARC. (What is stored
836 is a return address minus 8.) sparc_pop_frame knows how to
837 deal with that. Other routines might or might not.
838
839 See tm-sparc.h (PUSH_FRAME and friends) for CRITICAL information
840 about how this works. */
841
ee7b9e92
JK
842static void sparc_frame_find_saved_regs PARAMS ((struct frame_info *,
843 struct frame_saved_regs *));
844
845static void
bd5635a1
RP
846sparc_frame_find_saved_regs (fi, saved_regs_addr)
847 struct frame_info *fi;
848 struct frame_saved_regs *saved_regs_addr;
849{
850 register int regnum;
84bdfea6 851 CORE_ADDR frame_addr = FRAME_FP (fi);
bd5635a1 852
84bdfea6 853 if (!fi)
bd5635a1
RP
854 fatal ("Bad frame info struct in FRAME_FIND_SAVED_REGS");
855
b38f304c 856 memset (saved_regs_addr, 0, sizeof (*saved_regs_addr));
bd5635a1 857
bd5635a1
RP
858 if (fi->pc >= (fi->bottom ? fi->bottom :
859 read_register (SP_REGNUM))
860 && fi->pc <= FRAME_FP(fi))
861 {
862 /* Dummy frame. All but the window regs are in there somewhere. */
863 for (regnum = G1_REGNUM; regnum < G1_REGNUM+7; regnum++)
864 saved_regs_addr->regs[regnum] =
8b0f5a9d 865 frame_addr + (regnum - G0_REGNUM) * SPARC_INTREG_SIZE
012be3ce 866 - (FP_REGISTER_BYTES + 8 * SPARC_INTREG_SIZE);
bd5635a1
RP
867 for (regnum = I0_REGNUM; regnum < I0_REGNUM+8; regnum++)
868 saved_regs_addr->regs[regnum] =
8b0f5a9d 869 frame_addr + (regnum - I0_REGNUM) * SPARC_INTREG_SIZE
012be3ce
DP
870 - (FP_REGISTER_BYTES + 16 * SPARC_INTREG_SIZE);
871#ifdef FP0_REGNUM
bd5635a1
RP
872 for (regnum = FP0_REGNUM; regnum < FP0_REGNUM + 32; regnum++)
873 saved_regs_addr->regs[regnum] =
8b0f5a9d 874 frame_addr + (regnum - FP0_REGNUM) * 4
012be3ce
DP
875 - (FP_REGISTER_BYTES);
876#ifdef GDB_TARGET_IS_SPARC64
877 for (regnum = FP0_REGNUM + 32; regnum < FP_MAX_REGNUM; regnum++)
878 saved_regs_addr->regs[regnum] =
879 frame_addr + 32 * 4 + (regnum - FP0_REGNUM - 32) * 8
880 - (FP_REGISTER_BYTES);
881#endif
882#endif /* FP0_REGNUM */
bd5635a1
RP
883 for (regnum = Y_REGNUM; regnum < NUM_REGS; regnum++)
884 saved_regs_addr->regs[regnum] =
8b0f5a9d 885 frame_addr + (regnum - Y_REGNUM) * SPARC_INTREG_SIZE - 0xe0;
012be3ce 886 - (FP_REGISTER_BYTES + 24 * SPARC_INTREG_SIZE);
84bdfea6 887 frame_addr = fi->bottom ?
bd5635a1
RP
888 fi->bottom : read_register (SP_REGNUM);
889 }
84bdfea6
PS
890 else if (fi->flat)
891 {
ee7b9e92
JK
892 CORE_ADDR func_start;
893 find_pc_partial_function (fi->pc, NULL, &func_start, NULL);
894 examine_prologue (func_start, 0, fi, saved_regs_addr);
895
84bdfea6
PS
896 /* Flat register window frame. */
897 saved_regs_addr->regs[RP_REGNUM] = fi->pc_addr;
898 saved_regs_addr->regs[I7_REGNUM] = fi->fp_addr;
84bdfea6 899 }
bd5635a1
RP
900 else
901 {
902 /* Normal frame. Just Local and In registers */
84bdfea6 903 frame_addr = fi->bottom ?
bd5635a1 904 fi->bottom : read_register (SP_REGNUM);
84bdfea6 905 for (regnum = L0_REGNUM; regnum < L0_REGNUM+8; regnum++)
16726dd1 906 saved_regs_addr->regs[regnum] =
8b0f5a9d 907 (frame_addr + (regnum - L0_REGNUM) * SPARC_INTREG_SIZE
84bdfea6
PS
908 + FRAME_SAVED_L0);
909 for (regnum = I0_REGNUM; regnum < I0_REGNUM+8; regnum++)
910 saved_regs_addr->regs[regnum] =
8b0f5a9d 911 (frame_addr + (regnum - I0_REGNUM) * SPARC_INTREG_SIZE
84bdfea6 912 + FRAME_SAVED_I0);
bd5635a1
RP
913 }
914 if (fi->next)
915 {
84bdfea6
PS
916 if (fi->flat)
917 {
918 saved_regs_addr->regs[O7_REGNUM] = fi->pc_addr;
919 }
920 else
921 {
922 /* Pull off either the next frame pointer or the stack pointer */
923 CORE_ADDR next_next_frame_addr =
924 (fi->next->bottom ?
925 fi->next->bottom :
926 read_register (SP_REGNUM));
927 for (regnum = O0_REGNUM; regnum < O0_REGNUM+8; regnum++)
928 saved_regs_addr->regs[regnum] =
929 (next_next_frame_addr
8b0f5a9d 930 + (regnum - O0_REGNUM) * SPARC_INTREG_SIZE
84bdfea6
PS
931 + FRAME_SAVED_I0);
932 }
bd5635a1
RP
933 }
934 /* Otherwise, whatever we would get from ptrace(GETREGS) is accurate */
935 saved_regs_addr->regs[SP_REGNUM] = FRAME_FP (fi);
936}
937
bd5635a1
RP
938/* Discard from the stack the innermost frame, restoring all saved registers.
939
940 Note that the values stored in fsr by get_frame_saved_regs are *in
941 the context of the called frame*. What this means is that the i
942 regs of fsr must be restored into the o regs of the (calling) frame that
943 we pop into. We don't care about the output regs of the calling frame,
944 since unless it's a dummy frame, it won't have any output regs in it.
945
946 We never have to bother with %l (local) regs, since the called routine's
947 locals get tossed, and the calling routine's locals are already saved
948 on its stack. */
949
950/* Definitely see tm-sparc.h for more doc of the frame format here. */
951
952void
953sparc_pop_frame ()
954{
84bdfea6 955 register struct frame_info *frame = get_current_frame ();
bd5635a1
RP
956 register CORE_ADDR pc;
957 struct frame_saved_regs fsr;
bd5635a1 958 char raw_buffer[REGISTER_BYTES];
84bdfea6 959 int regnum;
bd5635a1 960
ee7b9e92 961 sparc_frame_find_saved_regs (frame, &fsr);
012be3ce 962#ifdef FP0_REGNUM
bd5635a1
RP
963 if (fsr.regs[FP0_REGNUM])
964 {
012be3ce 965 read_memory (fsr.regs[FP0_REGNUM], raw_buffer, FP_REGISTER_BYTES);
8b0f5a9d 966 write_register_bytes (REGISTER_BYTE (FP0_REGNUM),
012be3ce 967 raw_buffer, FP_REGISTER_BYTES);
bd5635a1 968 }
8b0f5a9d 969#ifndef GDB_TARGET_IS_SPARC64
f1de67d3
PS
970 if (fsr.regs[FPS_REGNUM])
971 {
972 read_memory (fsr.regs[FPS_REGNUM], raw_buffer, 4);
973 write_register_bytes (REGISTER_BYTE (FPS_REGNUM), raw_buffer, 4);
974 }
975 if (fsr.regs[CPS_REGNUM])
976 {
977 read_memory (fsr.regs[CPS_REGNUM], raw_buffer, 4);
978 write_register_bytes (REGISTER_BYTE (CPS_REGNUM), raw_buffer, 4);
979 }
8b0f5a9d 980#endif
012be3ce 981#endif /* FP0_REGNUM */
bd5635a1
RP
982 if (fsr.regs[G1_REGNUM])
983 {
8b0f5a9d
DE
984 read_memory (fsr.regs[G1_REGNUM], raw_buffer, 7 * SPARC_INTREG_SIZE);
985 write_register_bytes (REGISTER_BYTE (G1_REGNUM), raw_buffer,
986 7 * SPARC_INTREG_SIZE);
bd5635a1 987 }
84bdfea6
PS
988
989 if (frame->flat)
990 {
991 /* Each register might or might not have been saved, need to test
992 individually. */
993 for (regnum = L0_REGNUM; regnum < L0_REGNUM + 8; ++regnum)
994 if (fsr.regs[regnum])
8b0f5a9d
DE
995 write_register (regnum, read_memory_integer (fsr.regs[regnum],
996 SPARC_INTREG_SIZE));
84bdfea6
PS
997 for (regnum = I0_REGNUM; regnum < I0_REGNUM + 8; ++regnum)
998 if (fsr.regs[regnum])
8b0f5a9d
DE
999 write_register (regnum, read_memory_integer (fsr.regs[regnum],
1000 SPARC_INTREG_SIZE));
ee7b9e92
JK
1001
1002 /* Handle all outs except stack pointer (o0-o5; o7). */
1003 for (regnum = O0_REGNUM; regnum < O0_REGNUM + 6; ++regnum)
84bdfea6 1004 if (fsr.regs[regnum])
8b0f5a9d
DE
1005 write_register (regnum, read_memory_integer (fsr.regs[regnum],
1006 SPARC_INTREG_SIZE));
ee7b9e92
JK
1007 if (fsr.regs[O0_REGNUM + 7])
1008 write_register (O0_REGNUM + 7,
8b0f5a9d
DE
1009 read_memory_integer (fsr.regs[O0_REGNUM + 7],
1010 SPARC_INTREG_SIZE));
ee7b9e92 1011
6cb4e9e0 1012 write_register (SP_REGNUM, frame->frame);
84bdfea6
PS
1013 }
1014 else if (fsr.regs[I0_REGNUM])
bd5635a1 1015 {
dd99f8e4
JK
1016 CORE_ADDR sp;
1017
1018 char reg_temp[REGISTER_BYTES];
1019
8b0f5a9d 1020 read_memory (fsr.regs[I0_REGNUM], raw_buffer, 8 * SPARC_INTREG_SIZE);
dd99f8e4
JK
1021
1022 /* Get the ins and locals which we are about to restore. Just
1023 moving the stack pointer is all that is really needed, except
1024 store_inferior_registers is then going to write the ins and
1025 locals from the registers array, so we need to muck with the
1026 registers array. */
1027 sp = fsr.regs[SP_REGNUM];
8b0f5a9d 1028 read_memory (sp, reg_temp, SPARC_INTREG_SIZE * 16);
dd99f8e4
JK
1029
1030 /* Restore the out registers.
1031 Among other things this writes the new stack pointer. */
1032 write_register_bytes (REGISTER_BYTE (O0_REGNUM), raw_buffer,
8b0f5a9d 1033 SPARC_INTREG_SIZE * 8);
dd99f8e4
JK
1034
1035 write_register_bytes (REGISTER_BYTE (L0_REGNUM), reg_temp,
8b0f5a9d 1036 SPARC_INTREG_SIZE * 16);
bd5635a1 1037 }
8b0f5a9d 1038#ifndef GDB_TARGET_IS_SPARC64
bd5635a1
RP
1039 if (fsr.regs[PS_REGNUM])
1040 write_register (PS_REGNUM, read_memory_integer (fsr.regs[PS_REGNUM], 4));
8b0f5a9d 1041#endif
bd5635a1 1042 if (fsr.regs[Y_REGNUM])
8b0f5a9d 1043 write_register (Y_REGNUM, read_memory_integer (fsr.regs[Y_REGNUM], REGISTER_RAW_SIZE (Y_REGNUM)));
bd5635a1
RP
1044 if (fsr.regs[PC_REGNUM])
1045 {
1046 /* Explicitly specified PC (and maybe NPC) -- just restore them. */
8b0f5a9d
DE
1047 write_register (PC_REGNUM, read_memory_integer (fsr.regs[PC_REGNUM],
1048 REGISTER_RAW_SIZE (PC_REGNUM)));
bd5635a1
RP
1049 if (fsr.regs[NPC_REGNUM])
1050 write_register (NPC_REGNUM,
8b0f5a9d
DE
1051 read_memory_integer (fsr.regs[NPC_REGNUM],
1052 REGISTER_RAW_SIZE (NPC_REGNUM)));
bd5635a1 1053 }
ee7b9e92 1054 else if (frame->flat)
84bdfea6 1055 {
ee7b9e92 1056 if (frame->pc_addr)
8b0f5a9d
DE
1057 pc = PC_ADJUST ((CORE_ADDR)
1058 read_memory_integer (frame->pc_addr,
1059 REGISTER_RAW_SIZE (PC_REGNUM)));
ee7b9e92
JK
1060 else
1061 {
1062 /* I think this happens only in the innermost frame, if so then
1063 it is a complicated way of saying
1064 "pc = read_register (O7_REGNUM);". */
1065 char buf[MAX_REGISTER_RAW_SIZE];
1066 get_saved_register (buf, 0, 0, frame, O7_REGNUM, 0);
1067 pc = PC_ADJUST (extract_address
1068 (buf, REGISTER_RAW_SIZE (O7_REGNUM)));
1069 }
1070
84bdfea6
PS
1071 write_register (PC_REGNUM, pc);
1072 write_register (NPC_REGNUM, pc + 4);
1073 }
bd5635a1
RP
1074 else if (fsr.regs[I7_REGNUM])
1075 {
1076 /* Return address in %i7 -- adjust it, then restore PC and NPC from it */
8b0f5a9d
DE
1077 pc = PC_ADJUST ((CORE_ADDR) read_memory_integer (fsr.regs[I7_REGNUM],
1078 SPARC_INTREG_SIZE));
bd5635a1
RP
1079 write_register (PC_REGNUM, pc);
1080 write_register (NPC_REGNUM, pc + 4);
1081 }
1082 flush_cached_frames ();
bd5635a1
RP
1083}
1084
5e5215eb
JG
1085/* On the Sun 4 under SunOS, the compile will leave a fake insn which
1086 encodes the structure size being returned. If we detect such
1087 a fake insn, step past it. */
1088
1089CORE_ADDR
1090sparc_pc_adjust(pc)
1091 CORE_ADDR pc;
1092{
34df79fc
JK
1093 unsigned long insn;
1094 char buf[4];
5e5215eb
JG
1095 int err;
1096
34df79fc
JK
1097 err = target_read_memory (pc + 8, buf, sizeof(long));
1098 insn = extract_unsigned_integer (buf, 4);
e4dbd248 1099 if ((err == 0) && (insn & 0xffc00000) == 0)
5e5215eb
JG
1100 return pc+12;
1101 else
1102 return pc+8;
1103}
84bdfea6
PS
1104
1105/* If pc is in a shared library trampoline, return its target.
1106 The SunOs 4.x linker rewrites the jump table entries for PIC
1107 compiled modules in the main executable to bypass the dynamic linker
1108 with jumps of the form
1109 sethi %hi(addr),%g1
1110 jmp %g1+%lo(addr)
1111 and removes the corresponding jump table relocation entry in the
1112 dynamic relocations.
1113 find_solib_trampoline_target relies on the presence of the jump
1114 table relocation entry, so we have to detect these jump instructions
1115 by hand. */
1116
1117CORE_ADDR
1118sunos4_skip_trampoline_code (pc)
1119 CORE_ADDR pc;
1120{
1121 unsigned long insn1;
1122 char buf[4];
1123 int err;
1124
1125 err = target_read_memory (pc, buf, 4);
1126 insn1 = extract_unsigned_integer (buf, 4);
1127 if (err == 0 && (insn1 & 0xffc00000) == 0x03000000)
1128 {
1129 unsigned long insn2;
1130
1131 err = target_read_memory (pc + 4, buf, 4);
1132 insn2 = extract_unsigned_integer (buf, 4);
1133 if (err == 0 && (insn2 & 0xffffe000) == 0x81c06000)
1134 {
1135 CORE_ADDR target_pc = (insn1 & 0x3fffff) << 10;
1136 int delta = insn2 & 0x1fff;
1137
1138 /* Sign extend the displacement. */
1139 if (delta & 0x1000)
1140 delta |= ~0x1fff;
1141 return target_pc + delta;
1142 }
1143 }
1144 return find_solib_trampoline_target (pc);
1145}
8f86a4e4
JG
1146\f
1147#ifdef USE_PROC_FS /* Target dependent support for /proc */
1148
1149/* The /proc interface divides the target machine's register set up into
1150 two different sets, the general register set (gregset) and the floating
1151 point register set (fpregset). For each set, there is an ioctl to get
1152 the current register set and another ioctl to set the current values.
1153
1154 The actual structure passed through the ioctl interface is, of course,
1155 naturally machine dependent, and is different for each set of registers.
1156 For the sparc for example, the general register set is typically defined
1157 by:
1158
1159 typedef int gregset_t[38];
1160
1161 #define R_G0 0
1162 ...
1163 #define R_TBR 37
1164
1165 and the floating point set by:
1166
1167 typedef struct prfpregset {
1168 union {
1169 u_long pr_regs[32];
1170 double pr_dregs[16];
1171 } pr_fr;
1172 void * pr_filler;
1173 u_long pr_fsr;
1174 u_char pr_qcnt;
1175 u_char pr_q_entrysize;
1176 u_char pr_en;
1177 u_long pr_q[64];
1178 } prfpregset_t;
1179
1180 These routines provide the packing and unpacking of gregset_t and
1181 fpregset_t formatted data.
1182
1183 */
1184
8b0f5a9d
DE
1185/* Given a pointer to a general register set in /proc format (gregset_t *),
1186 unpack the register contents and supply them as gdb's idea of the current
1187 register values. */
8f86a4e4
JG
1188
1189void
1190supply_gregset (gregsetp)
1191prgregset_t *gregsetp;
1192{
b38f304c 1193 register int regi;
8f86a4e4 1194 register prgreg_t *regp = (prgreg_t *) gregsetp;
e4dbd248 1195 static char zerobuf[MAX_REGISTER_RAW_SIZE] = {0};
8f86a4e4
JG
1196
1197 /* GDB register numbers for Gn, On, Ln, In all match /proc reg numbers. */
b38f304c 1198 for (regi = G0_REGNUM ; regi <= I7_REGNUM ; regi++)
8f86a4e4 1199 {
b38f304c 1200 supply_register (regi, (char *) (regp + regi));
8f86a4e4
JG
1201 }
1202
1203 /* These require a bit more care. */
1204 supply_register (PS_REGNUM, (char *) (regp + R_PS));
1205 supply_register (PC_REGNUM, (char *) (regp + R_PC));
1206 supply_register (NPC_REGNUM,(char *) (regp + R_nPC));
1207 supply_register (Y_REGNUM, (char *) (regp + R_Y));
e4dbd248
PS
1208
1209 /* Fill inaccessible registers with zero. */
1210 supply_register (WIM_REGNUM, zerobuf);
1211 supply_register (TBR_REGNUM, zerobuf);
1212 supply_register (CPS_REGNUM, zerobuf);
8f86a4e4
JG
1213}
1214
1215void
1216fill_gregset (gregsetp, regno)
1217prgregset_t *gregsetp;
1218int regno;
1219{
1220 int regi;
1221 register prgreg_t *regp = (prgreg_t *) gregsetp;
8f86a4e4
JG
1222
1223 for (regi = 0 ; regi <= R_I7 ; regi++)
1224 {
1225 if ((regno == -1) || (regno == regi))
1226 {
b38f304c 1227 *(regp + regi) = *(int *) &registers[REGISTER_BYTE (regi)];
8f86a4e4
JG
1228 }
1229 }
1230 if ((regno == -1) || (regno == PS_REGNUM))
1231 {
1232 *(regp + R_PS) = *(int *) &registers[REGISTER_BYTE (PS_REGNUM)];
1233 }
1234 if ((regno == -1) || (regno == PC_REGNUM))
1235 {
1236 *(regp + R_PC) = *(int *) &registers[REGISTER_BYTE (PC_REGNUM)];
1237 }
1238 if ((regno == -1) || (regno == NPC_REGNUM))
1239 {
1240 *(regp + R_nPC) = *(int *) &registers[REGISTER_BYTE (NPC_REGNUM)];
1241 }
1242 if ((regno == -1) || (regno == Y_REGNUM))
1243 {
1244 *(regp + R_Y) = *(int *) &registers[REGISTER_BYTE (Y_REGNUM)];
1245 }
1246}
1247
1248#if defined (FP0_REGNUM)
1249
1250/* Given a pointer to a floating point register set in /proc format
1251 (fpregset_t *), unpack the register contents and supply them as gdb's
1252 idea of the current floating point register values. */
1253
1254void
1255supply_fpregset (fpregsetp)
1256prfpregset_t *fpregsetp;
1257{
1258 register int regi;
1259 char *from;
1260
012be3ce 1261 for (regi = FP0_REGNUM ; regi < FP_MAX_REGNUM ; regi++)
8f86a4e4
JG
1262 {
1263 from = (char *) &fpregsetp->pr_fr.pr_regs[regi-FP0_REGNUM];
1264 supply_register (regi, from);
1265 }
1266 supply_register (FPS_REGNUM, (char *) &(fpregsetp->pr_fsr));
1267}
1268
1269/* Given a pointer to a floating point register set in /proc format
1270 (fpregset_t *), update the register specified by REGNO from gdb's idea
1271 of the current floating point register set. If REGNO is -1, update
1272 them all. */
8b0f5a9d 1273/* ??? This will probably need some changes for sparc64. */
8f86a4e4
JG
1274
1275void
1276fill_fpregset (fpregsetp, regno)
1277prfpregset_t *fpregsetp;
1278int regno;
1279{
1280 int regi;
1281 char *to;
1282 char *from;
8f86a4e4 1283
012be3ce 1284 for (regi = FP0_REGNUM ; regi < FP_MAX_REGNUM ; regi++)
8f86a4e4
JG
1285 {
1286 if ((regno == -1) || (regno == regi))
1287 {
1288 from = (char *) &registers[REGISTER_BYTE (regi)];
1289 to = (char *) &fpregsetp->pr_fr.pr_regs[regi-FP0_REGNUM];
b38f304c 1290 memcpy (to, from, REGISTER_RAW_SIZE (regi));
8f86a4e4
JG
1291 }
1292 }
1293 if ((regno == -1) || (regno == FPS_REGNUM))
1294 {
1295 fpregsetp->pr_fsr = *(int *) &registers[REGISTER_BYTE (FPS_REGNUM)];
1296 }
1297}
1298
1299#endif /* defined (FP0_REGNUM) */
1300
1301#endif /* USE_PROC_FS */
1302
1303
1304#ifdef GET_LONGJMP_TARGET
f9e3b3cc
JG
1305
1306/* Figure out where the longjmp will land. We expect that we have just entered
1307 longjmp and haven't yet setup the stack frame, so the args are still in the
1308 output regs. %o0 (O0_REGNUM) points at the jmp_buf structure from which we
1309 extract the pc (JB_PC) that we will land at. The pc is copied into ADDR.
1310 This routine returns true on success */
1311
1312int
84bdfea6 1313get_longjmp_target (pc)
f9e3b3cc
JG
1314 CORE_ADDR *pc;
1315{
1316 CORE_ADDR jb_addr;
34df79fc
JK
1317#define LONGJMP_TARGET_SIZE 4
1318 char buf[LONGJMP_TARGET_SIZE];
f9e3b3cc 1319
84bdfea6 1320 jb_addr = read_register (O0_REGNUM);
f9e3b3cc 1321
84bdfea6
PS
1322 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
1323 LONGJMP_TARGET_SIZE))
f9e3b3cc
JG
1324 return 0;
1325
34df79fc 1326 *pc = extract_address (buf, LONGJMP_TARGET_SIZE);
f9e3b3cc
JG
1327
1328 return 1;
1329}
8f86a4e4 1330#endif /* GET_LONGJMP_TARGET */
8b0f5a9d
DE
1331\f
1332#ifdef STATIC_TRANSFORM_NAME
1333/* SunPRO (3.0 at least), encodes the static variables. This is not
1334 related to C++ mangling, it is done for C too. */
1335
1336char *
b9e58503 1337sunpro_static_transform_name (name)
8b0f5a9d
DE
1338 char *name;
1339{
1340 char *p;
1341 if (name[0] == '$')
1342 {
1343 /* For file-local statics there will be a dollar sign, a bunch
1344 of junk (the contents of which match a string given in the
1345 N_OPT), a period and the name. For function-local statics
1346 there will be a bunch of junk (which seems to change the
1347 second character from 'A' to 'B'), a period, the name of the
1348 function, and the name. So just skip everything before the
1349 last period. */
1350 p = strrchr (name, '.');
1351 if (p != NULL)
1352 name = p + 1;
1353 }
1354 return name;
1355}
1356#endif /* STATIC_TRANSFORM_NAME */
1357\f
1358#ifdef GDB_TARGET_IS_SPARC64
1359
8b0f5a9d
DE
1360/* Utilities for printing registers.
1361 Page numbers refer to the SPARC Architecture Manual. */
1362
b9e58503
PS
1363static void dump_ccreg PARAMS ((char *, int));
1364
8b0f5a9d
DE
1365static void
1366dump_ccreg (reg, val)
1367 char *reg;
1368 int val;
1369{
1370 /* page 41 */
1371 printf_unfiltered ("%s:%s,%s,%s,%s", reg,
1372 val & 8 ? "N" : "NN",
1373 val & 4 ? "Z" : "NZ",
1374 val & 2 ? "O" : "NO",
1375 val & 1 ? "C" : "NC"
1376 );
1377}
1378
1379static char *
1380decode_asi (val)
1381 int val;
1382{
1383 /* page 72 */
1384 switch (val)
1385 {
1386 case 4 : return "ASI_NUCLEUS";
1387 case 0x0c : return "ASI_NUCLEUS_LITTLE";
1388 case 0x10 : return "ASI_AS_IF_USER_PRIMARY";
1389 case 0x11 : return "ASI_AS_IF_USER_SECONDARY";
1390 case 0x18 : return "ASI_AS_IF_USER_PRIMARY_LITTLE";
1391 case 0x19 : return "ASI_AS_IF_USER_SECONDARY_LITTLE";
1392 case 0x80 : return "ASI_PRIMARY";
1393 case 0x81 : return "ASI_SECONDARY";
1394 case 0x82 : return "ASI_PRIMARY_NOFAULT";
1395 case 0x83 : return "ASI_SECONDARY_NOFAULT";
1396 case 0x88 : return "ASI_PRIMARY_LITTLE";
1397 case 0x89 : return "ASI_SECONDARY_LITTLE";
1398 case 0x8a : return "ASI_PRIMARY_NOFAULT_LITTLE";
1399 case 0x8b : return "ASI_SECONDARY_NOFAULT_LITTLE";
1400 default : return NULL;
1401 }
1402}
1403
1404/* PRINT_REGISTER_HOOK routine.
1405 Pretty print various registers. */
1406/* FIXME: Would be nice if this did some fancy things for 32 bit sparc. */
1407
1408void
1409sparc_print_register_hook (regno)
1410 int regno;
1411{
1412 unsigned LONGEST val;
1413
012be3ce
DP
1414 /* Handle double/quad versions of lower 32 fp regs. */
1415 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32
1416 && (regno & 1) == 0)
1417 {
1418 char value[16];
1419
1420 if (!read_relative_register_raw_bytes (regno, value)
1421 && !read_relative_register_raw_bytes (regno + 1, value + 4))
1422 {
1423 printf_unfiltered ("\t");
1424 print_floating (value, builtin_type_double, gdb_stdout);
1425 }
1426#if 0 /* FIXME: gdb doesn't handle long doubles */
1427 if ((regno & 3) == 0)
1428 {
1429 if (!read_relative_register_raw_bytes (regno + 2, value + 8)
1430 && !read_relative_register_raw_bytes (regno + 3, value + 12))
1431 {
1432 printf_unfiltered ("\t");
1433 print_floating (value, builtin_type_long_double, gdb_stdout);
1434 }
1435 }
1436#endif
1437 return;
1438 }
1439
1440#if 0 /* FIXME: gdb doesn't handle long doubles */
1441 /* Print upper fp regs as long double if appropriate. */
1442 if (regno >= FP0_REGNUM + 32 && regno < FP_MAX_REGNUM
1443 /* We test for even numbered regs and not a multiple of 4 because
1444 the upper fp regs are recorded as doubles. */
1445 && (regno & 1) == 0)
8b0f5a9d 1446 {
012be3ce
DP
1447 char value[16];
1448
1449 if (!read_relative_register_raw_bytes (regno, value)
1450 && !read_relative_register_raw_bytes (regno + 1, value + 8))
8b0f5a9d 1451 {
012be3ce
DP
1452 printf_unfiltered ("\t");
1453 print_floating (value, builtin_type_long_double, gdb_stdout);
8b0f5a9d
DE
1454 }
1455 return;
1456 }
012be3ce 1457#endif
8b0f5a9d
DE
1458
1459 /* FIXME: Some of these are priviledged registers.
1460 Not sure how they should be handled. */
1461
1462#define BITS(n, mask) ((int) (((val) >> (n)) & (mask)))
1463
1464 val = read_register (regno);
1465
1466 /* pages 40 - 60 */
1467 switch (regno)
1468 {
1469 case CCR_REGNUM :
1470 printf_unfiltered("\t");
1471 dump_ccreg ("xcc", val >> 4);
1472 printf_unfiltered(", ");
1473 dump_ccreg ("icc", val & 15);
1474 break;
1475 case FPRS_REGNUM :
1476 printf ("\tfef:%d, du:%d, dl:%d",
1477 BITS (2, 1), BITS (1, 1), BITS (0, 1));
1478 break;
1479 case FSR_REGNUM :
1480 {
1481 static char *fcc[4] = { "=", "<", ">", "?" };
1482 static char *rd[4] = { "N", "0", "+", "-" };
1483 /* Long, yes, but I'd rather leave it as is and use a wide screen. */
1484 printf ("\t0:%s, 1:%s, 2:%s, 3:%s, rd:%s, tem:%d, ns:%d, ver:%d, ftt:%d, qne:%d, aexc:%d, cexc:%d",
1485 fcc[BITS (10, 3)], fcc[BITS (32, 3)],
1486 fcc[BITS (34, 3)], fcc[BITS (36, 3)],
1487 rd[BITS (30, 3)], BITS (23, 31), BITS (22, 1), BITS (17, 7),
1488 BITS (14, 7), BITS (13, 1), BITS (5, 31), BITS (0, 31));
1489 break;
1490 }
1491 case ASI_REGNUM :
1492 {
1493 char *asi = decode_asi (val);
1494 if (asi != NULL)
1495 printf ("\t%s", asi);
1496 break;
1497 }
1498 case VER_REGNUM :
1499 printf ("\tmanuf:%d, impl:%d, mask:%d, maxtl:%d, maxwin:%d",
1500 BITS (48, 0xffff), BITS (32, 0xffff),
1501 BITS (24, 0xff), BITS (8, 0xff), BITS (0, 31));
1502 break;
1503 case PSTATE_REGNUM :
1504 {
1505 static char *mm[4] = { "tso", "pso", "rso", "?" };
1506 printf ("\tcle:%d, tle:%d, mm:%s, red:%d, pef:%d, am:%d, priv:%d, ie:%d, ag:%d",
1507 BITS (9, 1), BITS (8, 1), mm[BITS (6, 3)], BITS (5, 1),
1508 BITS (4, 1), BITS (3, 1), BITS (2, 1), BITS (1, 1),
1509 BITS (0, 1));
1510 break;
1511 }
1512 case TSTATE_REGNUM :
1513 /* FIXME: print all 4? */
1514 break;
1515 case TT_REGNUM :
1516 /* FIXME: print all 4? */
1517 break;
1518 case TPC_REGNUM :
1519 /* FIXME: print all 4? */
1520 break;
1521 case TNPC_REGNUM :
1522 /* FIXME: print all 4? */
1523 break;
1524 case WSTATE_REGNUM :
1525 printf ("\tother:%d, normal:%d", BITS (3, 7), BITS (0, 7));
1526 break;
1527 case CWP_REGNUM :
1528 printf ("\t%d", BITS (0, 31));
1529 break;
1530 case CANSAVE_REGNUM :
1531 printf ("\t%-2d before spill", BITS (0, 31));
1532 break;
1533 case CANRESTORE_REGNUM :
1534 printf ("\t%-2d before fill", BITS (0, 31));
1535 break;
1536 case CLEANWIN_REGNUM :
1537 printf ("\t%-2d before clean", BITS (0, 31));
1538 break;
1539 case OTHERWIN_REGNUM :
1540 printf ("\t%d", BITS (0, 31));
1541 break;
1542 }
1543
1544#undef BITS
1545}
1546
1547#endif
1548\f
1549void
1550_initialize_sparc_tdep ()
1551{
91550191
SG
1552 tm_print_insn = print_insn_sparc;
1553 tm_print_insn_info.mach = TM_PRINT_INSN_MACH; /* Selects sparc/sparclite */
8b0f5a9d 1554}
This page took 0.72575 seconds and 4 git commands to generate.