From patchwork Thu Feb 23 19:47:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 65537 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 1B3D9385B52D for ; Thu, 23 Feb 2023 19:48:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1B3D9385B52D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677181739; bh=xO86pLQnEuyIGtThBPu93pJstN3l4vmlraHtJmbhWVI=; 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=cdOGQY+nrYnXRJ7e/2Tx0V167ZVBIR0mKax1yc8KDt0L9wS/G9gzeoANtEYCU1g71 5JscbzUtoSHmXmE62a8WcUhM7cPUajZBfiCVIRH2/Trw5LTSMjaysGEFYee9tX6Gi9 6Ysy7hZYel4OslwhIKzf5UkemsAy4uu39PfkMqHg= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) by sourceware.org (Postfix) with ESMTPS id 28298385840F for ; Thu, 23 Feb 2023 19:48:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 28298385840F Received: by mail-il1-x135.google.com with SMTP id i4so4397816ils.1 for ; Thu, 23 Feb 2023 11:48:03 -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=xO86pLQnEuyIGtThBPu93pJstN3l4vmlraHtJmbhWVI=; b=WgjGNyAxF6C0ql4Nukf4nmc2eMcOfe4bXtQcG1a6wZc6X1SNs3bhE68klFIjNm2cRX EtV37xntuByfn7i82eRAXzyVdJXCJ+6CCFYIDPegAEY5UX7vjts81hOjIYieBaLl8ojA ReKWw/VlJK//5f4VTJEXk+2VJl9/WYokTV/VlEq7dJiJYURRR3OSUsq5Uw7lX/1tHp7o e4DPLj6RKhdH7Eh1wYSFrS7RLqFDRK2JplAl9E3B0fjipD6io5gkqTXhRuIXb2Gmtq01 j362WWxj9UPXU1SXLNsSv6yW7u4ojxUvvlxDFe2pYsbIELboTAxJay92C4FosJ4PYtXZ GDUw== X-Gm-Message-State: AO0yUKVOczTJ3BRtkjX67+5EjCG5eVVuYhfxEG3+2zTauDtd606JXgox GNbTSpD3O909OgvUkcERuY+50fh57bDP9Lqi X-Google-Smtp-Source: AK7set+ahUkZjHWw7Fbu0+dkWIz++UpUfEdSdzcf3gl0Uwb+3QKxeY2u/OggSHmnja2xq/IxnuWvgQ== X-Received: by 2002:a92:7a0e:0:b0:315:4c18:5142 with SMTP id v14-20020a927a0e000000b003154c185142mr9676591ilc.10.1677181682421; Thu, 23 Feb 2023 11:48:02 -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.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 11:48:01 -0800 (PST) Date: Thu, 23 Feb 2023 12:47:59 -0700 Subject: [PATCH 1/6] Clean up gmp-utils.h includes MIME-Version: 1.0 Message-Id: <20230223-submit-gmp-hiding-v1-1-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, KAM_SHORT, 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" gmp-utils.h includes "defs.h", but normally the rule in gdb is that the .c files include this first. This patch changes this code to match the rest of gdb. --- gdb/gmp-utils.c | 1 + gdb/gmp-utils.h | 2 -- gdb/unittests/gmp-utils-selftests.c | 1 + 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c index b9c9b495398..8150fa5dcdf 100644 --- a/gdb/gmp-utils.c +++ b/gdb/gmp-utils.c @@ -15,6 +15,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +#include "defs.h" #include "gmp-utils.h" /* See gmp-utils.h. */ diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index 0e57471b2ef..0522f3453aa 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -20,8 +20,6 @@ #ifndef GMP_UTILS_H #define GMP_UTILS_H -#include "defs.h" - /* Include and ahead of , so as to get access to GMP's various formatting functions. */ #include diff --git a/gdb/unittests/gmp-utils-selftests.c b/gdb/unittests/gmp-utils-selftests.c index cc525a13fc0..e2ccd2ca094 100644 --- a/gdb/unittests/gmp-utils-selftests.c +++ b/gdb/unittests/gmp-utils-selftests.c @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +#include "defs.h" #include "gmp-utils.h" #include "gdbsupport/selftest.h" From patchwork Thu Feb 23 19:48:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 65535 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 AA142385086B for ; Thu, 23 Feb 2023 19:48:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AA142385086B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677181718; bh=LXbkZOQ7ar5DP+EXnpCiL5n428qGDe47ynnSE9fZgRI=; 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=OBPy3xfy3guImh9c9yAdPE0K68qkj1ZVBZJqVk32buSiqBmWnejMUMyvAV/j+tLD2 HFbvxWphBFiLGb5FMUCmcpjnYm0YBECDuM2xk6cCRBmaX563zb9c4UfjCKIM9xT+DC 0Pk6u1S5uNBNAtKHDVf/BBcxsGqUI2KpFM3Rurec= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by sourceware.org (Postfix) with ESMTPS id 2F603385842B for ; Thu, 23 Feb 2023 19:48:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F603385842B Received: by mail-il1-x12a.google.com with SMTP id e10so5865755ilu.4 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=LXbkZOQ7ar5DP+EXnpCiL5n428qGDe47ynnSE9fZgRI=; b=VvYoGjU1EEYMUtciT8l30uVtkpfQPTEWevsxba9f7XOVlyRPDQKj9brRFpAl0H0cCt bvNHVukGlBheiTPex8L1p09OFiyn0CiC5GaoQq6rZT5dNkrW9czARZ2EWPbrW8lDTAwB e5fckoenn5oQKcHxLjNZx3nIWKrBwQ1iDN1TB6kn6FuQASJi6bBmigcj/rpvLvrEuixf igNK0+TNBrJDh/mpDV3tvIdB/26+9wUCXzxapSIjQozQ/IyOKqYRkjLKJApa39j205dU 2tsjN/XpUbDQpHQpK+XaYcbaL4mkuOS7/9JGJTQdMDFO3nxVOqFNDTdOildGJ1pN7mG6 dbeQ== X-Gm-Message-State: AO0yUKUr5qDW9DBerlzCczzuijf+tFuz9relQrx3rhjzqfmMMc5MoSMV ojXIsOagb+a5LJXuWmNhP3fniS3iFKyFEqfJ X-Google-Smtp-Source: AK7set9YP9s/OuoBLqMpVyTGk5HamfmVPnCFuvbZcDsJs7mjPM6Y3CfDCjdB1BTrrb8V/rn/RwUC1A== X-Received: by 2002:a05:6e02:1485:b0:315:3eac:6293 with SMTP id n5-20020a056e02148500b003153eac6293mr9984349ilk.4.1677181683258; Thu, 23 Feb 2023 11:48:03 -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.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 11:48:02 -0800 (PST) Date: Thu, 23 Feb 2023 12:48:00 -0700 Subject: [PATCH 2/6] Add methods and operators to gdb_mpz MIME-Version: 1.0 Message-Id: <20230223-submit-gmp-hiding-v1-2-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 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 " / ". */ 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 () == 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 () == l_expected); if (i >= 0) { ul_expected = (ULONGEST) i; - mpz_set_ui (v.val, i); + v = ul_expected; SELF_CHECK (v.as_integer () == 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 () == l_expected); SELF_CHECK (v.as_integer () == 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 () == 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 (v); check_as_integer_raises_out_of_range_error (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) LONGEST_MAX + 1); check_as_integer_raises_out_of_range_error (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 (v); check_as_integer_raises_out_of_range_error (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::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 (); } 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); } } From patchwork Thu Feb 23 19:48:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 65536 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 B08A0385085E for ; Thu, 23 Feb 2023 19:48:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B08A0385085E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677181728; bh=Zj5yyU1ekiszZFgMECdPigC0m3a4TkLqmMCOxun2XmI=; 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=oQHKsFcW+L7k7jqwASfuBIpxFPklTVSHneQ6TxJ+EhwiZerSCLSeBdTVYmCVgsyCe lGEUM1qRuzEY2A7ufoLUViFXIhEFKJjROT6oEyf//6VM+FpAEqpVE59cQ1yGlOXq8y sEbkXvDFPjdOgh1lVH/+7buJZlIlgyLqueJTrzKw= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x130.google.com (mail-il1-x130.google.com [IPv6:2607:f8b0:4864:20::130]) by sourceware.org (Postfix) with ESMTPS id A0808385802F for ; Thu, 23 Feb 2023 19:48:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A0808385802F Received: by mail-il1-x130.google.com with SMTP id i12so87550ila.5 for ; Thu, 23 Feb 2023 11:48:05 -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=Zj5yyU1ekiszZFgMECdPigC0m3a4TkLqmMCOxun2XmI=; b=rl+kbzk+Z/hnyBgOmg3oGHrK2H7ScOLx0B6U0ycX6q1qhi2C+Z8LfA8JMuxkW32FHi evuaB0YLfUGyGkm4IuqtKGYL2Y8tPq3bgDWpPCWGQS+ZsDTFU5grCidB4+4KTshnWLSJ Fwa6v1ohhlzQu+c3DIRCOEB+PjR4tMGJq1BDtFyDQ5YpwGQ9WC49KpB224jTe9Rl4VN+ tSjDExf8qXRSoQuv2ZJ1+n+v4bOCTGaOiykXmSJMxFeAjNKDxFvfZVGc8A9t2bMQ32di JUQCrdC6W9cWCFlpDZu4szTOi3v9x5mnueknuSBuaYcNQc/g+ZL+xFmtlGQ8LHi/gSW9 tExg== X-Gm-Message-State: AO0yUKWSjHcGNs4UB1r6lkWGdciXBkXO7r4sVuX6dP9lZl1nhenQJ2wO p6NLR66Kb9HR4OycWRYBZpTCJmhpst7EzXu5 X-Google-Smtp-Source: AK7set8vEpkk9MP0enYhalgKuwgZVNIJw5h6/rm53Rg+zZqcjrnSwufDwwWO0qCC5TZ/DKVYVQyYhQ== X-Received: by 2002:a05:6e02:1c22:b0:313:e6ae:3490 with SMTP id m2-20020a056e021c2200b00313e6ae3490mr10808907ilh.14.1677181684845; 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.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 11:48:04 -0800 (PST) Date: Thu, 23 Feb 2023 12:48:02 -0700 Subject: [PATCH 4/6] Add operators and methods to gdb_mpq MIME-Version: 1.0 Message-Id: <20230223-submit-gmp-hiding-v1-4-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 adds some operators and methods to gdb_mpq, in preparation for making its implementation private. This only adds the operators currently needed by gdb. More could be added as necessary. --- gdb/gmp-utils.h | 72 +++++++++++++++++++++++++++++++++++++ gdb/unittests/gmp-utils-selftests.c | 34 ++++++++---------- gdb/valarith.c | 18 +++++----- gdb/valops.c | 13 +++---- 4 files changed, 100 insertions(+), 37 deletions(-) diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index 601d9a6a1ec..dfb61302f6b 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -245,6 +245,13 @@ struct gdb_mpq mpq_canonicalize (val); } + gdb_mpq (long num, long denom) + { + mpq_init (val); + mpq_set_si (val, num, denom); + mpq_canonicalize (val); + } + /* Copy assignment operator. */ gdb_mpq &operator= (const gdb_mpq &from) { @@ -264,6 +271,67 @@ struct gdb_mpq return *this; } + gdb_mpq &operator= (double d) + { + mpq_set_d (val, d); + return *this; + } + + /* Return the sign of this value. This returns -1 for a negative + value, 0 if the value is 0, and 1 for a positive value. */ + int sgn () const + { return mpq_sgn (val); } + + gdb_mpq operator+ (const gdb_mpq &other) const + { + gdb_mpq result; + mpq_add (result.val, val, other.val); + return result; + } + + gdb_mpq operator- (const gdb_mpq &other) const + { + gdb_mpq result; + mpq_sub (result.val, val, other.val); + return result; + } + + gdb_mpq operator* (const gdb_mpq &other) const + { + gdb_mpq result; + mpq_mul (result.val, val, other.val); + return result; + } + + gdb_mpq operator/ (const gdb_mpq &other) const + { + gdb_mpq result; + mpq_div (result.val, val, other.val); + return result; + } + + gdb_mpq &operator*= (const gdb_mpq &other) + { + mpq_mul (val, val, other.val); + return *this; + } + + gdb_mpq &operator/= (const gdb_mpq &other) + { + mpq_div (val, val, other.val); + return *this; + } + + bool operator== (const gdb_mpq &other) const + { + return mpq_cmp (val, other.val) == 0; + } + + bool operator< (const gdb_mpq &other) const + { + return mpq_cmp (val, other.val) < 0; + } + /* Return a string representing VAL as " / ". */ std::string str () const { return gmp_string_printf ("%Qd", val); } @@ -278,6 +346,10 @@ struct gdb_mpq return result; } + /* Return this value converted to a host double. */ + double as_double () const + { return mpq_get_d (val); } + /* 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 8e028fec88c..384ca2df212 100644 --- a/gdb/unittests/gmp-utils-selftests.c +++ b/gdb/unittests/gmp-utils-selftests.c @@ -399,8 +399,8 @@ read_fp_test (int unscaled, const gdb_mpq &scaling_factor, actual.read_fixed_point ({buf, len}, byte_order, 0, scaling_factor); - mpq_set_si (expected.val, unscaled, 1); - mpq_mul (expected.val, expected.val, scaling_factor.val); + expected = gdb_mpq (unscaled, 1); + expected *= scaling_factor; } /* Perform various tests of the gdb_mpq::read_fixed_point method. */ @@ -409,38 +409,37 @@ static void gdb_mpq_read_fixed_point () { gdb_mpq expected, actual; - gdb_mpq scaling_factor; /* Pick an arbitrary scaling_factor; this operation is trivial enough thanks to GMP that the value we use isn't really important. */ - mpq_set_ui (scaling_factor.val, 3, 5); + gdb_mpq scaling_factor (3, 5); /* Try a few values, both negative and positive... */ read_fp_test (-256, scaling_factor, BFD_ENDIAN_BIG, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0); + SELF_CHECK (actual == expected); read_fp_test (-256, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0); + SELF_CHECK (actual == expected); read_fp_test (-1, scaling_factor, BFD_ENDIAN_BIG, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0); + SELF_CHECK (actual == expected); read_fp_test (-1, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0); + SELF_CHECK (actual == expected); read_fp_test (0, scaling_factor, BFD_ENDIAN_BIG, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0); + SELF_CHECK (actual == expected); read_fp_test (0, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0); + SELF_CHECK (actual == expected); read_fp_test (1, scaling_factor, BFD_ENDIAN_BIG, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0); + SELF_CHECK (actual == expected); read_fp_test (1, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0); + SELF_CHECK (actual == expected); read_fp_test (1025, scaling_factor, BFD_ENDIAN_BIG, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0); + SELF_CHECK (actual == expected); read_fp_test (1025, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0); + SELF_CHECK (actual == expected); } /* A helper function which builds a gdb_mpq object from the given @@ -463,9 +462,7 @@ write_fp_test (int numerator, unsigned int denominator, gdb_byte buf[len]; memset (buf, 0, len); - gdb_mpq v; - mpq_set_si (v.val, numerator, denominator); - mpq_canonicalize (v.val); + gdb_mpq v (numerator, denominator); v.write_fixed_point ({buf, len}, byte_order, 0, scaling_factor); return extract_unsigned_integer (buf, len, byte_order); @@ -479,8 +476,7 @@ gdb_mpq_write_fixed_point () /* Pick an arbitrary factor; this operations is sufficiently trivial with the use of GMP that the value of this factor is not really all that important. */ - gdb_mpq scaling_factor; - mpq_set_ui (scaling_factor.val, 1, 3); + gdb_mpq scaling_factor (1, 3); gdb_mpq vq; diff --git a/gdb/valarith.c b/gdb/valarith.c index a6a5f5102a2..f8ce508e825 100644 --- a/gdb/valarith.c +++ b/gdb/valarith.c @@ -883,43 +883,43 @@ fixed_point_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) switch (op) { case BINOP_ADD: - mpq_add (res.val, v1.val, v2.val); + res = v1 + v2; val = fixed_point_to_value (res); break; case BINOP_SUB: - mpq_sub (res.val, v1.val, v2.val); + res = v1 - v2; val = fixed_point_to_value (res); break; case BINOP_MIN: - val = fixed_point_to_value (mpq_cmp (v1.val, v2.val) < 0 ? v1 : v2); + val = fixed_point_to_value (std::min (v1, v2)); break; case BINOP_MAX: - val = fixed_point_to_value (mpq_cmp (v1.val, v2.val) > 0 ? v1 : v2); + val = fixed_point_to_value (std::max (v1, v2)); break; case BINOP_MUL: - mpq_mul (res.val, v1.val, v2.val); + res = v1 * v2; val = fixed_point_to_value (res); break; case BINOP_DIV: - if (mpq_sgn (v2.val) == 0) + if (v2.sgn () == 0) error (_("Division by zero")); - mpq_div (res.val, v1.val, v2.val); + res = v1 / v2; val = fixed_point_to_value (res); break; case BINOP_EQUAL: val = value_from_ulongest (language_bool_type (language, gdbarch), - mpq_cmp (v1.val, v2.val) == 0 ? 1 : 0); + v1 == v2 ? 1 : 0); break; case BINOP_LESS: val = value_from_ulongest (language_bool_type (language, gdbarch), - mpq_cmp (v1.val, v2.val) < 0 ? 1 : 0); + v1 < v2 ? 1 : 0); break; default: diff --git a/gdb/valops.c b/gdb/valops.c index e0936d45cb3..2cef24fc4c5 100644 --- a/gdb/valops.c +++ b/gdb/valops.c @@ -341,11 +341,7 @@ value_to_gdb_mpq (struct value *value) gdb_mpq result; if (is_floating_type (type)) - { - double d = target_float_to_host_double (value->contents ().data (), - type); - mpq_set_d (result.val, d); - } + result = target_float_to_host_double (value->contents ().data (), type); else { gdb_assert (is_integral_type (type) @@ -357,8 +353,7 @@ value_to_gdb_mpq (struct value *value) result = vz; if (is_fixed_point_type (type)) - mpq_mul (result.val, result.val, - type->fixed_point_scaling_factor ().val); + result *= type->fixed_point_scaling_factor (); } return result; @@ -386,7 +381,7 @@ value_cast_to_fixed_point (struct type *to_type, struct value *from_val) /* Divide that value by the scaling factor to obtain the unscaled value, first in rational form, and then in integer form. */ - mpq_div (vq.val, vq.val, to_type->fixed_point_scaling_factor ().val); + vq /= to_type->fixed_point_scaling_factor (); gdb_mpz unscaled = vq.get_rounded (); /* Finally, create the result value, and pack the unscaled value @@ -559,7 +554,7 @@ value_cast (struct type *type, struct value *arg2) struct value *v = value::allocate (to_type); target_float_from_host_double (v->contents_raw ().data (), - to_type, mpq_get_d (fp_val.val)); + to_type, fp_val.as_double ()); return v; } From patchwork Thu Feb 23 19:48:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 65538 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 4B7F5385021E for ; Thu, 23 Feb 2023 19:49:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4B7F5385021E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677181743; bh=jvHv1aYbbeYM4jOKEm4rMOs1oTRZ0KT/1rqbQhijY9U=; 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=PGWrn7XSqDqw60e1zXg8qYY2aoplKPpJQHaL1Ce5Cc5HRlNFjsPNZhFgzubpSDPcV kXojiov7qnW2j21ioOwSrBUdLbJUMLU/pCoB5vmq3HPVT77wLqi5ZLeS0ntiskJrfQ o+hGbD2YNPzvFqiGRdz+U/AjA1J1spTFevVJkrgk= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x133.google.com (mail-il1-x133.google.com [IPv6:2607:f8b0:4864:20::133]) by sourceware.org (Postfix) with ESMTPS id 63BB33857437 for ; Thu, 23 Feb 2023 19:48:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 63BB33857437 Received: by mail-il1-x133.google.com with SMTP id i12so87568ila.5 for ; Thu, 23 Feb 2023 11:48:06 -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=jvHv1aYbbeYM4jOKEm4rMOs1oTRZ0KT/1rqbQhijY9U=; b=HIc+2N113kswLvh4qnDgDZ1D5E7u+2bWDyHVz7bBR0O5scwDe4AmtJ5Y9fda7PUG+P 4YgUHtXMHHVKTUFIsrMtvvPaY0FDbcPuiVolou4D03Mel37gD1+4f+VrCxW4QYGDsYTr NgNyvK4ErenBMG0ifvJyM8iEJjuABIx3Xj/rmRO9Oj89Z71HyFjDEGp1tHoVoXYjVTK4 1Jkbpex6MlD3zHC/hCsJ4HE+zA3YHCu81DL3THMeRE9VajzR8aGQLTM7wWcpCbyLNJdO ArIzoOVhon/lJintU0LyxdG5SZt9qd3kAG+grsc+kQQyLdIvNuab5SZ2CydZQD3dsmVL wjBQ== X-Gm-Message-State: AO0yUKVOtjrSSDYxuYRJ6Kv8CoLm8CzxMI8ZUT2e/eUMPVAgOQMW+ysb +02P/bkfFCSHPAz/Ks87AspRjaxK+atIiHUL X-Google-Smtp-Source: AK7set+eayRUXNhKrAHKJhTiZqMAoZ36qekkZqRFlJ/kSy2oKakdtq7rcl1CQjbQnTIt7SyKv3XGBg== X-Received: by 2002:a05:6e02:154c:b0:313:e397:ee95 with SMTP id j12-20020a056e02154c00b00313e397ee95mr10433410ilu.4.1677181685538; Thu, 23 Feb 2023 11:48:05 -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.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 11:48:05 -0800 (PST) Date: Thu, 23 Feb 2023 12:48:03 -0700 Subject: [PATCH 5/6] Rename gdb_mpq::val and make contents private MIME-Version: 1.0 Message-Id: <20230223-submit-gmp-hiding-v1-5-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_mpq to hide its data, and renames the data member from 'val' to 'm_val', following gdb convention. --- gdb/gmp-utils.c | 18 +++++++-------- gdb/gmp-utils.h | 72 ++++++++++++++++++++++++++++++--------------------------- 2 files changed, 47 insertions(+), 43 deletions(-) diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c index aaa075e236e..d134bc32a1d 100644 --- a/gdb/gmp-utils.c +++ b/gdb/gmp-utils.c @@ -162,8 +162,8 @@ gdb_mpq::get_rounded () const /* Work with a positive number so as to make the "floor" rounding always round towards zero. */ - gdb_mpq abs_val (val); - mpq_abs (abs_val.val, abs_val.val); + gdb_mpq abs_val (m_val); + mpq_abs (abs_val.m_val, abs_val.m_val); /* Convert our rational number into a quotient and remainder, with "floor" rounding, which in our case means rounding @@ -171,17 +171,17 @@ gdb_mpq::get_rounded () const gdb_mpz quotient, remainder; mpz_fdiv_qr (quotient.m_val, remainder.m_val, - mpq_numref (abs_val.val), mpq_denref (abs_val.val)); + mpq_numref (abs_val.m_val), mpq_denref (abs_val.m_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.m_val, remainder.m_val, 2); - if (mpz_cmp (remainder.m_val, mpq_denref (abs_val.val)) >= 0) + if (mpz_cmp (remainder.m_val, mpq_denref (abs_val.m_val)) >= 0) mpz_add_ui (quotient.m_val, quotient.m_val, 1); /* Re-apply the sign if needed. */ - if (mpq_sgn (val) < 0) + if (mpq_sgn (m_val) < 0) mpz_neg (quotient.m_val, quotient.m_val); return quotient; @@ -197,8 +197,8 @@ gdb_mpq::read_fixed_point (gdb::array_view buf, gdb_mpz vz; vz.read (buf, byte_order, unsigned_p); - mpq_set_z (val, vz.m_val); - mpq_mul (val, val, scaling_factor.val); + mpq_set_z (m_val, vz.m_val); + mpq_mul (m_val, m_val, scaling_factor.m_val); } /* See gmp-utils.h. */ @@ -208,9 +208,9 @@ gdb_mpq::write_fixed_point (gdb::array_view buf, enum bfd_endian byte_order, bool unsigned_p, const gdb_mpq &scaling_factor) const { - gdb_mpq unscaled (val); + gdb_mpq unscaled (m_val); - mpq_div (unscaled.val, unscaled.val, scaling_factor.val); + mpq_div (unscaled.m_val, unscaled.m_val, scaling_factor.m_val); gdb_mpz unscaled_z = unscaled.get_rounded (); unscaled_z.write (buf, byte_order, unsigned_p); diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index dfb61302f6b..7889756a0ea 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -214,126 +214,124 @@ struct gdb_mpz struct gdb_mpq { - mpq_t val; - /* Constructors. */ - gdb_mpq () { mpq_init (val); } + gdb_mpq () { mpq_init (m_val); } explicit gdb_mpq (const mpq_t &from_val) { - mpq_init (val); - mpq_set (val, from_val); + mpq_init (m_val); + mpq_set (m_val, from_val); } gdb_mpq (const gdb_mpq &from) { - mpq_init (val); - mpq_set (val, from.val); + mpq_init (m_val); + mpq_set (m_val, from.m_val); } explicit gdb_mpq (gdb_mpq &&from) { - mpq_init (val); - mpq_swap (val, from.val); + mpq_init (m_val); + mpq_swap (m_val, from.m_val); } gdb_mpq (const gdb_mpz &num, const gdb_mpz &denom) { - mpq_init (val); - mpz_set (mpq_numref (val), num.m_val); - mpz_set (mpq_denref (val), denom.m_val); - mpq_canonicalize (val); + mpq_init (m_val); + mpz_set (mpq_numref (m_val), num.m_val); + mpz_set (mpq_denref (m_val), denom.m_val); + mpq_canonicalize (m_val); } gdb_mpq (long num, long denom) { - mpq_init (val); - mpq_set_si (val, num, denom); - mpq_canonicalize (val); + mpq_init (m_val); + mpq_set_si (m_val, num, denom); + mpq_canonicalize (m_val); } /* Copy assignment operator. */ gdb_mpq &operator= (const gdb_mpq &from) { - mpq_set (val, from.val); + mpq_set (m_val, from.m_val); return *this; } gdb_mpq &operator= (gdb_mpq &&from) { - mpq_swap (val, from.val); + mpq_swap (m_val, from.m_val); return *this; } gdb_mpq &operator= (const gdb_mpz &from) { - mpq_set_z (val, from.m_val); + mpq_set_z (m_val, from.m_val); return *this; } gdb_mpq &operator= (double d) { - mpq_set_d (val, d); + mpq_set_d (m_val, d); return *this; } /* Return the sign of this value. This returns -1 for a negative value, 0 if the value is 0, and 1 for a positive value. */ int sgn () const - { return mpq_sgn (val); } + { return mpq_sgn (m_val); } gdb_mpq operator+ (const gdb_mpq &other) const { gdb_mpq result; - mpq_add (result.val, val, other.val); + mpq_add (result.m_val, m_val, other.m_val); return result; } gdb_mpq operator- (const gdb_mpq &other) const { gdb_mpq result; - mpq_sub (result.val, val, other.val); + mpq_sub (result.m_val, m_val, other.m_val); return result; } gdb_mpq operator* (const gdb_mpq &other) const { gdb_mpq result; - mpq_mul (result.val, val, other.val); + mpq_mul (result.m_val, m_val, other.m_val); return result; } gdb_mpq operator/ (const gdb_mpq &other) const { gdb_mpq result; - mpq_div (result.val, val, other.val); + mpq_div (result.m_val, m_val, other.m_val); return result; } gdb_mpq &operator*= (const gdb_mpq &other) { - mpq_mul (val, val, other.val); + mpq_mul (m_val, m_val, other.m_val); return *this; } gdb_mpq &operator/= (const gdb_mpq &other) { - mpq_div (val, val, other.val); + mpq_div (m_val, m_val, other.m_val); return *this; } bool operator== (const gdb_mpq &other) const { - return mpq_cmp (val, other.val) == 0; + return mpq_cmp (m_val, other.m_val) == 0; } bool operator< (const gdb_mpq &other) const { - return mpq_cmp (val, other.val) < 0; + return mpq_cmp (m_val, other.m_val) < 0; } /* Return a string representing VAL as " / ". */ - std::string str () const { return gmp_string_printf ("%Qd", val); } + std::string str () const { return gmp_string_printf ("%Qd", m_val); } /* Return VAL rounded to the nearest integer. */ gdb_mpz get_rounded () const; @@ -342,13 +340,13 @@ struct gdb_mpq gdb_mpz as_integer () const { gdb_mpz result; - mpz_tdiv_q (result.m_val, mpq_numref (val), mpq_denref (val)); + mpz_tdiv_q (result.m_val, mpq_numref (m_val), mpq_denref (m_val)); return result; } /* Return this value converted to a host double. */ double as_double () const - { return mpq_get_d (val); } + { return mpq_get_d (m_val); } /* Set VAL from the contents of the given byte array (BUF), which contains the unscaled value of a fixed point type object. @@ -374,7 +372,13 @@ struct gdb_mpq const gdb_mpq &scaling_factor) const; /* The destructor. */ - ~gdb_mpq () { mpq_clear (val); } + ~gdb_mpq () { mpq_clear (m_val); } + +private: + + friend struct gdb_mpf; + + mpq_t m_val; }; /* A class to make it easier to use GMP's mpf_t values within GDB. @@ -405,7 +409,7 @@ struct gdb_mpf gdb_mpq tmp_q; tmp_q.read_fixed_point (buf, byte_order, unsigned_p, scaling_factor); - mpf_set_q (val, tmp_q.val); + mpf_set_q (val, tmp_q.m_val); } /* The destructor. */ From patchwork Thu Feb 23 19:48:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 65539 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 741AC3851C20 for ; Thu, 23 Feb 2023 19:49:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 741AC3851C20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677181769; bh=G2IKBNCIARdKpDafsvf6YxEvsA9MUyeCjC2nwgK2vtc=; 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=KeGKiCyqz05CuZpyeDnm8jz/x74XNzGFSvFL+Nv+DmzUpYpW0ISHaR18HFBDAXJV8 wKhpWdQLxgPERya0CL0FcdIY5pU9kH2R94kMe6dbVkvGpwRnX2EeO30cWZwWqsuYlQ q5tCiAbHtem8VSU2D4yaoJLVNGUL06zsLFaLZv8w= 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 012883857026 for ; Thu, 23 Feb 2023 19:48:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 012883857026 Received: by mail-il1-x12e.google.com with SMTP id e10so5865834ilu.4 for ; Thu, 23 Feb 2023 11:48:06 -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=G2IKBNCIARdKpDafsvf6YxEvsA9MUyeCjC2nwgK2vtc=; b=WVXbaogyUkYKqKe0vfOB5NEBqmGGwVFWCqW96W8jpWyMXMw1QcseBN7IDFMOY5jVzq Jr2JXRFbLZooDct8usnQ9tCLtM0sug1oNHwR37DcBo5BkTb+qRdxHQRvojAlYajJLMs6 GzBPXf5o0cPp5TJpEXe0tQ/+RWE4lbUeAtlIMpIGcQStQlg27HltAsNEhG7bo2jYtuTF 68bAM3bT2pfi25znSCze2SxPWMT/AkvmoKRRI4VE8MEqm4lD8irqro0hcKQg1YJIdhHw LSnMM3zwofNY823HesbDYy05If1VCgHvCVPUaR9lQe1TSTW2gE5agrgfvRNxFkcsBUN6 ht1g== X-Gm-Message-State: AO0yUKXaiok+Xr52a3CkwNma1fSzJ70E0BJgODQA3wPf5CnylpbqctCC BUv+gXCslpaAwIj2YSJff1nSa9faCfHGuph9 X-Google-Smtp-Source: AK7set9fs7ZlrQa8BGthXIghIC/OJlcQ5DNW+MpM+Izy2yanTiFXOE8GyMA6vgZyKafLmWU7Ltx+Sg== X-Received: by 2002:a92:ca49:0:b0:315:352e:d5d0 with SMTP id q9-20020a92ca49000000b00315352ed5d0mr13037075ilo.32.1677181686334; Thu, 23 Feb 2023 11:48:06 -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.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 11:48:05 -0800 (PST) Date: Thu, 23 Feb 2023 12:48:04 -0700 Subject: [PATCH 6/6] Hide the implementation of gdb_mpf MIME-Version: 1.0 Message-Id: <20230223-submit-gmp-hiding-v1-6-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 renames the data member of gdb_mpf and makes it private. It also adds a single new method to aid in this change. Unlike the earlier changes here, I did this one all together because gdb_mpf has very few uses. --- gdb/gmp-utils.h | 17 ++++++++++++----- gdb/valprint.c | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index 7889756a0ea..274c28c0ce8 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -388,10 +388,8 @@ struct gdb_mpq struct gdb_mpf { - mpf_t val; - /* Constructors. */ - gdb_mpf () { mpf_init (val); } + gdb_mpf () { mpf_init (m_val); } DISABLE_COPY_AND_ASSIGN (gdb_mpf); @@ -409,11 +407,20 @@ struct gdb_mpf gdb_mpq tmp_q; tmp_q.read_fixed_point (buf, byte_order, unsigned_p, scaling_factor); - mpf_set_q (val, tmp_q.m_val); + mpf_set_q (m_val, tmp_q.m_val); } + /* Convert this value to a string. FMT is the format to use, and + should have a single '%' substitution. */ + std::string str (const char *fmt) const + { return gmp_string_printf (fmt, m_val); } + /* The destructor. */ - ~gdb_mpf () { mpf_clear (val); } + ~gdb_mpf () { mpf_clear (m_val); } + +private: + + mpf_t m_val; }; /* See declaration above. */ diff --git a/gdb/valprint.c b/gdb/valprint.c index 357db3815b0..b05d90dd26b 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -856,7 +856,7 @@ generic_val_print_fixed_point (struct value *val, struct ui_file *stream, type->fixed_point_scaling_factor ()); const char *fmt = type->length () < 4 ? "%.11Fg" : "%.17Fg"; - std::string str = gmp_string_printf (fmt, f.val); + std::string str = f.str (fmt); gdb_printf (stream, "%s", str.c_str ()); } }