From patchwork Thu Feb 23 19:48:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 65534 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 061813851C00 for ; Thu, 23 Feb 2023 19:48:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 061813851C00 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677181714; bh=s3P1SFZ454Yq5kBCNic5Metel+JjCUIWh30NOVC722E=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=fwWpjaqepjbSaE7Vd/JqTTEr4OnyV0U9q3q0rqadzzA2YyPkCbkHeDjzVO6CtyprC F8kaGoHNthSJGqO0g+7uhNT0tJfBkpAHk9KCarDXpr8oLHdPFAWzKd7OelsBL6IWse y38m7HqcnZlGVUckDJKIWzQ01V6ZxxoD/5ujCifU= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12e.google.com (mail-il1-x12e.google.com [IPv6:2607:f8b0:4864:20::12e]) by sourceware.org (Postfix) with ESMTPS id 03116385843D for ; Thu, 23 Feb 2023 19:48:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 03116385843D Received: by mail-il1-x12e.google.com with SMTP id s8so5732702ilv.10 for ; Thu, 23 Feb 2023 11:48:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=s3P1SFZ454Yq5kBCNic5Metel+JjCUIWh30NOVC722E=; b=r/NKwGgA5KsMIUmv4w+ru2EyfWkAQ2xlT1Oha0QW+lGQMfpOdH9xA3Jf7QfQH4VXd3 Fp1kjrxtVEfrucMEayHh7aGswlht8QEe0uwb7WE1jSIPUR33cX9OU6ovZj9MLfjxJLVJ Kyfpwx71PvFnE4WGwdXtWgmGeo0HbVI7iCSlsxGhNP2+H9PXT2rVB8s3c5zwABMyvu6K ISF+Sf3KqLT56aEOaUdWDMOMubDorkYR+K5OzB6dLFtCqbLyRDXYiuCObNeVqm0twk3j mmgRkVDrCCUi9/j2o6f6jbj/dK+JGY24/pUUh+qGZK6uVQ1377UBxp1oGviQ0qMd99rl VMjg== X-Gm-Message-State: AO0yUKW78J5yCtl450l4eviWqfsvf6XjOzorkAR1IEmrGgu/TSl/h3ys U0poj5BVdb/LTuuZvrj7RGRfkeNl3I3rdUCH X-Google-Smtp-Source: AK7set9DIn8o6astc43EifXFVaYb70GWj3SebqXNhJK9z1X+8Kco4fvIImdkkDxPpC5YSSKhDXic4A== X-Received: by 2002:a05:6e02:1b0f:b0:315:9891:85f5 with SMTP id i15-20020a056e021b0f00b00315989185f5mr8756618ilv.15.1677181684145; Thu, 23 Feb 2023 11:48:04 -0800 (PST) Received: from localhost.localdomain (75-166-130-93.hlrn.qwest.net. [75.166.130.93]) by smtp.gmail.com with ESMTPSA id x4-20020a920604000000b00316e4139270sm2038351ilg.35.2023.02.23.11.48.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 11:48:03 -0800 (PST) Date: Thu, 23 Feb 2023 12:48:01 -0700 Subject: [PATCH 3/6] Rename gdb_mpz::val and make contents private MIME-Version: 1.0 Message-Id: <20230223-submit-gmp-hiding-v1-3-ea6ff9e32716@adacore.com> References: <20230223-submit-gmp-hiding-v1-0-ea6ff9e32716@adacore.com> In-Reply-To: <20230223-submit-gmp-hiding-v1-0-ea6ff9e32716@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.0 X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This changes gdb_mpz to hide its data, and renames the data member from 'val' to 'm_val', following gdb convention. --- gdb/gmp-utils.c | 46 ++++++++++++++++++------------------- gdb/gmp-utils.h | 70 +++++++++++++++++++++++++++++++-------------------------- 2 files changed, 61 insertions(+), 55 deletions(-) diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c index 8150fa5dcdf..aaa075e236e 100644 --- a/gdb/gmp-utils.c +++ b/gdb/gmp-utils.c @@ -46,7 +46,7 @@ void gdb_mpz::read (gdb::array_view buf, enum bfd_endian byte_order, bool unsigned_p) { - mpz_import (val, 1 /* count */, -1 /* order */, buf.size () /* size */, + mpz_import (m_val, 1 /* count */, -1 /* order */, buf.size () /* size */, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */, 0 /* nails */, buf.data () /* op */); @@ -57,9 +57,9 @@ gdb_mpz::read (gdb::array_view buf, enum bfd_endian byte_order, was in fact negative, we need to adjust VAL accordingly. */ gdb_mpz max; - mpz_ui_pow_ui (max.val, 2, buf.size () * HOST_CHAR_BIT - 1); - if (mpz_cmp (val, max.val) >= 0) - mpz_submul_ui (val, max.val, 2); + mpz_ui_pow_ui (max.m_val, 2, buf.size () * HOST_CHAR_BIT - 1); + if (mpz_cmp (m_val, max.m_val) >= 0) + mpz_submul_ui (m_val, max.m_val, 2); } } @@ -81,7 +81,7 @@ gdb_mpz::safe_export (gdb::array_view buf, { gdb_assert (buf.size () > 0); - if (mpz_sgn (val) == 0) + if (mpz_sgn (m_val) == 0) { /* Our value is zero, so no need to call mpz_export to do the work, especially since mpz_export's documentation explicitly says @@ -100,19 +100,19 @@ gdb_mpz::safe_export (gdb::array_view buf, { lo = 0; - mpz_ui_pow_ui (hi.val, 2, max_usable_bits); - mpz_sub_ui (hi.val, hi.val, 1); + mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits); + mpz_sub_ui (hi.m_val, hi.m_val, 1); } else { - mpz_ui_pow_ui (lo.val, 2, max_usable_bits - 1); - mpz_neg (lo.val, lo.val); + mpz_ui_pow_ui (lo.m_val, 2, max_usable_bits - 1); + mpz_neg (lo.m_val, lo.m_val); - mpz_ui_pow_ui (hi.val, 2, max_usable_bits - 1); - mpz_sub_ui (hi.val, hi.val, 1); + mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits - 1); + mpz_sub_ui (hi.m_val, hi.m_val, 1); } - if (mpz_cmp (val, lo.val) < 0 || mpz_cmp (val, hi.val) > 0) + if (mpz_cmp (m_val, lo.m_val) < 0 || mpz_cmp (m_val, hi.m_val) > 0) error (_("Cannot export value %s as %zu-bits %s integer" " (must be between %s and %s)"), this->str ().c_str (), @@ -121,17 +121,17 @@ gdb_mpz::safe_export (gdb::array_view buf, lo.str ().c_str (), hi.str ().c_str ()); - gdb_mpz exported_val (val); + gdb_mpz exported_val (m_val); - if (mpz_cmp_ui (exported_val.val, 0) < 0) + if (mpz_cmp_ui (exported_val.m_val, 0) < 0) { /* mpz_export does not handle signed values, so create a positive value whose bit representation as an unsigned of the same length would be the same as our negative value. */ gdb_mpz neg_offset; - mpz_ui_pow_ui (neg_offset.val, 2, buf.size () * HOST_CHAR_BIT); - mpz_add (exported_val.val, exported_val.val, neg_offset.val); + mpz_ui_pow_ui (neg_offset.m_val, 2, buf.size () * HOST_CHAR_BIT); + mpz_add (exported_val.m_val, exported_val.m_val, neg_offset.m_val); } /* Do the export into a buffer allocated by GMP itself; that way, @@ -147,7 +147,7 @@ gdb_mpz::safe_export (gdb::array_view buf, size_t word_countp; gdb::unique_xmalloc_ptr exported (mpz_export (NULL, &word_countp, -1 /* order */, buf.size () /* size */, - endian, 0 /* nails */, exported_val.val)); + endian, 0 /* nails */, exported_val.m_val)); gdb_assert (word_countp == 1); @@ -170,19 +170,19 @@ gdb_mpq::get_rounded () const towards zero. */ gdb_mpz quotient, remainder; - mpz_fdiv_qr (quotient.val, remainder.val, + mpz_fdiv_qr (quotient.m_val, remainder.m_val, mpq_numref (abs_val.val), mpq_denref (abs_val.val)); /* Multiply the remainder by 2, and see if it is greater or equal to abs_val's denominator. If yes, round to the next integer. */ - mpz_mul_ui (remainder.val, remainder.val, 2); - if (mpz_cmp (remainder.val, mpq_denref (abs_val.val)) >= 0) - mpz_add_ui (quotient.val, quotient.val, 1); + mpz_mul_ui (remainder.m_val, remainder.m_val, 2); + if (mpz_cmp (remainder.m_val, mpq_denref (abs_val.val)) >= 0) + mpz_add_ui (quotient.m_val, quotient.m_val, 1); /* Re-apply the sign if needed. */ if (mpq_sgn (val) < 0) - mpz_neg (quotient.val, quotient.val); + mpz_neg (quotient.m_val, quotient.m_val); return quotient; } @@ -197,7 +197,7 @@ gdb_mpq::read_fixed_point (gdb::array_view buf, gdb_mpz vz; vz.read (buf, byte_order, unsigned_p); - mpq_set_z (val, vz.val); + mpq_set_z (val, vz.m_val); mpq_mul (val, val, scaling_factor.val); } diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index dcb998a1656..601d9a6a1ec 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -31,25 +31,26 @@ std::string gmp_string_printf (const char *fmt, ...); +struct gdb_mpq; +struct gdb_mpf; + /* A class to make it easier to use GMP's mpz_t values within GDB. */ struct gdb_mpz { - mpz_t val; - /* Constructors. */ - gdb_mpz () { mpz_init (val); } + gdb_mpz () { mpz_init (m_val); } explicit gdb_mpz (const mpz_t &from_val) { - mpz_init (val); - mpz_set (val, from_val); + mpz_init (m_val); + mpz_set (m_val, from_val); } gdb_mpz (const gdb_mpz &from) { - mpz_init (val); - mpz_set (val, from.val); + mpz_init (m_val); + mpz_set (m_val, from.m_val); } /* Initialize using the given integral value. @@ -59,26 +60,26 @@ struct gdb_mpz template>> explicit gdb_mpz (T src) { - mpz_init (val); + mpz_init (m_val); set (src); } explicit gdb_mpz (gdb_mpz &&from) { - mpz_init (val); - mpz_swap (val, from.val); + mpz_init (m_val); + mpz_swap (m_val, from.m_val); } gdb_mpz &operator= (const gdb_mpz &from) { - mpz_set (val, from.val); + mpz_set (m_val, from.m_val); return *this; } gdb_mpz &operator= (gdb_mpz &&other) { - mpz_swap (val, other.val); + mpz_swap (m_val, other.m_val); return *this; } @@ -93,14 +94,14 @@ struct gdb_mpz the string was parsed successfully, false otherwise. */ bool set (const char *str, int base) { - return mpz_set_str (val, str, base) != -1; + return mpz_set_str (m_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); + mpz_ui_pow_ui (result.m_val, base, exp); return result; } @@ -125,59 +126,59 @@ struct gdb_mpz bool unsigned_p) const; /* Return a string containing VAL. */ - std::string str () const { return gmp_string_printf ("%Zd", val); } + std::string str () const { return gmp_string_printf ("%Zd", m_val); } /* The destructor. */ - ~gdb_mpz () { mpz_clear (val); } + ~gdb_mpz () { mpz_clear (m_val); } /* Negate this value in place. */ void negate () { - mpz_neg (val, val); + mpz_neg (m_val, m_val); } gdb_mpz &operator*= (long other) { - mpz_mul_si (val, val, other); + mpz_mul_si (m_val, m_val, other); return *this; } gdb_mpz &operator+= (unsigned long other) { - mpz_add_ui (val, val, other); + mpz_add_ui (m_val, m_val, other); return *this; } gdb_mpz &operator-= (unsigned long other) { - mpz_sub_ui (val, val, other); + mpz_sub_ui (m_val, m_val, other); return *this; } gdb_mpz &operator<<= (unsigned long nbits) { - mpz_mul_2exp (val, val, nbits); + mpz_mul_2exp (m_val, m_val, nbits); return *this; } bool operator> (const gdb_mpz &other) const { - return mpz_cmp (val, other.val) > 0; + return mpz_cmp (m_val, other.m_val) > 0; } bool operator< (int other) const { - return mpz_cmp_si (val, other) < 0; + return mpz_cmp_si (m_val, other) < 0; } bool operator== (int other) const { - return mpz_cmp_si (val, other) == 0; + return mpz_cmp_si (m_val, other) == 0; } bool operator== (const gdb_mpz &other) const { - return mpz_cmp (val, other.val) == 0; + return mpz_cmp (m_val, other.m_val) == 0; } private: @@ -202,6 +203,11 @@ struct gdb_mpz being exported. */ void safe_export (gdb::array_view buf, int endian, bool unsigned_p) const; + + friend struct gdb_mpq; + friend struct gdb_mpf; + + mpz_t m_val; }; /* A class to make it easier to use GMP's mpq_t values within GDB. */ @@ -234,8 +240,8 @@ struct gdb_mpq 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); + mpz_set (mpq_numref (val), num.m_val); + mpz_set (mpq_denref (val), denom.m_val); mpq_canonicalize (val); } @@ -254,7 +260,7 @@ struct gdb_mpq gdb_mpq &operator= (const gdb_mpz &from) { - mpq_set_z (val, from.val); + mpq_set_z (val, from.m_val); return *this; } @@ -268,7 +274,7 @@ struct gdb_mpq gdb_mpz as_integer () const { gdb_mpz result; - mpz_tdiv_q (result.val, mpq_numref (val), mpq_denref (val)); + mpz_tdiv_q (result.m_val, mpq_numref (val), mpq_denref (val)); return result; } @@ -340,7 +346,7 @@ template void gdb_mpz::set (T src) { - mpz_import (val, 1 /* count */, -1 /* order */, + mpz_import (m_val, 1 /* count */, -1 /* order */, sizeof (T) /* size */, 0 /* endian (0 = native) */, 0 /* nails */, &src /* op */); if (std::is_signed::value && src < 0) @@ -350,8 +356,8 @@ gdb_mpz::set (T src) the correct negative value. */ gdb_mpz neg_offset; - mpz_ui_pow_ui (neg_offset.val, 2, sizeof (T) * HOST_CHAR_BIT); - mpz_sub (val, val, neg_offset.val); + mpz_ui_pow_ui (neg_offset.m_val, 2, sizeof (T) * HOST_CHAR_BIT); + mpz_sub (m_val, m_val, neg_offset.m_val); } }