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: 1784309 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=TI+7TmkJ; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QPqqz22WFz20Pr for ; Mon, 22 May 2023 18:10:07 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4B8963858D35 for ; Mon, 22 May 2023 08:10:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4B8963858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684743005; 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=TI+7TmkJkDRWEDQu4zfcUQDXolx9sW10BURIS7McbFjRZWgSGRhAkghMitxoQ3uTj qp6LTlXR/8g0RNhZLnRvFcG+W1x2HsbfQFbsD35BDGaal7b6Y+T2q46zaDil/uhkmf WcQSvm0/+HH9uYhZJd9PGwJ8imDbAYcaOcHcIyvQ= 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-Google-Smtp-Source: ACHHUZ5Gm9XKGa59YYlUE5ZeYrOFr+jHtv51wSUEU/lAUuZl4I93PjmIUVm84JDCfe8qEtRm6zHlUg== 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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)