2003-03-17 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / d10v-tdep.c
index c6b94bd21bd48b606ea56d678a03f75aced082f3..f443ceff8ddccd45c49dcc986bb030380b2e1268 100644 (file)
 
 #include "gdb_assert.h"
 
-struct frame_extra_info
-  {
-    CORE_ADDR return_pc;
-    int frameless;
-    int size;
-  };
-
 struct gdbarch_tdep
   {
     int a0_regnum;
@@ -348,7 +341,7 @@ d10v_register_raw_size (int reg_nr)
    of data in register N.  */
 
 static struct type *
-d10v_register_virtual_type (int reg_nr)
+d10v_register_type (struct gdbarch *gdbarch, int reg_nr)
 {
   if (reg_nr == PC_REGNUM)
     return builtin_type_void_func_ptr;
@@ -617,10 +610,15 @@ d10v_skip_prologue (CORE_ADDR pc)
 struct d10v_unwind_cache
 {
   CORE_ADDR return_pc;
-  int frameless;
+  /* The frame's base.  Used when constructing a frame ID.  */
+  CORE_ADDR base;
   int size;
   CORE_ADDR *saved_regs;
-  CORE_ADDR next_addr;
+  /* How far the SP and r11 (FP) have been offset from the start of
+     the stack frame (as defined by the previous frame's stack
+     pointer).  */
+  LONGEST sp_offset;
+  LONGEST r11_offset;
   int uses_frame;
   void **regs;
 };
@@ -635,8 +633,8 @@ prologue_find_regs (struct d10v_unwind_cache *info, unsigned short op,
   if ((op & 0x7E1F) == 0x6C1F)
     {
       n = (op & 0x1E0) >> 5;
-      info->next_addr -= 2;
-      info->saved_regs[n] = info->next_addr;
+      info->sp_offset -= 2;
+      info->saved_regs[n] = info->sp_offset;
       return 1;
     }
 
@@ -644,9 +642,9 @@ prologue_find_regs (struct d10v_unwind_cache *info, unsigned short op,
   else if ((op & 0x7E3F) == 0x6E1F)
     {
       n = (op & 0x1E0) >> 5;
-      info->next_addr -= 4;
-      info->saved_regs[n] = info->next_addr;
-      info->saved_regs[n + 1] = info->next_addr + 2;
+      info->sp_offset -= 4;
+      info->saved_regs[n] = info->sp_offset;
+      info->saved_regs[n + 1] = info->sp_offset + 2;
       return 1;
     }
 
@@ -656,7 +654,7 @@ prologue_find_regs (struct d10v_unwind_cache *info, unsigned short op,
       n = (op & 0x1E) >> 1;
       if (n == 0)
        n = 16;
-      info->next_addr -= n;
+      info->sp_offset -= n;
       return 1;
     }
 
@@ -664,6 +662,15 @@ prologue_find_regs (struct d10v_unwind_cache *info, unsigned short op,
   if (op == 0x417E)
     {
       info->uses_frame = 1;
+      info->r11_offset = info->sp_offset;
+      return 1;
+    }
+
+  /* st  rn, @r11 */
+  if ((op & 0x7E1F) == 0x6816)
+    {
+      n = (op & 0x1E0) >> 5;
+      info->saved_regs[n] = info->r11_offset;
       return 1;
     }
 
@@ -675,7 +682,7 @@ prologue_find_regs (struct d10v_unwind_cache *info, unsigned short op,
   if ((op & 0x7E1F) == 0x681E)
     {
       n = (op & 0x1E0) >> 5;
-      info->saved_regs[n] = info->next_addr;
+      info->saved_regs[n] = info->sp_offset;
       return 1;
     }
 
@@ -683,8 +690,8 @@ prologue_find_regs (struct d10v_unwind_cache *info, unsigned short op,
   if ((op & 0x7E3F) == 0x3A1E)
     {
       n = (op & 0x1E0) >> 5;
-      info->saved_regs[n] = info->next_addr;
-      info->saved_regs[n + 1] = info->next_addr + 2;
+      info->saved_regs[n] = info->sp_offset;
+      info->saved_regs[n + 1] = info->sp_offset + 2;
       return 1;
     }
 
@@ -698,30 +705,29 @@ prologue_find_regs (struct d10v_unwind_cache *info, unsigned short op,
    for it IS the sp for the next frame. */
 
 struct d10v_unwind_cache *
-d10v_frame_unwind_cache (struct frame_info *fi,
-                        void **cache)
+d10v_frame_unwind_cache (struct frame_info *next_frame,
+                        void **this_prologue_cache)
 {
-  CORE_ADDR fp, pc;
+  CORE_ADDR pc;
+  ULONGEST prev_sp;
+  ULONGEST this_base;
   unsigned long op;
   unsigned short op1, op2;
   int i;
   struct d10v_unwind_cache *info;
 
-  if ((*cache))
-    return (*cache);
+  if ((*this_prologue_cache))
+    return (*this_prologue_cache);
 
   info = FRAME_OBSTACK_ZALLOC (struct d10v_unwind_cache);
-  (*cache) = info;
+  (*this_prologue_cache) = info;
   info->saved_regs = frame_obstack_zalloc (SIZEOF_FRAME_SAVED_REGS);
 
-  info->frameless = 0;
   info->size = 0;
   info->return_pc = 0;
+  info->sp_offset = 0;
 
-  fp = get_frame_base (fi);
-  info->next_addr = 0;
-
-  pc = get_pc_function_start (get_frame_pc (fi));
+  pc = get_pc_function_start (frame_pc_unwind (next_frame));
 
   info->uses_frame = 0;
   while (1)
@@ -734,22 +740,22 @@ d10v_frame_unwind_cache (struct frame_info *fi,
            {
              /* add3 sp,sp,n */
              short n = op & 0xFFFF;
-             info->next_addr += n;
+             info->sp_offset += n;
            }
          else if ((op & 0x3F0F0000) == 0x340F0000)
            {
              /* st  rn, @(offset,sp) */
              short offset = op & 0xFFFF;
              short n = (op >> 20) & 0xF;
-             info->saved_regs[n] = info->next_addr + offset;
+             info->saved_regs[n] = info->sp_offset + offset;
            }
          else if ((op & 0x3F1F0000) == 0x350F0000)
            {
              /* st2w  rn, @(offset,sp) */
              short offset = op & 0xFFFF;
              short n = (op >> 20) & 0xF;
-             info->saved_regs[n] = info->next_addr + offset;
-             info->saved_regs[n + 1] = info->next_addr + offset + 2;
+             info->saved_regs[n] = info->sp_offset + offset;
+             info->saved_regs[n + 1] = info->sp_offset + offset + 2;
            }
          else
            break;
@@ -774,46 +780,64 @@ d10v_frame_unwind_cache (struct frame_info *fi,
       pc += 4;
     }
 
-  info->size = -info->next_addr;
+  info->size = -info->sp_offset;
+
+  /* Compute the frame's base, and the previous frame's SP.  */
+  if (info->uses_frame)
+    {
+      /* The SP was moved to the FP.  This indicates that a new frame
+         was created.  Get THIS frame's FP value by unwinding it from
+         the next frame.  */
+      frame_unwind_unsigned_register (next_frame, FP_REGNUM, &this_base);
+      /* The FP points at the last saved register.  Adjust the FP back
+         to before the first saved register giving the SP.  */
+      prev_sp = this_base + info->size;
+    }
+  else if (info->saved_regs[SP_REGNUM])
+    {
+      /* The SP was saved (which is very unusual), the frame base is
+        just the PREV's frame's TOP-OF-STACK.  */
+      this_base = read_memory_unsigned_integer (info->saved_regs[SP_REGNUM], 
+                                               register_size (current_gdbarch,
+                                                              SP_REGNUM));
+      prev_sp = this_base;
+    }
+  else
+    {
+      /* Assume that the FP is this frame's SP but with that pushed
+         stack space added back.  */
+      frame_unwind_unsigned_register (next_frame, SP_REGNUM, &this_base);
+      prev_sp = this_base + info->size;
+    }
 
-  if (!(fp & 0xffff))
-    fp = d10v_read_sp ();
+  info->base = d10v_make_daddr (this_base);
+  prev_sp = d10v_make_daddr (prev_sp);
 
+  /* Adjust all the saved registers so that they contain addresses and
+     not offsets.  */
   for (i = 0; i < NUM_REGS - 1; i++)
     if (info->saved_regs[i])
       {
-       info->saved_regs[i] = fp - (info->next_addr - info->saved_regs[i]);
+       info->saved_regs[i] = (prev_sp + info->saved_regs[i]);
       }
 
   if (info->saved_regs[LR_REGNUM])
     {
       CORE_ADDR return_pc 
        = read_memory_unsigned_integer (info->saved_regs[LR_REGNUM], 
-                                       REGISTER_RAW_SIZE (LR_REGNUM));
+                                       register_size (current_gdbarch, LR_REGNUM));
       info->return_pc = d10v_make_iaddr (return_pc);
     }
   else
     {
       ULONGEST return_pc;
-      frame_read_unsigned_register (fi, LR_REGNUM, &return_pc);
+      frame_unwind_unsigned_register (next_frame, LR_REGNUM, &return_pc);
       info->return_pc = d10v_make_iaddr (return_pc);
     }
 
-  /* The SP is not normally (ever?) saved, but check anyway */
-  if (!info->saved_regs[SP_REGNUM])
-    {
-      /* if the FP was saved, that means the current FP is valid, */
-      /* otherwise, it isn't being used, so we use the SP instead */
-      if (info->uses_frame)
-       info->saved_regs[SP_REGNUM] 
-         = d10v_read_fp () + info->size;
-      else
-       {
-         info->saved_regs[SP_REGNUM] = fp + info->size;
-         info->frameless = 1;
-         info->saved_regs[FP_REGNUM] = 0;
-       }
-    }
+  /* The SP_REGNUM is special.  Instead of the address of the SP, the
+     previous frame's SP value is saved.  */
+  info->saved_regs[SP_REGNUM] = prev_sp;
 
   return info;
 }
@@ -889,7 +913,7 @@ d10v_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
        int i;
        fprintf_filtered (file, "  ");
        frame_register_read (frame, a, num);
-       for (i = 0; i < MAX_REGISTER_RAW_SIZE; i++)
+       for (i = 0; i < max_register_size (current_gdbarch); i++)
          {
            fprintf_filtered (file, "%02x", (num[i] & 0xff));
          }
@@ -1078,7 +1102,7 @@ d10v_extract_return_value (struct type *type, struct regcache *regcache,
   printf("RET: TYPE=%d len=%d r%d=0x%x\n", TYPE_CODE (type), 
         TYPE_LENGTH (type), RET1_REGNUM - R0_REGNUM, 
         (int) extract_unsigned_integer (regbuf + REGISTER_BYTE(RET1_REGNUM), 
-                                        REGISTER_RAW_SIZE (RET1_REGNUM)));
+                                        register_size (current_gdbarch, RET1_REGNUM)));
 #endif
   if (TYPE_LENGTH (type) == 1)
     {
@@ -1421,71 +1445,67 @@ display_trace (int low, int high)
     }
 }
 
-
 static CORE_ADDR
-d10v_frame_pc_unwind (struct frame_info *frame,
-                     void **cache)
+d10v_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
 {
-  struct d10v_unwind_cache *info = d10v_frame_unwind_cache (frame, cache);
-  return info->return_pc;
+  ULONGEST pc;
+  frame_unwind_unsigned_register (next_frame, PC_REGNUM, &pc);
+  return d10v_make_iaddr (pc);
 }
 
 /* Given a GDB frame, determine the address of the calling function's
    frame.  This will be used to create a new GDB frame struct.  */
 
 static void
-d10v_frame_id_unwind (struct frame_info *frame,
-                     void **cache,
-                     struct frame_id *id)
+d10v_frame_this_id (struct frame_info *next_frame,
+                   void **this_prologue_cache,
+                   struct frame_id *this_id)
 {
-  struct d10v_unwind_cache *info = d10v_frame_unwind_cache (frame, cache);
-  CORE_ADDR addr;
+  struct d10v_unwind_cache *info
+    = d10v_frame_unwind_cache (next_frame, this_prologue_cache);
+  CORE_ADDR base;
+  CORE_ADDR pc;
 
   /* Start with a NULL frame ID.  */
-  (*id) = null_frame_id;
+  (*this_id) = null_frame_id;
 
-  if (info->return_pc == IMEM_START
-      || info->return_pc <= IMEM_START
-      || inside_entry_file (info->return_pc))
-    {
-      /* This is meant to halt the backtrace at "_start".
-        Make sure we don't halt it at a generic dummy frame. */
-      return;
-    }
+  /* The PC is easy.  */
+  pc = frame_pc_unwind (next_frame);
 
-  if (!info->saved_regs[FP_REGNUM])
-    {
-      if (!info->saved_regs[SP_REGNUM]
-         || info->saved_regs[SP_REGNUM] == STACK_START)
-       return;
+  /* This is meant to halt the backtrace at "_start".  Make sure we
+     don't halt it at a generic dummy frame. */
+  if (pc == IMEM_START || pc <= IMEM_START || inside_entry_file (pc))
+    return;
 
-      id->base = info->saved_regs[SP_REGNUM];
-      id->pc = info->return_pc;
-    }
+  /* Hopefully the prologue analysis either correctly determined the
+     frame's base (which is the SP from the previous frame), or set
+     that base to "NULL".  */
+  base = info->base;
+  if (base == STACK_START || base == 0)
+    return;
 
-  addr = read_memory_unsigned_integer (info->saved_regs[FP_REGNUM],
-                                      REGISTER_RAW_SIZE (FP_REGNUM));
-  if (addr == 0)
+  /* Check that we're not going round in circles with the same frame
+     ID (but avoid applying the test to sentinel frames which do go
+     round in circles).  Can't use frame_id_eq() as that doesn't yet
+     compare the frame's PC value.  */
+  if (frame_relative_level (next_frame) >= 0
+      && get_frame_type (next_frame) != DUMMY_FRAME
+      && get_frame_id (next_frame).pc == pc
+      && get_frame_id (next_frame).base == base)
     return;
 
-  id->base = d10v_make_daddr (addr);
-  id->pc = info->return_pc;
+  this_id->base = base;
+  this_id->pc = pc;
 }
 
 static void
-saved_regs_unwinder (struct frame_info *frame,
-                    CORE_ADDR *saved_regs,
+saved_regs_unwinder (struct frame_info *next_frame,
+                    CORE_ADDR *this_saved_regs,
                     int regnum, int *optimizedp,
                     enum lval_type *lvalp, CORE_ADDR *addrp,
                     int *realnump, void *bufferp)
 {
-  /* If we're using generic dummy frames, we'd better not be in a call
-     dummy.  (generic_call_dummy_register_unwind ought to have been called
-     instead.)  */
-  gdb_assert (!(DEPRECATED_USE_GENERIC_DUMMY_FRAMES
-               && (get_frame_type (frame) == DUMMY_FRAME)));
-
-  if (saved_regs[regnum] != 0)
+  if (this_saved_regs[regnum] != 0)
     {
       if (regnum == SP_REGNUM)
        {
@@ -1495,8 +1515,8 @@ saved_regs_unwinder (struct frame_info *frame,
          *addrp = 0;
          *realnump = -1;
          if (bufferp != NULL)
-           store_address (bufferp, REGISTER_RAW_SIZE (regnum),
-                          saved_regs[regnum]);
+           store_address (bufferp, register_size (current_gdbarch, regnum),
+                          this_saved_regs[regnum]);
        }
       else
        {
@@ -1504,13 +1524,13 @@ saved_regs_unwinder (struct frame_info *frame,
             a local copy of its value.  */
          *optimizedp = 0;
          *lvalp = lval_memory;
-         *addrp = saved_regs[regnum];
+         *addrp = this_saved_regs[regnum];
          *realnump = -1;
          if (bufferp != NULL)
            {
              /* Read the value in from memory.  */
-             read_memory (saved_regs[regnum], bufferp,
-                          REGISTER_RAW_SIZE (regnum));
+             read_memory (this_saved_regs[regnum], bufferp,
+                          register_size (current_gdbarch, regnum));
            }
        }
       return;
@@ -1520,65 +1540,39 @@ saved_regs_unwinder (struct frame_info *frame,
      value.  If a value is needed, pass the request on down the chain;
      otherwise just return an indication that the value is in the same
      register as the next frame.  */
-  frame_register (frame, regnum, optimizedp, lvalp, addrp,
-                 realnump, bufferp);
-}
-
-
-static void
-d10v_frame_register_unwind (struct frame_info *frame,
-                           void **cache,
-                           int regnum, int *optimizedp,
-                           enum lval_type *lvalp, CORE_ADDR *addrp,
-                           int *realnump, void *bufferp)
-{
-  struct d10v_unwind_cache *info = d10v_frame_unwind_cache (frame, cache);
-  saved_regs_unwinder (frame, info->saved_regs, regnum, optimizedp,
-                      lvalp, addrp, realnump, bufferp);
+  frame_register_unwind (next_frame, regnum, optimizedp, lvalp, addrp,
+                        realnump, bufferp);
 }
 
 
 static void
-d10v_frame_pop (struct frame_info *fi, void **unwind_cache,
-               struct regcache *regcache)
-{
-  struct d10v_unwind_cache *info = d10v_frame_unwind_cache (fi, unwind_cache);
-  CORE_ADDR fp;
-  int regnum;
-  char raw_buffer[8];
-
-  fp = get_frame_base (fi);
-
-  /* now update the current registers with the old values */
-  for (regnum = A0_REGNUM; regnum < A0_REGNUM + NR_A_REGS; regnum++)
+d10v_frame_prev_register (struct frame_info *next_frame,
+                         void **this_prologue_cache,
+                         int regnum, int *optimizedp,
+                         enum lval_type *lvalp, CORE_ADDR *addrp,
+                         int *realnump, void *bufferp)
+{
+  struct d10v_unwind_cache *info
+    = d10v_frame_unwind_cache (next_frame, this_prologue_cache);
+  if (regnum == PC_REGNUM)
     {
-      frame_unwind_register (fi, regnum, raw_buffer);
-      regcache_cooked_write (regcache, regnum, raw_buffer);
+      /* The call instruction saves the caller's PC in LR.  The
+        function prologue of the callee may then save the LR on the
+        stack.  Find that possibly saved LR value and return it.  */
+      saved_regs_unwinder (next_frame, info->saved_regs, LR_REGNUM, optimizedp,
+                          lvalp, addrp, realnump, bufferp);
     }
-  for (regnum = 0; regnum < SP_REGNUM; regnum++)
+  else
     {
-      frame_unwind_register (fi, regnum, raw_buffer);
-      regcache_cooked_write (regcache, regnum, raw_buffer);
+      saved_regs_unwinder (next_frame, info->saved_regs, regnum, optimizedp,
+                          lvalp, addrp, realnump, bufferp);
     }
-  frame_unwind_register (fi, PSW_REGNUM, raw_buffer);
-  regcache_cooked_write (regcache, PSW_REGNUM, raw_buffer);
-
-  frame_unwind_register (fi, LR_REGNUM, raw_buffer);
-  regcache_cooked_write (regcache, PC_REGNUM, raw_buffer);
-
-  store_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (SP_REGNUM),
-                         fp + info->size);
-  regcache_cooked_write (regcache, SP_REGNUM, raw_buffer);
-
-  target_store_registers (-1);
-  flush_cached_frames ();
 }
 
+
 static struct frame_unwind d10v_frame_unwind = {
-  d10v_frame_pop,
-  d10v_frame_pc_unwind,
-  d10v_frame_id_unwind,
-  d10v_frame_register_unwind
+  d10v_frame_this_id,
+  d10v_frame_prev_register
 };
 
 const struct frame_unwind *
@@ -1587,6 +1581,22 @@ d10v_frame_p (CORE_ADDR pc)
   return &d10v_frame_unwind;
 }
 
+/* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
+   dummy frame.  The frame ID's base needs to match the TOS value
+   saved by save_dummy_frame_tos(), and the PC match the dummy frame's
+   breakpoint.  */
+
+static struct frame_id
+d10v_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
+{
+  ULONGEST base;
+  struct frame_id id;
+  id.pc = frame_pc_unwind (next_frame);
+  frame_unwind_unsigned_register (next_frame, SP_REGNUM, &base);
+  id.base = d10v_make_daddr (base);
+  return id;
+}
+
 static gdbarch_init_ftype d10v_gdbarch_init;
 
 static struct gdbarch *
@@ -1648,10 +1658,8 @@ d10v_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_register_bytes (gdbarch, (d10v_num_regs - 2) * 2 + 16);
   set_gdbarch_register_byte (gdbarch, d10v_register_byte);
   set_gdbarch_register_raw_size (gdbarch, d10v_register_raw_size);
-  set_gdbarch_max_register_raw_size (gdbarch, 8);
   set_gdbarch_register_virtual_size (gdbarch, generic_register_size);
-  set_gdbarch_max_register_virtual_size (gdbarch, 8);
-  set_gdbarch_register_virtual_type (gdbarch, d10v_register_virtual_type);
+  set_gdbarch_register_type (gdbarch, d10v_register_type);
 
   set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
   set_gdbarch_addr_bit (gdbarch, 32);
@@ -1697,7 +1705,6 @@ d10v_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
 
   set_gdbarch_extract_return_value (gdbarch, d10v_extract_return_value);
   set_gdbarch_push_arguments (gdbarch, d10v_push_arguments);
-  set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
   set_gdbarch_push_return_address (gdbarch, d10v_push_return_address);
 
   set_gdbarch_store_struct_return (gdbarch, d10v_store_struct_return);
@@ -1727,6 +1734,13 @@ d10v_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
 
   frame_unwind_append_predicate (gdbarch, d10v_frame_p);
 
+  /* Methods for saving / extracting a dummy frame's ID.  */
+  set_gdbarch_unwind_dummy_id (gdbarch, d10v_unwind_dummy_id);
+  set_gdbarch_save_dummy_frame_tos (gdbarch, generic_save_dummy_frame_tos);
+
+  /* Return the unwound PC value.  */
+  set_gdbarch_unwind_pc (gdbarch, d10v_unwind_pc);
+
   return gdbarch;
 }
 
This page took 0.034865 seconds and 4 git commands to generate.