From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-io1-xd2b.google.com with SMTP id bl9so5538794iob.8 for ; Tue, 28 Mar 2023 08:49:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1680018580; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=RsYSRw+9fCV4P0R4xSROpAkR79efvHcFBXpMga/TUyA=; b=hU2UNRsKFSKPicPtAnk2jE56uA0zCM9aLQss4uMw17dM8Oey3Fkc8ncODzbBV8faE2 VNFzte4SloHgBxlPnNj+quF7i35TjnM4oJUMRb2IqfxUtiOkcqoegRoIssKyTSlSds4U pIUZ5dU/GLlDGgDmfuB6gEPEYKSwr1p6ukqeAAkyEhQPS3w5Q1c1xZ6uRuWwsDPL3Ga3 bPUvRB89sR9HHANbjryl0Jpuf3xTAZXtd4pAI+Fe9N4ljVnMzLqFxogcalPtytwQD3Pc tAAOcdKaBmCatkdnsVZYhDZBaPWZC1qLdmWcG/bs9Vjw/hk5uH4s1A7IYrRtf8mkyPaD p8gw== 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) From: Tom Tromey Date: Tue, 28 Mar 2023 09:49:37 -0600 Subject: [PATCH 1/6] Additions to gdb_mpz MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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 List-Id: 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 -- 2.39.1