public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/6] Add methods and operators to gdb_mpz
Date: Thu, 23 Feb 2023 12:48:00 -0700	[thread overview]
Message-ID: <20230223-submit-gmp-hiding-v1-2-ea6ff9e32716@adacore.com> (raw)
In-Reply-To: <20230223-submit-gmp-hiding-v1-0-ea6ff9e32716@adacore.com>

This adds various methods and operators to gdb_mpz, as a step toward
hiding the implementation.

This only adds the operators that were needed.  Many more could be
added as required.
---
 gdb/ada-lex.l                       |  9 ++--
 gdb/dwarf2/read.c                   | 46 +++++++++-----------
 gdb/gmp-utils.h                     | 87 +++++++++++++++++++++++++++++++++++++
 gdb/unittests/gmp-utils-selftests.c | 54 +++++++++++------------
 gdb/valops.c                        |  2 +-
 gdb/value.c                         |  3 +-
 6 files changed, 140 insertions(+), 61 deletions(-)

diff --git a/gdb/ada-lex.l b/gdb/ada-lex.l
index a5cfb84dcae..69fc14f7107 100644
--- a/gdb/ada-lex.l
+++ b/gdb/ada-lex.l
@@ -422,14 +422,14 @@ processInt (struct parser_state *par_state, const char *base0,
       int dig = fromhex (*num0);
       if (dig >= base)
 	error (_("Invalid digit `%c' in based literal"), *num0);
-      mpz_mul_ui (result.val, result.val, base);
-      mpz_add_ui (result.val, result.val, dig);
+      result *= base;
+      result += dig;
       ++num0;
     }
 
   while (exp > 0)
     {
-      mpz_mul_ui (result.val, result.val, base);
+      result *= base;
       exp -= 1;
     }
 
@@ -462,8 +462,7 @@ processInt (struct parser_state *par_state, const char *base0,
       return FLOAT;
     }
 
-  gdb_mpz maxval (ULONGEST_MAX);
-  if (mpz_cmp (result.val, maxval.val) > 0)
+  if (result > gdb_mpz (ULONGEST_MAX))
     error (_("Integer literal out of range"));
 
   int int_bits = gdbarch_int_bit (par_state->gdbarch ());
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index a5bc1d89a2d..cef79b6531b 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -14756,10 +14756,10 @@ get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
 						   &len);
 	  if (ptr - blk->data + len <= blk->size)
 	    {
-	      mpz_import (value->val, len,
-			  bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
-			  ? 1 : -1,
-			  1, 0, 0, ptr);
+	      value->read (gdb::make_array_view (ptr, len),
+			   bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
+			   ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
+			   true);
 	      return;
 	    }
 	}
@@ -14770,10 +14770,10 @@ get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
   else if (attr->form_is_block ())
     {
       dwarf_block *blk = attr->as_block ();
-      mpz_import (value->val, blk->size,
-		  bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
-		  ? 1 : -1,
-		  1, 0, 0, blk->data);
+      value->read (gdb::make_array_view (blk->data, blk->size),
+		   bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
+		   ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
+		   true);
     }
   else
     *value = gdb_mpz (attr->constant_value (1));
@@ -14825,19 +14825,19 @@ get_dwarf2_unsigned_rational_constant (struct die_info *die,
   gdb_mpz denom (1);
 
   get_dwarf2_rational_constant (die, cu, &num, &denom);
-  if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
+  if (num < 0 && denom < 0)
     {
-      mpz_neg (num.val, num.val);
-      mpz_neg (denom.val, denom.val);
+      num.negate ();
+      denom.negate ();
     }
-  else if (mpz_sgn (num.val) == -1)
+  else if (num < 0)
     {
       complaint (_("unexpected negative value for DW_AT_GNU_numerator"
 		   " in DIE at %s"),
 		 sect_offset_str (die->sect_off));
       return;
     }
-  else if (mpz_sgn (denom.val) == -1)
+  else if (denom < 0)
     {
       complaint (_("unexpected negative value for DW_AT_GNU_denominator"
 		   " in DIE at %s"),
@@ -14876,10 +14876,7 @@ ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
     return false;
 
   std::string copy (&encoding[start], k - start);
-  if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
-    return false;
-
-  return true;
+  return result->set (copy.c_str (), 10);
 }
 
 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
@@ -14959,16 +14956,16 @@ finish_fixed_point_type (struct type *type, const char *suffix,
   else if (attr->name == DW_AT_binary_scale)
     {
       LONGEST scale_exp = attr->constant_value (0);
-      gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
+      gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
 
-      mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
+      num_or_denom <<= std::abs (scale_exp);
     }
   else if (attr->name == DW_AT_decimal_scale)
     {
       LONGEST scale_exp = attr->constant_value (0);
-      gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
+      gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
 
-      mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
+      num_or_denom = gdb_mpz::pow (10, std::abs (scale_exp));
     }
   else if (attr->name == DW_AT_small)
     {
@@ -14992,10 +14989,7 @@ finish_fixed_point_type (struct type *type, const char *suffix,
 		 sect_offset_str (die->sect_off));
     }
 
-  gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
-  mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
-  mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
-  mpq_canonicalize (scaling_factor.val);
+  type->fixed_point_info ().scaling_factor = gdb_mpq (scale_num, scale_denom);
 }
 
 /* The gnat-encoding suffix for fixed point.  */
@@ -15076,7 +15070,7 @@ has_zero_over_zero_small_attribute (struct die_info *die,
 
   gdb_mpz num (1), denom (1);
   get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
-  return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
+  return num == 0 && denom == 0;
 }
 
 /* Initialise and return a floating point type of size BITS suitable for
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index 0522f3453aa..dcb998a1656 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -89,6 +89,21 @@ struct gdb_mpz
     return *this;
   }
 
+  /* Initialize this value from a string and a base.  Returns true if
+     the string was parsed successfully, false otherwise.  */
+  bool set (const char *str, int base)
+  {
+    return mpz_set_str (val, str, base) != -1;
+  }
+
+  /* Return a new value that is BASE**EXP.  */
+  static gdb_mpz pow (unsigned long base, unsigned long exp)
+  {
+    gdb_mpz result;
+    mpz_ui_pow_ui (result.val, base, exp);
+    return result;
+  }
+
   /* Convert VAL to an integer of the given type.
 
      The return type can signed or unsigned, with no size restriction.  */
@@ -115,6 +130,56 @@ struct gdb_mpz
   /* The destructor.  */
   ~gdb_mpz () { mpz_clear (val); }
 
+  /* Negate this value in place.  */
+  void negate ()
+  {
+    mpz_neg (val, val);
+  }
+
+  gdb_mpz &operator*= (long other)
+  {
+    mpz_mul_si (val, val, other);
+    return *this;
+  }
+
+  gdb_mpz &operator+= (unsigned long other)
+  {
+    mpz_add_ui (val, val, other);
+    return *this;
+  }
+
+  gdb_mpz &operator-= (unsigned long other)
+  {
+    mpz_sub_ui (val, val, other);
+    return *this;
+  }
+
+  gdb_mpz &operator<<= (unsigned long nbits)
+  {
+    mpz_mul_2exp (val, val, nbits);
+    return *this;
+  }
+
+  bool operator> (const gdb_mpz &other) const
+  {
+    return mpz_cmp (val, other.val) > 0;
+  }
+
+  bool operator< (int other) const
+  {
+    return mpz_cmp_si (val, other) < 0;
+  }
+
+  bool operator== (int other) const
+  {
+    return mpz_cmp_si (val, other) == 0;
+  }
+
+  bool operator== (const gdb_mpz &other) const
+  {
+    return mpz_cmp (val, other.val) == 0;
+  }
+
 private:
 
   /* Helper template for constructor and operator=.  */
@@ -166,6 +231,14 @@ struct gdb_mpq
     mpq_swap (val, from.val);
   }
 
+  gdb_mpq (const gdb_mpz &num, const gdb_mpz &denom)
+  {
+    mpq_init (val);
+    mpz_set (mpq_numref (val), num.val);
+    mpz_set (mpq_denref (val), denom.val);
+    mpq_canonicalize (val);
+  }
+
   /* Copy assignment operator.  */
   gdb_mpq &operator= (const gdb_mpq &from)
   {
@@ -179,12 +252,26 @@ struct gdb_mpq
     return *this;
   }
 
+  gdb_mpq &operator= (const gdb_mpz &from)
+  {
+    mpq_set_z (val, from.val);
+    return *this;
+  }
+
   /* Return a string representing VAL as "<numerator> / <denominator>".  */
   std::string str () const { return gmp_string_printf ("%Qd", val); }
 
   /* Return VAL rounded to the nearest integer.  */
   gdb_mpz get_rounded () const;
 
+  /* Return this value as an integer, rounded toward zero.  */
+  gdb_mpz as_integer () const
+  {
+    gdb_mpz result;
+    mpz_tdiv_q (result.val, mpq_numref (val), mpq_denref (val));
+    return result;
+  }
+
   /* Set VAL from the contents of the given byte array (BUF), which
      contains the unscaled value of a fixed point type object.
      The byte size of the data is the size of BUF.
diff --git a/gdb/unittests/gmp-utils-selftests.c b/gdb/unittests/gmp-utils-selftests.c
index e2ccd2ca094..8e028fec88c 100644
--- a/gdb/unittests/gmp-utils-selftests.c
+++ b/gdb/unittests/gmp-utils-selftests.c
@@ -43,8 +43,8 @@ gdb_mpz_as_integer ()
   /* Start with the smallest LONGEST  */
   l_expected = (LONGEST) 1 << (sizeof (LONGEST) * 8 - 1);
 
-  mpz_ui_pow_ui (v.val, 2, sizeof (LONGEST) * 8 - 1);
-  mpz_neg (v.val, v.val);
+  v = gdb_mpz::pow (2, sizeof (LONGEST) * 8 - 1);
+  v.negate ();
 
   SELF_CHECK (v.as_integer<LONGEST> () == l_expected);
 
@@ -53,13 +53,13 @@ gdb_mpz_as_integer ()
   for (int i = -256; i <= 256; i++)
     {
       l_expected = (LONGEST) i;
-      mpz_set_si (v.val, i);
+      v = i;
       SELF_CHECK (v.as_integer<LONGEST> () == l_expected);
 
       if (i >= 0)
 	{
 	  ul_expected = (ULONGEST) i;
-	  mpz_set_ui (v.val, i);
+	  v = ul_expected;
 	  SELF_CHECK (v.as_integer<ULONGEST> () == ul_expected);
 	}
     }
@@ -68,16 +68,16 @@ gdb_mpz_as_integer ()
   l_expected = LONGEST_MAX;
   ul_expected = (ULONGEST) l_expected;
 
-  mpz_ui_pow_ui (v.val, 2, sizeof (LONGEST) * 8 - 1);
-  mpz_sub_ui (v.val, v.val, 1);
+  v = gdb_mpz::pow (2, sizeof (LONGEST) * 8 - 1);
+  v -= 1;
 
   SELF_CHECK (v.as_integer<LONGEST> () == l_expected);
   SELF_CHECK (v.as_integer<ULONGEST> () == ul_expected);
 
   /* Try with ULONGEST_MAX.  */
   ul_expected = ULONGEST_MAX;
-  mpz_ui_pow_ui (v.val, 2, sizeof (LONGEST) * 8);
-  mpz_sub_ui (v.val, v.val, 1);
+  v = gdb_mpz::pow (2, sizeof (LONGEST) * 8);
+  v -= 1;
 
   SELF_CHECK (v.as_integer<ULONGEST> () == ul_expected);
 }
@@ -116,9 +116,9 @@ gdb_mpz_as_integer_out_of_range ()
   gdb_mpz v;
 
   /* Try LONGEST_MIN minus 1.  */
-  mpz_ui_pow_ui (v.val, 2, sizeof (LONGEST) * 8 - 1);
-  mpz_neg (v.val, v.val);
-  mpz_sub_ui (v.val, v.val, 1);
+  v = gdb_mpz::pow (2, sizeof (LONGEST) * 8 - 1);
+  v.negate ();
+  v -= 1;
 
   check_as_integer_raises_out_of_range_error<ULONGEST> (v);
   check_as_integer_raises_out_of_range_error<LONGEST> (v);
@@ -131,14 +131,14 @@ gdb_mpz_as_integer_out_of_range ()
 
   /* Try LONGEST_MAX plus 1.  */
   v = LONGEST_MAX;
-  mpz_add_ui (v.val, v.val, 1);
+  v += 1;
 
   SELF_CHECK (v.as_integer<ULONGEST> () == (ULONGEST) LONGEST_MAX + 1);
   check_as_integer_raises_out_of_range_error<LONGEST> (v);
 
   /* Try ULONGEST_MAX plus 1.  */
   v = ULONGEST_MAX;
-  mpz_add_ui (v.val, v.val, 1);
+  v += 1;
 
   check_as_integer_raises_out_of_range_error<ULONGEST> (v);
   check_as_integer_raises_out_of_range_error<LONGEST> (v);
@@ -170,8 +170,8 @@ store_and_read_back (T val, size_t buf_len, enum bfd_endian byte_order,
   store_integer (buf, buf_len, byte_order, val);
 
   /* Pre-initialize ACTUAL to something that's not the expected value.  */
-  mpz_set (actual.val, expected.val);
-  mpz_sub_ui (actual.val, actual.val, 500);
+  actual = expected;
+  actual -= 500;
 
   actual.read ({buf, buf_len}, byte_order, !std::is_signed<T>::value);
 }
@@ -197,10 +197,10 @@ gdb_mpz_read_all_from_small ()
       gdb_mpz expected, actual;
 
       store_and_read_back (l, buf_len, BFD_ENDIAN_BIG, expected, actual);
-      SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+      SELF_CHECK (actual ==  expected);
 
       store_and_read_back (l, buf_len, BFD_ENDIAN_LITTLE, expected, actual);
-      SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+      SELF_CHECK (actual == expected);
     }
 
   /* Do the same as above, but with an unsigned type.  */
@@ -212,10 +212,10 @@ gdb_mpz_read_all_from_small ()
       gdb_mpz expected, actual;
 
       store_and_read_back (ul, buf_len, BFD_ENDIAN_BIG, expected, actual);
-      SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+      SELF_CHECK (actual == expected);
 
       store_and_read_back (ul, buf_len, BFD_ENDIAN_LITTLE, expected, actual);
-      SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+      SELF_CHECK (actual == expected);
     }
 }
 
@@ -232,11 +232,11 @@ gdb_mpz_read_min_max ()
 
   store_and_read_back (l_min, sizeof (LONGEST), BFD_ENDIAN_BIG,
 		       expected, actual);
-  SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+  SELF_CHECK (actual == expected);
 
   store_and_read_back (l_min, sizeof (LONGEST), BFD_ENDIAN_LITTLE,
 		       expected, actual);
-  SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+  SELF_CHECK (actual == expected);
 
   /* Same with LONGEST_MAX.  */
 
@@ -244,11 +244,11 @@ gdb_mpz_read_min_max ()
 
   store_and_read_back (l_max, sizeof (LONGEST), BFD_ENDIAN_BIG,
 		       expected, actual);
-  SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+  SELF_CHECK (actual == expected);
 
   store_and_read_back (l_max, sizeof (LONGEST), BFD_ENDIAN_LITTLE,
 		       expected, actual);
-  SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+  SELF_CHECK (actual == expected);
 
   /* Same with the smallest ULONGEST.  */
 
@@ -256,11 +256,11 @@ gdb_mpz_read_min_max ()
 
   store_and_read_back (ul_min, sizeof (ULONGEST), BFD_ENDIAN_BIG,
 		       expected, actual);
-  SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+  SELF_CHECK (actual == expected);
 
   store_and_read_back (ul_min, sizeof (ULONGEST), BFD_ENDIAN_LITTLE,
 		       expected, actual);
-  SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+  SELF_CHECK (actual == expected);
 
   /* Same with ULONGEST_MAX.  */
 
@@ -268,11 +268,11 @@ gdb_mpz_read_min_max ()
 
   store_and_read_back (ul_max, sizeof (ULONGEST), BFD_ENDIAN_BIG,
 		       expected, actual);
-  SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+  SELF_CHECK (actual == expected);
 
   store_and_read_back (ul_max, sizeof (ULONGEST), BFD_ENDIAN_LITTLE,
 		       expected, actual);
-  SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+  SELF_CHECK (actual == expected);
 }
 
 /* A helper function which creates a gdb_mpz object from the given
diff --git a/gdb/valops.c b/gdb/valops.c
index 3a1b14c3d44..e0936d45cb3 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -354,7 +354,7 @@ value_to_gdb_mpq (struct value *value)
       gdb_mpz vz;
       vz.read (value->contents (), type_byte_order (type),
 	       type->is_unsigned ());
-      mpq_set_z (result.val, vz.val);
+      result = vz;
 
       if (is_fixed_point_type (type))
 	mpq_mul (result.val, result.val,
diff --git a/gdb/value.c b/gdb/value.c
index d2c2a2d7859..672e0c7023a 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -2721,8 +2721,7 @@ unpack_long (struct type *type, const gdb_byte *valaddr)
 			     byte_order, nosign,
 			     type->fixed_point_scaling_factor ());
 
-	gdb_mpz vz;
-	mpz_tdiv_q (vz.val, mpq_numref (vq.val), mpq_denref (vq.val));
+	gdb_mpz vz = vq.as_integer ();
 	return vz.as_integer<LONGEST> ();
       }
 

-- 
2.39.1


  parent reply	other threads:[~2023-02-23 19:48 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-23 19:47 [PATCH 0/6] Make GMP data members private Tom Tromey
2023-02-23 19:47 ` [PATCH 1/6] Clean up gmp-utils.h includes Tom Tromey
2023-02-23 19:48 ` Tom Tromey [this message]
2023-02-23 19:48 ` [PATCH 3/6] Rename gdb_mpz::val and make contents private Tom Tromey
2023-02-23 19:48 ` [PATCH 4/6] Add operators and methods to gdb_mpq Tom Tromey
2023-02-23 19:48 ` [PATCH 5/6] Rename gdb_mpq::val and make contents private Tom Tromey
2023-02-23 19:48 ` [PATCH 6/6] Hide the implementation of gdb_mpf Tom Tromey
2023-03-14 14:15 ` [PATCH 0/6] Make GMP data members private Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230223-submit-gmp-hiding-v1-2-ea6ff9e32716@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).