From patchwork Mon May 22 08:08:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Botcazou X-Patchwork-Id: 97169 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1283147vqo; Mon, 22 May 2023 01:10:32 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7ZEpzIAl2BNtqEcYFzr6s7JR+2ei+HMfBVP7YG/J4+YD41kbKafeml4DLq+ZdzP5pxZdNt X-Received: by 2002:a17:907:7ea6:b0:94a:56ec:7f12 with SMTP id qb38-20020a1709077ea600b0094a56ec7f12mr10107648ejc.30.1684743032243; Mon, 22 May 2023 01:10:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684743032; cv=none; d=google.com; s=arc-20160816; b=NLLV87/7OGilKsojelkcLsUpyAHp6Nmwbz2mqtzcZG8uDewX1YeHkdw6oktAOoMJ0m agYsrFsV5AH7m1ohGYk7XXpQngi3k65sOK3LQPAO8vwra6Un/FXM7V/+FehrAfwhLriR nk2P+RLie6COfaUP7O8XAvdq+ULzQiNsFkm4vI2DqEYwrXKox8WJ3mwWNAbqCdZlxxXR 4W/DWjqIQutf5ggM0DlySItxgZ36x+w1BS7NtbVhusEQVXGSl/DkGIUHFHKtOBH9BZpy IhcXsZ+smuqkfvAOt2BeBj2VuPf5pAkCvWxaNNdRbuzqbE+fWeNWHujSotr+s0uhoZNP aI+Q== 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=2IC1zsX3cON1+9q7xUuX68v4I7dBbD1Bz5W9slJV/wg=; b=mYnX6Po7ciXzSnO1JDkUKgoho4muxM63lEPj93o2YUWCSpe5tkRiIZc/XzWX7fFNXY f0AX6jxAYFl6A3tIvTlLUCw6gQeMO5YH43y4fg9qN/qqoGNjlikS9GSx3+a0hW+2H4LR w0Frsw2GzNYV4vTq7aIT57nr8scvwgaXy24rFjlJGvrXCkKdg40CTTu0iPQHJTC15gJ2 QanWDkdkd8OPluh09vZL2+wWqnvHg2/4kRtHVdWjjwIgZkH4uRN/sQrkihTqVvTOgdr0 7vCzZ7MEidAUUnpS7HQURuR27A28OjATUWda5txIlf0zI6tsatiCEHdtzGBDAnjusj9e P1fA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=J3kin9Ig; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id h24-20020a170906591800b0096f66fcabb3si78815ejq.172.2023.05.22.01.10.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 01:10:32 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=J3kin9Ig; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 DE0F1385840E for ; Mon, 22 May 2023 08:10:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DE0F1385840E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684743030; bh=2IC1zsX3cON1+9q7xUuX68v4I7dBbD1Bz5W9slJV/wg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=J3kin9IgA/58dMbYXrICUU9z0e34G2TX9Yd/RlAzCsNsvaekpDMf25chx3wdg3Pak Pbu7fjf29G8dkyNf/uHkFxvOCCGeLFG7oQO24p6g7pThLuPMj+TVAqIpoxY+dXNCuU WFZ0cRLs7vXKpPiTOS6IiJi/Py/HW1OK2JZWb2jo= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by sourceware.org (Postfix) with ESMTPS id B22713858C31 for ; Mon, 22 May 2023 08:08:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B22713858C31 Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-307d20548adso3572454f8f.0 for ; Mon, 22 May 2023 01:08:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684742935; x=1687334935; 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=2IC1zsX3cON1+9q7xUuX68v4I7dBbD1Bz5W9slJV/wg=; b=KvSl9V9HWWZ7H2myGQFoBelhMnfanYcDnkdsxyP13isJWWVUpnJNt3eyptxVIWmd0m D2AYsZOVWgp3yhGQWA8jDGNo13/L2SyNJRuH6qxEExSUlWAs+Ct3Ma6+IKuwfPsOebkE 6ijqlekF5Ck+r239l1Wg/Gkyj3UorbplTfEoMHwxicW6VTmeUCgV91UQgY6sVLTv18HC +9mfzPFKTvHboxE1sGBU1giT4xUHua/RTR91I5NnjENErkD14iTKUtCoNhnuixdBcrVU DpsGfXyIW5qBmThJq81um8pXOP8hIex07HZDXPwkZ2po2gKe0+5sYVQTRDFzs2em/P0Q od9A== X-Gm-Message-State: AC+VfDwoRStrdNOvUth/jTYwTGa2OGcUhoYUsM1zmkMMJ0aIF4YpEKLR Pv5DoH7JBdN79UEwzrdawFsplh55lt4SMhFgeXchiw== X-Received: by 2002:adf:cd10:0:b0:307:8ca1:975a with SMTP id w16-20020adfcd10000000b003078ca1975amr6328399wrm.41.1684742935422; Mon, 22 May 2023 01:08:55 -0700 (PDT) Received: from fomalhaut.localnet ([2a01:e0a:8d5:d990:e654:e8ff:fe8f:2ce6]) by smtp.gmail.com with ESMTPSA id o16-20020a5d62d0000000b002fb60c7995esm6931677wrv.8.2023.05.22.01.08.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 01:08:54 -0700 (PDT) X-Google-Original-From: Eric Botcazou To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix handling of non-integral bit-fields in native_encode_initializer Date: Mon, 22 May 2023 10:08:10 +0200 Message-ID: <13270277.uLZWGnKmhe@fomalhaut> MIME-Version: 1.0 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Eric Botcazou via Gcc-patches From: Eric Botcazou Reply-To: Eric Botcazou Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766581109768136347?= X-GMAIL-MSGID: =?utf-8?q?1766581109768136347?= Hi, the encoder for CONSTRUCTORs assumes that all bit-fields (DECL_BIT_FIELD) have integral types, but that's not the case in Ada where they may have pretty much any type, resulting in a wrong encoding for them. The attached fix filters out non-integral bit-fields, except if they start and end on a byte boundary because they are correctly handled in this case. Bootstrapped/regtested on x86-64/Linux, OK for mainline and 13 branch? 2023-05-22 Eric Botcazou * fold-const.cc (native_encode_initializer) : Apply the specific treatment for bit-fields only if they have an integral type and filter out non-integral bit-fields that do not start and end on a byte boundary. 2023-05-22 Eric Botcazou * gnat.dg/opt101.adb: New test. * gnat.dg/opt101_pkg.ads: New helper. diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 25466e97220..57521501fff 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -8360,20 +8360,26 @@ native_encode_initializer (tree init, unsigned char *ptr, int len, if (fieldsize == 0) continue; + /* Prepare to deal with integral bit-fields and filter out other + bit-fields that do not start and end on a byte boundary. */ if (DECL_BIT_FIELD (field)) { if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field))) return 0; - fieldsize = TYPE_PRECISION (TREE_TYPE (field)); bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field)); - if (bpos % BITS_PER_UNIT) - bpos %= BITS_PER_UNIT; - else - bpos = 0; - fieldsize += bpos; - epos = fieldsize % BITS_PER_UNIT; - fieldsize += BITS_PER_UNIT - 1; - fieldsize /= BITS_PER_UNIT; + if (INTEGRAL_TYPE_P (TREE_TYPE (field))) + { + bpos %= BITS_PER_UNIT; + fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos; + epos = fieldsize % BITS_PER_UNIT; + fieldsize += BITS_PER_UNIT - 1; + fieldsize /= BITS_PER_UNIT; + } + else if (bpos % BITS_PER_UNIT + || DECL_SIZE (field) == NULL_TREE + || !tree_fits_shwi_p (DECL_SIZE (field)) + || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT) + return 0; } if (off != -1 && pos + fieldsize <= off) @@ -8382,7 +8388,8 @@ native_encode_initializer (tree init, unsigned char *ptr, int len, if (val == NULL_TREE) continue; - if (DECL_BIT_FIELD (field)) + if (DECL_BIT_FIELD (field) + && INTEGRAL_TYPE_P (TREE_TYPE (field))) { /* FIXME: Handle PDP endian. */ if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)