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