public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
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	[thread overview]
Message-ID: <20220721152132.3489524-4-pedro@palves.net> (raw)
In-Reply-To: <20220721152132.3489524-1-pedro@palves.net>

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<int, 1>" 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<int, 1>" 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<packed<T>> and packed<T>
-- 
2.36.0


  parent reply	other threads:[~2022-07-21 15:21 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-21 15:21 [PATCH 0/3] struct packed and Windows ports (PR build/29373) Pedro Alves
2022-07-21 15:21 ` [PATCH 1/3] struct packed: Use gcc_struct on Windows Pedro Alves
2022-07-21 16:03   ` Eli Zaretskii
2022-07-21 17:05     ` Pedro Alves
2022-07-21 17:40       ` Eli Zaretskii
2022-07-21 18:15         ` Pedro Alves
2022-07-21 18:23           ` Eli Zaretskii
2022-07-21 18:30             ` Pedro Alves
2022-07-21 18:45               ` Eli Zaretskii
2022-07-21 18:57                 ` Pedro Alves
2022-07-21 18:18         ` Pedro Alves
2022-07-21 18:28           ` Eli Zaretskii
2022-07-21 18:38             ` Pedro Alves
2022-07-21 15:21 ` [PATCH 2/3] struct packed: Unit tests and more operators Pedro Alves
2022-07-21 15:21 ` Pedro Alves [this message]
2022-07-25 14:58 ` [PATCH 0/3] struct packed and Windows ports (PR build/29373) Tom Tromey
2022-07-25 15:18   ` Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220721152132.3489524-4-pedro@palves.net \
    --to=pedro@palves.net \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).