1 /* Machine-dependent code which would otherwise be in inflow.c and core.c,
2 for GDB, the GNU debugger. This code is for the HP PA-RISC cpu.
3 Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
5 Contributed by the Center for Software Science at the
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 /* For argument passing to the inferior */
33 #include <sys/types.h>
36 #include <sys/param.h>
39 #include <sys/ioctl.h>
41 #ifdef COFF_ENCAPSULATE
42 #include "a.out.encap.h"
47 #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
50 /*#include <sys/user.h> After a.out.h */
53 #include <machine/psl.h>
62 static int restore_pc_queue PARAMS ((struct frame_saved_regs *fsr));
63 static int hppa_alignof PARAMS ((struct type *arg));
64 CORE_ADDR frame_saved_pc PARAMS ((FRAME frame));
65 static int prologue_inst_adjust_sp PARAMS ((unsigned long));
66 static int is_branch PARAMS ((unsigned long));
67 static int inst_saves_gr PARAMS ((unsigned long));
68 static int inst_saves_fr PARAMS ((unsigned long));
71 /* Routines to extract various sized constants out of hppa
74 /* This assumes that no garbage lies outside of the lower bits of
78 sign_extend (val, bits)
81 return (int)(val >> bits - 1 ? (-1 << bits) | val : val);
84 /* For many immediate values the sign bit is the low bit! */
87 low_sign_extend (val, bits)
90 return (int)((val & 0x1 ? (-1 << (bits - 1)) : 0) | val >> 1);
92 /* extract the immediate field from a ld{bhw}s instruction */
95 get_field (val, from, to)
96 unsigned val, from, to;
99 return val & ((1 << 32 - from) - 1);
103 set_field (val, from, to, new_val)
104 unsigned *val, from, to;
106 unsigned mask = ~((1 << (to - from + 1)) << (31 - from));
107 return *val = *val & mask | (new_val << (31 - from));
110 /* extract a 3-bit space register number from a be, ble, mtsp or mfsp */
115 return GET_FIELD (word, 18, 18) << 2 | GET_FIELD (word, 16, 17);
118 extract_5_load (word)
121 return low_sign_extend (word >> 16 & MASK_5, 5);
124 /* extract the immediate field from a st{bhw}s instruction */
127 extract_5_store (word)
130 return low_sign_extend (word & MASK_5, 5);
133 /* extract the immediate field from a break instruction */
136 extract_5r_store (word)
139 return (word & MASK_5);
142 /* extract the immediate field from a {sr}sm instruction */
145 extract_5R_store (word)
148 return (word >> 16 & MASK_5);
151 /* extract an 11 bit immediate field */
157 return low_sign_extend (word & MASK_11, 11);
160 /* extract a 14 bit immediate field */
166 return low_sign_extend (word & MASK_14, 14);
169 /* deposit a 14 bit constant in a word */
172 deposit_14 (opnd, word)
176 unsigned sign = (opnd < 0 ? 1 : 0);
178 return word | ((unsigned)opnd << 1 & MASK_14) | sign;
181 /* extract a 21 bit constant */
191 val = GET_FIELD (word, 20, 20);
193 val |= GET_FIELD (word, 9, 19);
195 val |= GET_FIELD (word, 5, 6);
197 val |= GET_FIELD (word, 0, 4);
199 val |= GET_FIELD (word, 7, 8);
200 return sign_extend (val, 21) << 11;
203 /* deposit a 21 bit constant in a word. Although 21 bit constants are
204 usually the top 21 bits of a 32 bit constant, we assume that only
205 the low 21 bits of opnd are relevant */
208 deposit_21 (opnd, word)
213 val |= GET_FIELD (opnd, 11 + 14, 11 + 18);
215 val |= GET_FIELD (opnd, 11 + 12, 11 + 13);
217 val |= GET_FIELD (opnd, 11 + 19, 11 + 20);
219 val |= GET_FIELD (opnd, 11 + 1, 11 + 11);
221 val |= GET_FIELD (opnd, 11 + 0, 11 + 0);
225 /* extract a 12 bit constant from branch instructions */
231 return sign_extend (GET_FIELD (word, 19, 28) |
232 GET_FIELD (word, 29, 29) << 10 |
233 (word & 0x1) << 11, 12) << 2;
236 /* extract a 17 bit constant from branch instructions, returning the
237 19 bit signed value. */
243 return sign_extend (GET_FIELD (word, 19, 28) |
244 GET_FIELD (word, 29, 29) << 10 |
245 GET_FIELD (word, 11, 15) << 11 |
246 (word & 0x1) << 16, 17) << 2;
249 /* Lookup the unwind (stack backtrace) info for the given PC. We search all
250 of the objfiles seeking the unwind table entry for this PC. Each objfile
251 contains a sorted list of struct unwind_table_entry. Since we do a binary
252 search of the unwind tables, we depend upon them to be sorted. */
254 static struct unwind_table_entry *
255 find_unwind_entry(pc)
258 int first, middle, last;
259 struct objfile *objfile;
261 ALL_OBJFILES (objfile)
263 struct obj_unwind_info *ui;
265 ui = OBJ_UNWIND_INFO (objfile);
270 /* First, check the cache */
273 && pc >= ui->cache->region_start
274 && pc <= ui->cache->region_end)
277 /* Not in the cache, do a binary search */
282 while (first <= last)
284 middle = (first + last) / 2;
285 if (pc >= ui->table[middle].region_start
286 && pc <= ui->table[middle].region_end)
288 ui->cache = &ui->table[middle];
289 return &ui->table[middle];
292 if (pc < ui->table[middle].region_start)
297 } /* ALL_OBJFILES() */
301 /* Called when no unwind descriptor was found for PC. Returns 1 if it
302 appears that PC is in a linker stub. */
303 static int pc_in_linker_stub PARAMS ((CORE_ADDR));
306 pc_in_linker_stub (pc)
309 int found_magic_instruction = 0;
313 /* If unable to read memory, assume pc is not in a linker stub. */
314 if (target_read_memory (pc, buf, 4) != 0)
317 /* We are looking for something like
319 ; $$dyncall jams RP into this special spot in the frame (RP')
320 ; before calling the "call stub"
323 ldsid (rp),r1 ; Get space associated with RP into r1
324 mtsp r1,sp ; Move it into space register 0
325 be,n 0(sr0),rp) ; back to your regularly scheduled program
328 /* Maximum known linker stub size is 4 instructions. Search forward
329 from the given PC, then backward. */
330 for (i = 0; i < 4; i++)
332 /* If we hit something with an unwind, stop searching this direction. */
334 if (find_unwind_entry (pc + i * 4) != 0)
337 /* Check for ldsid (rp),r1 which is the magic instruction for a
338 return from a cross-space function call. */
339 if (read_memory_integer (pc + i * 4, 4) == 0x004010a1)
341 found_magic_instruction = 1;
344 /* Add code to handle long call/branch and argument relocation stubs
348 if (found_magic_instruction != 0)
351 /* Now look backward. */
352 for (i = 0; i < 4; i++)
354 /* If we hit something with an unwind, stop searching this direction. */
356 if (find_unwind_entry (pc - i * 4) != 0)
359 /* Check for ldsid (rp),r1 which is the magic instruction for a
360 return from a cross-space function call. */
361 if (read_memory_integer (pc - i * 4, 4) == 0x004010a1)
363 found_magic_instruction = 1;
366 /* Add code to handle long call/branch and argument relocation stubs
369 return found_magic_instruction;
373 find_return_regnum(pc)
376 struct unwind_table_entry *u;
378 u = find_unwind_entry (pc);
389 /* Return size of frame, or -1 if we should use a frame pointer. */
391 find_proc_framesize(pc)
394 struct unwind_table_entry *u;
396 u = find_unwind_entry (pc);
400 if (pc_in_linker_stub (pc))
401 /* Linker stubs have a zero size frame. */
408 /* If this bit is set, it means there is a frame pointer and we should
412 return u->Total_frame_size << 3;
415 /* Return offset from sp at which rp is saved, or 0 if not saved. */
416 static int rp_saved PARAMS ((CORE_ADDR));
422 struct unwind_table_entry *u;
424 u = find_unwind_entry (pc);
428 if (pc_in_linker_stub (pc))
429 /* This is the so-called RP'. */
442 frameless_function_invocation (frame)
445 struct unwind_table_entry *u;
447 u = find_unwind_entry (frame->pc);
450 return frameless_look_for_prologue (frame);
452 return (u->Total_frame_size == 0);
456 saved_pc_after_call (frame)
461 ret_regnum = find_return_regnum (get_frame_pc (frame));
463 return read_register (ret_regnum) & ~0x3;
467 frame_saved_pc (frame)
470 CORE_ADDR pc = get_frame_pc (frame);
472 if (frameless_function_invocation (frame))
476 ret_regnum = find_return_regnum (pc);
478 return read_register (ret_regnum) & ~0x3;
482 int rp_offset = rp_saved (pc);
485 return read_register (RP_REGNUM) & ~0x3;
487 return read_memory_integer (frame->frame + rp_offset, 4) & ~0x3;
491 /* We need to correct the PC and the FP for the outermost frame when we are
495 init_extra_frame_info (fromleaf, frame)
497 struct frame_info *frame;
502 if (frame->next && !fromleaf)
505 /* If the next frame represents a frameless function invocation
506 then we have to do some adjustments that are normally done by
507 FRAME_CHAIN. (FRAME_CHAIN is not called in this case.) */
510 /* Find the framesize of *this* frame without peeking at the PC
511 in the current frame structure (it isn't set yet). */
512 framesize = find_proc_framesize (FRAME_SAVED_PC (get_next_frame (frame)));
514 /* Now adjust our base frame accordingly. If we have a frame pointer
515 use it, else subtract the size of this frame from the current
516 frame. (we always want frame->frame to point at the lowest address
519 frame->frame = read_register (FP_REGNUM);
521 frame->frame -= framesize;
525 flags = read_register (FLAGS_REGNUM);
526 if (flags & 2) /* In system call? */
527 frame->pc = read_register (31) & ~0x3;
529 /* The outermost frame is always derived from PC-framesize
531 One might think frameless innermost frames should have
532 a frame->frame that is the same as the parent's frame->frame.
533 That is wrong; frame->frame in that case should be the *high*
534 address of the parent's frame. It's complicated as hell to
535 explain, but the parent *always* creates some stack space for
536 the child. So the child actually does have a frame of some
537 sorts, and its base is the high address in its parent's frame. */
538 framesize = find_proc_framesize(frame->pc);
540 frame->frame = read_register (FP_REGNUM);
542 frame->frame = read_register (SP_REGNUM) - framesize;
545 /* Given a GDB frame, determine the address of the calling function's frame.
546 This will be used to create a new GDB frame struct, and then
547 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
549 This may involve searching through prologues for several functions
550 at boundaries where GCC calls HP C code, or where code which has
551 a frame pointer calls code without a frame pointer. */
556 struct frame_info *frame;
558 int my_framesize, caller_framesize;
559 struct unwind_table_entry *u;
561 /* Get frame sizes for the current frame and the frame of the
563 my_framesize = find_proc_framesize (frame->pc);
564 caller_framesize = find_proc_framesize (FRAME_SAVED_PC(frame));
566 /* If caller does not have a frame pointer, then its frame
567 can be found at current_frame - caller_framesize. */
568 if (caller_framesize != -1)
569 return frame->frame - caller_framesize;
571 /* Both caller and callee have frame pointers and are GCC compiled
572 (SAVE_SP bit in unwind descriptor is on for both functions.
573 The previous frame pointer is found at the top of the current frame. */
574 if (caller_framesize == -1 && my_framesize == -1)
575 return read_memory_integer (frame->frame, 4);
577 /* Caller has a frame pointer, but callee does not. This is a little
578 more difficult as GCC and HP C lay out locals and callee register save
579 areas very differently.
581 The previous frame pointer could be in a register, or in one of
582 several areas on the stack.
584 Walk from the current frame to the innermost frame examining
585 unwind descriptors to determine if %r3 ever gets saved into the
586 stack. If so return whatever value got saved into the stack.
587 If it was never saved in the stack, then the value in %r3 is still
590 We use information from unwind descriptors to determine if %r3
591 is saved into the stack (Entry_GR field has this information). */
595 u = find_unwind_entry (frame->pc);
599 /* We could find this information by examining prologues. I don't
600 think anyone has actually written any tools (not even "strip")
601 which leave them out of an executable, so maybe this is a moot
603 warning ("Unable to find unwind for PC 0x%x -- Help!", frame->pc);
607 /* Entry_GR specifies the number of callee-saved general registers
608 saved in the stack. It starts at %r3, so %r3 would be 1. */
609 if (u->Entry_GR >= 1 || u->Save_SP)
617 /* We may have walked down the chain into a function with a frame
620 return read_memory_integer (frame->frame, 4);
621 /* %r3 was saved somewhere in the stack. Dig it out. */
624 struct frame_info *fi;
625 struct frame_saved_regs saved_regs;
627 fi = get_frame_info (frame);
628 get_frame_saved_regs (fi, &saved_regs);
629 return read_memory_integer (saved_regs.regs[FP_REGNUM], 4);
634 /* The value in %r3 was never saved into the stack (thus %r3 still
635 holds the value of the previous frame pointer). */
636 return read_register (FP_REGNUM);
641 /* To see if a frame chain is valid, see if the caller looks like it
642 was compiled with gcc. */
645 frame_chain_valid (chain, thisframe)
649 struct minimal_symbol *msym_us;
650 struct minimal_symbol *msym_start;
651 struct unwind_table_entry *u;
656 u = find_unwind_entry (thisframe->pc);
658 /* We can't just check that the same of msym_us is "_start", because
659 someone idiotically decided that they were going to make a Ltext_end
660 symbol with the same address. This Ltext_end symbol is totally
661 indistinguishable (as nearly as I can tell) from the symbol for a function
662 which is (legitimately, since it is in the user's namespace)
663 named Ltext_end, so we can't just ignore it. */
664 msym_us = lookup_minimal_symbol_by_pc (FRAME_SAVED_PC (thisframe));
665 msym_start = lookup_minimal_symbol ("_start", NULL);
668 && SYMBOL_VALUE_ADDRESS (msym_us) == SYMBOL_VALUE_ADDRESS (msym_start))
674 if (u->Save_SP || u->Total_frame_size)
677 if (pc_in_linker_stub (thisframe->pc))
684 * These functions deal with saving and restoring register state
685 * around a function call in the inferior. They keep the stack
686 * double-word aligned; eventually, on an hp700, the stack will have
687 * to be aligned to a 64-byte boundary.
693 register CORE_ADDR sp;
698 /* Space for "arguments"; the RP goes in here. */
699 sp = read_register (SP_REGNUM) + 48;
700 int_buffer = read_register (RP_REGNUM) | 0x3;
701 write_memory (sp - 20, (char *)&int_buffer, 4);
703 int_buffer = read_register (FP_REGNUM);
704 write_memory (sp, (char *)&int_buffer, 4);
706 write_register (FP_REGNUM, sp);
710 for (regnum = 1; regnum < 32; regnum++)
711 if (regnum != RP_REGNUM && regnum != FP_REGNUM)
712 sp = push_word (sp, read_register (regnum));
716 for (regnum = FP0_REGNUM; regnum < NUM_REGS; regnum++)
718 read_register_bytes (REGISTER_BYTE (regnum), (char *)&freg_buffer, 8);
719 sp = push_bytes (sp, (char *)&freg_buffer, 8);
721 sp = push_word (sp, read_register (IPSW_REGNUM));
722 sp = push_word (sp, read_register (SAR_REGNUM));
723 sp = push_word (sp, read_register (PCOQ_HEAD_REGNUM));
724 sp = push_word (sp, read_register (PCSQ_HEAD_REGNUM));
725 sp = push_word (sp, read_register (PCOQ_TAIL_REGNUM));
726 sp = push_word (sp, read_register (PCSQ_TAIL_REGNUM));
727 write_register (SP_REGNUM, sp);
730 find_dummy_frame_regs (frame, frame_saved_regs)
731 struct frame_info *frame;
732 struct frame_saved_regs *frame_saved_regs;
734 CORE_ADDR fp = frame->frame;
737 frame_saved_regs->regs[RP_REGNUM] = fp - 20 & ~0x3;
738 frame_saved_regs->regs[FP_REGNUM] = fp;
739 frame_saved_regs->regs[1] = fp + 8;
741 for (fp += 12, i = 3; i < 32; i++)
745 frame_saved_regs->regs[i] = fp;
751 for (i = FP0_REGNUM; i < NUM_REGS; i++, fp += 8)
752 frame_saved_regs->regs[i] = fp;
754 frame_saved_regs->regs[IPSW_REGNUM] = fp;
755 frame_saved_regs->regs[SAR_REGNUM] = fp + 4;
756 frame_saved_regs->regs[PCOQ_HEAD_REGNUM] = fp + 8;
757 frame_saved_regs->regs[PCSQ_HEAD_REGNUM] = fp + 12;
758 frame_saved_regs->regs[PCOQ_TAIL_REGNUM] = fp + 16;
759 frame_saved_regs->regs[PCSQ_TAIL_REGNUM] = fp + 20;
765 register FRAME frame = get_current_frame ();
766 register CORE_ADDR fp;
768 struct frame_saved_regs fsr;
769 struct frame_info *fi;
772 fi = get_frame_info (frame);
774 get_frame_saved_regs (fi, &fsr);
776 if (fsr.regs[IPSW_REGNUM]) /* Restoring a call dummy frame */
777 restore_pc_queue (&fsr);
779 for (regnum = 31; regnum > 0; regnum--)
780 if (fsr.regs[regnum])
781 write_register (regnum, read_memory_integer (fsr.regs[regnum], 4));
783 for (regnum = NUM_REGS - 1; regnum >= FP0_REGNUM ; regnum--)
784 if (fsr.regs[regnum])
786 read_memory (fsr.regs[regnum], (char *)&freg_buffer, 8);
787 write_register_bytes (REGISTER_BYTE (regnum), (char *)&freg_buffer, 8);
790 if (fsr.regs[IPSW_REGNUM])
791 write_register (IPSW_REGNUM,
792 read_memory_integer (fsr.regs[IPSW_REGNUM], 4));
794 if (fsr.regs[SAR_REGNUM])
795 write_register (SAR_REGNUM,
796 read_memory_integer (fsr.regs[SAR_REGNUM], 4));
798 /* If the PC was explicitly saved, then just restore it. */
799 if (fsr.regs[PCOQ_TAIL_REGNUM])
800 write_register (PCOQ_TAIL_REGNUM,
801 read_memory_integer (fsr.regs[PCOQ_TAIL_REGNUM], 4));
803 /* Else use the value in %rp to set the new PC. */
805 target_write_pc (read_register (RP_REGNUM));
807 write_register (FP_REGNUM, read_memory_integer (fp, 4));
809 if (fsr.regs[IPSW_REGNUM]) /* call dummy */
810 write_register (SP_REGNUM, fp - 48);
812 write_register (SP_REGNUM, fp);
814 flush_cached_frames ();
815 set_current_frame (create_new_frame (read_register (FP_REGNUM),
820 * After returning to a dummy on the stack, restore the instruction
821 * queue space registers. */
824 restore_pc_queue (fsr)
825 struct frame_saved_regs *fsr;
827 CORE_ADDR pc = read_pc ();
828 CORE_ADDR new_pc = read_memory_integer (fsr->regs[PCOQ_HEAD_REGNUM], 4);
830 struct target_waitstatus w;
833 /* Advance past break instruction in the call dummy. */
834 write_register (PCOQ_HEAD_REGNUM, pc + 4);
835 write_register (PCOQ_TAIL_REGNUM, pc + 8);
838 * HPUX doesn't let us set the space registers or the space
839 * registers of the PC queue through ptrace. Boo, hiss.
840 * Conveniently, the call dummy has this sequence of instructions
845 * So, load up the registers and single step until we are in the
849 write_register (21, read_memory_integer (fsr->regs[PCSQ_HEAD_REGNUM], 4));
850 write_register (22, new_pc);
852 for (insn_count = 0; insn_count < 3; insn_count++)
854 /* FIXME: What if the inferior gets a signal right now? Want to
855 merge this into wait_for_inferior (as a special kind of
856 watchpoint? By setting a breakpoint at the end? Is there
857 any other choice? Is there *any* way to do this stuff with
858 ptrace() or some equivalent?). */
860 target_wait (inferior_pid, &w);
862 if (w.kind == TARGET_WAITKIND_SIGNALLED)
864 stop_signal = w.value.sig;
865 terminal_ours_for_output ();
866 printf_unfiltered ("\nProgram terminated with signal %s, %s.\n",
867 target_signal_to_name (stop_signal),
868 target_signal_to_string (stop_signal));
869 gdb_flush (gdb_stdout);
873 target_terminal_ours ();
874 fetch_inferior_registers (-1);
879 hppa_push_arguments (nargs, args, sp, struct_return, struct_addr)
884 CORE_ADDR struct_addr;
886 /* array of arguments' offsets */
887 int *offset = (int *)alloca(nargs * sizeof (int));
891 for (i = 0; i < nargs; i++)
893 /* Coerce chars to int & float to double if necessary */
894 args[i] = value_arg_coerce (args[i]);
896 cum += TYPE_LENGTH (VALUE_TYPE (args[i]));
898 /* value must go at proper alignment. Assume alignment is a
900 alignment = hppa_alignof (VALUE_TYPE (args[i]));
902 cum = (cum + alignment) & -alignment;
905 sp += max ((cum + 7) & -8, 16);
907 for (i = 0; i < nargs; i++)
908 write_memory (sp + offset[i], VALUE_CONTENTS (args[i]),
909 TYPE_LENGTH (VALUE_TYPE (args[i])));
912 write_register (28, struct_addr);
917 * Insert the specified number of args and function address
918 * into a call sequence of the above form stored at DUMMYNAME.
920 * On the hppa we need to call the stack dummy through $$dyncall.
921 * Therefore our version of FIX_CALL_DUMMY takes an extra argument,
922 * real_pc, which is the location where gdb should start up the
923 * inferior to do the function call.
927 hppa_fix_call_dummy (dummy, pc, fun, nargs, args, type, gcc_p)
936 CORE_ADDR dyncall_addr, sr4export_addr;
937 struct minimal_symbol *msymbol;
938 int flags = read_register (FLAGS_REGNUM);
940 msymbol = lookup_minimal_symbol ("$$dyncall", (struct objfile *) NULL);
942 error ("Can't find an address for $$dyncall trampoline");
944 dyncall_addr = SYMBOL_VALUE_ADDRESS (msymbol);
946 msymbol = lookup_minimal_symbol ("_sr4export", (struct objfile *) NULL);
948 error ("Can't find an address for _sr4export trampoline");
950 sr4export_addr = SYMBOL_VALUE_ADDRESS (msymbol);
952 store_unsigned_integer
953 (&dummy[9*REGISTER_SIZE],
955 deposit_21 (fun >> 11,
956 extract_unsigned_integer (&dummy[9*REGISTER_SIZE],
958 store_unsigned_integer
959 (&dummy[10*REGISTER_SIZE],
961 deposit_14 (fun & MASK_11,
962 extract_unsigned_integer (&dummy[10*REGISTER_SIZE],
964 store_unsigned_integer
965 (&dummy[12*REGISTER_SIZE],
967 deposit_21 (sr4export_addr >> 11,
968 extract_unsigned_integer (&dummy[12*REGISTER_SIZE],
970 store_unsigned_integer
971 (&dummy[13*REGISTER_SIZE],
973 deposit_14 (sr4export_addr & MASK_11,
974 extract_unsigned_integer (&dummy[13*REGISTER_SIZE],
977 write_register (22, pc);
979 /* If we are in a syscall, then we should call the stack dummy
980 directly. $$dyncall is not needed as the kernel sets up the
981 space id registers properly based on the value in %r31. In
982 fact calling $$dyncall will not work because the value in %r22
983 will be clobbered on the syscall exit path. */
991 /* Get the PC from %r31 if currently in a syscall. Also mask out privilege
996 int flags = read_register (FLAGS_REGNUM);
999 return read_register (31) & ~0x3;
1000 return read_register (PC_REGNUM) & ~0x3;
1003 /* Write out the PC. If currently in a syscall, then also write the new
1004 PC value into %r31. */
1009 int flags = read_register (FLAGS_REGNUM);
1011 /* If in a syscall, then set %r31. Also make sure to get the
1012 privilege bits set correctly. */
1014 write_register (31, (long) (v | 0x3));
1016 write_register (PC_REGNUM, (long) v);
1017 write_register (NPC_REGNUM, (long) v + 4);
1020 /* return the alignment of a type in bytes. Structures have the maximum
1021 alignment required by their fields. */
1027 int max_align, align, i;
1028 switch (TYPE_CODE (arg))
1033 return TYPE_LENGTH (arg);
1034 case TYPE_CODE_ARRAY:
1035 return hppa_alignof (TYPE_FIELD_TYPE (arg, 0));
1036 case TYPE_CODE_STRUCT:
1037 case TYPE_CODE_UNION:
1039 for (i = 0; i < TYPE_NFIELDS (arg); i++)
1041 /* Bit fields have no real alignment. */
1042 if (!TYPE_FIELD_BITPOS (arg, i))
1044 align = hppa_alignof (TYPE_FIELD_TYPE (arg, i));
1045 max_align = max (max_align, align);
1054 /* Print the register regnum, or all registers if regnum is -1 */
1056 pa_do_registers_info (regnum, fpregs)
1060 char raw_regs [REGISTER_BYTES];
1063 for (i = 0; i < NUM_REGS; i++)
1064 read_relative_register_raw_bytes (i, raw_regs + REGISTER_BYTE (i));
1066 pa_print_registers (raw_regs, regnum, fpregs);
1067 else if (regnum < FP0_REGNUM)
1068 printf_unfiltered ("%s %x\n", reg_names[regnum], *(long *)(raw_regs +
1069 REGISTER_BYTE (regnum)));
1071 pa_print_fp_reg (regnum);
1074 pa_print_registers (raw_regs, regnum, fpregs)
1081 for (i = 0; i < 18; i++)
1082 printf_unfiltered ("%8.8s: %8x %8.8s: %8x %8.8s: %8x %8.8s: %8x\n",
1084 *(int *)(raw_regs + REGISTER_BYTE (i)),
1086 *(int *)(raw_regs + REGISTER_BYTE (i + 18)),
1088 *(int *)(raw_regs + REGISTER_BYTE (i + 36)),
1090 *(int *)(raw_regs + REGISTER_BYTE (i + 54)));
1093 for (i = 72; i < NUM_REGS; i++)
1094 pa_print_fp_reg (i);
1100 unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
1101 unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
1103 /* Get the data in raw format. */
1104 read_relative_register_raw_bytes (i, raw_buffer);
1106 /* Convert raw data to virtual format if necessary. */
1107 #ifdef REGISTER_CONVERTIBLE
1108 if (REGISTER_CONVERTIBLE (i))
1110 REGISTER_CONVERT_TO_VIRTUAL (i, REGISTER_VIRTUAL_TYPE (i),
1111 raw_buffer, virtual_buffer);
1115 memcpy (virtual_buffer, raw_buffer,
1116 REGISTER_VIRTUAL_SIZE (i));
1118 fputs_filtered (reg_names[i], gdb_stdout);
1119 print_spaces_filtered (15 - strlen (reg_names[i]), gdb_stdout);
1121 val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, gdb_stdout, 0,
1122 1, 0, Val_pretty_default);
1123 printf_filtered ("\n");
1126 /* Function calls that pass into a new compilation unit must pass through a
1127 small piece of code that does long format (`external' in HPPA parlance)
1128 jumps. We figure out where the trampoline is going to end up, and return
1129 the PC of the final destination. If we aren't in a trampoline, we just
1132 For computed calls, we just extract the new PC from r22. */
1135 skip_trampoline_code (pc, name)
1140 static CORE_ADDR dyncall = 0;
1141 struct minimal_symbol *msym;
1143 /* FIXME XXX - dyncall must be initialized whenever we get a new exec file */
1147 msym = lookup_minimal_symbol ("$$dyncall", NULL);
1149 dyncall = SYMBOL_VALUE_ADDRESS (msym);
1155 return (CORE_ADDR)(read_register (22) & ~0x3);
1157 inst0 = read_memory_integer (pc, 4);
1158 inst1 = read_memory_integer (pc+4, 4);
1160 if ( (inst0 & 0xffe00000) == 0x20200000 /* ldil xxx, r1 */
1161 && (inst1 & 0xffe0e002) == 0xe0202002) /* be,n yyy(sr4, r1) */
1162 pc = extract_21 (inst0) + extract_17 (inst1);
1164 pc = (CORE_ADDR)NULL;
1169 /* For the given instruction (INST), return any adjustment it makes
1170 to the stack pointer or zero for no adjustment.
1172 This only handles instructions commonly found in prologues. */
1175 prologue_inst_adjust_sp (inst)
1178 /* This must persist across calls. */
1179 static int save_high21;
1181 /* The most common way to perform a stack adjustment ldo X(sp),sp */
1182 if ((inst & 0xffffc000) == 0x37de0000)
1183 return extract_14 (inst);
1186 if ((inst & 0xffe00000) == 0x6fc00000)
1187 return extract_14 (inst);
1189 /* addil high21,%r1; ldo low11,(%r1),%r30)
1190 save high bits in save_high21 for later use. */
1191 if ((inst & 0xffe00000) == 0x28200000)
1193 save_high21 = extract_21 (inst);
1197 if ((inst & 0xffff0000) == 0x343e0000)
1198 return save_high21 + extract_14 (inst);
1200 /* fstws as used by the HP compilers. */
1201 if ((inst & 0xffffffe0) == 0x2fd01220)
1202 return extract_5_load (inst);
1204 /* No adjustment. */
1208 /* Return nonzero if INST is a branch of some kind, else return zero. */
1238 /* Return the register number for a GR which is saved by INST or
1239 zero it INST does not save a GR.
1241 Note we only care about full 32bit register stores (that's the only
1242 kind of stores the prologue will use). */
1245 inst_saves_gr (inst)
1248 /* Does it look like a stw? */
1249 if ((inst >> 26) == 0x1a)
1250 return extract_5R_store (inst);
1252 /* Does it look like a stwm? */
1253 if ((inst >> 26) == 0x1b)
1254 return extract_5R_store (inst);
1259 /* Return the register number for a FR which is saved by INST or
1260 zero it INST does not save a FR.
1262 Note we only care about full 64bit register stores (that's the only
1263 kind of stores the prologue will use). */
1266 inst_saves_fr (inst)
1269 if ((inst & 0xfc1fffe0) == 0x2c101220)
1270 return extract_5r_store (inst);
1274 /* Advance PC across any function entry prologue instructions
1275 to reach some "real" code.
1277 Use information in the unwind table to determine what exactly should
1278 be in the prologue. */
1285 unsigned long inst, stack_remaining, save_gr, save_fr, save_rp, save_sp;
1287 struct unwind_table_entry *u;
1289 u = find_unwind_entry (pc);
1293 /* This is how much of a frame adjustment we need to account for. */
1294 stack_remaining = u->Total_frame_size << 3;
1296 /* Magic register saves we want to know about. */
1297 save_rp = u->Save_RP;
1298 save_sp = u->Save_SP;
1300 /* Turn the Entry_GR field into a bitmask. */
1302 for (i = 3; i < u->Entry_GR + 3; i++)
1304 /* Frame pointer gets saved into a special location. */
1305 if (u->Save_SP && i == FP_REGNUM)
1308 save_gr |= (1 << i);
1311 /* Turn the Entry_FR field into a bitmask too. */
1313 for (i = 12; i < u->Entry_FR + 12; i++)
1314 save_fr |= (1 << i);
1316 /* Loop until we find everything of interest or hit a branch.
1318 For unoptimized GCC code and for any HP CC code this will never ever
1319 examine any user instructions.
1321 For optimzied GCC code we're faced with problems. GCC will schedule
1322 its prologue and make prologue instructions available for delay slot
1323 filling. The end result is user code gets mixed in with the prologue
1324 and a prologue instruction may be in the delay slot of the first branch
1327 Some unexpected things are expected with debugging optimized code, so
1328 we allow this routine to walk past user instructions in optimized
1330 while (save_gr || save_fr || save_rp || save_sp || stack_remaining > 0)
1332 status = target_read_memory (pc, buf, 4);
1333 inst = extract_unsigned_integer (buf, 4);
1339 /* Note the interesting effects of this instruction. */
1340 stack_remaining -= prologue_inst_adjust_sp (inst);
1342 /* There is only one instruction used for saving RP into the stack. */
1343 if (inst == 0x6bc23fd9)
1346 /* This is the only way we save SP into the stack. At this time
1347 the HP compilers never bother to save SP into the stack. */
1348 if ((inst & 0xffffc000) == 0x6fc10000)
1351 /* Account for general and floating-point register saves. */
1352 save_gr &= ~(1 << inst_saves_gr (inst));
1353 save_fr &= ~(1 << inst_saves_fr (inst));
1355 /* Quit if we hit any kind of branch. This can happen if a prologue
1356 instruction is in the delay slot of the first call/branch. */
1357 if (is_branch (inst))
1367 /* Put here the code to store, into a struct frame_saved_regs,
1368 the addresses of the saved registers of frame described by FRAME_INFO.
1369 This includes special registers such as pc and fp saved in special
1370 ways in the stack frame. sp is even more special:
1371 the address we return for it IS the sp for the next frame. */
1374 hppa_frame_find_saved_regs (frame_info, frame_saved_regs)
1375 struct frame_info *frame_info;
1376 struct frame_saved_regs *frame_saved_regs;
1379 struct unwind_table_entry *u;
1380 unsigned long inst, stack_remaining, save_gr, save_fr, save_rp, save_sp;
1385 /* Zero out everything. */
1386 memset (frame_saved_regs, '\0', sizeof (struct frame_saved_regs));
1388 /* Call dummy frames always look the same, so there's no need to
1389 examine the dummy code to determine locations of saved registers;
1390 instead, let find_dummy_frame_regs fill in the correct offsets
1391 for the saved registers. */
1392 if ((frame_info->pc >= frame_info->frame
1393 && frame_info->pc <= (frame_info->frame + CALL_DUMMY_LENGTH
1394 + 32 * 4 + (NUM_REGS - FP0_REGNUM) * 8
1396 find_dummy_frame_regs (frame_info, frame_saved_regs);
1398 /* Get the starting address of the function referred to by the PC
1399 saved in frame_info. */
1400 pc = get_pc_function_start (frame_info->pc);
1403 u = find_unwind_entry (pc);
1407 /* This is how much of a frame adjustment we need to account for. */
1408 stack_remaining = u->Total_frame_size << 3;
1410 /* Magic register saves we want to know about. */
1411 save_rp = u->Save_RP;
1412 save_sp = u->Save_SP;
1414 /* Turn the Entry_GR field into a bitmask. */
1416 for (i = 3; i < u->Entry_GR + 3; i++)
1418 /* Frame pointer gets saved into a special location. */
1419 if (u->Save_SP && i == FP_REGNUM)
1422 save_gr |= (1 << i);
1425 /* Turn the Entry_FR field into a bitmask too. */
1427 for (i = 12; i < u->Entry_FR + 12; i++)
1428 save_fr |= (1 << i);
1430 /* Loop until we find everything of interest or hit a branch.
1432 For unoptimized GCC code and for any HP CC code this will never ever
1433 examine any user instructions.
1435 For optimzied GCC code we're faced with problems. GCC will schedule
1436 its prologue and make prologue instructions available for delay slot
1437 filling. The end result is user code gets mixed in with the prologue
1438 and a prologue instruction may be in the delay slot of the first branch
1441 Some unexpected things are expected with debugging optimized code, so
1442 we allow this routine to walk past user instructions in optimized
1444 while (save_gr || save_fr || save_rp || save_sp || stack_remaining > 0)
1446 status = target_read_memory (pc, buf, 4);
1447 inst = extract_unsigned_integer (buf, 4);
1453 /* Note the interesting effects of this instruction. */
1454 stack_remaining -= prologue_inst_adjust_sp (inst);
1456 /* There is only one instruction used for saving RP into the stack. */
1457 if (inst == 0x6bc23fd9)
1460 frame_saved_regs->regs[RP_REGNUM] = frame_info->frame - 20;
1463 /* This is the only way we save SP into the stack. At this time
1464 the HP compilers never bother to save SP into the stack. */
1465 if ((inst & 0xffffc000) == 0x6fc10000)
1468 frame_saved_regs->regs[SP_REGNUM] = frame_info->frame;
1471 /* Account for general and floating-point register saves. */
1472 reg = inst_saves_gr (inst);
1473 if (reg >= 3 && reg <= 18
1474 && (!u->Save_SP || reg != FP_REGNUM))
1476 save_gr &= ~(1 << reg);
1478 /* stwm with a positive displacement is a *post modify*. */
1479 if ((inst >> 26) == 0x1b
1480 && extract_14 (inst) >= 0)
1481 frame_saved_regs->regs[reg] = frame_info->frame;
1484 /* Handle code with and without frame pointers. */
1486 frame_saved_regs->regs[reg]
1487 = frame_info->frame + extract_14 (inst);
1489 frame_saved_regs->regs[reg]
1490 = frame_info->frame + (u->Total_frame_size << 3)
1491 + extract_14 (inst);
1496 /* GCC handles callee saved FP regs a little differently.
1498 It emits an instruction to put the value of the start of
1499 the FP store area into %r1. It then uses fstds,ma with
1500 a basereg of %r1 for the stores.
1502 HP CC emits them at the current stack pointer modifying
1503 the stack pointer as it stores each register. */
1505 /* ldo X(%r3),%r1 or ldo X(%r30),%r1. */
1506 if ((inst & 0xffffc000) == 0x34610000
1507 || (inst & 0xffffc000) == 0x37c10000)
1508 fp_loc = extract_14 (inst);
1510 reg = inst_saves_fr (inst);
1511 if (reg >= 12 && reg <= 21)
1513 /* Note +4 braindamage below is necessary because the FP status
1514 registers are internally 8 registers rather than the expected
1516 save_fr &= ~(1 << reg);
1519 /* 1st HP CC FP register store. After this instruction
1520 we've set enough state that the GCC and HPCC code are
1521 both handled in the same manner. */
1522 frame_saved_regs->regs[reg + FP4_REGNUM + 4] = frame_info->frame;
1527 frame_saved_regs->regs[reg + FP0_REGNUM + 4]
1528 = frame_info->frame + fp_loc;
1533 /* Quit if we hit any kind of branch. This can happen if a prologue
1534 instruction is in the delay slot of the first call/branch. */
1535 if (is_branch (inst))
1543 #ifdef MAINTENANCE_CMDS
1546 unwind_command (exp, from_tty)
1554 struct unwind_table_entry *u;
1557 /* If we have an expression, evaluate it and use it as the address. */
1559 if (exp != 0 && *exp != 0)
1560 address = parse_and_eval_address (exp);
1564 xxx.u = find_unwind_entry (address);
1568 printf_unfiltered ("Can't find unwind table entry for PC 0x%x\n", address);
1572 printf_unfiltered ("%08x\n%08X\n%08X\n%08X\n", xxx.foo[0], xxx.foo[1], xxx.foo[2],
1575 #endif /* MAINTENANCE_CMDS */
1578 _initialize_hppa_tdep ()
1580 #ifdef MAINTENANCE_CMDS
1581 add_cmd ("unwind", class_maintenance, unwind_command,
1582 "Print unwind table entry at given address.",
1583 &maintenanceprintlist);
1584 #endif /* MAINTENANCE_CMDS */