From patchwork Tue Mar 28 15:49:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 67026 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 14C5D38708A5 for ; Tue, 28 Mar 2023 15:50:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 14C5D38708A5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680018604; bh=RsYSRw+9fCV4P0R4xSROpAkR79efvHcFBXpMga/TUyA=; 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=P8DH39pAz/lQYssNjiQdT8Ha5gBjsKiZnsUyWm3kJPBaSmlBsvQtbF47IfvVzRIo0 Ny6aZ5rOHBVuYV+ghhizxDlNFiRu8TAVJJ9CoaQoI3VHw8wlCWPFz42llo4W6wWXkl 1Dd4n08WneBVz+LVBhIOrGbk13QTTCSeIvrJHuFg= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2b.google.com (mail-io1-xd2b.google.com [IPv6:2607:f8b0:4864:20::d2b]) by sourceware.org (Postfix) with ESMTPS id DBBC4385B519 for ; Tue, 28 Mar 2023 15:49:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DBBC4385B519 Received: by mail-io1-xd2b.google.com with SMTP id bl9so5538794iob.8 for ; Tue, 28 Mar 2023 08:49:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680018580; 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=RsYSRw+9fCV4P0R4xSROpAkR79efvHcFBXpMga/TUyA=; b=SeUW2WO0Coov+gc4bC8JXkl8fpfGlMbVRb+Eozmmyl06nFED6mQCd6t3PMOKzGyJsP X2DMpVBDtM3F3a0QiLDIOWkBoXXSBpGtieaH8tWMmLX6tGEv8wGC6vuDIIlOl0lnQcHR tlGw/5wEoqfWom2+k8dm+zsZ1+92oHTXLeuSB81ZlLlMO9OA2j78eqe7bGiVyiBMD/jc yNPBqXU71abMZ4hyRasfKVCSeJ5w8vwIp1PeVy8kcH21sLi4IAqrzojaQXG1166FT55q si1Kq9CC9ahrVCGLuRnP/wPpA8DvFKjjPHlPVoSZ6b4BJHhRmEs7cTkJSaA4OPQUuGLq k/8g== X-Gm-Message-State: AO0yUKUu3XefVz4xVXJ3T5NGqKsJc20H6hr+kt4wvHqrd+JvdjnFdIXZ RjXP0yeQ/25u+KnGnpZFMPRvhnXhqRbqDDtHF/ML4g== X-Google-Smtp-Source: AK7set9OfaQmye+aamzMLvYsRifY3AOe2mAERilDeDnZ+/uKRV4oH3msTzk4ioAOpdhvvtuZWJFO7w== X-Received: by 2002:a5e:d601:0:b0:74c:a578:e3a0 with SMTP id w1-20020a5ed601000000b0074ca578e3a0mr10917363iom.5.1680018580169; Tue, 28 Mar 2023 08:49:40 -0700 (PDT) Received: from localhost.localdomain (71-211-185-113.hlrn.qwest.net. [71.211.185.113]) by smtp.gmail.com with ESMTPSA id u5-20020a5ec005000000b0075c37601b5csm1684522iol.4.2023.03.28.08.49.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 08:49:39 -0700 (PDT) Date: Tue, 28 Mar 2023 09:49:37 -0600 Subject: [PATCH 1/6] Additions to gdb_mpz MIME-Version: 1.0 Message-Id: <20230328-expr-128-bit-v1-1-f9eeb0143318@adacore.com> References: <20230328-expr-128-bit-v1-0-f9eeb0143318@adacore.com> In-Reply-To: <20230328-expr-128-bit-v1-0-f9eeb0143318@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.1 X-Spam-Status: No, score=-11.6 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" In preparation for adding more 128-bit support to gdb, a few additions to gdb_mpz are needed. First, this adds a new 'as_integer_truncate' method. This method works like 'as_integer' but does not require the value to fit in the target type -- it just truncates. Second, gdb_mpz::export_bits is changed to handle the somewhat unusual situation of zero-length types. This can happen for a Rust '()' type; but I think other languages have zero-bit integer types as well. Finally, this adds some operator== overloads. --- gdb/gmp-utils.c | 10 ++++++---- gdb/gmp-utils.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c index 0afa344781b..13fa61d61e3 100644 --- a/gdb/gmp-utils.c +++ b/gdb/gmp-utils.c @@ -69,19 +69,21 @@ void gdb_mpz::export_bits (gdb::array_view buf, int endian, bool unsigned_p, bool safe) const { - gdb_assert (buf.size () > 0); - int sign = mpz_sgn (m_val); if (sign == 0) { /* Our value is zero, so no need to call mpz_export to do the work, especially since mpz_export's documentation explicitly says that the function is a noop in this case. Just write zero to - BUF ourselves. */ - memset (buf.data (), 0, buf.size ()); + BUF ourselves, if it is non-empty. In some languages, a + zero-bit type can exist and this is also fine. */ + if (buf.size () > 0) + memset (buf.data (), 0, buf.size ()); return; } + gdb_assert (buf.size () > 0); + if (safe) { /* Determine the maximum range of values that our buffer can diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index f294ab622ee..d05c11ecbe4 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -119,11 +119,17 @@ struct gdb_mpz return result; } - /* Convert VAL to an integer of the given type. + /* Convert this value to an integer of the given type. The return type can signed or unsigned, with no size restriction. */ template T as_integer () const; + /* Convert this value to an integer of the given type. If this + value is too large, it is truncated. + + The return type can signed or unsigned, with no size restriction. */ + template T as_integer_truncate () const; + /* Set VAL by importing the number stored in the byte array (BUF), using the given BYTE_ORDER. The size of the data to read is the byte array's size. @@ -312,7 +318,7 @@ struct gdb_mpz return mpz_cmp (m_val, other.m_val) <= 0; } - bool operator< (int other) const + bool operator< (long other) const { return mpz_cmp_si (m_val, other) < 0; } @@ -322,6 +328,28 @@ struct gdb_mpz return mpz_cmp_si (m_val, other) == 0; } + bool operator== (long other) const + { + return mpz_cmp_si (m_val, other) == 0; + } + + bool operator== (unsigned long other) const + { + return mpz_cmp_ui (m_val, other) == 0; + } + + template, + std::integral_constant sizeof (long))>> + > + > + bool operator== (T src) + { + return *this == gdb_mpz (src); + } + bool operator== (const gdb_mpz &other) const { return mpz_cmp (m_val, other.m_val) == 0; @@ -612,4 +640,20 @@ gdb_mpz::as_integer () const return result; } +/* See declaration above. */ + +template +T +gdb_mpz::as_integer_truncate () const +{ + T result; + + this->export_bits ({(gdb_byte *) &result, sizeof (result)}, + 0 /* endian (0 = native) */, + !std::is_signed::value /* unsigned_p */, + false /* safe */); + + return result; +} + #endif