]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/sparc-tdep.c
import gdb-1999-07-07 pre reformat
[thirdparty/binutils-gdb.git] / gdb / sparc-tdep.c
CommitLineData
c906108c
SS
1/* Target-dependent code for the SPARC for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 Free Software Foundation, Inc.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
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
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21/* ??? Support for calling functions from gdb in sparc64 is unfinished. */
22
23#include "defs.h"
24#include "frame.h"
25#include "inferior.h"
26#include "obstack.h"
27#include "target.h"
28#include "value.h"
29#include "bfd.h"
30#include "gdb_string.h"
31
32#ifdef USE_PROC_FS
33#include <sys/procfs.h>
34#endif
35
36#include "gdbcore.h"
37
38#if defined(TARGET_SPARCLET) || defined(TARGET_SPARCLITE)
39#define SPARC_HAS_FPU 0
40#else
41#define SPARC_HAS_FPU 1
42#endif
43
44#ifdef GDB_TARGET_IS_SPARC64
45#define FP_REGISTER_BYTES (64 * 4)
46#else
47#define FP_REGISTER_BYTES (32 * 4)
48#endif
49
50/* If not defined, assume 32 bit sparc. */
51#ifndef FP_MAX_REGNUM
52#define FP_MAX_REGNUM (FP0_REGNUM + 32)
53#endif
54
55#define SPARC_INTREG_SIZE (REGISTER_RAW_SIZE (G0_REGNUM))
56
57/* From infrun.c */
58extern int stop_after_trap;
59
60/* We don't store all registers immediately when requested, since they
61 get sent over in large chunks anyway. Instead, we accumulate most
62 of the changes and send them over once. "deferred_stores" keeps
63 track of which sets of registers we have locally-changed copies of,
64 so we only need send the groups that have changed. */
65
66int deferred_stores = 0; /* Cumulates stores we want to do eventually. */
67
68
69/* Some machines, such as Fujitsu SPARClite 86x, have a bi-endian mode
70 where instructions are big-endian and data are little-endian.
71 This flag is set when we detect that the target is of this type. */
72
73int bi_endian = 0;
74
75
76/* Fetch a single instruction. Even on bi-endian machines
77 such as sparc86x, instructions are always big-endian. */
78
79static unsigned long
80fetch_instruction (pc)
81 CORE_ADDR pc;
82{
83 unsigned long retval;
84 int i;
85 unsigned char buf[4];
86
87 read_memory (pc, buf, sizeof (buf));
88
89 /* Start at the most significant end of the integer, and work towards
90 the least significant. */
91 retval = 0;
92 for (i = 0; i < sizeof (buf); ++i)
93 retval = (retval << 8) | buf[i];
94 return retval;
95}
96
97
98/* Branches with prediction are treated like their non-predicting cousins. */
99/* FIXME: What about floating point branches? */
100
101/* Macros to extract fields from sparc instructions. */
102#define X_OP(i) (((i) >> 30) & 0x3)
103#define X_RD(i) (((i) >> 25) & 0x1f)
104#define X_A(i) (((i) >> 29) & 1)
105#define X_COND(i) (((i) >> 25) & 0xf)
106#define X_OP2(i) (((i) >> 22) & 0x7)
107#define X_IMM22(i) ((i) & 0x3fffff)
108#define X_OP3(i) (((i) >> 19) & 0x3f)
109#define X_RS1(i) (((i) >> 14) & 0x1f)
110#define X_I(i) (((i) >> 13) & 1)
111#define X_IMM13(i) ((i) & 0x1fff)
112/* Sign extension macros. */
113#define X_SIMM13(i) ((X_IMM13 (i) ^ 0x1000) - 0x1000)
114#define X_DISP22(i) ((X_IMM22 (i) ^ 0x200000) - 0x200000)
115#define X_CC(i) (((i) >> 20) & 3)
116#define X_P(i) (((i) >> 19) & 1)
117#define X_DISP19(i) ((((i) & 0x7ffff) ^ 0x40000) - 0x40000)
118#define X_RCOND(i) (((i) >> 25) & 7)
119#define X_DISP16(i) ((((((i) >> 6) && 0xc000) | ((i) & 0x3fff)) ^ 0x8000) - 0x8000)
120#define X_FCN(i) (((i) >> 25) & 31)
121
122typedef enum
123{
124 Error, not_branch, bicc, bicca, ba, baa, ticc, ta,
125#ifdef GDB_TARGET_IS_SPARC64
126 done_retry
127#endif
128} branch_type;
129
130/* Simulate single-step ptrace call for sun4. Code written by Gary
131 Beihl (beihl@mcc.com). */
132
133/* npc4 and next_pc describe the situation at the time that the
134 step-breakpoint was set, not necessary the current value of NPC_REGNUM. */
135static CORE_ADDR next_pc, npc4, target;
136static int brknpc4, brktrg;
137typedef char binsn_quantum[BREAKPOINT_MAX];
138static binsn_quantum break_mem[3];
139
140static branch_type isbranch PARAMS ((long, CORE_ADDR, CORE_ADDR *));
141
142/* single_step() is called just before we want to resume the inferior,
143 if we want to single-step it but there is no hardware or kernel single-step
144 support (as on all SPARCs). We find all the possible targets of the
145 coming instruction and breakpoint them.
146
147 single_step is also called just after the inferior stops. If we had
148 set up a simulated single-step, we undo our damage. */
149
150void
151sparc_software_single_step (ignore, insert_breakpoints_p)
152 enum target_signal ignore; /* pid, but we don't need it */
153 int insert_breakpoints_p;
154{
155 branch_type br;
156 CORE_ADDR pc;
157 long pc_instruction;
158
159 if (insert_breakpoints_p)
160 {
161 /* Always set breakpoint for NPC. */
162 next_pc = read_register (NPC_REGNUM);
163 npc4 = next_pc + 4; /* branch not taken */
164
165 target_insert_breakpoint (next_pc, break_mem[0]);
166 /* printf_unfiltered ("set break at %x\n",next_pc); */
167
168 pc = read_register (PC_REGNUM);
169 pc_instruction = fetch_instruction (pc);
170 br = isbranch (pc_instruction, pc, &target);
171 brknpc4 = brktrg = 0;
172
173 if (br == bicca)
174 {
175 /* Conditional annulled branch will either end up at
176 npc (if taken) or at npc+4 (if not taken).
177 Trap npc+4. */
178 brknpc4 = 1;
179 target_insert_breakpoint (npc4, break_mem[1]);
180 }
181 else if (br == baa && target != next_pc)
182 {
183 /* Unconditional annulled branch will always end up at
184 the target. */
185 brktrg = 1;
186 target_insert_breakpoint (target, break_mem[2]);
187 }
188#ifdef GDB_TARGET_IS_SPARC64
189 else if (br == done_retry)
190 {
191 brktrg = 1;
192 target_insert_breakpoint (target, break_mem[2]);
193 }
194#endif
195 }
196 else
197 {
198 /* Remove breakpoints */
199 target_remove_breakpoint (next_pc, break_mem[0]);
200
201 if (brknpc4)
202 target_remove_breakpoint (npc4, break_mem[1]);
203
204 if (brktrg)
205 target_remove_breakpoint (target, break_mem[2]);
206 }
207}
208\f
209/* Call this for each newly created frame. For SPARC, we need to calculate
210 the bottom of the frame, and do some extra work if the prologue
211 has been generated via the -mflat option to GCC. In particular,
212 we need to know where the previous fp and the pc have been stashed,
213 since their exact position within the frame may vary. */
214
215void
216sparc_init_extra_frame_info (fromleaf, fi)
217 int fromleaf;
218 struct frame_info *fi;
219{
220 char *name;
221 CORE_ADDR prologue_start, prologue_end;
222 int insn;
223
224 fi->bottom =
225 (fi->next ?
226 (fi->frame == fi->next->frame ? fi->next->bottom : fi->next->frame) :
227 read_sp ());
228
229 /* If fi->next is NULL, then we already set ->frame by passing read_fp()
230 to create_new_frame. */
231 if (fi->next)
232 {
233 char buf[MAX_REGISTER_RAW_SIZE];
234
235 /* Compute ->frame as if not flat. If it is flat, we'll change
236 it later. */
237 if (fi->next->next != NULL
238 && (fi->next->next->signal_handler_caller
239 || frame_in_dummy (fi->next->next))
240 && frameless_look_for_prologue (fi->next))
241 {
242 /* A frameless function interrupted by a signal did not change
243 the frame pointer, fix up frame pointer accordingly. */
244 fi->frame = FRAME_FP (fi->next);
245 fi->bottom = fi->next->bottom;
246 }
247 else
248 {
249 /* Should we adjust for stack bias here? */
250 get_saved_register (buf, 0, 0, fi, FP_REGNUM, 0);
251 fi->frame = extract_address (buf, REGISTER_RAW_SIZE (FP_REGNUM));
252#ifdef GDB_TARGET_IS_SPARC64
253 if (fi->frame & 1)
254 fi->frame += 2047;
255#endif
256
257 }
258 }
259
260 /* Decide whether this is a function with a ``flat register window''
261 frame. For such functions, the frame pointer is actually in %i7. */
262 fi->flat = 0;
263 fi->in_prologue = 0;
264 if (find_pc_partial_function (fi->pc, &name, &prologue_start, &prologue_end))
265 {
266 /* See if the function starts with an add (which will be of a
267 negative number if a flat frame) to the sp. FIXME: Does not
268 handle large frames which will need more than one instruction
269 to adjust the sp. */
270 insn = fetch_instruction (prologue_start, 4);
271 if (X_OP (insn) == 2 && X_RD (insn) == 14 && X_OP3 (insn) == 0
272 && X_I (insn) && X_SIMM13 (insn) < 0)
273 {
274 int offset = X_SIMM13 (insn);
275
276 /* Then look for a save of %i7 into the frame. */
277 insn = fetch_instruction (prologue_start + 4);
278 if (X_OP (insn) == 3
279 && X_RD (insn) == 31
280 && X_OP3 (insn) == 4
281 && X_RS1 (insn) == 14)
282 {
283 char buf[MAX_REGISTER_RAW_SIZE];
284
285 /* We definitely have a flat frame now. */
286 fi->flat = 1;
287
288 fi->sp_offset = offset;
289
290 /* Overwrite the frame's address with the value in %i7. */
291 get_saved_register (buf, 0, 0, fi, I7_REGNUM, 0);
292 fi->frame = extract_address (buf, REGISTER_RAW_SIZE (I7_REGNUM));
293#ifdef GDB_TARGET_IS_SPARC64
294 if (fi->frame & 1)
295 fi->frame += 2047;
296#endif
297 /* Record where the fp got saved. */
298 fi->fp_addr = fi->frame + fi->sp_offset + X_SIMM13 (insn);
299
300 /* Also try to collect where the pc got saved to. */
301 fi->pc_addr = 0;
302 insn = fetch_instruction (prologue_start + 12);
303 if (X_OP (insn) == 3
304 && X_RD (insn) == 15
305 && X_OP3 (insn) == 4
306 && X_RS1 (insn) == 14)
307 fi->pc_addr = fi->frame + fi->sp_offset + X_SIMM13 (insn);
308 }
309 }
310 else
311 {
312 /* Check if the PC is in the function prologue before a SAVE
313 instruction has been executed yet. If so, set the frame
314 to the current value of the stack pointer and set
315 the in_prologue flag. */
316 CORE_ADDR addr;
317 struct symtab_and_line sal;
318
319 sal = find_pc_line (prologue_start, 0);
320 if (sal.line == 0) /* no line info, use PC */
321 prologue_end = fi->pc;
322 else if (sal.end < prologue_end)
323 prologue_end = sal.end;
324 if (fi->pc < prologue_end)
325 {
326 for (addr = prologue_start; addr < fi->pc; addr += 4)
327 {
328 insn = read_memory_integer (addr, 4);
329 if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3c)
330 break; /* SAVE seen, stop searching */
331 }
332 if (addr >= fi->pc)
333 {
334 fi->in_prologue = 1;
335 fi->frame = read_register (SP_REGNUM);
336 }
337 }
338 }
339 }
340 if (fi->next && fi->frame == 0)
341 {
342 /* Kludge to cause init_prev_frame_info to destroy the new frame. */
343 fi->frame = fi->next->frame;
344 fi->pc = fi->next->pc;
345 }
346}
347
348CORE_ADDR
349sparc_frame_chain (frame)
350 struct frame_info *frame;
351{
352 /* Value that will cause FRAME_CHAIN_VALID to not worry about the chain
353 value. If it realy is zero, we detect it later in
354 sparc_init_prev_frame. */
355 return (CORE_ADDR)1;
356}
357
358CORE_ADDR
359sparc_extract_struct_value_address (regbuf)
360 char regbuf[REGISTER_BYTES];
361{
362 return extract_address (regbuf + REGISTER_BYTE (O0_REGNUM),
363 REGISTER_RAW_SIZE (O0_REGNUM));
364}
365
366/* Find the pc saved in frame FRAME. */
367
368CORE_ADDR
369sparc_frame_saved_pc (frame)
370 struct frame_info *frame;
371{
372 char buf[MAX_REGISTER_RAW_SIZE];
373 CORE_ADDR addr;
374
375 if (frame->signal_handler_caller)
376 {
377 /* This is the signal trampoline frame.
378 Get the saved PC from the sigcontext structure. */
379
380#ifndef SIGCONTEXT_PC_OFFSET
381#define SIGCONTEXT_PC_OFFSET 12
382#endif
383
384 CORE_ADDR sigcontext_addr;
385 char scbuf[TARGET_PTR_BIT / HOST_CHAR_BIT];
386 int saved_pc_offset = SIGCONTEXT_PC_OFFSET;
387 char *name = NULL;
388
389 /* Solaris2 ucbsigvechandler passes a pointer to a sigcontext
390 as the third parameter. The offset to the saved pc is 12. */
391 find_pc_partial_function (frame->pc, &name,
392 (CORE_ADDR *)NULL,(CORE_ADDR *)NULL);
393 if (name && STREQ (name, "ucbsigvechandler"))
394 saved_pc_offset = 12;
395
396 /* The sigcontext address is contained in register O2. */
397 get_saved_register (buf, (int *)NULL, (CORE_ADDR *)NULL,
398 frame, O0_REGNUM + 2, (enum lval_type *)NULL);
399 sigcontext_addr = extract_address (buf, REGISTER_RAW_SIZE (O0_REGNUM + 2));
400
401 /* Don't cause a memory_error when accessing sigcontext in case the
402 stack layout has changed or the stack is corrupt. */
403 target_read_memory (sigcontext_addr + saved_pc_offset,
404 scbuf, sizeof (scbuf));
405 return extract_address (scbuf, sizeof (scbuf));
406 }
407 else if (frame->in_prologue ||
408 (frame->next != NULL
409 && (frame->next->signal_handler_caller
410 || frame_in_dummy (frame->next))
411 && frameless_look_for_prologue (frame)))
412 {
413 /* A frameless function interrupted by a signal did not save
414 the PC, it is still in %o7. */
415 get_saved_register (buf, (int *)NULL, (CORE_ADDR *)NULL,
416 frame, O7_REGNUM, (enum lval_type *)NULL);
417 return PC_ADJUST (extract_address (buf, SPARC_INTREG_SIZE));
418 }
419 if (frame->flat)
420 addr = frame->pc_addr;
421 else
422 addr = frame->bottom + FRAME_SAVED_I0 +
423 SPARC_INTREG_SIZE * (I7_REGNUM - I0_REGNUM);
424
425 if (addr == 0)
426 /* A flat frame leaf function might not save the PC anywhere,
427 just leave it in %o7. */
428 return PC_ADJUST (read_register (O7_REGNUM));
429
430 read_memory (addr, buf, SPARC_INTREG_SIZE);
431 return PC_ADJUST (extract_address (buf, SPARC_INTREG_SIZE));
432}
433
434/* Since an individual frame in the frame cache is defined by two
435 arguments (a frame pointer and a stack pointer), we need two
436 arguments to get info for an arbitrary stack frame. This routine
437 takes two arguments and makes the cached frames look as if these
438 two arguments defined a frame on the cache. This allows the rest
439 of info frame to extract the important arguments without
440 difficulty. */
441
442struct frame_info *
443setup_arbitrary_frame (argc, argv)
444 int argc;
445 CORE_ADDR *argv;
446{
447 struct frame_info *frame;
448
449 if (argc != 2)
450 error ("Sparc frame specifications require two arguments: fp and sp");
451
452 frame = create_new_frame (argv[0], 0);
453
454 if (!frame)
455 fatal ("internal: create_new_frame returned invalid frame");
456
457 frame->bottom = argv[1];
458 frame->pc = FRAME_SAVED_PC (frame);
459 return frame;
460}
461
462/* Given a pc value, skip it forward past the function prologue by
463 disassembling instructions that appear to be a prologue.
464
465 If FRAMELESS_P is set, we are only testing to see if the function
466 is frameless. This allows a quicker answer.
467
468 This routine should be more specific in its actions; making sure
469 that it uses the same register in the initial prologue section. */
470
471static CORE_ADDR examine_prologue PARAMS ((CORE_ADDR, int, struct frame_info *,
472 struct frame_saved_regs *));
473
474static CORE_ADDR
475examine_prologue (start_pc, frameless_p, fi, saved_regs)
476 CORE_ADDR start_pc;
477 int frameless_p;
478 struct frame_info *fi;
479 struct frame_saved_regs *saved_regs;
480{
481 int insn;
482 int dest = -1;
483 CORE_ADDR pc = start_pc;
484 int is_flat = 0;
485
486 insn = fetch_instruction (pc);
487
488 /* Recognize the `sethi' insn and record its destination. */
489 if (X_OP (insn) == 0 && X_OP2 (insn) == 4)
490 {
491 dest = X_RD (insn);
492 pc += 4;
493 insn = fetch_instruction (pc);
494 }
495
496 /* Recognize an add immediate value to register to either %g1 or
497 the destination register recorded above. Actually, this might
498 well recognize several different arithmetic operations.
499 It doesn't check that rs1 == rd because in theory "sub %g0, 5, %g1"
500 followed by "save %sp, %g1, %sp" is a valid prologue (Not that
501 I imagine any compiler really does that, however). */
502 if (X_OP (insn) == 2
503 && X_I (insn)
504 && (X_RD (insn) == 1 || X_RD (insn) == dest))
505 {
506 pc += 4;
507 insn = fetch_instruction (pc);
508 }
509
510 /* Recognize any SAVE insn. */
511 if (X_OP (insn) == 2 && X_OP3 (insn) == 60)
512 {
513 pc += 4;
514 if (frameless_p) /* If the save is all we care about, */
515 return pc; /* return before doing more work */
516 insn = fetch_instruction (pc);
517 }
518 /* Recognize add to %sp. */
519 else if (X_OP (insn) == 2 && X_RD (insn) == 14 && X_OP3 (insn) == 0)
520 {
521 pc += 4;
522 if (frameless_p) /* If the add is all we care about, */
523 return pc; /* return before doing more work */
524 is_flat = 1;
525 insn = fetch_instruction (pc);
526 /* Recognize store of frame pointer (i7). */
527 if (X_OP (insn) == 3
528 && X_RD (insn) == 31
529 && X_OP3 (insn) == 4
530 && X_RS1 (insn) == 14)
531 {
532 pc += 4;
533 insn = fetch_instruction (pc);
534
535 /* Recognize sub %sp, <anything>, %i7. */
536 if (X_OP (insn) == 2
537 && X_OP3 (insn) == 4
538 && X_RS1 (insn) == 14
539 && X_RD (insn) == 31)
540 {
541 pc += 4;
542 insn = fetch_instruction (pc);
543 }
544 else
545 return pc;
546 }
547 else
548 return pc;
549 }
550 else
551 /* Without a save or add instruction, it's not a prologue. */
552 return start_pc;
553
554 while (1)
555 {
556 /* Recognize stores into the frame from the input registers.
557 This recognizes all non alternate stores of input register,
558 into a location offset from the frame pointer. */
559 if ((X_OP (insn) == 3
560 && (X_OP3 (insn) & 0x3c) == 4 /* Store, non-alternate. */
561 && (X_RD (insn) & 0x18) == 0x18 /* Input register. */
562 && X_I (insn) /* Immediate mode. */
563 && X_RS1 (insn) == 30 /* Off of frame pointer. */
564 /* Into reserved stack space. */
565 && X_SIMM13 (insn) >= 0x44
566 && X_SIMM13 (insn) < 0x5b))
567 ;
568 else if (is_flat
569 && X_OP (insn) == 3
570 && X_OP3 (insn) == 4
571 && X_RS1 (insn) == 14
572 )
573 {
574 if (saved_regs && X_I (insn))
575 saved_regs->regs[X_RD (insn)] =
576 fi->frame + fi->sp_offset + X_SIMM13 (insn);
577 }
578 else
579 break;
580 pc += 4;
581 insn = fetch_instruction (pc);
582 }
583
584 return pc;
585}
586
587CORE_ADDR
b83266a0 588sparc_skip_prologue (start_pc, frameless_p)
c906108c
SS
589 CORE_ADDR start_pc;
590 int frameless_p;
591{
592 return examine_prologue (start_pc, frameless_p, NULL, NULL);
593}
594
595/* Check instruction at ADDR to see if it is a branch.
596 All non-annulled instructions will go to NPC or will trap.
597 Set *TARGET if we find a candidate branch; set to zero if not.
598
599 This isn't static as it's used by remote-sa.sparc.c. */
600
601static branch_type
602isbranch (instruction, addr, target)
603 long instruction;
604 CORE_ADDR addr, *target;
605{
606 branch_type val = not_branch;
607 long int offset = 0; /* Must be signed for sign-extend. */
608
609 *target = 0;
610
611 if (X_OP (instruction) == 0
612 && (X_OP2 (instruction) == 2
613 || X_OP2 (instruction) == 6
614 || X_OP2 (instruction) == 1
615 || X_OP2 (instruction) == 3
616 || X_OP2 (instruction) == 5
617#ifndef GDB_TARGET_IS_SPARC64
618 || X_OP2 (instruction) == 7
619#endif
620 ))
621 {
622 if (X_COND (instruction) == 8)
623 val = X_A (instruction) ? baa : ba;
624 else
625 val = X_A (instruction) ? bicca : bicc;
626 switch (X_OP2 (instruction))
627 {
628 case 2:
629 case 6:
630#ifndef GDB_TARGET_IS_SPARC64
631 case 7:
632#endif
633 offset = 4 * X_DISP22 (instruction);
634 break;
635 case 1:
636 case 5:
637 offset = 4 * X_DISP19 (instruction);
638 break;
639 case 3:
640 offset = 4 * X_DISP16 (instruction);
641 break;
642 }
643 *target = addr + offset;
644 }
645#ifdef GDB_TARGET_IS_SPARC64
646 else if (X_OP (instruction) == 2
647 && X_OP3 (instruction) == 62)
648 {
649 if (X_FCN (instruction) == 0)
650 {
651 /* done */
652 *target = read_register (TNPC_REGNUM);
653 val = done_retry;
654 }
655 else if (X_FCN (instruction) == 1)
656 {
657 /* retry */
658 *target = read_register (TPC_REGNUM);
659 val = done_retry;
660 }
661 }
662#endif
663
664 return val;
665}
666\f
667/* Find register number REGNUM relative to FRAME and put its
668 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
669 was optimized out (and thus can't be fetched). If the variable
670 was fetched from memory, set *ADDRP to where it was fetched from,
671 otherwise it was fetched from a register.
672
673 The argument RAW_BUFFER must point to aligned memory. */
674
675void
7a292a7a 676sparc_get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
c906108c
SS
677 char *raw_buffer;
678 int *optimized;
679 CORE_ADDR *addrp;
680 struct frame_info *frame;
681 int regnum;
682 enum lval_type *lval;
683{
684 struct frame_info *frame1;
685 CORE_ADDR addr;
686
687 if (!target_has_registers)
688 error ("No registers.");
689
690 if (optimized)
691 *optimized = 0;
692
693 addr = 0;
694
695 /* FIXME This code extracted from infcmd.c; should put elsewhere! */
696 if (frame == NULL)
697 {
698 /* error ("No selected frame."); */
699 if (!target_has_registers)
700 error ("The program has no registers now.");
701 if (selected_frame == NULL)
702 error ("No selected frame.");
703 /* Try to use selected frame */
704 frame = get_prev_frame (selected_frame);
705 if (frame == 0)
706 error ("Cmd not meaningful in the outermost frame.");
707 }
708
709
710 frame1 = frame->next;
711
712 /* Get saved PC from the frame info if not in innermost frame. */
713 if (regnum == PC_REGNUM && frame1 != NULL)
714 {
715 if (lval != NULL)
716 *lval = not_lval;
717 if (raw_buffer != NULL)
718 {
719 /* Put it back in target format. */
720 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), frame->pc);
721 }
722 if (addrp != NULL)
723 *addrp = 0;
724 return;
725 }
726
727 while (frame1 != NULL)
728 {
729 if (frame1->pc >= (frame1->bottom ? frame1->bottom :
730 read_sp ())
731 && frame1->pc <= FRAME_FP (frame1))
732 {
733 /* Dummy frame. All but the window regs are in there somewhere.
734 The window registers are saved on the stack, just like in a
735 normal frame. */
736 if (regnum >= G1_REGNUM && regnum < G1_REGNUM + 7)
737 addr = frame1->frame + (regnum - G0_REGNUM) * SPARC_INTREG_SIZE
738 - (FP_REGISTER_BYTES + 8 * SPARC_INTREG_SIZE);
739 else if (regnum >= I0_REGNUM && regnum < I0_REGNUM + 8)
740 addr = (frame1->prev->bottom
741 + (regnum - I0_REGNUM) * SPARC_INTREG_SIZE
742 + FRAME_SAVED_I0);
743 else if (regnum >= L0_REGNUM && regnum < L0_REGNUM + 8)
744 addr = (frame1->prev->bottom
745 + (regnum - L0_REGNUM) * SPARC_INTREG_SIZE
746 + FRAME_SAVED_L0);
747 else if (regnum >= O0_REGNUM && regnum < O0_REGNUM + 8)
748 addr = frame1->frame + (regnum - O0_REGNUM) * SPARC_INTREG_SIZE
749 - (FP_REGISTER_BYTES + 16 * SPARC_INTREG_SIZE);
750#ifdef FP0_REGNUM
751 else if (regnum >= FP0_REGNUM && regnum < FP0_REGNUM + 32)
752 addr = frame1->frame + (regnum - FP0_REGNUM) * 4
753 - (FP_REGISTER_BYTES);
754#ifdef GDB_TARGET_IS_SPARC64
755 else if (regnum >= FP0_REGNUM + 32 && regnum < FP_MAX_REGNUM)
756 addr = frame1->frame + 32 * 4 + (regnum - FP0_REGNUM - 32) * 8
757 - (FP_REGISTER_BYTES);
758#endif
759#endif /* FP0_REGNUM */
760 else if (regnum >= Y_REGNUM && regnum < NUM_REGS)
761 addr = frame1->frame + (regnum - Y_REGNUM) * SPARC_INTREG_SIZE
762 - (FP_REGISTER_BYTES + 24 * SPARC_INTREG_SIZE);
763 }
764 else if (frame1->flat)
765 {
766
767 if (regnum == RP_REGNUM)
768 addr = frame1->pc_addr;
769 else if (regnum == I7_REGNUM)
770 addr = frame1->fp_addr;
771 else
772 {
773 CORE_ADDR func_start;
774 struct frame_saved_regs regs;
775 memset (&regs, 0, sizeof (regs));
776
777 find_pc_partial_function (frame1->pc, NULL, &func_start, NULL);
778 examine_prologue (func_start, 0, frame1, &regs);
779 addr = regs.regs[regnum];
780 }
781 }
782 else
783 {
784 /* Normal frame. Local and In registers are saved on stack. */
785 if (regnum >= I0_REGNUM && regnum < I0_REGNUM + 8)
786 addr = (frame1->prev->bottom
787 + (regnum - I0_REGNUM) * SPARC_INTREG_SIZE
788 + FRAME_SAVED_I0);
789 else if (regnum >= L0_REGNUM && regnum < L0_REGNUM + 8)
790 addr = (frame1->prev->bottom
791 + (regnum - L0_REGNUM) * SPARC_INTREG_SIZE
792 + FRAME_SAVED_L0);
793 else if (regnum >= O0_REGNUM && regnum < O0_REGNUM + 8)
794 {
795 /* Outs become ins. */
796 get_saved_register (raw_buffer, optimized, addrp, frame1,
797 (regnum - O0_REGNUM + I0_REGNUM), lval);
798 return;
799 }
800 }
801 if (addr != 0)
802 break;
803 frame1 = frame1->next;
804 }
805 if (addr != 0)
806 {
807 if (lval != NULL)
808 *lval = lval_memory;
809 if (regnum == SP_REGNUM)
810 {
811 if (raw_buffer != NULL)
812 {
813 /* Put it back in target format. */
814 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), addr);
815 }
816 if (addrp != NULL)
817 *addrp = 0;
818 return;
819 }
820 if (raw_buffer != NULL)
821 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
822 }
823 else
824 {
825 if (lval != NULL)
826 *lval = lval_register;
827 addr = REGISTER_BYTE (regnum);
828 if (raw_buffer != NULL)
829 read_register_gen (regnum, raw_buffer);
830 }
831 if (addrp != NULL)
832 *addrp = addr;
833}
834
835/* Push an empty stack frame, and record in it the current PC, regs, etc.
836
837 We save the non-windowed registers and the ins. The locals and outs
838 are new; they don't need to be saved. The i's and l's of
839 the last frame were already saved on the stack. */
840
841/* Definitely see tm-sparc.h for more doc of the frame format here. */
842
843#ifdef GDB_TARGET_IS_SPARC64
844#define DUMMY_REG_SAVE_OFFSET (128 + 16)
845#else
846#define DUMMY_REG_SAVE_OFFSET 0x60
847#endif
848
849/* See tm-sparc.h for how this is calculated. */
850#ifdef FP0_REGNUM
851#define DUMMY_STACK_REG_BUF_SIZE \
852(((8+8+8) * SPARC_INTREG_SIZE) + FP_REGISTER_BYTES)
853#else
854#define DUMMY_STACK_REG_BUF_SIZE \
855(((8+8+8) * SPARC_INTREG_SIZE) )
856#endif /* FP0_REGNUM */
857#define DUMMY_STACK_SIZE (DUMMY_STACK_REG_BUF_SIZE + DUMMY_REG_SAVE_OFFSET)
858
859void
860sparc_push_dummy_frame ()
861{
862 CORE_ADDR sp, old_sp;
863 char register_temp[DUMMY_STACK_SIZE];
864
865 old_sp = sp = read_sp ();
866
867#ifdef GDB_TARGET_IS_SPARC64
868 /* PC, NPC, CCR, FSR, FPRS, Y, ASI */
869 read_register_bytes (REGISTER_BYTE (PC_REGNUM), &register_temp[0],
870 REGISTER_RAW_SIZE (PC_REGNUM) * 7);
871 read_register_bytes (REGISTER_BYTE (PSTATE_REGNUM), &register_temp[8],
872 REGISTER_RAW_SIZE (PSTATE_REGNUM));
873 /* FIXME: not sure what needs to be saved here. */
874#else
875 /* Y, PS, WIM, TBR, PC, NPC, FPS, CPS regs */
876 read_register_bytes (REGISTER_BYTE (Y_REGNUM), &register_temp[0],
877 REGISTER_RAW_SIZE (Y_REGNUM) * 8);
878#endif
879
880 read_register_bytes (REGISTER_BYTE (O0_REGNUM),
881 &register_temp[8 * SPARC_INTREG_SIZE],
882 SPARC_INTREG_SIZE * 8);
883
884 read_register_bytes (REGISTER_BYTE (G0_REGNUM),
885 &register_temp[16 * SPARC_INTREG_SIZE],
886 SPARC_INTREG_SIZE * 8);
887
888#ifdef FP0_REGNUM
889 read_register_bytes (REGISTER_BYTE (FP0_REGNUM),
890 &register_temp[24 * SPARC_INTREG_SIZE],
891 FP_REGISTER_BYTES);
892#endif /* FP0_REGNUM */
893
894 sp -= DUMMY_STACK_SIZE;
895
896 write_sp (sp);
897
898 write_memory (sp + DUMMY_REG_SAVE_OFFSET, &register_temp[0],
899 DUMMY_STACK_REG_BUF_SIZE);
900
901 if (strcmp (target_shortname, "sim") != 0)
902 {
903 write_fp (old_sp);
904
905 /* Set return address register for the call dummy to the current PC. */
906 write_register (I7_REGNUM, read_pc() - 8);
907 }
908 else
909 {
910 /* The call dummy will write this value to FP before executing
911 the 'save'. This ensures that register window flushes work
912 correctly in the simulator. */
913 write_register (G0_REGNUM+1, read_register (FP_REGNUM));
914
915 /* The call dummy will write this value to FP after executing
916 the 'save'. */
917 write_register (G0_REGNUM+2, old_sp);
918
919 /* The call dummy will write this value to the return address (%i7) after
920 executing the 'save'. */
921 write_register (G0_REGNUM+3, read_pc() - 8);
922
923 /* Set the FP that the call dummy will be using after the 'save'.
924 This makes backtraces from an inferior function call work properly. */
925 write_register (FP_REGNUM, old_sp);
926 }
927}
928
929/* sparc_frame_find_saved_regs (). This function is here only because
930 pop_frame uses it. Note there is an interesting corner case which
931 I think few ports of GDB get right--if you are popping a frame
932 which does not save some register that *is* saved by a more inner
933 frame (such a frame will never be a dummy frame because dummy
934 frames save all registers). Rewriting pop_frame to use
935 get_saved_register would solve this problem and also get rid of the
936 ugly duplication between sparc_frame_find_saved_regs and
937 get_saved_register.
938
939 Stores, into a struct frame_saved_regs,
940 the addresses of the saved registers of frame described by FRAME_INFO.
941 This includes special registers such as pc and fp saved in special
942 ways in the stack frame. sp is even more special:
943 the address we return for it IS the sp for the next frame.
944
945 Note that on register window machines, we are currently making the
946 assumption that window registers are being saved somewhere in the
947 frame in which they are being used. If they are stored in an
948 inferior frame, find_saved_register will break.
949
950 On the Sun 4, the only time all registers are saved is when
951 a dummy frame is involved. Otherwise, the only saved registers
952 are the LOCAL and IN registers which are saved as a result
953 of the "save/restore" opcodes. This condition is determined
954 by address rather than by value.
955
956 The "pc" is not stored in a frame on the SPARC. (What is stored
957 is a return address minus 8.) sparc_pop_frame knows how to
958 deal with that. Other routines might or might not.
959
960 See tm-sparc.h (PUSH_DUMMY_FRAME and friends) for CRITICAL information
961 about how this works. */
962
963static void sparc_frame_find_saved_regs PARAMS ((struct frame_info *,
964 struct frame_saved_regs *));
965
966static void
967sparc_frame_find_saved_regs (fi, saved_regs_addr)
968 struct frame_info *fi;
969 struct frame_saved_regs *saved_regs_addr;
970{
971 register int regnum;
972 CORE_ADDR frame_addr = FRAME_FP (fi);
973
974 if (!fi)
975 fatal ("Bad frame info struct in FRAME_FIND_SAVED_REGS");
976
977 memset (saved_regs_addr, 0, sizeof (*saved_regs_addr));
978
979 if (fi->pc >= (fi->bottom ? fi->bottom :
980 read_sp ())
981 && fi->pc <= FRAME_FP(fi))
982 {
983 /* Dummy frame. All but the window regs are in there somewhere. */
984 for (regnum = G1_REGNUM; regnum < G1_REGNUM+7; regnum++)
985 saved_regs_addr->regs[regnum] =
986 frame_addr + (regnum - G0_REGNUM) * SPARC_INTREG_SIZE
987 - DUMMY_STACK_REG_BUF_SIZE + 16 * SPARC_INTREG_SIZE;
988 for (regnum = I0_REGNUM; regnum < I0_REGNUM+8; regnum++)
989 saved_regs_addr->regs[regnum] =
990 frame_addr + (regnum - I0_REGNUM) * SPARC_INTREG_SIZE
991 - DUMMY_STACK_REG_BUF_SIZE + 8 * SPARC_INTREG_SIZE;
992#ifdef FP0_REGNUM
993 for (regnum = FP0_REGNUM; regnum < FP0_REGNUM + 32; regnum++)
994 saved_regs_addr->regs[regnum] =
995 frame_addr + (regnum - FP0_REGNUM) * 4
996 - DUMMY_STACK_REG_BUF_SIZE + 24 * SPARC_INTREG_SIZE;
997#ifdef GDB_TARGET_IS_SPARC64
998 for (regnum = FP0_REGNUM + 32; regnum < FP_MAX_REGNUM; regnum++)
999 saved_regs_addr->regs[regnum] =
1000 frame_addr + 32 * 4 + (regnum - FP0_REGNUM - 32) * 4
1001 - DUMMY_STACK_REG_BUF_SIZE + 24 * SPARC_INTREG_SIZE;
1002#endif
1003#endif /* FP0_REGNUM */
1004#ifdef GDB_TARGET_IS_SPARC64
1005 for (regnum = PC_REGNUM; regnum < PC_REGNUM + 7; regnum++)
1006 {
1007 saved_regs_addr->regs[regnum] =
1008 frame_addr + (regnum - PC_REGNUM) * SPARC_INTREG_SIZE
1009 - DUMMY_STACK_REG_BUF_SIZE;
1010 }
1011 saved_regs_addr->regs[PSTATE_REGNUM] =
1012 frame_addr + 8 * SPARC_INTREG_SIZE - DUMMY_STACK_REG_BUF_SIZE;
1013#else
1014 for (regnum = Y_REGNUM; regnum < NUM_REGS; regnum++)
1015 saved_regs_addr->regs[regnum] =
1016 frame_addr + (regnum - Y_REGNUM) * SPARC_INTREG_SIZE
1017 - DUMMY_STACK_REG_BUF_SIZE;
1018#endif
1019 frame_addr = fi->bottom ?
1020 fi->bottom : read_sp ();
1021 }
1022 else if (fi->flat)
1023 {
1024 CORE_ADDR func_start;
1025 find_pc_partial_function (fi->pc, NULL, &func_start, NULL);
1026 examine_prologue (func_start, 0, fi, saved_regs_addr);
1027
1028 /* Flat register window frame. */
1029 saved_regs_addr->regs[RP_REGNUM] = fi->pc_addr;
1030 saved_regs_addr->regs[I7_REGNUM] = fi->fp_addr;
1031 }
1032 else
1033 {
1034 /* Normal frame. Just Local and In registers */
1035 frame_addr = fi->bottom ?
1036 fi->bottom : read_sp ();
1037 for (regnum = L0_REGNUM; regnum < L0_REGNUM+8; regnum++)
1038 saved_regs_addr->regs[regnum] =
1039 (frame_addr + (regnum - L0_REGNUM) * SPARC_INTREG_SIZE
1040 + FRAME_SAVED_L0);
1041 for (regnum = I0_REGNUM; regnum < I0_REGNUM+8; regnum++)
1042 saved_regs_addr->regs[regnum] =
1043 (frame_addr + (regnum - I0_REGNUM) * SPARC_INTREG_SIZE
1044 + FRAME_SAVED_I0);
1045 }
1046 if (fi->next)
1047 {
1048 if (fi->flat)
1049 {
1050 saved_regs_addr->regs[O7_REGNUM] = fi->pc_addr;
1051 }
1052 else
1053 {
1054 /* Pull off either the next frame pointer or the stack pointer */
1055 CORE_ADDR next_next_frame_addr =
1056 (fi->next->bottom ?
1057 fi->next->bottom :
1058 read_sp ());
1059 for (regnum = O0_REGNUM; regnum < O0_REGNUM+8; regnum++)
1060 saved_regs_addr->regs[regnum] =
1061 (next_next_frame_addr
1062 + (regnum - O0_REGNUM) * SPARC_INTREG_SIZE
1063 + FRAME_SAVED_I0);
1064 }
1065 }
1066 /* Otherwise, whatever we would get from ptrace(GETREGS) is accurate */
1067 /* FIXME -- should this adjust for the sparc64 offset? */
1068 saved_regs_addr->regs[SP_REGNUM] = FRAME_FP (fi);
1069}
1070
1071/* Discard from the stack the innermost frame, restoring all saved registers.
1072
1073 Note that the values stored in fsr by get_frame_saved_regs are *in
1074 the context of the called frame*. What this means is that the i
1075 regs of fsr must be restored into the o regs of the (calling) frame that
1076 we pop into. We don't care about the output regs of the calling frame,
1077 since unless it's a dummy frame, it won't have any output regs in it.
1078
1079 We never have to bother with %l (local) regs, since the called routine's
1080 locals get tossed, and the calling routine's locals are already saved
1081 on its stack. */
1082
1083/* Definitely see tm-sparc.h for more doc of the frame format here. */
1084
1085void
1086sparc_pop_frame ()
1087{
1088 register struct frame_info *frame = get_current_frame ();
1089 register CORE_ADDR pc;
1090 struct frame_saved_regs fsr;
1091 char raw_buffer[REGISTER_BYTES];
1092 int regnum;
1093
1094 sparc_frame_find_saved_regs (frame, &fsr);
1095#ifdef FP0_REGNUM
1096 if (fsr.regs[FP0_REGNUM])
1097 {
1098 read_memory (fsr.regs[FP0_REGNUM], raw_buffer, FP_REGISTER_BYTES);
1099 write_register_bytes (REGISTER_BYTE (FP0_REGNUM),
1100 raw_buffer, FP_REGISTER_BYTES);
1101 }
1102#ifndef GDB_TARGET_IS_SPARC64
1103 if (fsr.regs[FPS_REGNUM])
1104 {
1105 read_memory (fsr.regs[FPS_REGNUM], raw_buffer, 4);
1106 write_register_bytes (REGISTER_BYTE (FPS_REGNUM), raw_buffer, 4);
1107 }
1108 if (fsr.regs[CPS_REGNUM])
1109 {
1110 read_memory (fsr.regs[CPS_REGNUM], raw_buffer, 4);
1111 write_register_bytes (REGISTER_BYTE (CPS_REGNUM), raw_buffer, 4);
1112 }
1113#endif
1114#endif /* FP0_REGNUM */
1115 if (fsr.regs[G1_REGNUM])
1116 {
1117 read_memory (fsr.regs[G1_REGNUM], raw_buffer, 7 * SPARC_INTREG_SIZE);
1118 write_register_bytes (REGISTER_BYTE (G1_REGNUM), raw_buffer,
1119 7 * SPARC_INTREG_SIZE);
1120 }
1121
1122 if (frame->flat)
1123 {
1124 /* Each register might or might not have been saved, need to test
1125 individually. */
1126 for (regnum = L0_REGNUM; regnum < L0_REGNUM + 8; ++regnum)
1127 if (fsr.regs[regnum])
1128 write_register (regnum, read_memory_integer (fsr.regs[regnum],
1129 SPARC_INTREG_SIZE));
1130 for (regnum = I0_REGNUM; regnum < I0_REGNUM + 8; ++regnum)
1131 if (fsr.regs[regnum])
1132 write_register (regnum, read_memory_integer (fsr.regs[regnum],
1133 SPARC_INTREG_SIZE));
1134
1135 /* Handle all outs except stack pointer (o0-o5; o7). */
1136 for (regnum = O0_REGNUM; regnum < O0_REGNUM + 6; ++regnum)
1137 if (fsr.regs[regnum])
1138 write_register (regnum, read_memory_integer (fsr.regs[regnum],
1139 SPARC_INTREG_SIZE));
1140 if (fsr.regs[O0_REGNUM + 7])
1141 write_register (O0_REGNUM + 7,
1142 read_memory_integer (fsr.regs[O0_REGNUM + 7],
1143 SPARC_INTREG_SIZE));
1144
1145 write_sp (frame->frame);
1146 }
1147 else if (fsr.regs[I0_REGNUM])
1148 {
1149 CORE_ADDR sp;
1150
1151 char reg_temp[REGISTER_BYTES];
1152
1153 read_memory (fsr.regs[I0_REGNUM], raw_buffer, 8 * SPARC_INTREG_SIZE);
1154
1155 /* Get the ins and locals which we are about to restore. Just
1156 moving the stack pointer is all that is really needed, except
1157 store_inferior_registers is then going to write the ins and
1158 locals from the registers array, so we need to muck with the
1159 registers array. */
1160 sp = fsr.regs[SP_REGNUM];
1161#ifdef GDB_TARGET_IS_SPARC64
1162 if (sp & 1)
1163 sp += 2047;
1164#endif
1165 read_memory (sp, reg_temp, SPARC_INTREG_SIZE * 16);
1166
1167 /* Restore the out registers.
1168 Among other things this writes the new stack pointer. */
1169 write_register_bytes (REGISTER_BYTE (O0_REGNUM), raw_buffer,
1170 SPARC_INTREG_SIZE * 8);
1171
1172 write_register_bytes (REGISTER_BYTE (L0_REGNUM), reg_temp,
1173 SPARC_INTREG_SIZE * 16);
1174 }
1175#ifndef GDB_TARGET_IS_SPARC64
1176 if (fsr.regs[PS_REGNUM])
1177 write_register (PS_REGNUM, read_memory_integer (fsr.regs[PS_REGNUM], 4));
1178#endif
1179 if (fsr.regs[Y_REGNUM])
1180 write_register (Y_REGNUM, read_memory_integer (fsr.regs[Y_REGNUM], REGISTER_RAW_SIZE (Y_REGNUM)));
1181 if (fsr.regs[PC_REGNUM])
1182 {
1183 /* Explicitly specified PC (and maybe NPC) -- just restore them. */
1184 write_register (PC_REGNUM, read_memory_integer (fsr.regs[PC_REGNUM],
1185 REGISTER_RAW_SIZE (PC_REGNUM)));
1186 if (fsr.regs[NPC_REGNUM])
1187 write_register (NPC_REGNUM,
1188 read_memory_integer (fsr.regs[NPC_REGNUM],
1189 REGISTER_RAW_SIZE (NPC_REGNUM)));
1190 }
1191 else if (frame->flat)
1192 {
1193 if (frame->pc_addr)
1194 pc = PC_ADJUST ((CORE_ADDR)
1195 read_memory_integer (frame->pc_addr,
1196 REGISTER_RAW_SIZE (PC_REGNUM)));
1197 else
1198 {
1199 /* I think this happens only in the innermost frame, if so then
1200 it is a complicated way of saying
1201 "pc = read_register (O7_REGNUM);". */
1202 char buf[MAX_REGISTER_RAW_SIZE];
1203 get_saved_register (buf, 0, 0, frame, O7_REGNUM, 0);
1204 pc = PC_ADJUST (extract_address
1205 (buf, REGISTER_RAW_SIZE (O7_REGNUM)));
1206 }
1207
1208 write_register (PC_REGNUM, pc);
1209 write_register (NPC_REGNUM, pc + 4);
1210 }
1211 else if (fsr.regs[I7_REGNUM])
1212 {
1213 /* Return address in %i7 -- adjust it, then restore PC and NPC from it */
1214 pc = PC_ADJUST ((CORE_ADDR) read_memory_integer (fsr.regs[I7_REGNUM],
1215 SPARC_INTREG_SIZE));
1216 write_register (PC_REGNUM, pc);
1217 write_register (NPC_REGNUM, pc + 4);
1218 }
1219 flush_cached_frames ();
1220}
1221
1222/* On the Sun 4 under SunOS, the compile will leave a fake insn which
1223 encodes the structure size being returned. If we detect such
1224 a fake insn, step past it. */
1225
1226CORE_ADDR
1227sparc_pc_adjust(pc)
1228 CORE_ADDR pc;
1229{
1230 unsigned long insn;
1231 char buf[4];
1232 int err;
1233
1234 err = target_read_memory (pc + 8, buf, 4);
1235 insn = extract_unsigned_integer (buf, 4);
1236 if ((err == 0) && (insn & 0xffc00000) == 0)
1237 return pc+12;
1238 else
1239 return pc+8;
1240}
1241
1242/* If pc is in a shared library trampoline, return its target.
1243 The SunOs 4.x linker rewrites the jump table entries for PIC
1244 compiled modules in the main executable to bypass the dynamic linker
1245 with jumps of the form
1246 sethi %hi(addr),%g1
1247 jmp %g1+%lo(addr)
1248 and removes the corresponding jump table relocation entry in the
1249 dynamic relocations.
1250 find_solib_trampoline_target relies on the presence of the jump
1251 table relocation entry, so we have to detect these jump instructions
1252 by hand. */
1253
1254CORE_ADDR
1255sunos4_skip_trampoline_code (pc)
1256 CORE_ADDR pc;
1257{
1258 unsigned long insn1;
1259 char buf[4];
1260 int err;
1261
1262 err = target_read_memory (pc, buf, 4);
1263 insn1 = extract_unsigned_integer (buf, 4);
1264 if (err == 0 && (insn1 & 0xffc00000) == 0x03000000)
1265 {
1266 unsigned long insn2;
1267
1268 err = target_read_memory (pc + 4, buf, 4);
1269 insn2 = extract_unsigned_integer (buf, 4);
1270 if (err == 0 && (insn2 & 0xffffe000) == 0x81c06000)
1271 {
1272 CORE_ADDR target_pc = (insn1 & 0x3fffff) << 10;
1273 int delta = insn2 & 0x1fff;
1274
1275 /* Sign extend the displacement. */
1276 if (delta & 0x1000)
1277 delta |= ~0x1fff;
1278 return target_pc + delta;
1279 }
1280 }
1281 return find_solib_trampoline_target (pc);
1282}
1283\f
1284#ifdef USE_PROC_FS /* Target dependent support for /proc */
1285
9846de1b 1286/* *INDENT-OFF* */
c906108c
SS
1287/* The /proc interface divides the target machine's register set up into
1288 two different sets, the general register set (gregset) and the floating
1289 point register set (fpregset). For each set, there is an ioctl to get
1290 the current register set and another ioctl to set the current values.
1291
1292 The actual structure passed through the ioctl interface is, of course,
1293 naturally machine dependent, and is different for each set of registers.
1294 For the sparc for example, the general register set is typically defined
1295 by:
1296
1297 typedef int gregset_t[38];
1298
1299 #define R_G0 0
1300 ...
1301 #define R_TBR 37
1302
1303 and the floating point set by:
1304
1305 typedef struct prfpregset {
1306 union {
1307 u_long pr_regs[32];
1308 double pr_dregs[16];
1309 } pr_fr;
1310 void * pr_filler;
1311 u_long pr_fsr;
1312 u_char pr_qcnt;
1313 u_char pr_q_entrysize;
1314 u_char pr_en;
1315 u_long pr_q[64];
1316 } prfpregset_t;
1317
1318 These routines provide the packing and unpacking of gregset_t and
1319 fpregset_t formatted data.
1320
1321 */
9846de1b 1322/* *INDENT-ON* */
c906108c
SS
1323
1324/* Given a pointer to a general register set in /proc format (gregset_t *),
1325 unpack the register contents and supply them as gdb's idea of the current
1326 register values. */
1327
1328void
1329supply_gregset (gregsetp)
1330prgregset_t *gregsetp;
1331{
1332 register int regi;
1333 register prgreg_t *regp = (prgreg_t *) gregsetp;
1334 static char zerobuf[MAX_REGISTER_RAW_SIZE] = {0};
1335
1336 /* GDB register numbers for Gn, On, Ln, In all match /proc reg numbers. */
1337 for (regi = G0_REGNUM ; regi <= I7_REGNUM ; regi++)
1338 {
1339 supply_register (regi, (char *) (regp + regi));
1340 }
1341
1342 /* These require a bit more care. */
1343 supply_register (PS_REGNUM, (char *) (regp + R_PS));
1344 supply_register (PC_REGNUM, (char *) (regp + R_PC));
1345 supply_register (NPC_REGNUM,(char *) (regp + R_nPC));
1346 supply_register (Y_REGNUM, (char *) (regp + R_Y));
1347
1348 /* Fill inaccessible registers with zero. */
1349 supply_register (WIM_REGNUM, zerobuf);
1350 supply_register (TBR_REGNUM, zerobuf);
1351 supply_register (CPS_REGNUM, zerobuf);
1352}
1353
1354void
1355fill_gregset (gregsetp, regno)
1356prgregset_t *gregsetp;
1357int regno;
1358{
1359 int regi;
1360 register prgreg_t *regp = (prgreg_t *) gregsetp;
1361
1362 for (regi = 0 ; regi <= R_I7 ; regi++)
1363 {
1364 if ((regno == -1) || (regno == regi))
1365 {
1366 *(regp + regi) = *(int *) &registers[REGISTER_BYTE (regi)];
1367 }
1368 }
1369 if ((regno == -1) || (regno == PS_REGNUM))
1370 {
1371 *(regp + R_PS) = *(int *) &registers[REGISTER_BYTE (PS_REGNUM)];
1372 }
1373 if ((regno == -1) || (regno == PC_REGNUM))
1374 {
1375 *(regp + R_PC) = *(int *) &registers[REGISTER_BYTE (PC_REGNUM)];
1376 }
1377 if ((regno == -1) || (regno == NPC_REGNUM))
1378 {
1379 *(regp + R_nPC) = *(int *) &registers[REGISTER_BYTE (NPC_REGNUM)];
1380 }
1381 if ((regno == -1) || (regno == Y_REGNUM))
1382 {
1383 *(regp + R_Y) = *(int *) &registers[REGISTER_BYTE (Y_REGNUM)];
1384 }
1385}
1386
1387#if defined (FP0_REGNUM)
1388
1389/* Given a pointer to a floating point register set in /proc format
1390 (fpregset_t *), unpack the register contents and supply them as gdb's
1391 idea of the current floating point register values. */
1392
1393void
1394supply_fpregset (fpregsetp)
1395prfpregset_t *fpregsetp;
1396{
1397 register int regi;
1398 char *from;
1399
1400 for (regi = FP0_REGNUM ; regi < FP_MAX_REGNUM ; regi++)
1401 {
1402 from = (char *) &fpregsetp->pr_fr.pr_regs[regi-FP0_REGNUM];
1403 supply_register (regi, from);
1404 }
1405 supply_register (FPS_REGNUM, (char *) &(fpregsetp->pr_fsr));
1406}
1407
1408/* Given a pointer to a floating point register set in /proc format
1409 (fpregset_t *), update the register specified by REGNO from gdb's idea
1410 of the current floating point register set. If REGNO is -1, update
1411 them all. */
1412/* ??? This will probably need some changes for sparc64. */
1413
1414void
1415fill_fpregset (fpregsetp, regno)
1416prfpregset_t *fpregsetp;
1417int regno;
1418{
1419 int regi;
1420 char *to;
1421 char *from;
1422
1423 for (regi = FP0_REGNUM ; regi < FP_MAX_REGNUM ; regi++)
1424 {
1425 if ((regno == -1) || (regno == regi))
1426 {
1427 from = (char *) &registers[REGISTER_BYTE (regi)];
1428 to = (char *) &fpregsetp->pr_fr.pr_regs[regi-FP0_REGNUM];
1429 memcpy (to, from, REGISTER_RAW_SIZE (regi));
1430 }
1431 }
1432 if ((regno == -1) || (regno == FPS_REGNUM))
1433 {
1434 fpregsetp->pr_fsr = *(int *) &registers[REGISTER_BYTE (FPS_REGNUM)];
1435 }
1436}
1437
1438#endif /* defined (FP0_REGNUM) */
1439
1440#endif /* USE_PROC_FS */
1441
1442
1443#ifdef GET_LONGJMP_TARGET
1444
1445/* Figure out where the longjmp will land. We expect that we have just entered
1446 longjmp and haven't yet setup the stack frame, so the args are still in the
1447 output regs. %o0 (O0_REGNUM) points at the jmp_buf structure from which we
1448 extract the pc (JB_PC) that we will land at. The pc is copied into ADDR.
1449 This routine returns true on success */
1450
1451int
1452get_longjmp_target (pc)
1453 CORE_ADDR *pc;
1454{
1455 CORE_ADDR jb_addr;
1456#define LONGJMP_TARGET_SIZE 4
1457 char buf[LONGJMP_TARGET_SIZE];
1458
1459 jb_addr = read_register (O0_REGNUM);
1460
1461 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
1462 LONGJMP_TARGET_SIZE))
1463 return 0;
1464
1465 *pc = extract_address (buf, LONGJMP_TARGET_SIZE);
1466
1467 return 1;
1468}
1469#endif /* GET_LONGJMP_TARGET */
1470\f
1471#ifdef STATIC_TRANSFORM_NAME
1472/* SunPRO (3.0 at least), encodes the static variables. This is not
1473 related to C++ mangling, it is done for C too. */
1474
1475char *
1476sunpro_static_transform_name (name)
1477 char *name;
1478{
1479 char *p;
1480 if (name[0] == '$')
1481 {
1482 /* For file-local statics there will be a dollar sign, a bunch
1483 of junk (the contents of which match a string given in the
1484 N_OPT), a period and the name. For function-local statics
1485 there will be a bunch of junk (which seems to change the
1486 second character from 'A' to 'B'), a period, the name of the
1487 function, and the name. So just skip everything before the
1488 last period. */
1489 p = strrchr (name, '.');
1490 if (p != NULL)
1491 name = p + 1;
1492 }
1493 return name;
1494}
1495#endif /* STATIC_TRANSFORM_NAME */
1496\f
1497
1498/* Utilities for printing registers.
1499 Page numbers refer to the SPARC Architecture Manual. */
1500
1501static void dump_ccreg PARAMS ((char *, int));
1502
1503static void
1504dump_ccreg (reg, val)
1505 char *reg;
1506 int val;
1507{
1508 /* page 41 */
1509 printf_unfiltered ("%s:%s,%s,%s,%s", reg,
1510 val & 8 ? "N" : "NN",
1511 val & 4 ? "Z" : "NZ",
1512 val & 2 ? "O" : "NO",
1513 val & 1 ? "C" : "NC"
1514 );
1515}
1516
1517static char *
1518decode_asi (val)
1519 int val;
1520{
1521 /* page 72 */
1522 switch (val)
1523 {
1524 case 4 : return "ASI_NUCLEUS";
1525 case 0x0c : return "ASI_NUCLEUS_LITTLE";
1526 case 0x10 : return "ASI_AS_IF_USER_PRIMARY";
1527 case 0x11 : return "ASI_AS_IF_USER_SECONDARY";
1528 case 0x18 : return "ASI_AS_IF_USER_PRIMARY_LITTLE";
1529 case 0x19 : return "ASI_AS_IF_USER_SECONDARY_LITTLE";
1530 case 0x80 : return "ASI_PRIMARY";
1531 case 0x81 : return "ASI_SECONDARY";
1532 case 0x82 : return "ASI_PRIMARY_NOFAULT";
1533 case 0x83 : return "ASI_SECONDARY_NOFAULT";
1534 case 0x88 : return "ASI_PRIMARY_LITTLE";
1535 case 0x89 : return "ASI_SECONDARY_LITTLE";
1536 case 0x8a : return "ASI_PRIMARY_NOFAULT_LITTLE";
1537 case 0x8b : return "ASI_SECONDARY_NOFAULT_LITTLE";
1538 default : return NULL;
1539 }
1540}
1541
1542/* PRINT_REGISTER_HOOK routine.
1543 Pretty print various registers. */
1544/* FIXME: Would be nice if this did some fancy things for 32 bit sparc. */
1545
1546void
1547sparc_print_register_hook (regno)
1548 int regno;
1549{
1550 ULONGEST val;
1551
1552 /* Handle double/quad versions of lower 32 fp regs. */
1553 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32
1554 && (regno & 1) == 0)
1555 {
1556 char value[16];
1557
1558 if (!read_relative_register_raw_bytes (regno, value)
1559 && !read_relative_register_raw_bytes (regno + 1, value + 4))
1560 {
1561 printf_unfiltered ("\t");
1562 print_floating (value, builtin_type_double, gdb_stdout);
1563 }
1564#if 0 /* FIXME: gdb doesn't handle long doubles */
1565 if ((regno & 3) == 0)
1566 {
1567 if (!read_relative_register_raw_bytes (regno + 2, value + 8)
1568 && !read_relative_register_raw_bytes (regno + 3, value + 12))
1569 {
1570 printf_unfiltered ("\t");
1571 print_floating (value, builtin_type_long_double, gdb_stdout);
1572 }
1573 }
1574#endif
1575 return;
1576 }
1577
1578#if 0 /* FIXME: gdb doesn't handle long doubles */
1579 /* Print upper fp regs as long double if appropriate. */
1580 if (regno >= FP0_REGNUM + 32 && regno < FP_MAX_REGNUM
1581 /* We test for even numbered regs and not a multiple of 4 because
1582 the upper fp regs are recorded as doubles. */
1583 && (regno & 1) == 0)
1584 {
1585 char value[16];
1586
1587 if (!read_relative_register_raw_bytes (regno, value)
1588 && !read_relative_register_raw_bytes (regno + 1, value + 8))
1589 {
1590 printf_unfiltered ("\t");
1591 print_floating (value, builtin_type_long_double, gdb_stdout);
1592 }
1593 return;
1594 }
1595#endif
1596
1597 /* FIXME: Some of these are priviledged registers.
1598 Not sure how they should be handled. */
1599
1600#define BITS(n, mask) ((int) (((val) >> (n)) & (mask)))
1601
1602 val = read_register (regno);
1603
1604 /* pages 40 - 60 */
1605 switch (regno)
1606 {
1607#ifdef GDB_TARGET_IS_SPARC64
1608 case CCR_REGNUM :
1609 printf_unfiltered("\t");
1610 dump_ccreg ("xcc", val >> 4);
1611 printf_unfiltered(", ");
1612 dump_ccreg ("icc", val & 15);
1613 break;
1614 case FPRS_REGNUM :
1615 printf ("\tfef:%d, du:%d, dl:%d",
1616 BITS (2, 1), BITS (1, 1), BITS (0, 1));
1617 break;
1618 case FSR_REGNUM :
1619 {
1620 static char *fcc[4] = { "=", "<", ">", "?" };
1621 static char *rd[4] = { "N", "0", "+", "-" };
1622 /* Long, yes, but I'd rather leave it as is and use a wide screen. */
1623 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",
1624 fcc[BITS (10, 3)], fcc[BITS (32, 3)],
1625 fcc[BITS (34, 3)], fcc[BITS (36, 3)],
1626 rd[BITS (30, 3)], BITS (23, 31), BITS (22, 1), BITS (17, 7),
1627 BITS (14, 7), BITS (13, 1), BITS (5, 31), BITS (0, 31));
1628 break;
1629 }
1630 case ASI_REGNUM :
1631 {
1632 char *asi = decode_asi (val);
1633 if (asi != NULL)
1634 printf ("\t%s", asi);
1635 break;
1636 }
1637 case VER_REGNUM :
1638 printf ("\tmanuf:%d, impl:%d, mask:%d, maxtl:%d, maxwin:%d",
1639 BITS (48, 0xffff), BITS (32, 0xffff),
1640 BITS (24, 0xff), BITS (8, 0xff), BITS (0, 31));
1641 break;
1642 case PSTATE_REGNUM :
1643 {
1644 static char *mm[4] = { "tso", "pso", "rso", "?" };
1645 printf ("\tcle:%d, tle:%d, mm:%s, red:%d, pef:%d, am:%d, priv:%d, ie:%d, ag:%d",
1646 BITS (9, 1), BITS (8, 1), mm[BITS (6, 3)], BITS (5, 1),
1647 BITS (4, 1), BITS (3, 1), BITS (2, 1), BITS (1, 1),
1648 BITS (0, 1));
1649 break;
1650 }
1651 case TSTATE_REGNUM :
1652 /* FIXME: print all 4? */
1653 break;
1654 case TT_REGNUM :
1655 /* FIXME: print all 4? */
1656 break;
1657 case TPC_REGNUM :
1658 /* FIXME: print all 4? */
1659 break;
1660 case TNPC_REGNUM :
1661 /* FIXME: print all 4? */
1662 break;
1663 case WSTATE_REGNUM :
1664 printf ("\tother:%d, normal:%d", BITS (3, 7), BITS (0, 7));
1665 break;
1666 case CWP_REGNUM :
1667 printf ("\t%d", BITS (0, 31));
1668 break;
1669 case CANSAVE_REGNUM :
1670 printf ("\t%-2d before spill", BITS (0, 31));
1671 break;
1672 case CANRESTORE_REGNUM :
1673 printf ("\t%-2d before fill", BITS (0, 31));
1674 break;
1675 case CLEANWIN_REGNUM :
1676 printf ("\t%-2d before clean", BITS (0, 31));
1677 break;
1678 case OTHERWIN_REGNUM :
1679 printf ("\t%d", BITS (0, 31));
1680 break;
1681#else
1682 case PS_REGNUM:
1683 printf ("\ticc:%c%c%c%c, pil:%d, s:%d, ps:%d, et:%d, cwp:%d",
1684 BITS (23, 1) ? 'N' : '-', BITS (22, 1) ? 'Z' : '-',
1685 BITS (21, 1) ? 'V' : '-', BITS (20, 1) ? 'C' : '-',
1686 BITS (8, 15), BITS (7, 1), BITS (6, 1), BITS (5, 1),
1687 BITS (0, 31));
1688 break;
1689 case FPS_REGNUM:
1690 {
1691 static char *fcc[4] = { "=", "<", ">", "?" };
1692 static char *rd[4] = { "N", "0", "+", "-" };
1693 /* Long, yes, but I'd rather leave it as is and use a wide screen. */
1694 printf ("\trd:%s, tem:%d, ns:%d, ver:%d, ftt:%d, qne:%d, "
1695 "fcc:%s, aexc:%d, cexc:%d",
1696 rd[BITS (30, 3)], BITS (23, 31), BITS (22, 1), BITS (17, 7),
1697 BITS (14, 7), BITS (13, 1), fcc[BITS (10, 3)], BITS (5, 31),
1698 BITS (0, 31));
1699 break;
1700 }
1701
1702#endif /* GDB_TARGET_IS_SPARC64 */
1703 }
1704
1705#undef BITS
1706}
1707\f
1708int
1709gdb_print_insn_sparc (memaddr, info)
1710 bfd_vma memaddr;
1711 disassemble_info *info;
1712{
1713 /* It's necessary to override mach again because print_insn messes it up. */
1714 info->mach = TM_PRINT_INSN_MACH;
1715 return print_insn_sparc (memaddr, info);
1716}
1717\f
1718/* The SPARC passes the arguments on the stack; arguments smaller
1719 than an int are promoted to an int. */
1720
1721CORE_ADDR
1722sparc_push_arguments (nargs, args, sp, struct_return, struct_addr)
1723 int nargs;
1724 value_ptr *args;
1725 CORE_ADDR sp;
1726 int struct_return;
1727 CORE_ADDR struct_addr;
1728{
1729 int i;
1730 int accumulate_size = 0;
1731 struct sparc_arg
1732 {
1733 char *contents;
1734 int len;
1735 int offset;
1736 };
1737 struct sparc_arg *sparc_args =
1738 (struct sparc_arg*)alloca (nargs * sizeof (struct sparc_arg));
1739 struct sparc_arg *m_arg;
1740
1741 /* Promote arguments if necessary, and calculate their stack offsets
1742 and sizes. */
1743 for (i = 0, m_arg = sparc_args; i < nargs; i++, m_arg++)
1744 {
1745 value_ptr arg = args[i];
1746 struct type *arg_type = check_typedef (VALUE_TYPE (arg));
1747 /* Cast argument to long if necessary as the compiler does it too. */
1748 switch (TYPE_CODE (arg_type))
1749 {
1750 case TYPE_CODE_INT:
1751 case TYPE_CODE_BOOL:
1752 case TYPE_CODE_CHAR:
1753 case TYPE_CODE_RANGE:
1754 case TYPE_CODE_ENUM:
1755 if (TYPE_LENGTH (arg_type) < TYPE_LENGTH (builtin_type_long))
1756 {
1757 arg_type = builtin_type_long;
1758 arg = value_cast (arg_type, arg);
1759 }
1760 break;
1761 default:
1762 break;
1763 }
1764 m_arg->len = TYPE_LENGTH (arg_type);
1765 m_arg->offset = accumulate_size;
1766 accumulate_size = (accumulate_size + m_arg->len + 3) & ~3;
1767 m_arg->contents = VALUE_CONTENTS(arg);
1768 }
1769
1770 /* Make room for the arguments on the stack. */
1771 accumulate_size += CALL_DUMMY_STACK_ADJUST;
1772 sp = ((sp - accumulate_size) & ~7) + CALL_DUMMY_STACK_ADJUST;
1773
1774 /* `Push' arguments on the stack. */
1775 for (i = nargs; m_arg--, --i >= 0; )
1776 write_memory(sp + m_arg->offset, m_arg->contents, m_arg->len);
1777
1778 return sp;
1779}
1780
1781
1782/* Extract from an array REGBUF containing the (raw) register state
1783 a function return value of type TYPE, and copy that, in virtual format,
1784 into VALBUF. */
1785
1786void
1787sparc_extract_return_value (type, regbuf, valbuf)
1788 struct type *type;
1789 char *regbuf;
1790 char *valbuf;
1791{
1792 int typelen = TYPE_LENGTH (type);
1793 int regsize = REGISTER_RAW_SIZE (O0_REGNUM);
1794
1795 if (TYPE_CODE (type) == TYPE_CODE_FLT && SPARC_HAS_FPU)
1796 memcpy (valbuf, &regbuf [REGISTER_BYTE (FP0_REGNUM)], typelen);
1797 else
1798 memcpy (valbuf,
1799 &regbuf [O0_REGNUM * regsize +
1800 (typelen >= regsize
1801 || TARGET_BYTE_ORDER == LITTLE_ENDIAN ? 0
1802 : regsize - typelen)],
1803 typelen);
1804}
1805
1806
1807/* Write into appropriate registers a function return value
1808 of type TYPE, given in virtual format. On SPARCs with FPUs,
1809 float values are returned in %f0 (and %f1). In all other cases,
1810 values are returned in register %o0. */
1811
1812void
1813sparc_store_return_value (type, valbuf)
1814 struct type *type;
1815 char *valbuf;
1816{
1817 int regno;
1818 char buffer[MAX_REGISTER_RAW_SIZE];
1819
1820 if (TYPE_CODE (type) == TYPE_CODE_FLT && SPARC_HAS_FPU)
1821 /* Floating-point values are returned in the register pair */
1822 /* formed by %f0 and %f1 (doubles are, anyway). */
1823 regno = FP0_REGNUM;
1824 else
1825 /* Other values are returned in register %o0. */
1826 regno = O0_REGNUM;
1827
1828 /* Add leading zeros to the value. */
1829 if (TYPE_LENGTH (type) < REGISTER_RAW_SIZE(regno))
1830 {
1831 bzero (buffer, REGISTER_RAW_SIZE(regno));
1832 memcpy (buffer + REGISTER_RAW_SIZE(regno) - TYPE_LENGTH (type), valbuf,
1833 TYPE_LENGTH (type));
1834 write_register_bytes (REGISTER_BYTE (regno), buffer,
1835 REGISTER_RAW_SIZE(regno));
1836 }
1837 else
1838 write_register_bytes (REGISTER_BYTE (regno), valbuf, TYPE_LENGTH (type));
1839}
1840
1841
1842/* Insert the function address into a call dummy instruction sequence
1843 stored at DUMMY.
1844
1845 For structs and unions, if the function was compiled with Sun cc,
1846 it expects 'unimp' after the call. But gcc doesn't use that
1847 (twisted) convention. So leave a nop there for gcc (FIX_CALL_DUMMY
1848 can assume it is operating on a pristine CALL_DUMMY, not one that
1849 has already been customized for a different function). */
1850
1851void
1852sparc_fix_call_dummy (dummy, pc, fun, value_type, using_gcc)
1853 char *dummy;
1854 CORE_ADDR pc;
1855 CORE_ADDR fun;
1856 struct type *value_type;
1857 int using_gcc;
1858{
1859 int i;
1860
1861 /* Store the relative adddress of the target function into the
1862 'call' instruction. */
1863 store_unsigned_integer (dummy + CALL_DUMMY_CALL_OFFSET, 4,
1864 (0x40000000
1865 | (((fun - (pc + CALL_DUMMY_CALL_OFFSET)) >> 2)
1866 & 0x3fffffff)));
1867
1868 /* Comply with strange Sun cc calling convention for struct-returning
1869 functions. */
1870 if (!using_gcc
1871 && (TYPE_CODE (value_type) == TYPE_CODE_STRUCT
1872 || TYPE_CODE (value_type) == TYPE_CODE_UNION))
1873 store_unsigned_integer (dummy + CALL_DUMMY_CALL_OFFSET + 8, 4,
1874 TYPE_LENGTH (value_type) & 0x1fff);
1875
1876#ifndef GDB_TARGET_IS_SPARC64
1877 /* If this is not a simulator target, change the first four instructions
1878 of the call dummy to NOPs. Those instructions include a 'save'
1879 instruction and are designed to work around problems with register
1880 window flushing in the simulator. */
1881 if (strcmp (target_shortname, "sim") != 0)
1882 {
1883 for (i = 0; i < 4; i++)
1884 store_unsigned_integer (dummy + (i * 4), 4, 0x01000000);
1885 }
1886#endif
1887
1888 /* If this is a bi-endian target, GDB has written the call dummy
1889 in little-endian order. We must byte-swap it back to big-endian. */
1890 if (bi_endian)
1891 {
1892 for (i = 0; i < CALL_DUMMY_LENGTH; i += 4)
1893 {
1894 char tmp = dummy [i];
1895 dummy [i] = dummy [i+3];
1896 dummy [i+3] = tmp;
1897 tmp = dummy [i+1];
1898 dummy [i+1] = dummy [i+2];
1899 dummy [i+2] = tmp;
1900 }
1901 }
1902}
1903
1904
1905/* Set target byte order based on machine type. */
1906
1907static int
1908sparc_target_architecture_hook (ap)
1909 const bfd_arch_info_type *ap;
1910{
1911 int i, j;
1912
1913 if (ap->mach == bfd_mach_sparc_sparclite_le)
1914 {
1915 if (TARGET_BYTE_ORDER_SELECTABLE_P)
1916 {
1917 target_byte_order = LITTLE_ENDIAN;
1918 bi_endian = 1;
1919 }
1920 else
1921 {
1922 warning ("This GDB does not support little endian sparclite.");
1923 }
1924 }
1925 else
1926 bi_endian = 0;
1927 return 1;
1928}
1929
1930\f
1931void
1932_initialize_sparc_tdep ()
1933{
1934 tm_print_insn = gdb_print_insn_sparc;
1935 tm_print_insn_info.mach = TM_PRINT_INSN_MACH; /* Selects sparc/sparclite */
1936 target_architecture_hook = sparc_target_architecture_hook;
1937}
1938
1939
1940#ifdef GDB_TARGET_IS_SPARC64
1941
1942/* Compensate for stack bias. Note that we currently don't handle mixed
1943 32/64 bit code. */
1944CORE_ADDR
1945sparc64_read_sp ()
1946{
1947 CORE_ADDR sp = read_register (SP_REGNUM);
1948
1949 if (sp & 1)
1950 sp += 2047;
1951 return sp;
1952}
1953
1954CORE_ADDR
1955sparc64_read_fp ()
1956{
1957 CORE_ADDR fp = read_register (FP_REGNUM);
1958
1959 if (fp & 1)
1960 fp += 2047;
1961 return fp;
1962}
1963
1964void
1965sparc64_write_sp (val)
1966 CORE_ADDR val;
1967{
1968 CORE_ADDR oldsp = read_register (SP_REGNUM);
1969 if (oldsp & 1)
1970 write_register (SP_REGNUM, val - 2047);
1971 else
1972 write_register (SP_REGNUM, val);
1973}
1974
1975void
1976sparc64_write_fp (val)
1977 CORE_ADDR val;
1978{
1979 CORE_ADDR oldfp = read_register (FP_REGNUM);
1980 if (oldfp & 1)
1981 write_register (FP_REGNUM, val - 2047);
1982 else
1983 write_register (FP_REGNUM, val);
1984}
1985
1986/* The SPARC 64 ABI passes floating-point arguments in FP0-31. They are
1987 also copied onto the stack in the correct places. */
1988
1989CORE_ADDR
1990sp64_push_arguments (nargs, args, sp, struct_return, struct_retaddr)
1991 int nargs;
1992 value_ptr *args;
1993 CORE_ADDR sp;
1994 unsigned char struct_return;
1995 CORE_ADDR struct_retaddr;
1996{
1997 int x;
1998 int regnum = 0;
1999 CORE_ADDR tempsp;
2000
2001 sp = (sp & ~(((unsigned long)TYPE_LENGTH (builtin_type_long)) - 1UL));
2002
2003 /* Figure out how much space we'll need. */
2004 for (x = nargs - 1; x >= 0; x--)
2005 {
2006 int len = TYPE_LENGTH (check_typedef (VALUE_TYPE (args[x])));
2007 value_ptr copyarg = args[x];
2008 int copylen = len;
2009
2010 /* This code is, of course, no longer correct. */
2011 if (copylen < TYPE_LENGTH (builtin_type_long))
2012 {
2013 copyarg = value_cast(builtin_type_long, copyarg);
2014 copylen = TYPE_LENGTH (builtin_type_long);
2015 }
2016 sp -= copylen;
2017 }
2018
2019 /* Round down. */
2020 sp = sp & ~7;
2021 tempsp = sp;
2022
2023 /* Now write the arguments onto the stack, while writing FP arguments
2024 into the FP registers. */
2025 for (x = 0; x < nargs; x++)
2026 {
2027 int len = TYPE_LENGTH (check_typedef (VALUE_TYPE (args[x])));
2028 value_ptr copyarg = args[x];
2029 int copylen = len;
2030
2031 /* This code is, of course, no longer correct. */
2032 if (copylen < TYPE_LENGTH (builtin_type_long))
2033 {
2034 copyarg = value_cast(builtin_type_long, copyarg);
2035 copylen = TYPE_LENGTH (builtin_type_long);
2036 }
2037 write_memory (tempsp, VALUE_CONTENTS (copyarg), copylen);
2038 tempsp += copylen;
2039 if (TYPE_CODE (VALUE_TYPE (args[x])) == TYPE_CODE_FLT && regnum < 32)
2040 {
2041 /* This gets copied into a FP register. */
2042 int nextreg = regnum + 2;
2043 char *data = VALUE_CONTENTS (args[x]);
2044 /* Floats go into the lower half of a FP register pair; quads
2045 use 2 pairs. */
2046
2047 if (len == 16)
2048 nextreg += 2;
2049 else if (len == 4)
2050 regnum++;
2051
2052 write_register_bytes (REGISTER_BYTE (FP0_REGNUM + regnum),
2053 data,
2054 len);
2055 regnum = nextreg;
2056 }
2057 }
2058 return sp;
2059}
2060
2061/* Values <= 32 bytes are returned in o0-o3 (floating-point values are
2062 returned in f0-f3). */
2063void
2064sparc64_extract_return_value (type, regbuf, valbuf, bitoffset)
2065 struct type *type;
2066 char *regbuf;
2067 char *valbuf;
2068 int bitoffset;
2069{
2070 int typelen = TYPE_LENGTH (type);
2071 int regsize = REGISTER_RAW_SIZE (O0_REGNUM);
2072
2073 if (TYPE_CODE (type) == TYPE_CODE_FLT && SPARC_HAS_FPU)
2074 {
2075 memcpy (valbuf, &regbuf [REGISTER_BYTE (FP0_REGNUM)], typelen);
2076 return;
2077 }
2078
2079 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2080 || (TYPE_LENGTH (type) > 32))
2081 {
2082 memcpy (valbuf,
2083 &regbuf [O0_REGNUM * regsize +
2084 (typelen >= regsize ? 0 : regsize - typelen)],
2085 typelen);
2086 return;
2087 }
2088 else
2089 {
2090 char *o0 = &regbuf[O0_REGNUM * regsize];
2091 char *f0 = &regbuf[FP0_REGNUM * regsize];
2092 int x;
2093
2094 for (x = 0; x < TYPE_NFIELDS (type); x++)
2095 {
2096 struct field *f = &TYPE_FIELDS(type)[x];
2097 /* FIXME: We may need to handle static fields here. */
2098 int whichreg = (f->loc.bitpos + bitoffset) / 32;
2099 int remainder = ((f->loc.bitpos + bitoffset) % 32) / 8;
2100 int where = (f->loc.bitpos + bitoffset) / 8;
2101 int size = TYPE_LENGTH (f->type);
2102 int typecode = TYPE_CODE (f->type);
2103
2104 if (typecode == TYPE_CODE_STRUCT)
2105 {
2106 sparc64_extract_return_value (f->type,
2107 regbuf,
2108 valbuf,
2109 bitoffset + f->loc.bitpos);
2110 }
2111 else if (typecode == TYPE_CODE_FLT)
2112 {
2113 memcpy (valbuf + where, &f0[whichreg * 4] + remainder, size);
2114 }
2115 else
2116 {
2117 memcpy (valbuf + where, &o0[whichreg * 4] + remainder, size);
2118 }
2119 }
2120 }
2121}
2122#endif