home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / gdb-4.12-src.lha / src / amiga / gdb-4.12 / gdb / hppa-tdep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  41.7 KB  |  1,598 lines

  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.
  4.  
  5.    Contributed by the Center for Software Science at the
  6.    University of Utah (pa-gdb-bugs@cs.utah.edu).
  7.  
  8. This file is part of GDB.
  9.  
  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.
  14.  
  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.
  19.  
  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.  */
  23.  
  24. #include "defs.h"
  25. #include "frame.h"
  26. #include "inferior.h"
  27. #include "value.h"
  28.  
  29. /* For argument passing to the inferior */
  30. #include "symtab.h"
  31.  
  32. #ifdef USG
  33. #include <sys/types.h>
  34. #endif
  35.  
  36. #include <sys/param.h>
  37. #include <sys/dir.h>
  38. #include <signal.h>
  39. #include <sys/ioctl.h>
  40.  
  41. #ifdef COFF_ENCAPSULATE
  42. #include "a.out.encap.h"
  43. #else
  44. #include <a.out.h>
  45. #endif
  46. #ifndef N_SET_MAGIC
  47. #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
  48. #endif
  49.  
  50. /*#include <sys/user.h>        After a.out.h  */
  51. #include <sys/file.h>
  52. #include <sys/stat.h>
  53. #include <machine/psl.h>
  54. #include "wait.h"
  55.  
  56. #include "gdbcore.h"
  57. #include "gdbcmd.h"
  58. #include "target.h"
  59. #include "symfile.h"
  60. #include "objfiles.h"
  61.  
  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));
  69.  
  70.  
  71. /* Routines to extract various sized constants out of hppa 
  72.    instructions. */
  73.  
  74. /* This assumes that no garbage lies outside of the lower bits of 
  75.    value. */
  76.  
  77. int
  78. sign_extend (val, bits)
  79.      unsigned val, bits;
  80. {
  81.   return (int)(val >> bits - 1 ? (-1 << bits) | val : val);
  82. }
  83.  
  84. /* For many immediate values the sign bit is the low bit! */
  85.  
  86. int
  87. low_sign_extend (val, bits)
  88.      unsigned val, bits;
  89. {
  90.   return (int)((val & 0x1 ? (-1 << (bits - 1)) : 0) | val >> 1);
  91. }
  92. /* extract the immediate field from a ld{bhw}s instruction */
  93.  
  94. unsigned
  95. get_field (val, from, to)
  96.      unsigned val, from, to;
  97. {
  98.   val = val >> 31 - to;
  99.   return val & ((1 << 32 - from) - 1);
  100. }
  101.  
  102. unsigned
  103. set_field (val, from, to, new_val)
  104.      unsigned *val, from, to;
  105. {
  106.   unsigned mask = ~((1 << (to - from + 1)) << (31 - from));
  107.   return *val = *val & mask | (new_val << (31 - from));
  108. }
  109.  
  110. /* extract a 3-bit space register number from a be, ble, mtsp or mfsp */
  111.  
  112. extract_3 (word)
  113.      unsigned word;
  114. {
  115.   return GET_FIELD (word, 18, 18) << 2 | GET_FIELD (word, 16, 17);
  116. }
  117.        
  118. extract_5_load (word)
  119.      unsigned word;
  120. {
  121.   return low_sign_extend (word >> 16 & MASK_5, 5);
  122. }
  123.  
  124. /* extract the immediate field from a st{bhw}s instruction */
  125.  
  126. int
  127. extract_5_store (word)
  128.      unsigned word;
  129. {
  130.   return low_sign_extend (word & MASK_5, 5);
  131. }
  132.  
  133. /* extract the immediate field from a break instruction */
  134.  
  135. unsigned
  136. extract_5r_store (word)
  137.      unsigned word;
  138. {
  139.   return (word & MASK_5);
  140. }
  141.  
  142. /* extract the immediate field from a {sr}sm instruction */
  143.  
  144. unsigned
  145. extract_5R_store (word)
  146.      unsigned word;
  147. {
  148.   return (word >> 16 & MASK_5);
  149. }
  150.  
  151. /* extract an 11 bit immediate field */
  152.  
  153. int
  154. extract_11 (word)
  155.      unsigned word;
  156. {
  157.   return low_sign_extend (word & MASK_11, 11);
  158. }
  159.  
  160. /* extract a 14 bit immediate field */
  161.  
  162. int
  163. extract_14 (word)
  164.      unsigned word;
  165. {
  166.   return low_sign_extend (word & MASK_14, 14);
  167. }
  168.  
  169. /* deposit a 14 bit constant in a word */
  170.  
  171. unsigned
  172. deposit_14 (opnd, word)
  173.      int opnd;
  174.      unsigned word;
  175. {
  176.   unsigned sign = (opnd < 0 ? 1 : 0);
  177.  
  178.   return word | ((unsigned)opnd << 1 & MASK_14)  | sign;
  179. }
  180.  
  181. /* extract a 21 bit constant */
  182.  
  183. int
  184. extract_21 (word)
  185.      unsigned word;
  186. {
  187.   int val;
  188.  
  189.   word &= MASK_21;
  190.   word <<= 11;
  191.   val = GET_FIELD (word, 20, 20);
  192.   val <<= 11;
  193.   val |= GET_FIELD (word, 9, 19);
  194.   val <<= 2;
  195.   val |= GET_FIELD (word, 5, 6);
  196.   val <<= 5;
  197.   val |= GET_FIELD (word, 0, 4);
  198.   val <<= 2;
  199.   val |= GET_FIELD (word, 7, 8);
  200.   return sign_extend (val, 21) << 11;
  201. }
  202.  
  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 */
  206.  
  207. unsigned
  208. deposit_21 (opnd, word)
  209.      unsigned opnd, word;
  210. {
  211.   unsigned val = 0;
  212.  
  213.   val |= GET_FIELD (opnd, 11 + 14, 11 + 18);
  214.   val <<= 2;
  215.   val |= GET_FIELD (opnd, 11 + 12, 11 + 13);
  216.   val <<= 2;
  217.   val |= GET_FIELD (opnd, 11 + 19, 11 + 20);
  218.   val <<= 11;
  219.   val |= GET_FIELD (opnd, 11 + 1, 11 + 11);
  220.   val <<= 1;
  221.   val |= GET_FIELD (opnd, 11 + 0, 11 + 0);
  222.   return word | val;
  223. }
  224.  
  225. /* extract a 12 bit constant from branch instructions */
  226.  
  227. int
  228. extract_12 (word)
  229.      unsigned word;
  230. {
  231.   return sign_extend (GET_FIELD (word, 19, 28) |
  232.               GET_FIELD (word, 29, 29) << 10 |
  233.               (word & 0x1) << 11, 12) << 2;
  234. }
  235.  
  236. /* extract a 17 bit constant from branch instructions, returning the
  237.    19 bit signed value. */
  238.  
  239. int
  240. extract_17 (word)
  241.      unsigned word;
  242. {
  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;
  247. }
  248.  
  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.  */
  253.  
  254. static struct unwind_table_entry *
  255. find_unwind_entry(pc)
  256.      CORE_ADDR pc;
  257. {
  258.   int first, middle, last;
  259.   struct objfile *objfile;
  260.  
  261.   ALL_OBJFILES (objfile)
  262.     {
  263.       struct obj_unwind_info *ui;
  264.  
  265.       ui = OBJ_UNWIND_INFO (objfile);
  266.  
  267.       if (!ui)
  268.     continue;
  269.  
  270.       /* First, check the cache */
  271.  
  272.       if (ui->cache
  273.       && pc >= ui->cache->region_start
  274.       && pc <= ui->cache->region_end)
  275.     return ui->cache;
  276.  
  277.       /* Not in the cache, do a binary search */
  278.  
  279.       first = 0;
  280.       last = ui->last;
  281.  
  282.       while (first <= last)
  283.     {
  284.       middle = (first + last) / 2;
  285.       if (pc >= ui->table[middle].region_start
  286.           && pc <= ui->table[middle].region_end)
  287.         {
  288.           ui->cache = &ui->table[middle];
  289.           return &ui->table[middle];
  290.         }
  291.  
  292.       if (pc < ui->table[middle].region_start)
  293.         last = middle - 1;
  294.       else
  295.         first = middle + 1;
  296.     }
  297.     }                /* ALL_OBJFILES() */
  298.   return NULL;
  299. }
  300.  
  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));
  304.  
  305. static int
  306. pc_in_linker_stub (pc)
  307.      CORE_ADDR pc;
  308. {
  309.   int found_magic_instruction = 0;
  310.   int i;
  311.   char buf[4];
  312.  
  313.   /* If unable to read memory, assume pc is not in a linker stub.  */
  314.   if (target_read_memory (pc, buf, 4) != 0)
  315.     return 0;
  316.  
  317.   /* We are looking for something like
  318.  
  319.      ; $$dyncall jams RP into this special spot in the frame (RP')
  320.      ; before calling the "call stub"
  321.      ldw     -18(sp),rp
  322.  
  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
  326.      */
  327.  
  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++)
  331.     {
  332.       /* If we hit something with an unwind, stop searching this direction.  */
  333.  
  334.       if (find_unwind_entry (pc + i * 4) != 0)
  335.     break;
  336.  
  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)
  340.     {
  341.       found_magic_instruction = 1;
  342.       break;
  343.     }
  344.       /* Add code to handle long call/branch and argument relocation stubs
  345.      here.  */
  346.     }
  347.  
  348.   if (found_magic_instruction != 0)
  349.     return 1;
  350.  
  351.   /* Now look backward.  */
  352.   for (i = 0; i < 4; i++)
  353.     {
  354.       /* If we hit something with an unwind, stop searching this direction.  */
  355.  
  356.       if (find_unwind_entry (pc - i * 4) != 0)
  357.     break;
  358.  
  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)
  362.     {
  363.       found_magic_instruction = 1;
  364.       break;
  365.     }
  366.       /* Add code to handle long call/branch and argument relocation stubs
  367.      here.  */
  368.     }
  369.   return found_magic_instruction;
  370. }
  371.  
  372. static int
  373. find_return_regnum(pc)
  374.      CORE_ADDR pc;
  375. {
  376.   struct unwind_table_entry *u;
  377.  
  378.   u = find_unwind_entry (pc);
  379.  
  380.   if (!u)
  381.     return RP_REGNUM;
  382.  
  383.   if (u->Millicode)
  384.     return 31;
  385.  
  386.   return RP_REGNUM;
  387. }
  388.  
  389. /* Return size of frame, or -1 if we should use a frame pointer.  */
  390. int
  391. find_proc_framesize(pc)
  392.      CORE_ADDR pc;
  393. {
  394.   struct unwind_table_entry *u;
  395.  
  396.   u = find_unwind_entry (pc);
  397.  
  398.   if (!u)
  399.     {
  400.       if (pc_in_linker_stub (pc))
  401.     /* Linker stubs have a zero size frame.  */
  402.     return 0;
  403.       else
  404.     return -1;
  405.     }
  406.  
  407.   if (u->Save_SP)
  408.     /* If this bit is set, it means there is a frame pointer and we should
  409.        use it.  */
  410.     return -1;
  411.  
  412.   return u->Total_frame_size << 3;
  413. }
  414.  
  415. /* Return offset from sp at which rp is saved, or 0 if not saved.  */
  416. static int rp_saved PARAMS ((CORE_ADDR));
  417.  
  418. static int
  419. rp_saved (pc)
  420.      CORE_ADDR pc;
  421. {
  422.   struct unwind_table_entry *u;
  423.  
  424.   u = find_unwind_entry (pc);
  425.  
  426.   if (!u)
  427.     {
  428.       if (pc_in_linker_stub (pc))
  429.     /* This is the so-called RP'.  */
  430.     return -24;
  431.       else
  432.     return 0;
  433.     }
  434.  
  435.   if (u->Save_RP)
  436.     return -20;
  437.   else if (u->stub_type != 0)
  438.     {
  439.       switch (u->stub_type)
  440.     {
  441.     case EXPORT:
  442.       return -24;
  443.     case PARAMETER_RELOCATION:
  444.       return -8;
  445.     default:
  446.       return 0;
  447.     }
  448.     }
  449.   else
  450.     return 0;
  451. }
  452.  
  453. int
  454. frameless_function_invocation (frame)
  455.      FRAME frame;
  456. {
  457.   struct unwind_table_entry *u;
  458.  
  459.   u = find_unwind_entry (frame->pc);
  460.  
  461.   if (u == 0)
  462.     return frameless_look_for_prologue (frame);
  463.  
  464.   return (u->Total_frame_size == 0 && u->stub_type == 0);
  465. }
  466.  
  467. CORE_ADDR
  468. saved_pc_after_call (frame)
  469.      FRAME frame;
  470. {
  471.   int ret_regnum;
  472.  
  473.   ret_regnum = find_return_regnum (get_frame_pc (frame));
  474.  
  475.   return read_register (ret_regnum) & ~0x3;
  476. }
  477.  
  478. CORE_ADDR
  479. frame_saved_pc (frame)
  480.      FRAME frame;
  481. {
  482.   CORE_ADDR pc = get_frame_pc (frame);
  483.  
  484.   if (frameless_function_invocation (frame))
  485.     {
  486.       int ret_regnum;
  487.  
  488.       ret_regnum = find_return_regnum (pc);
  489.  
  490.       return read_register (ret_regnum) & ~0x3;
  491.     }
  492.   else
  493.     {
  494.       int rp_offset = rp_saved (pc);
  495.  
  496.       if (rp_offset == 0)
  497.     return read_register (RP_REGNUM) & ~0x3;
  498.       else
  499.     return read_memory_integer (frame->frame + rp_offset, 4) & ~0x3;
  500.     }
  501. }
  502.  
  503. /* We need to correct the PC and the FP for the outermost frame when we are
  504.    in a system call.  */
  505.  
  506. void
  507. init_extra_frame_info (fromleaf, frame)
  508.      int fromleaf;
  509.      struct frame_info *frame;
  510. {
  511.   int flags;
  512.   int framesize;
  513.  
  514.   if (frame->next && !fromleaf)
  515.     return;
  516.  
  517.   /* If the next frame represents a frameless function invocation
  518.      then we have to do some adjustments that are normally done by
  519.      FRAME_CHAIN.  (FRAME_CHAIN is not called in this case.)  */
  520.   if (fromleaf)
  521.     {
  522.       /* Find the framesize of *this* frame without peeking at the PC
  523.      in the current frame structure (it isn't set yet).  */
  524.       framesize = find_proc_framesize (FRAME_SAVED_PC (get_next_frame (frame)));
  525.  
  526.       /* Now adjust our base frame accordingly.  If we have a frame pointer
  527.      use it, else subtract the size of this frame from the current
  528.      frame.  (we always want frame->frame to point at the lowest address
  529.      in the frame).  */
  530.       if (framesize == -1)
  531.     frame->frame = read_register (FP_REGNUM);
  532.       else
  533.     frame->frame -= framesize;
  534.       return;
  535.     }
  536.  
  537.   flags = read_register (FLAGS_REGNUM);
  538.   if (flags & 2)    /* In system call? */
  539.     frame->pc = read_register (31) & ~0x3;
  540.  
  541.   /* The outermost frame is always derived from PC-framesize
  542.  
  543.      One might think frameless innermost frames should have
  544.      a frame->frame that is the same as the parent's frame->frame.
  545.      That is wrong; frame->frame in that case should be the *high*
  546.      address of the parent's frame.  It's complicated as hell to
  547.      explain, but the parent *always* creates some stack space for
  548.      the child.  So the child actually does have a frame of some
  549.      sorts, and its base is the high address in its parent's frame.  */
  550.   framesize = find_proc_framesize(frame->pc);
  551.   if (framesize == -1)
  552.     frame->frame = read_register (FP_REGNUM);
  553.   else
  554.     frame->frame = read_register (SP_REGNUM) - framesize;
  555. }
  556.  
  557. /* Given a GDB frame, determine the address of the calling function's frame.
  558.    This will be used to create a new GDB frame struct, and then
  559.    INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
  560.  
  561.    This may involve searching through prologues for several functions
  562.    at boundaries where GCC calls HP C code, or where code which has
  563.    a frame pointer calls code without a frame pointer.  */
  564.   
  565.  
  566. FRAME_ADDR
  567. frame_chain (frame)
  568.      struct frame_info *frame;
  569. {
  570.   int my_framesize, caller_framesize;
  571.   struct unwind_table_entry *u;
  572.  
  573.   /* Get frame sizes for the current frame and the frame of the 
  574.      caller.  */
  575.   my_framesize = find_proc_framesize (frame->pc);
  576.   caller_framesize = find_proc_framesize (FRAME_SAVED_PC(frame));
  577.  
  578.   /* If caller does not have a frame pointer, then its frame
  579.      can be found at current_frame - caller_framesize.  */
  580.   if (caller_framesize != -1)
  581.     return frame->frame - caller_framesize;
  582.  
  583.   /* Both caller and callee have frame pointers and are GCC compiled
  584.      (SAVE_SP bit in unwind descriptor is on for both functions.
  585.      The previous frame pointer is found at the top of the current frame.  */
  586.   if (caller_framesize == -1 && my_framesize == -1)
  587.     return read_memory_integer (frame->frame, 4);
  588.  
  589.   /* Caller has a frame pointer, but callee does not.  This is a little
  590.      more difficult as GCC and HP C lay out locals and callee register save
  591.      areas very differently.
  592.  
  593.      The previous frame pointer could be in a register, or in one of 
  594.      several areas on the stack.
  595.  
  596.      Walk from the current frame to the innermost frame examining 
  597.      unwind descriptors to determine if %r3 ever gets saved into the
  598.      stack.  If so return whatever value got saved into the stack.
  599.      If it was never saved in the stack, then the value in %r3 is still
  600.      valid, so use it. 
  601.  
  602.      We use information from unwind descriptors to determine if %r3
  603.      is saved into the stack (Entry_GR field has this information).  */
  604.  
  605.   while (frame)
  606.     {
  607.       u = find_unwind_entry (frame->pc);
  608.  
  609.       if (!u)
  610.     {
  611.       /* We could find this information by examining prologues.  I don't
  612.          think anyone has actually written any tools (not even "strip")
  613.          which leave them out of an executable, so maybe this is a moot
  614.          point.  */
  615.       warning ("Unable to find unwind for PC 0x%x -- Help!", frame->pc);
  616.       return 0;
  617.     }
  618.  
  619.       /* Entry_GR specifies the number of callee-saved general registers
  620.      saved in the stack.  It starts at %r3, so %r3 would be 1.  */
  621.       if (u->Entry_GR >= 1 || u->Save_SP)
  622.     break;
  623.       else
  624.     frame = frame->next;
  625.     }
  626.  
  627.   if (frame)
  628.     {
  629.       /* We may have walked down the chain into a function with a frame
  630.      pointer.  */
  631.       if (u->Save_SP)
  632.     return read_memory_integer (frame->frame, 4);
  633.       /* %r3 was saved somewhere in the stack.  Dig it out.  */
  634.       else 
  635.     {
  636.       struct frame_info *fi;
  637.       struct frame_saved_regs saved_regs;
  638.  
  639.       fi = get_frame_info (frame);
  640.       get_frame_saved_regs (fi, &saved_regs);
  641.       return read_memory_integer (saved_regs.regs[FP_REGNUM], 4);
  642.     }
  643.     }
  644.   else
  645.     {
  646.       /* The value in %r3 was never saved into the stack (thus %r3 still
  647.      holds the value of the previous frame pointer).  */
  648.       return read_register (FP_REGNUM);
  649.     }
  650. }
  651.  
  652.  
  653. /* To see if a frame chain is valid, see if the caller looks like it
  654.    was compiled with gcc. */
  655.  
  656. int
  657. frame_chain_valid (chain, thisframe)
  658.      FRAME_ADDR chain;
  659.      FRAME thisframe;
  660. {
  661.   struct minimal_symbol *msym_us;
  662.   struct minimal_symbol *msym_start;
  663.   struct unwind_table_entry *u;
  664.  
  665.   if (!chain)
  666.     return 0;
  667.  
  668.   u = find_unwind_entry (thisframe->pc);
  669.  
  670.   /* We can't just check that the same of msym_us is "_start", because
  671.      someone idiotically decided that they were going to make a Ltext_end
  672.      symbol with the same address.  This Ltext_end symbol is totally
  673.      indistinguishable (as nearly as I can tell) from the symbol for a function
  674.      which is (legitimately, since it is in the user's namespace)
  675.      named Ltext_end, so we can't just ignore it.  */
  676.   msym_us = lookup_minimal_symbol_by_pc (FRAME_SAVED_PC (thisframe));
  677.   msym_start = lookup_minimal_symbol ("_start", NULL);
  678.   if (msym_us
  679.       && msym_start
  680.       && SYMBOL_VALUE_ADDRESS (msym_us) == SYMBOL_VALUE_ADDRESS (msym_start))
  681.     return 0;
  682.  
  683.   if (u == NULL)
  684.     return 1;
  685.  
  686.   if (u->Save_SP || u->Total_frame_size || u->stub_type != 0)
  687.     return 1;
  688.  
  689.   if (pc_in_linker_stub (thisframe->pc))
  690.     return 1;
  691.  
  692.   return 0;
  693. }
  694.  
  695. /*
  696.  * These functions deal with saving and restoring register state
  697.  * around a function call in the inferior. They keep the stack
  698.  * double-word aligned; eventually, on an hp700, the stack will have
  699.  * to be aligned to a 64-byte boundary.
  700.  */
  701.  
  702. int
  703. push_dummy_frame ()
  704. {
  705.   register CORE_ADDR sp;
  706.   register int regnum;
  707.   int int_buffer;
  708.   double freg_buffer;
  709.  
  710.   /* Space for "arguments"; the RP goes in here. */
  711.   sp = read_register (SP_REGNUM) + 48;
  712.   int_buffer = read_register (RP_REGNUM) | 0x3;
  713.   write_memory (sp - 20, (char *)&int_buffer, 4);
  714.  
  715.   int_buffer = read_register (FP_REGNUM);
  716.   write_memory (sp, (char *)&int_buffer, 4);
  717.  
  718.   write_register (FP_REGNUM, sp);
  719.  
  720.   sp += 8;
  721.  
  722.   for (regnum = 1; regnum < 32; regnum++)
  723.     if (regnum != RP_REGNUM && regnum != FP_REGNUM)
  724.       sp = push_word (sp, read_register (regnum));
  725.  
  726.   sp += 4;
  727.  
  728.   for (regnum = FP0_REGNUM; regnum < NUM_REGS; regnum++)
  729.     {
  730.       read_register_bytes (REGISTER_BYTE (regnum), (char *)&freg_buffer, 8);
  731.       sp = push_bytes (sp, (char *)&freg_buffer, 8);
  732.     }
  733.   sp = push_word (sp, read_register (IPSW_REGNUM));
  734.   sp = push_word (sp, read_register (SAR_REGNUM));
  735.   sp = push_word (sp, read_register (PCOQ_HEAD_REGNUM));
  736.   sp = push_word (sp, read_register (PCSQ_HEAD_REGNUM));
  737.   sp = push_word (sp, read_register (PCOQ_TAIL_REGNUM));
  738.   sp = push_word (sp, read_register (PCSQ_TAIL_REGNUM));
  739.   write_register (SP_REGNUM, sp);
  740. }
  741.  
  742. find_dummy_frame_regs (frame, frame_saved_regs)
  743.      struct frame_info *frame;
  744.      struct frame_saved_regs *frame_saved_regs;
  745. {
  746.   CORE_ADDR fp = frame->frame;
  747.   int i;
  748.  
  749.   frame_saved_regs->regs[RP_REGNUM] = fp - 20 & ~0x3;
  750.   frame_saved_regs->regs[FP_REGNUM] = fp;
  751.   frame_saved_regs->regs[1] = fp + 8;
  752.  
  753.   for (fp += 12, i = 3; i < 32; i++)
  754.     {
  755.       if (i != FP_REGNUM)
  756.     {
  757.       frame_saved_regs->regs[i] = fp;
  758.       fp += 4;
  759.     }
  760.     }
  761.  
  762.   fp += 4;
  763.   for (i = FP0_REGNUM; i < NUM_REGS; i++, fp += 8)
  764.     frame_saved_regs->regs[i] = fp;
  765.  
  766.   frame_saved_regs->regs[IPSW_REGNUM] = fp;
  767.   frame_saved_regs->regs[SAR_REGNUM] = fp + 4;
  768.   frame_saved_regs->regs[PCOQ_HEAD_REGNUM] = fp + 8;
  769.   frame_saved_regs->regs[PCSQ_HEAD_REGNUM] = fp + 12;
  770.   frame_saved_regs->regs[PCOQ_TAIL_REGNUM] = fp + 16;
  771.   frame_saved_regs->regs[PCSQ_TAIL_REGNUM] = fp + 20;
  772. }
  773.  
  774. int
  775. hppa_pop_frame ()
  776. {
  777.   register FRAME frame = get_current_frame ();
  778.   register CORE_ADDR fp;
  779.   register int regnum;
  780.   struct frame_saved_regs fsr;
  781.   struct frame_info *fi;
  782.   double freg_buffer;
  783.  
  784.   fi = get_frame_info (frame);
  785.   fp = fi->frame;
  786.   get_frame_saved_regs (fi, &fsr);
  787.  
  788.   if (fsr.regs[IPSW_REGNUM])    /* Restoring a call dummy frame */
  789.     restore_pc_queue (&fsr);
  790.  
  791.   for (regnum = 31; regnum > 0; regnum--)
  792.     if (fsr.regs[regnum])
  793.       write_register (regnum, read_memory_integer (fsr.regs[regnum], 4));
  794.  
  795.   for (regnum = NUM_REGS - 1; regnum >= FP0_REGNUM ; regnum--)
  796.     if (fsr.regs[regnum])
  797.       {
  798.     read_memory (fsr.regs[regnum], (char *)&freg_buffer, 8);
  799.         write_register_bytes (REGISTER_BYTE (regnum), (char *)&freg_buffer, 8);
  800.       }
  801.  
  802.   if (fsr.regs[IPSW_REGNUM])
  803.     write_register (IPSW_REGNUM,
  804.                     read_memory_integer (fsr.regs[IPSW_REGNUM], 4));
  805.  
  806.   if (fsr.regs[SAR_REGNUM])
  807.     write_register (SAR_REGNUM,
  808.                     read_memory_integer (fsr.regs[SAR_REGNUM], 4));
  809.  
  810.   /* If the PC was explicitly saved, then just restore it.  */
  811.   if (fsr.regs[PCOQ_TAIL_REGNUM])
  812.     write_register (PCOQ_TAIL_REGNUM,
  813.                     read_memory_integer (fsr.regs[PCOQ_TAIL_REGNUM], 4));
  814.  
  815.   /* Else use the value in %rp to set the new PC.  */
  816.   else 
  817.     target_write_pc (read_register (RP_REGNUM));
  818.  
  819.   write_register (FP_REGNUM, read_memory_integer (fp, 4));
  820.  
  821.   if (fsr.regs[IPSW_REGNUM])    /* call dummy */
  822.     write_register (SP_REGNUM, fp - 48);
  823.   else
  824.     write_register (SP_REGNUM, fp);
  825.  
  826.   flush_cached_frames ();
  827.   set_current_frame (create_new_frame (read_register (FP_REGNUM),
  828.                                        read_pc ()));
  829. }
  830.  
  831. /*
  832.  * After returning to a dummy on the stack, restore the instruction
  833.  * queue space registers. */
  834.  
  835. static int
  836. restore_pc_queue (fsr)
  837.      struct frame_saved_regs *fsr;
  838. {
  839.   CORE_ADDR pc = read_pc ();
  840.   CORE_ADDR new_pc = read_memory_integer (fsr->regs[PCOQ_HEAD_REGNUM], 4);
  841.   int pid;
  842.   struct target_waitstatus w;
  843.   int insn_count;
  844.  
  845.   /* Advance past break instruction in the call dummy. */
  846.   write_register (PCOQ_HEAD_REGNUM, pc + 4);
  847.   write_register (PCOQ_TAIL_REGNUM, pc + 8);
  848.  
  849.   /*
  850.    * HPUX doesn't let us set the space registers or the space
  851.    * registers of the PC queue through ptrace. Boo, hiss.
  852.    * Conveniently, the call dummy has this sequence of instructions
  853.    * after the break:
  854.    *    mtsp r21, sr0
  855.    *    ble,n 0(sr0, r22)
  856.    *
  857.    * So, load up the registers and single step until we are in the
  858.    * right place.
  859.    */
  860.  
  861.   write_register (21, read_memory_integer (fsr->regs[PCSQ_HEAD_REGNUM], 4));
  862.   write_register (22, new_pc);
  863.  
  864.   for (insn_count = 0; insn_count < 3; insn_count++)
  865.     {
  866.       /* FIXME: What if the inferior gets a signal right now?  Want to
  867.      merge this into wait_for_inferior (as a special kind of
  868.      watchpoint?  By setting a breakpoint at the end?  Is there
  869.      any other choice?  Is there *any* way to do this stuff with
  870.      ptrace() or some equivalent?).  */
  871.       resume (1, 0);
  872.       target_wait (inferior_pid, &w);
  873.  
  874.       if (w.kind == TARGET_WAITKIND_SIGNALLED)
  875.         {
  876.           stop_signal = w.value.sig;
  877.           terminal_ours_for_output ();
  878.           printf_unfiltered ("\nProgram terminated with signal %s, %s.\n",
  879.                  target_signal_to_name (stop_signal),
  880.                  target_signal_to_string (stop_signal));
  881.           gdb_flush (gdb_stdout);
  882.           return 0;
  883.         }
  884.     }
  885.   target_terminal_ours ();
  886.   fetch_inferior_registers (-1);
  887.   return 1;
  888. }
  889.  
  890. CORE_ADDR
  891. hppa_push_arguments (nargs, args, sp, struct_return, struct_addr)
  892.      int nargs;
  893.      value *args;
  894.      CORE_ADDR sp;
  895.      int struct_return;
  896.      CORE_ADDR struct_addr;
  897. {
  898.   /* array of arguments' offsets */
  899.   int *offset = (int *)alloca(nargs * sizeof (int));
  900.   int cum = 0;
  901.   int i, alignment;
  902.   
  903.   for (i = 0; i < nargs; i++)
  904.     {
  905.       /* Coerce chars to int & float to double if necessary */
  906.       args[i] = value_arg_coerce (args[i]);
  907.  
  908.       cum += TYPE_LENGTH (VALUE_TYPE (args[i]));
  909.  
  910.     /* value must go at proper alignment. Assume alignment is a
  911.      power of two.*/
  912.       alignment = hppa_alignof (VALUE_TYPE (args[i]));
  913.       if (cum % alignment)
  914.     cum = (cum + alignment) & -alignment;
  915.       offset[i] = -cum;
  916.     }
  917.   sp += max ((cum + 7) & -8, 16);
  918.  
  919.   for (i = 0; i < nargs; i++)
  920.     write_memory (sp + offset[i], VALUE_CONTENTS (args[i]),
  921.           TYPE_LENGTH (VALUE_TYPE (args[i])));
  922.  
  923.   if (struct_return)
  924.     write_register (28, struct_addr);
  925.   return sp + 32;
  926. }
  927.  
  928. /*
  929.  * Insert the specified number of args and function address
  930.  * into a call sequence of the above form stored at DUMMYNAME.
  931.  *
  932.  * On the hppa we need to call the stack dummy through $$dyncall.
  933.  * Therefore our version of FIX_CALL_DUMMY takes an extra argument,
  934.  * real_pc, which is the location where gdb should start up the
  935.  * inferior to do the function call.
  936.  */
  937.  
  938. CORE_ADDR
  939. hppa_fix_call_dummy (dummy, pc, fun, nargs, args, type, gcc_p)
  940.      char *dummy;
  941.      CORE_ADDR pc;
  942.      CORE_ADDR fun;
  943.      int nargs;
  944.      value *args;
  945.      struct type *type;
  946.      int gcc_p;
  947. {
  948.   CORE_ADDR dyncall_addr, sr4export_addr;
  949.   struct minimal_symbol *msymbol;
  950.   int flags = read_register (FLAGS_REGNUM);
  951.  
  952.   msymbol = lookup_minimal_symbol ("$$dyncall", (struct objfile *) NULL);
  953.   if (msymbol == NULL)
  954.     error ("Can't find an address for $$dyncall trampoline");
  955.  
  956.   dyncall_addr = SYMBOL_VALUE_ADDRESS (msymbol);
  957.  
  958.   msymbol = lookup_minimal_symbol ("_sr4export", (struct objfile *) NULL);
  959.   if (msymbol == NULL)
  960.     error ("Can't find an address for _sr4export trampoline");
  961.  
  962.   sr4export_addr = SYMBOL_VALUE_ADDRESS (msymbol);
  963.  
  964.   store_unsigned_integer
  965.     (&dummy[9*REGISTER_SIZE],
  966.      REGISTER_SIZE,
  967.      deposit_21 (fun >> 11,
  968.          extract_unsigned_integer (&dummy[9*REGISTER_SIZE],
  969.                        REGISTER_SIZE)));
  970.   store_unsigned_integer
  971.     (&dummy[10*REGISTER_SIZE],
  972.      REGISTER_SIZE,
  973.      deposit_14 (fun & MASK_11,
  974.          extract_unsigned_integer (&dummy[10*REGISTER_SIZE],
  975.                        REGISTER_SIZE)));
  976.   store_unsigned_integer
  977.     (&dummy[12*REGISTER_SIZE],
  978.      REGISTER_SIZE,
  979.      deposit_21 (sr4export_addr >> 11,
  980.          extract_unsigned_integer (&dummy[12*REGISTER_SIZE],
  981.                        REGISTER_SIZE)));
  982.   store_unsigned_integer
  983.     (&dummy[13*REGISTER_SIZE],
  984.      REGISTER_SIZE,
  985.      deposit_14 (sr4export_addr & MASK_11,
  986.          extract_unsigned_integer (&dummy[13*REGISTER_SIZE],
  987.                        REGISTER_SIZE)));
  988.  
  989.   write_register (22, pc);
  990.  
  991.   /* If we are in a syscall, then we should call the stack dummy
  992.      directly.  $$dyncall is not needed as the kernel sets up the
  993.      space id registers properly based on the value in %r31.  In
  994.      fact calling $$dyncall will not work because the value in %r22
  995.      will be clobbered on the syscall exit path.  */
  996.   if (flags & 2)
  997.     return pc;
  998.   else
  999.     return dyncall_addr;
  1000.  
  1001. }
  1002.  
  1003. /* Get the PC from %r31 if currently in a syscall.  Also mask out privilege
  1004.    bits.  */
  1005. CORE_ADDR
  1006. target_read_pc ()
  1007. {
  1008.   int flags = read_register (FLAGS_REGNUM);
  1009.  
  1010.   if (flags & 2)
  1011.     return read_register (31) & ~0x3;
  1012.   return read_register (PC_REGNUM) & ~0x3;
  1013. }
  1014.  
  1015. /* Write out the PC.  If currently in a syscall, then also write the new
  1016.    PC value into %r31.  */
  1017. void
  1018. target_write_pc (v)
  1019.      CORE_ADDR v;
  1020. {
  1021.   int flags = read_register (FLAGS_REGNUM);
  1022.  
  1023.   /* If in a syscall, then set %r31.  Also make sure to get the 
  1024.      privilege bits set correctly.  */
  1025.   if (flags & 2)
  1026.     write_register (31, (long) (v | 0x3));
  1027.  
  1028.   write_register (PC_REGNUM, (long) v);
  1029.   write_register (NPC_REGNUM, (long) v + 4);
  1030. }
  1031.  
  1032. /* return the alignment of a type in bytes. Structures have the maximum
  1033.    alignment required by their fields. */
  1034.  
  1035. static int
  1036. hppa_alignof (arg)
  1037.      struct type *arg;
  1038. {
  1039.   int max_align, align, i;
  1040.   switch (TYPE_CODE (arg))
  1041.     {
  1042.     case TYPE_CODE_PTR:
  1043.     case TYPE_CODE_INT:
  1044.     case TYPE_CODE_FLT:
  1045.       return TYPE_LENGTH (arg);
  1046.     case TYPE_CODE_ARRAY:
  1047.       return hppa_alignof (TYPE_FIELD_TYPE (arg, 0));
  1048.     case TYPE_CODE_STRUCT:
  1049.     case TYPE_CODE_UNION:
  1050.       max_align = 2;
  1051.       for (i = 0; i < TYPE_NFIELDS (arg); i++)
  1052.     {
  1053.       /* Bit fields have no real alignment. */
  1054.       if (!TYPE_FIELD_BITPOS (arg, i))
  1055.         {
  1056.           align = hppa_alignof (TYPE_FIELD_TYPE (arg, i));
  1057.           max_align = max (max_align, align);
  1058.         }
  1059.     }
  1060.       return max_align;
  1061.     default:
  1062.       return 4;
  1063.     }
  1064. }
  1065.  
  1066. /* Print the register regnum, or all registers if regnum is -1 */
  1067.  
  1068. pa_do_registers_info (regnum, fpregs)
  1069.      int regnum;
  1070.      int fpregs;
  1071. {
  1072.   char raw_regs [REGISTER_BYTES];
  1073.   int i;
  1074.   
  1075.   for (i = 0; i < NUM_REGS; i++)
  1076.     read_relative_register_raw_bytes (i, raw_regs + REGISTER_BYTE (i));
  1077.   if (regnum == -1)
  1078.     pa_print_registers (raw_regs, regnum, fpregs);
  1079.   else if (regnum < FP0_REGNUM)
  1080.     printf_unfiltered ("%s %x\n", reg_names[regnum], *(long *)(raw_regs +
  1081.                             REGISTER_BYTE (regnum)));
  1082.   else
  1083.     pa_print_fp_reg (regnum);
  1084. }
  1085.  
  1086. pa_print_registers (raw_regs, regnum, fpregs)
  1087.      char *raw_regs;
  1088.      int regnum;
  1089.      int fpregs;
  1090. {
  1091.   int i;
  1092.  
  1093.   for (i = 0; i < 18; i++)
  1094.     printf_unfiltered ("%8.8s: %8x  %8.8s: %8x  %8.8s: %8x  %8.8s: %8x\n",
  1095.         reg_names[i],
  1096.         *(int *)(raw_regs + REGISTER_BYTE (i)),
  1097.         reg_names[i + 18],
  1098.         *(int *)(raw_regs + REGISTER_BYTE (i + 18)),
  1099.         reg_names[i + 36],
  1100.         *(int *)(raw_regs + REGISTER_BYTE (i + 36)),
  1101.         reg_names[i + 54],
  1102.         *(int *)(raw_regs + REGISTER_BYTE (i + 54)));
  1103.  
  1104.   if (fpregs)
  1105.     for (i = 72; i < NUM_REGS; i++)
  1106.       pa_print_fp_reg (i);
  1107. }
  1108.  
  1109. pa_print_fp_reg (i)
  1110.      int i;
  1111. {
  1112.   unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
  1113.   unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
  1114.  
  1115.   /* Get the data in raw format.  */
  1116.   read_relative_register_raw_bytes (i, raw_buffer);
  1117.  
  1118.   /* Convert raw data to virtual format if necessary.  */
  1119. #ifdef REGISTER_CONVERTIBLE
  1120.   if (REGISTER_CONVERTIBLE (i))
  1121.     {
  1122.       REGISTER_CONVERT_TO_VIRTUAL (i, REGISTER_VIRTUAL_TYPE (i),
  1123.                    raw_buffer, virtual_buffer);
  1124.     }
  1125.   else
  1126. #endif
  1127.     memcpy (virtual_buffer, raw_buffer,
  1128.         REGISTER_VIRTUAL_SIZE (i));
  1129.  
  1130.   fputs_filtered (reg_names[i], gdb_stdout);
  1131.   print_spaces_filtered (15 - strlen (reg_names[i]), gdb_stdout);
  1132.  
  1133.   val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, gdb_stdout, 0,
  1134.          1, 0, Val_pretty_default);
  1135.   printf_filtered ("\n");
  1136. }
  1137.  
  1138. /* Function calls that pass into a new compilation unit must pass through a
  1139.    small piece of code that does long format (`external' in HPPA parlance)
  1140.    jumps.  We figure out where the trampoline is going to end up, and return
  1141.    the PC of the final destination.  If we aren't in a trampoline, we just
  1142.    return NULL. 
  1143.  
  1144.    For computed calls, we just extract the new PC from r22.  */
  1145.  
  1146. CORE_ADDR
  1147. skip_trampoline_code (pc, name)
  1148.      CORE_ADDR pc;
  1149.      char *name;
  1150. {
  1151.   long inst0, inst1;
  1152.   static CORE_ADDR dyncall = 0;
  1153.   struct minimal_symbol *msym;
  1154.  
  1155. /* FIXME XXX - dyncall must be initialized whenever we get a new exec file */
  1156.  
  1157.   if (!dyncall)
  1158.     {
  1159.       msym = lookup_minimal_symbol ("$$dyncall", NULL);
  1160.       if (msym)
  1161.     dyncall = SYMBOL_VALUE_ADDRESS (msym);
  1162.       else
  1163.     dyncall = -1;
  1164.     }
  1165.  
  1166.   if (pc == dyncall)
  1167.     return (CORE_ADDR)(read_register (22) & ~0x3);
  1168.  
  1169.   inst0 = read_memory_integer (pc, 4);
  1170.   inst1 = read_memory_integer (pc+4, 4);
  1171.  
  1172.   if (   (inst0 & 0xffe00000) == 0x20200000 /* ldil xxx, r1 */
  1173.       && (inst1 & 0xffe0e002) == 0xe0202002) /* be,n yyy(sr4, r1) */
  1174.     pc = extract_21 (inst0) + extract_17 (inst1);
  1175.   else
  1176.     pc = (CORE_ADDR)NULL;
  1177.  
  1178.   return pc;
  1179. }
  1180.  
  1181. /* For the given instruction (INST), return any adjustment it makes
  1182.    to the stack pointer or zero for no adjustment. 
  1183.  
  1184.    This only handles instructions commonly found in prologues.  */
  1185.  
  1186. static int
  1187. prologue_inst_adjust_sp (inst)
  1188.      unsigned long inst;
  1189. {
  1190.   /* This must persist across calls.  */
  1191.   static int save_high21;
  1192.  
  1193.   /* The most common way to perform a stack adjustment ldo X(sp),sp */
  1194.   if ((inst & 0xffffc000) == 0x37de0000)
  1195.     return extract_14 (inst);
  1196.  
  1197.   /* stwm X,D(sp) */
  1198.   if ((inst & 0xffe00000) == 0x6fc00000)
  1199.     return extract_14 (inst);
  1200.  
  1201.   /* addil high21,%r1; ldo low11,(%r1),%r30)
  1202.      save high bits in save_high21 for later use.  */
  1203.   if ((inst & 0xffe00000) == 0x28200000)
  1204.     {
  1205.       save_high21 = extract_21 (inst);
  1206.       return 0;
  1207.     }
  1208.  
  1209.   if ((inst & 0xffff0000) == 0x343e0000)
  1210.     return save_high21 + extract_14 (inst);
  1211.  
  1212.   /* fstws as used by the HP compilers.  */
  1213.   if ((inst & 0xffffffe0) == 0x2fd01220)
  1214.     return extract_5_load (inst);
  1215.  
  1216.   /* No adjustment.  */
  1217.   return 0;
  1218. }
  1219.  
  1220. /* Return nonzero if INST is a branch of some kind, else return zero.  */
  1221.  
  1222. static int
  1223. is_branch (inst)
  1224.      unsigned long inst;
  1225. {
  1226.   switch (inst >> 26)
  1227.     {
  1228.     case 0x20:
  1229.     case 0x21:
  1230.     case 0x22:
  1231.     case 0x23:
  1232.     case 0x28:
  1233.     case 0x29:
  1234.     case 0x2a:
  1235.     case 0x2b:
  1236.     case 0x30:
  1237.     case 0x31:
  1238.     case 0x32:
  1239.     case 0x33:
  1240.     case 0x38:
  1241.     case 0x39:
  1242.     case 0x3a:
  1243.       return 1;
  1244.  
  1245.     default:
  1246.       return 0;
  1247.     }
  1248. }
  1249.  
  1250. /* Return the register number for a GR which is saved by INST or
  1251.    zero it INST does not save a GR.
  1252.  
  1253.    Note we only care about full 32bit register stores (that's the only
  1254.    kind of stores the prologue will use).  */
  1255.  
  1256. static int
  1257. inst_saves_gr (inst)
  1258.      unsigned long inst;
  1259. {
  1260.   /* Does it look like a stw?  */
  1261.   if ((inst >> 26) == 0x1a)
  1262.     return extract_5R_store (inst);
  1263.  
  1264.   /* Does it look like a stwm?  */
  1265.   if ((inst >> 26) == 0x1b)
  1266.     return extract_5R_store (inst);
  1267.  
  1268.   return 0;
  1269. }
  1270.  
  1271. /* Return the register number for a FR which is saved by INST or
  1272.    zero it INST does not save a FR.
  1273.  
  1274.    Note we only care about full 64bit register stores (that's the only
  1275.    kind of stores the prologue will use).  */
  1276.  
  1277. static int
  1278. inst_saves_fr (inst)
  1279.      unsigned long inst;
  1280. {
  1281.   if ((inst & 0xfc1fffe0) == 0x2c101220)
  1282.     return extract_5r_store (inst);
  1283.   return 0;
  1284. }
  1285.  
  1286. /* Advance PC across any function entry prologue instructions
  1287.    to reach some "real" code. 
  1288.  
  1289.    Use information in the unwind table to determine what exactly should
  1290.    be in the prologue.  */
  1291.  
  1292. CORE_ADDR
  1293. skip_prologue(pc)
  1294.      CORE_ADDR pc;
  1295. {
  1296.   char buf[4];
  1297.   unsigned long inst, stack_remaining, save_gr, save_fr, save_rp, save_sp;
  1298.   int status, i;
  1299.   struct unwind_table_entry *u;
  1300.  
  1301.   u = find_unwind_entry (pc);
  1302.   if (!u)
  1303.     return 0;
  1304.  
  1305.   /* This is how much of a frame adjustment we need to account for.  */
  1306.   stack_remaining = u->Total_frame_size << 3;
  1307.  
  1308.   /* Magic register saves we want to know about.  */
  1309.   save_rp = u->Save_RP;
  1310.   save_sp = u->Save_SP;
  1311.  
  1312.   /* Turn the Entry_GR field into a bitmask.  */
  1313.   save_gr = 0;
  1314.   for (i = 3; i < u->Entry_GR + 3; i++)
  1315.     {
  1316.       /* Frame pointer gets saved into a special location.  */
  1317.       if (u->Save_SP && i == FP_REGNUM)
  1318.     continue;
  1319.  
  1320.       save_gr |= (1 << i);
  1321.     }
  1322.  
  1323.   /* Turn the Entry_FR field into a bitmask too.  */
  1324.   save_fr = 0;
  1325.   for (i = 12; i < u->Entry_FR + 12; i++)
  1326.     save_fr |= (1 << i);
  1327.  
  1328.   /* Loop until we find everything of interest or hit a branch.
  1329.  
  1330.      For unoptimized GCC code and for any HP CC code this will never ever
  1331.      examine any user instructions.
  1332.  
  1333.      For optimzied GCC code we're faced with problems.  GCC will schedule
  1334.      its prologue and make prologue instructions available for delay slot
  1335.      filling.  The end result is user code gets mixed in with the prologue
  1336.      and a prologue instruction may be in the delay slot of the first branch
  1337.      or call.
  1338.  
  1339.      Some unexpected things are expected with debugging optimized code, so
  1340.      we allow this routine to walk past user instructions in optimized
  1341.      GCC code.  */
  1342.   while (save_gr || save_fr || save_rp || save_sp || stack_remaining > 0)
  1343.     {
  1344.       status = target_read_memory (pc, buf, 4);
  1345.       inst = extract_unsigned_integer (buf, 4);
  1346.  
  1347.       /* Yow! */
  1348.       if (status != 0)
  1349.     return pc;
  1350.  
  1351.       /* Note the interesting effects of this instruction.  */
  1352.       stack_remaining -= prologue_inst_adjust_sp (inst);
  1353.  
  1354.       /* There is only one instruction used for saving RP into the stack.  */
  1355.       if (inst == 0x6bc23fd9)
  1356.     save_rp = 0;
  1357.  
  1358.       /* This is the only way we save SP into the stack.  At this time
  1359.      the HP compilers never bother to save SP into the stack.  */
  1360.       if ((inst & 0xffffc000) == 0x6fc10000)
  1361.     save_sp = 0;
  1362.  
  1363.       /* Account for general and floating-point register saves.  */
  1364.       save_gr &= ~(1 << inst_saves_gr (inst));
  1365.       save_fr &= ~(1 << inst_saves_fr (inst));
  1366.  
  1367.       /* Quit if we hit any kind of branch.  This can happen if a prologue
  1368.      instruction is in the delay slot of the first call/branch.  */
  1369.       if (is_branch (inst))
  1370.     break;
  1371.  
  1372.       /* Bump the PC.  */
  1373.       pc += 4;
  1374.     }
  1375.  
  1376.   return pc;
  1377. }
  1378.  
  1379. /* Put here the code to store, into a struct frame_saved_regs,
  1380.    the addresses of the saved registers of frame described by FRAME_INFO.
  1381.    This includes special registers such as pc and fp saved in special
  1382.    ways in the stack frame.  sp is even more special:
  1383.    the address we return for it IS the sp for the next frame.  */
  1384.  
  1385. void
  1386. hppa_frame_find_saved_regs (frame_info, frame_saved_regs)
  1387.      struct frame_info *frame_info;
  1388.      struct frame_saved_regs *frame_saved_regs;
  1389. {
  1390.   CORE_ADDR pc;
  1391.   struct unwind_table_entry *u;
  1392.   unsigned long inst, stack_remaining, save_gr, save_fr, save_rp, save_sp;
  1393.   int status, i, reg;
  1394.   char buf[4];
  1395.   int fp_loc = -1;
  1396.  
  1397.   /* Zero out everything.  */
  1398.   memset (frame_saved_regs, '\0', sizeof (struct frame_saved_regs));
  1399.  
  1400.   /* Call dummy frames always look the same, so there's no need to
  1401.      examine the dummy code to determine locations of saved registers;
  1402.      instead, let find_dummy_frame_regs fill in the correct offsets
  1403.      for the saved registers.  */
  1404.   if ((frame_info->pc >= frame_info->frame
  1405.        && frame_info->pc <= (frame_info->frame + CALL_DUMMY_LENGTH
  1406.                  + 32 * 4 + (NUM_REGS - FP0_REGNUM) * 8
  1407.                  + 6 * 4)))
  1408.     find_dummy_frame_regs (frame_info, frame_saved_regs);
  1409.  
  1410.   /* Get the starting address of the function referred to by the PC
  1411.      saved in frame_info.  */
  1412.   pc = get_pc_function_start (frame_info->pc);
  1413.  
  1414.   /* Yow! */
  1415.   u = find_unwind_entry (pc);
  1416.   if (!u)
  1417.     return;
  1418.  
  1419.   /* This is how much of a frame adjustment we need to account for.  */
  1420.   stack_remaining = u->Total_frame_size << 3;
  1421.  
  1422.   /* Magic register saves we want to know about.  */
  1423.   save_rp = u->Save_RP;
  1424.   save_sp = u->Save_SP;
  1425.  
  1426.   /* Turn the Entry_GR field into a bitmask.  */
  1427.   save_gr = 0;
  1428.   for (i = 3; i < u->Entry_GR + 3; i++)
  1429.     {
  1430.       /* Frame pointer gets saved into a special location.  */
  1431.       if (u->Save_SP && i == FP_REGNUM)
  1432.     continue;
  1433.  
  1434.       save_gr |= (1 << i);
  1435.     }
  1436.  
  1437.   /* Turn the Entry_FR field into a bitmask too.  */
  1438.   save_fr = 0;
  1439.   for (i = 12; i < u->Entry_FR + 12; i++)
  1440.     save_fr |= (1 << i);
  1441.  
  1442.   /* Loop until we find everything of interest or hit a branch.
  1443.  
  1444.      For unoptimized GCC code and for any HP CC code this will never ever
  1445.      examine any user instructions.
  1446.  
  1447.      For optimzied GCC code we're faced with problems.  GCC will schedule
  1448.      its prologue and make prologue instructions available for delay slot
  1449.      filling.  The end result is user code gets mixed in with the prologue
  1450.      and a prologue instruction may be in the delay slot of the first branch
  1451.      or call.
  1452.  
  1453.      Some unexpected things are expected with debugging optimized code, so
  1454.      we allow this routine to walk past user instructions in optimized
  1455.      GCC code.  */
  1456.   while (save_gr || save_fr || save_rp || save_sp || stack_remaining > 0)
  1457.     {
  1458.       status = target_read_memory (pc, buf, 4);
  1459.       inst = extract_unsigned_integer (buf, 4);
  1460.  
  1461.       /* Yow! */
  1462.       if (status != 0)
  1463.     return;
  1464.  
  1465.       /* Note the interesting effects of this instruction.  */
  1466.       stack_remaining -= prologue_inst_adjust_sp (inst);
  1467.  
  1468.       /* There is only one instruction used for saving RP into the stack.  */
  1469.       if (inst == 0x6bc23fd9)
  1470.     {
  1471.       save_rp = 0;
  1472.       frame_saved_regs->regs[RP_REGNUM] = frame_info->frame - 20;
  1473.     }
  1474.  
  1475.       /* This is the only way we save SP into the stack.  At this time
  1476.      the HP compilers never bother to save SP into the stack.  */
  1477.       if ((inst & 0xffffc000) == 0x6fc10000)
  1478.     {
  1479.       save_sp = 0;
  1480.       frame_saved_regs->regs[SP_REGNUM] = frame_info->frame;
  1481.     }
  1482.  
  1483.       /* Account for general and floating-point register saves.  */
  1484.       reg = inst_saves_gr (inst);
  1485.       if (reg >= 3 && reg <= 18
  1486.       && (!u->Save_SP || reg != FP_REGNUM))
  1487.     {
  1488.       save_gr &= ~(1 << reg);
  1489.  
  1490.       /* stwm with a positive displacement is a *post modify*.  */
  1491.       if ((inst >> 26) == 0x1b
  1492.           && extract_14 (inst) >= 0)
  1493.         frame_saved_regs->regs[reg] = frame_info->frame;
  1494.       else
  1495.         {
  1496.           /* Handle code with and without frame pointers.  */
  1497.           if (u->Save_SP)
  1498.         frame_saved_regs->regs[reg]
  1499.           = frame_info->frame + extract_14 (inst);
  1500.           else
  1501.         frame_saved_regs->regs[reg]
  1502.           = frame_info->frame + (u->Total_frame_size << 3)
  1503.             + extract_14 (inst);
  1504.         }
  1505.     }
  1506.  
  1507.  
  1508.       /* GCC handles callee saved FP regs a little differently.  
  1509.  
  1510.      It emits an instruction to put the value of the start of
  1511.      the FP store area into %r1.  It then uses fstds,ma with
  1512.      a basereg of %r1 for the stores.
  1513.  
  1514.      HP CC emits them at the current stack pointer modifying
  1515.      the stack pointer as it stores each register.  */
  1516.  
  1517.       /* ldo X(%r3),%r1 or ldo X(%r30),%r1.  */
  1518.       if ((inst & 0xffffc000) == 0x34610000
  1519.       || (inst & 0xffffc000) == 0x37c10000)
  1520.     fp_loc = extract_14 (inst);
  1521.     
  1522.       reg = inst_saves_fr (inst);
  1523.       if (reg >= 12 && reg <= 21)
  1524.     {
  1525.       /* Note +4 braindamage below is necessary because the FP status
  1526.          registers are internally 8 registers rather than the expected
  1527.          4 registers.  */
  1528.       save_fr &= ~(1 << reg);
  1529.       if (fp_loc == -1)
  1530.         {
  1531.           /* 1st HP CC FP register store.  After this instruction
  1532.          we've set enough state that the GCC and HPCC code are
  1533.          both handled in the same manner.  */
  1534.           frame_saved_regs->regs[reg + FP4_REGNUM + 4] = frame_info->frame;
  1535.           fp_loc = 8;
  1536.         }
  1537.       else
  1538.         {
  1539.           frame_saved_regs->regs[reg + FP0_REGNUM + 4]
  1540.         = frame_info->frame + fp_loc;
  1541.           fp_loc += 8;
  1542.         }
  1543.     }
  1544.  
  1545.       /* Quit if we hit any kind of branch.  This can happen if a prologue
  1546.      instruction is in the delay slot of the first call/branch.  */
  1547.       if (is_branch (inst))
  1548.     break;
  1549.  
  1550.       /* Bump the PC.  */
  1551.       pc += 4;
  1552.     }
  1553. }
  1554.  
  1555. #ifdef MAINTENANCE_CMDS
  1556.  
  1557. static void
  1558. unwind_command (exp, from_tty)
  1559.      char *exp;
  1560.      int from_tty;
  1561. {
  1562.   CORE_ADDR address;
  1563.   union
  1564.     {
  1565.       int *foo;
  1566.       struct unwind_table_entry *u;
  1567.     } xxx;
  1568.  
  1569.   /* If we have an expression, evaluate it and use it as the address.  */
  1570.  
  1571.   if (exp != 0 && *exp != 0)
  1572.     address = parse_and_eval_address (exp);
  1573.   else
  1574.     return;
  1575.  
  1576.   xxx.u = find_unwind_entry (address);
  1577.  
  1578.   if (!xxx.u)
  1579.     {
  1580.       printf_unfiltered ("Can't find unwind table entry for PC 0x%x\n", address);
  1581.       return;
  1582.     }
  1583.  
  1584.   printf_unfiltered ("%08x\n%08X\n%08X\n%08X\n", xxx.foo[0], xxx.foo[1], xxx.foo[2],
  1585.       xxx.foo[3]);
  1586. }
  1587. #endif /* MAINTENANCE_CMDS */
  1588.  
  1589. void
  1590. _initialize_hppa_tdep ()
  1591. {
  1592. #ifdef MAINTENANCE_CMDS
  1593.   add_cmd ("unwind", class_maintenance, unwind_command,
  1594.        "Print unwind table entry at given address.",
  1595.        &maintenanceprintlist);
  1596. #endif /* MAINTENANCE_CMDS */
  1597. }
  1598.