]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/prologue-value.c
Automatic Copyright Year update after running gdb/copyright.py
[thirdparty/binutils-gdb.git] / gdb / prologue-value.c
index 02d653ac76cef4a46ee8e3cf48b9796dc6bafcb3..032c4a685d202bdb0b4d859c4717f951123cde10 100644 (file)
@@ -1,5 +1,5 @@
 /* Prologue value handling for GDB.
-   Copyright 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
+   Copyright (C) 2003-2022 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
-#include "gdb_string.h"
-#include "gdb_assert.h"
 #include "prologue-value.h"
 #include "regcache.h"
 
@@ -92,7 +90,7 @@ pv_add (pv_t a, pv_t b)
 
   /* We can add a constant to another constant.  */
   else if (a.kind == pvk_constant
-           && b.kind == pvk_constant)
+          && b.kind == pvk_constant)
     return pv_constant (a.k + b.k);
 
   /* Anything else we don't know how to add.  We don't have a
@@ -136,13 +134,13 @@ pv_subtract (pv_t a, pv_t b)
 
   /* We can subtract a constant from a register.  */
   else if (a.kind == pvk_register
-           && b.kind == pvk_constant)
+          && b.kind == pvk_constant)
     return pv_register (a.reg, a.k - b.k);
 
   /* We can subtract a register from itself, yielding a constant.  */
   else if (a.kind == pvk_register
-           && b.kind == pvk_register
-           && a.reg == b.reg)
+          && b.kind == pvk_register
+          && a.reg == b.reg)
     return pv_constant (a.k - b.k);
 
   /* We don't know how to subtract anything else.  */
@@ -163,19 +161,19 @@ pv_logical_and (pv_t a, pv_t b)
 
   /* We can 'and' anything with the constant zero.  */
   else if (b.kind == pvk_constant
-           && b.k == 0)
+          && b.k == 0)
     return pv_constant (0);
 
   /* We can 'and' anything with ~0.  */
   else if (b.kind == pvk_constant
-           && b.k == ~ (CORE_ADDR) 0)
+          && b.k == ~ (CORE_ADDR) 0)
     return a;
 
   /* We can 'and' a register with itself.  */
   else if (a.kind == pvk_register
-           && b.kind == pvk_register
-           && a.reg == b.reg
-           && a.k == b.k)
+          && b.kind == pvk_register
+          && a.reg == b.reg
+          && a.k == b.k)
     return a;
 
   /* Otherwise, we don't know.  */
@@ -202,7 +200,7 @@ pv_is_identical (pv_t a, pv_t b)
     case pvk_register:
       return (a.reg == b.reg && a.k == b.k);
     default:
-      gdb_assert (0);
+      gdb_assert_not_reached ("unexpected prologue value kind");
     }
 }
 
@@ -218,7 +216,7 @@ int
 pv_is_register (pv_t a, int r)
 {
   return (a.kind == pvk_register
-          && a.reg == r);
+         && a.reg == r);
 }
 
 
@@ -226,16 +224,16 @@ int
 pv_is_register_k (pv_t a, int r, CORE_ADDR k)
 {
   return (a.kind == pvk_register
-          && a.reg == r
-          && a.k == k);
+         && a.reg == r
+         && a.k == k);
 }
 
 
 enum pv_boolean
 pv_is_array_ref (pv_t addr, CORE_ADDR size,
-                 pv_t array_addr, CORE_ADDR array_len,
-                 CORE_ADDR elt_size,
-                 int *i)
+                pv_t array_addr, CORE_ADDR array_len,
+                CORE_ADDR elt_size,
+                int *i)
 {
   /* Note that, since .k is a CORE_ADDR, and CORE_ADDR is unsigned, if
      addr is *before* the start of the array, then this isn't going to
@@ -245,22 +243,22 @@ pv_is_array_ref (pv_t addr, CORE_ADDR size,
   if (offset.kind == pvk_constant)
     {
       /* This is a rather odd test.  We want to know if the SIZE bytes
-         at ADDR don't overlap the array at all, so you'd expect it to
-         be an || expression: "if we're completely before || we're
-         completely after".  But with unsigned arithmetic, things are
-         different: since it's a number circle, not a number line, the
-         right values for offset.k are actually one contiguous range.  */
+        at ADDR don't overlap the array at all, so you'd expect it to
+        be an || expression: "if we're completely before || we're
+        completely after".  But with unsigned arithmetic, things are
+        different: since it's a number circle, not a number line, the
+        right values for offset.k are actually one contiguous range.  */
       if (offset.k <= -size
-          && offset.k >= array_len * elt_size)
-        return pv_definite_no;
+         && offset.k >= array_len * elt_size)
+       return pv_definite_no;
       else if (offset.k % elt_size != 0
-               || size != elt_size)
-        return pv_maybe;
+              || size != elt_size)
+       return pv_maybe;
       else
-        {
-          *i = offset.k / elt_size;
-          return pv_definite_yes;
-        }
+       {
+         *i = offset.k / elt_size;
+         return pv_definite_yes;
+       }
     }
   else
     return pv_maybe;
@@ -280,7 +278,7 @@ pv_is_array_ref (pv_t addr, CORE_ADDR size,
    The entry with the lowest offset simply follows the entry with the
    highest offset.  Entries may abut, but never overlap.  The area's
    'entry' pointer points to an arbitrary node in the ring.  */
-struct area_entry
+struct pv_area::area_entry
 {
   /* Links in the doubly-linked ring.  */
   struct area_entry *prev, *next;
@@ -298,88 +296,53 @@ struct area_entry
 };
 
 
-struct pv_area
-{
-  /* This area's base register.  */
-  int base_reg;
-
-  /* The mask to apply to addresses, to make the wrap-around happen at
-     the right place.  */
-  CORE_ADDR addr_mask;
-
-  /* An element of the doubly-linked ring of entries, or zero if we
-     have none.  */
-  struct area_entry *entry;
-};
+/* See prologue-value.h.  */
 
-
-struct pv_area *
-make_pv_area (int base_reg)
+pv_area::pv_area (int base_reg, int addr_bit)
+  : m_base_reg (base_reg),
+    /* Remember that shift amounts equal to the type's width are
+       undefined.  */
+    m_addr_mask (((((CORE_ADDR) 1 << (addr_bit - 1)) - 1) << 1) | 1),
+    m_entry (nullptr)
 {
-  struct pv_area *a = (struct pv_area *) xmalloc (sizeof (*a));
-
-  memset (a, 0, sizeof (*a));
-
-  a->base_reg = base_reg;
-  a->entry = 0;
-
-  /* Remember that shift amounts equal to the type's width are
-     undefined.  */
-  a->addr_mask = ((((CORE_ADDR) 1
-                  << (gdbarch_addr_bit (current_gdbarch) - 1)) - 1) << 1) | 1;
-
-  return a;
 }
 
+/* See prologue-value.h.  */
 
-/* Delete all entries from AREA.  */
-static void
-clear_entries (struct pv_area *area)
+void
+pv_area::clear_entries ()
 {
-  struct area_entry *e = area->entry;
+  struct area_entry *e = m_entry;
 
   if (e)
     {
       /* This needs to be a do-while loop, in order to actually
-         process the node being checked for in the terminating
-         condition.  */
+        process the node being checked for in the terminating
+        condition.  */
       do
-        {
-          struct area_entry *next = e->next;
-          xfree (e);
-          e = next;
-        }
-      while (e != area->entry);
-
-      area->entry = 0;
-    }
-}
+       {
+         struct area_entry *next = e->next;
 
+         xfree (e);
+         e = next;
+       }
+      while (e != m_entry);
 
-void
-free_pv_area (struct pv_area *area)
-{
-  clear_entries (area);
-  xfree (area);
+      m_entry = 0;
+    }
 }
 
 
-static void
-do_free_pv_area_cleanup (void *arg)
+pv_area::~pv_area ()
 {
-  free_pv_area ((struct pv_area *) arg);
+  clear_entries ();
 }
 
 
-struct cleanup *
-make_cleanup_free_pv_area (struct pv_area *area)
-{
-  return make_cleanup (do_free_pv_area_cleanup, (void *) area);
-}
-
+/* See prologue-value.h.  */
 
-int
-pv_area_store_would_trash (struct pv_area *area, pv_t addr)
+bool
+pv_area::store_would_trash (pv_t addr)
 {
   /* It may seem odd that pvk_constant appears here --- after all,
      that's the case where we know the most about the address!  But
@@ -387,24 +350,17 @@ pv_area_store_would_trash (struct pv_area *area, pv_t addr)
      value of the register, so we can't compare entry addresses to
      constants.  */
   return (addr.kind == pvk_unknown
-          || addr.kind == pvk_constant
-          || (addr.kind == pvk_register && addr.reg != area->base_reg));
+         || addr.kind == pvk_constant
+         || (addr.kind == pvk_register && addr.reg != m_base_reg));
 }
 
 
-/* Return a pointer to the first entry we hit in AREA starting at
-   OFFSET and going forward.
-
-   This may return zero, if AREA has no entries.
+/* See prologue-value.h.  */
 
-   And since the entries are a ring, this may return an entry that
-   entirely preceeds OFFSET.  This is the correct behavior: depending
-   on the sizes involved, we could still overlap such an area, with
-   wrap-around.  */
-static struct area_entry *
-find_entry (struct pv_area *area, CORE_ADDR offset)
+struct pv_area::area_entry *
+pv_area::find_entry (CORE_ADDR offset)
 {
-  struct area_entry *e = area->entry;
+  struct area_entry *e = m_entry;
 
   if (! e)
     return 0;
@@ -418,69 +374,66 @@ find_entry (struct pv_area *area, CORE_ADDR offset)
      with wrap-around.  We have to subtract offset from both sides to
      make sure both things we're comparing are on the same side of the
      discontinuity.  */
-  while (((e->next->offset - offset) & area->addr_mask)
-         < ((e->offset - offset) & area->addr_mask))
+  while (((e->next->offset - offset) & m_addr_mask)
+        < ((e->offset - offset) & m_addr_mask))
     e = e->next;
 
   /* If the previous entry would be better than the current one, then
      scan backwards.  */
-  while (((e->prev->offset - offset) & area->addr_mask)
-         < ((e->offset - offset) & area->addr_mask))
+  while (((e->prev->offset - offset) & m_addr_mask)
+        < ((e->offset - offset) & m_addr_mask))
     e = e->prev;
 
   /* In case there's some locality to the searches, set the area's
      pointer to the entry we've found.  */
-  area->entry = e;
+  m_entry = e;
 
   return e;
 }
 
 
-/* Return non-zero if the SIZE bytes at OFFSET would overlap ENTRY;
-   return zero otherwise.  AREA is the area to which ENTRY belongs.  */
-static int
-overlaps (struct pv_area *area,
-          struct area_entry *entry,
-          CORE_ADDR offset,
-          CORE_ADDR size)
+/* See prologue-value.h.  */
+
+int
+pv_area::overlaps (struct area_entry *entry, CORE_ADDR offset, CORE_ADDR size)
 {
   /* Think carefully about wrap-around before simplifying this.  */
-  return (((entry->offset - offset) & area->addr_mask) < size
-          || ((offset - entry->offset) & area->addr_mask) < entry->size);
+  return (((entry->offset - offset) & m_addr_mask) < size
+         || ((offset - entry->offset) & m_addr_mask) < entry->size);
 }
 
 
+/* See prologue-value.h.  */
+
 void
-pv_area_store (struct pv_area *area,
-               pv_t addr,
-               CORE_ADDR size,
-               pv_t value)
+pv_area::store (pv_t addr, CORE_ADDR size, pv_t value)
 {
   /* Remove any (potentially) overlapping entries.  */
-  if (pv_area_store_would_trash (area, addr))
-    clear_entries (area);
+  if (store_would_trash (addr))
+    clear_entries ();
   else
     {
       CORE_ADDR offset = addr.k;
-      struct area_entry *e = find_entry (area, offset);
+      struct area_entry *e = find_entry (offset);
 
       /* Delete all entries that we would overlap.  */
-      while (e && overlaps (area, e, offset, size))
-        {
-          struct area_entry *next = (e->next == e) ? 0 : e->next;
-          e->prev->next = e->next;
-          e->next->prev = e->prev;
+      while (e && overlaps (e, offset, size))
+       {
+         struct area_entry *next = (e->next == e) ? 0 : e->next;
+
+         e->prev->next = e->next;
+         e->next->prev = e->prev;
 
-          xfree (e);
-          e = next;
-        }
+         xfree (e);
+         e = next;
+       }
 
       /* Move the area's pointer to the next remaining entry.  This
-         will also zero the pointer if we've deleted all the entries.  */
-      area->entry = e;
+        will also zero the pointer if we've deleted all the entries.  */
+      m_entry = e;
     }
 
-  /* Now, there are no entries overlapping us, and area->entry is
+  /* Now, there are no entries overlapping us, and m_entry is
      either zero or pointing at the closest entry after us.  We can
      just insert ourselves before that.
 
@@ -491,98 +444,101 @@ pv_area_store (struct pv_area *area,
   else
     {
       CORE_ADDR offset = addr.k;
-      struct area_entry *e = (struct area_entry *) xmalloc (sizeof (*e));
+      struct area_entry *e = XNEW (struct area_entry);
+
       e->offset = offset;
       e->size = size;
       e->value = value;
 
-      if (area->entry)
-        {
-          e->prev = area->entry->prev;
-          e->next = area->entry;
-          e->prev->next = e->next->prev = e;
-        }
+      if (m_entry)
+       {
+         e->prev = m_entry->prev;
+         e->next = m_entry;
+         e->prev->next = e->next->prev = e;
+       }
       else
-        {
-          e->prev = e->next = e;
-          area->entry = e;
-        }
+       {
+         e->prev = e->next = e;
+         m_entry = e;
+       }
     }
 }
 
 
+/* See prologue-value.h.  */
+
 pv_t
-pv_area_fetch (struct pv_area *area, pv_t addr, CORE_ADDR size)
+pv_area::fetch (pv_t addr, CORE_ADDR size)
 {
   /* If we have no entries, or we can't decide how ADDR relates to the
      entries we do have, then the value is unknown.  */
-  if (! area->entry
-      || pv_area_store_would_trash (area, addr))
+  if (! m_entry
+      || store_would_trash (addr))
     return pv_unknown ();
   else
     {
       CORE_ADDR offset = addr.k;
-      struct area_entry *e = find_entry (area, offset);
+      struct area_entry *e = find_entry (offset);
 
       /* If this entry exactly matches what we're looking for, then
-         we're set.  Otherwise, say it's unknown.  */
+        we're set.  Otherwise, say it's unknown.  */
       if (e->offset == offset && e->size == size)
-        return e->value;
+       return e->value;
       else
-        return pv_unknown ();
+       return pv_unknown ();
     }
 }
 
 
-int
-pv_area_find_reg (struct pv_area *area,
-                  struct gdbarch *gdbarch,
-                  int reg,
-                  CORE_ADDR *offset_p)
+/* See prologue-value.h.  */
+
+bool
+pv_area::find_reg (struct gdbarch *gdbarch, int reg, CORE_ADDR *offset_p)
 {
-  struct area_entry *e = area->entry;
+  struct area_entry *e = m_entry;
 
   if (e)
     do
       {
-        if (e->value.kind == pvk_register
-            && e->value.reg == reg
-            && e->value.k == 0
-            && e->size == register_size (gdbarch, reg))
-          {
-            if (offset_p)
-              *offset_p = e->offset;
-            return 1;
-          }
-
-        e = e->next;
+       if (e->value.kind == pvk_register
+           && e->value.reg == reg
+           && e->value.k == 0
+           && e->size == register_size (gdbarch, reg))
+         {
+           if (offset_p)
+             *offset_p = e->offset;
+           return true;
+         }
+
+       e = e->next;
       }
-    while (e != area->entry);
+    while (e != m_entry);
 
-  return 0;
+  return false;
 }
 
 
+/* See prologue-value.h.  */
+
 void
-pv_area_scan (struct pv_area *area,
-              void (*func) (void *closure,
-                            pv_t addr,
-                            CORE_ADDR size,
-                            pv_t value),
-              void *closure)
+pv_area::scan (void (*func) (void *closure,
+                            pv_t addr,
+                            CORE_ADDR size,
+                            pv_t value),
+              void *closure)
 {
-  struct area_entry *e = area->entry;
+  struct area_entry *e = m_entry;
   pv_t addr;
 
   addr.kind = pvk_register;
-  addr.reg = area->base_reg;
+  addr.reg = m_base_reg;
 
   if (e)
     do
       {
-        addr.k = e->offset;
-        func (closure, addr, e->size, e->value);
-        e = e->next;
+       addr.k = e->offset;
+       func (closure, addr, e->size, e->value);
+       e = e->next;
       }
-    while (e != area->entry);
+    while (e != m_entry);
 }