Message ID | 4801877.GXAFRqVoOG@fomalhaut |
---|---|
State | Accepted |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp366100vqo; Thu, 18 May 2023 02:51:56 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5GYwfXits4zBJ6HK+oPTJkqq9SSutGqaI5J6DEy1ZdPsNsgoupKOQ0z4YoDg59k5LStzxt X-Received: by 2002:a17:907:7da9:b0:966:538f:843b with SMTP id oz41-20020a1709077da900b00966538f843bmr36344179ejc.77.1684403516420; Thu, 18 May 2023 02:51:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684403516; cv=none; d=google.com; s=arc-20160816; b=iiXl/3SsHWxIbPHemdx2vapeu8BaNHQtC2t6t7T+Nc2qWZJhx2avOG+ghl61yzs59a UR48TCIwdwRbOOOrTECAz66aLp2M7jGSHhYuLF+rgcip3czbLDrHkRAoKz2vMQhZOTbi sFqpLL7IJpKYAug6ZdJVmBI2ZF5SMZNJULqvmVaLwXz31SGzZ8mcPl5qnos9rZwCoGWO 48i+GtOxGY/6pUkAAkf3kenwVJ744VYKQogkR5VgNUH7RqbHE9lqG5oLWhKw78DjzFSs wzPEI/GQT5bCiURmos+69NLEIgVF7MRXU8WVdDcGAGwoL+bSFpSPOGKjanpKEv+lpT28 utZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=974t5yJOUUIAuS0FmdylNC7LzCJTaDjsBgG8FBTXmU0=; b=OWQgxmEAsCfW/QTyBPWVx78j+lS/t5utox7ISQwcZtY0U8r1LeU6ofYkfuQ58uINqE 6iWGB1zMUiQF2h64I11Fd9DEAXEZlzTfZ5gNOssXXRbuKtxYYEiWPqNfi7kN89/ijNRu YVQQHPqR2K1rZG/s4cKAxGfN1cCcpXGO4yrEyW00ukDfbLySHwLSKHjb1F9I9/sXHB2q B5QArZojN5esAKriy+vvF8P9XLyhC3gNV+HUixQsMnsa1Z+Rp3kBPk0GF4ZFQywlAdiZ tnHxAo0K4CQ975KpBATAVtyVlB6q7quVLPxFmjM85cP/w1NC+zFt1sH+u7ELVsaGUbaZ WaMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=KDUTWUFY; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id j8-20020a170906474800b00965f9352c46si1070114ejs.372.2023.05.18.02.51.56 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 02:51:56 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=KDUTWUFY; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 51A74385770F for <ouuuleilei@gmail.com>; Thu, 18 May 2023 09:51:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 51A74385770F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684403515; bh=974t5yJOUUIAuS0FmdylNC7LzCJTaDjsBgG8FBTXmU0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=KDUTWUFYUS7NGtv7F5ja1br4nKbiWFOhqQ6SyQt5b8iZ7lSZ1CoaHL8iubQC5nymO joFaE7Ag4L3g3E6Y+i8hCGxH7EX/3B2r0m8xmYvAsU6mcSKtAb8MvPDDEri0TxmZr/ AYoIusJR/aSkxcyXW3MwT+H9saCZHjZqYWYLdFo8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by sourceware.org (Postfix) with ESMTPS id 8FF203858CDB for <gcc-patches@gcc.gnu.org>; Thu, 18 May 2023 09:51:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8FF203858CDB Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-306dbad5182so1194741f8f.1 for <gcc-patches@gcc.gnu.org>; Thu, 18 May 2023 02:51:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684403467; x=1686995467; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=974t5yJOUUIAuS0FmdylNC7LzCJTaDjsBgG8FBTXmU0=; b=WNqdD4ICeTP25gt7uCmLtx4sWZ1TeheuJGwhrjJPZ7u3RUKfCWdPpB8qC46RoaCFbE 7LK1VTLYYtw50jh7S3z8IGflOh9xnlOgYXYuVRKgtk7scg5HgWgN1E+SrrLt7amm0HeE FwqzWKBMAeuNDsrspuJ119dI76Gr+ZJMF4vB6XOU+OUfKaKO2C70tOI7IvIuxkkNpAOr F7u/mRa0yVXdeCtiqRQEiukvoBAgW6rbYfMN2rQawypgzSjK1iD44uZHxfjIV7QNO7dO hideK0z7z5YwdWu5JprOE/Tq1nceDJ+PtUZvFHKBb7SNapgOc+VKAohSiygZhBlgJDOD pBWA== X-Gm-Message-State: AC+VfDxFATk9jJEMOexN5Lf3ErHs9ELyVtwrbOXoncShmgikDogU2loQ /63bMLX6NtjQZbA0h/Hw8NmLQpHsBfzHwYhb/rfNpQ== X-Received: by 2002:a5d:574b:0:b0:306:3286:69a2 with SMTP id q11-20020a5d574b000000b00306328669a2mr894764wrw.48.1684403467206; Thu, 18 May 2023 02:51:07 -0700 (PDT) Received: from fomalhaut.localnet ([2a01:e0a:8d5:d990:e654:e8ff:fe8f:2ce6]) by smtp.gmail.com with ESMTPSA id e18-20020a056000121200b0030796e103a1sm1665293wrx.5.2023.05.18.02.51.06 for <gcc-patches@gcc.gnu.org> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 02:51:06 -0700 (PDT) X-Google-Original-From: Eric Botcazou <ebotcazou@adacore.com> To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix internal error on small array with negative lower bound Date: Thu, 18 May 2023 11:28:51 +0200 Message-ID: <4801877.GXAFRqVoOG@fomalhaut> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart1851970.tdWV9SEqCh" Content-Transfer-Encoding: 7Bit X-Spam-Status: No, score=-10.8 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, T_SCC_BODY_TEXT_LINE 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Eric Botcazou via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Eric Botcazou <botcazou@adacore.com> Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766225101584097617?= X-GMAIL-MSGID: =?utf-8?q?1766225101584097617?= |
Series |
Fix internal error on small array with negative lower bound
|
|
Checks
Context | Check | Description |
---|---|---|
snail/gcc-patch-check | success | Github commit url |
Commit Message
Eric Botcazou
May 18, 2023, 9:28 a.m. UTC
Hi, Ada supports arrays with negative indices, although the internal index type is sizetype like in other languages, which is unsigned. This means that negative values are represented by very large numbers, which works with a bit of care. The attached test exposes a small loophole in output_constructor_bitfield. Tested on x86-64/Linux, OK for the mainline? 2023-05-18 Eric Botcazou <ebotcazou@adacore.com> * varasm.cc (output_constructor_bitfield): Call tree_to_uhwi instead of tree_to_shwi on array indices. Minor tweaks. 2023-05-18 Eric Botcazou <ebotcazou@adacore.com> * gnat.dg/specs/array6.ads: New test.
Comments
On Thu, May 18, 2023 at 11:51 AM Eric Botcazou via Gcc-patches <gcc-patches@gcc.gnu.org> wrote: > > Hi, > > Ada supports arrays with negative indices, although the internal index type is > sizetype like in other languages, which is unsigned. This means that negative > values are represented by very large numbers, which works with a bit of care. > The attached test exposes a small loophole in output_constructor_bitfield. > > Tested on x86-64/Linux, OK for the mainline? Would it be better to use wi::to_uhwi (wi::to_wide (local->index) - wi::to_wide (local->min_index)) to honor the actual sign of the indices? I think nothing forbids frontends to use a signed TYPE_DOMAIN here? But the difference should be always representable in an unsigned value of course. > > 2023-05-18 Eric Botcazou <ebotcazou@adacore.com> > > * varasm.cc (output_constructor_bitfield): Call tree_to_uhwi instead > of tree_to_shwi on array indices. Minor tweaks. > > > 2023-05-18 Eric Botcazou <ebotcazou@adacore.com> > > * gnat.dg/specs/array6.ads: New test. > > -- > Eric Botcazou
> Would it be better to use > > wi::to_uhwi (wi::to_wide (local->index) - wi::to_wide (local->min_index)) > > to honor the actual sign of the indices? I think nothing forbids frontends > to use a signed TYPE_DOMAIN here? But the difference should be always > representable in an unsigned value of course. We use tree_to_uhwi everywhere else though, see categorize_ctor_elements_1: if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index)) mult = (tree_to_uhwi (hi_index) - tree_to_uhwi (lo_index) + 1); or store_constructor this_node_count = (tree_to_uhwi (hi_index) - tree_to_uhwi (lo_index) + 1); so the proposed form looks better for the sake of consistency.
> Am 18.05.2023 um 19:44 schrieb Eric Botcazou <botcazou@adacore.com>: > > >> >> Would it be better to use >> >> wi::to_uhwi (wi::to_wide (local->index) - wi::to_wide (local->min_index)) >> >> to honor the actual sign of the indices? I think nothing forbids frontends >> to use a signed TYPE_DOMAIN here? But the difference should be always >> representable in an unsigned value of course. > > We use tree_to_uhwi everywhere else though, see categorize_ctor_elements_1: > > if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index)) > mult = (tree_to_uhwi (hi_index) > - tree_to_uhwi (lo_index) + 1); > > or store_constructor > > this_node_count = (tree_to_uhwi (hi_index) > - tree_to_uhwi (lo_index) + 1); > > so the proposed form looks better for the sake of consistency. Ok, thanks for checking. Richard > -- > Eric Botcazou > >
diff --git a/gcc/varasm.cc b/gcc/varasm.cc index 2256194d934..478cbfe6736 100644 --- a/gcc/varasm.cc +++ b/gcc/varasm.cc @@ -5585,19 +5585,18 @@ output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset) /* Relative index of this element if this is an array component. */ HOST_WIDE_INT relative_index - = (!local->field - ? (local->index - ? (tree_to_shwi (local->index) - - tree_to_shwi (local->min_index)) - : local->last_relative_index + 1) - : 0); + = (local->field + ? 0 + : (local->index + ? tree_to_uhwi (local->index) - tree_to_uhwi (local->min_index) + : local->last_relative_index + 1)); /* Bit position of this element from the start of the containing constructor. */ HOST_WIDE_INT constructor_relative_ebitpos - = (local->field - ? int_bit_position (local->field) - : ebitsize * relative_index); + = (local->field + ? int_bit_position (local->field) + : ebitsize * relative_index); /* Bit position of this element from the start of a possibly ongoing outer byte buffer. */