From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) by sourceware.org (Postfix) with ESMTPS id 344DA384D149 for ; Thu, 21 Jul 2022 15:21:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 344DA384D149 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f41.google.com with SMTP id n12so2745414wrc.8 for ; Thu, 21 Jul 2022 08:21:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WJLXqiJC3523MaiO2Z/VGiPXUE9IfOOKxb5ELHAu96s=; b=q4mVrhn5jjvzFyOwfkRY4lFDU7zI2nHMkAJWTVMy/XFlRF47ayTRoZqILDmz2NaSfs zj5aD4aDJqv0YZSo6NKvURmOjCjX64OosLmCXAsHH6CVjTs9+mvCWpszuvceayEuK/A5 n1cQEVQMnF6st+xe6fkmsM7YJ6VWXrZnS8K/JdHxElr5nbhrFhanno0kWbejUTaInYVa 98tDygumN666b59tCtg2PLeNhbyjvhZ2yVfQ333ohsbFPvHgw+iGulz2gG0OJbvsnF1G BOUrbAf52qi3mFblGwLBqCgYVUXKaFC573/8Wj7nAyZhW5lhLPt1/jPZLWJKMGAT97zE +Rfg== X-Gm-Message-State: AJIora9DQugvICRMntI/8KycoN0lfbzYBw06VioF0g/jTqWYd5Mr1Mad NIPIVyvy08La/PtcqFhZPDhVgBHW7ko= X-Google-Smtp-Source: AGRyM1v1e2iwyoOGe5qybltmpKlX7PPVmGhFrKeE6iRoQiZZlo+vKcRiRZ4CUAoXDiWqX4i13k/suw== X-Received: by 2002:a05:6000:1885:b0:21d:ae7a:4f97 with SMTP id a5-20020a056000188500b0021dae7a4f97mr35939023wri.74.1658416902314; Thu, 21 Jul 2022 08:21:42 -0700 (PDT) Received: from localhost ([2001:8a0:f924:2600:209d:85e2:409e:8726]) by smtp.gmail.com with ESMTPSA id b15-20020a5d4b8f000000b0021b98d73a4esm2182792wrt.114.2022.07.21.08.21.41 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 21 Jul 2022 08:21:41 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 3/3] struct packed: Add fallback byte array implementation Date: Thu, 21 Jul 2022 16:21:32 +0100 Message-Id: <20220721152132.3489524-4-pedro@palves.net> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220721152132.3489524-1-pedro@palves.net> References: <20220721152132.3489524-1-pedro@palves.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, 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-List-Received-Date: Thu, 21 Jul 2022 15:21:46 -0000 Attribute gcc_struct is not implemented in Clang targeting Windows, so add a fallback standard-conforming implementation based on arrays. I ran the testsuite on x86_64 GNU/Linux with this implementation forced, and saw no regressions. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29373 Change-Id: I023315ee03622c59c397bf4affc0b68179c32374 --- gdbsupport/packed.h | 51 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/gdbsupport/packed.h b/gdbsupport/packed.h index d721b02c056..dbece2a29aa 100644 --- a/gdbsupport/packed.h +++ b/gdbsupport/packed.h @@ -28,9 +28,27 @@ bit-fields (and ENUM_BITFIELD), when the fields must have separate memory locations to avoid data races. */ -/* We need gcc_struct on Windows GCC, as otherwise the size of e.g., - "packed" will be larger than what we want. */ -#if defined _WIN32 +/* There are two implementations here -- one standard compliant, using + a byte array for internal representation, and another that relies + on bitfields and attribute packed (and attribute gcc_struct on + Windows). The latter is preferable, as it is more convenient when + debugging GDB -- printing a struct packed variable prints its field + using its natural type, which is particularly useful if the type is + an enum -- but may not work on all compilers. */ + +/* Clang targeting Windows does not support attribute gcc_struct, so + we use the alternative byte array implemention there. */ +#if defined _WIN32 && defined __clang__ +# define PACKED_USE_ARRAY 1 +#else +# define PACKED_USE_ARRAY 0 +#endif + +/* For the preferred implementation, we need gcc_struct on Windows, as + otherwise the size of e.g., "packed" will be larger than + what we want. Clang targeting Windows does not support attribute + gcc_struct. */ +#if !PACKED_USE_ARRAY && defined _WIN32 && !defined __clang__ # define ATTRIBUTE_GCC_STRUCT __attribute__((__gcc_struct__)) #else # define ATTRIBUTE_GCC_STRUCT @@ -44,7 +62,18 @@ struct ATTRIBUTE_GCC_STRUCT packed packed (T val) { + gdb_static_assert (sizeof (ULONGEST) >= sizeof (T)); + +#if PACKED_USE_ARRAY + ULONGEST tmp = val; + for (int i = (Bytes - 1); i >= 0; --i) + { + m_bytes[i] = (gdb_byte) tmp; + tmp >>= HOST_CHAR_BIT; + } +#else m_val = val; +#endif /* Ensure size and aligment are what we expect. */ gdb_static_assert (sizeof (packed) == Bytes); @@ -62,11 +91,27 @@ struct ATTRIBUTE_GCC_STRUCT packed operator T () const noexcept { +#if PACKED_USE_ARRAY + ULONGEST tmp = 0; + for (int i = 0;;) + { + tmp |= m_bytes[i]; + if (++i == Bytes) + break; + tmp <<= HOST_CHAR_BIT; + } + return (T) tmp; +#else return m_val; +#endif } private: +#if PACKED_USE_ARRAY + gdb_byte m_bytes[Bytes]; +#else T m_val : (Bytes * HOST_CHAR_BIT) ATTRIBUTE_PACKED; +#endif }; /* Add some comparisons between std::atomic> and packed -- 2.36.0