[clang] [clang][bytecode] Handle bitcasts involving bitfields (PR #116843)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 20 06:38:39 PST 2024


================
@@ -0,0 +1,88 @@
+//===-------------------- Bitcastbuffer.cpp ---------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#include "BitcastBuffer.h"
+
+using namespace clang;
+using namespace clang::interp;
+
+void BitcastBuffer::pushData(const std::byte *In, size_t BitOffset,
+                             size_t BitWidth, Endian TargetEndianness) {
+  for (unsigned It = 0; It != BitWidth; ++It) {
+    bool BitValue = bitof(In, It);
+    if (!BitValue)
+      continue;
+
+    unsigned DstBit;
+    if (TargetEndianness == Endian::Little)
+      DstBit = BitOffset + It;
+    else
+      DstBit = size() - BitOffset - BitWidth + It;
+
+    unsigned DstByte = (DstBit / 8);
+    Data[DstByte] |= std::byte{1} << (DstBit % 8);
+  }
+}
+
+std::unique_ptr<std::byte[]>
+BitcastBuffer::copyBits(unsigned BitOffset, unsigned BitWidth,
+                        unsigned FullBitWidth, Endian TargetEndianness) const {
+  assert(BitWidth <= FullBitWidth);
+  assert(fullByte(FullBitWidth));
+  auto Out = std::make_unique<std::byte[]>(FullBitWidth / 8);
+
+  for (unsigned It = 0; It != BitWidth; ++It) {
+    unsigned BitIndex;
+    if (TargetEndianness == Endian::Little)
+      BitIndex = BitOffset + It;
+    else
+      BitIndex = size() - BitWidth - BitOffset + It;
+
+    bool BitValue = bitof(Data.get(), BitIndex);
+    if (!BitValue)
+      continue;
+    unsigned DstBit = It;
+    unsigned DstByte = (DstBit / 8);
+    Out[DstByte] |= std::byte{1} << (DstBit % 8);
+  }
+
+  return Out;
+}
+
+#if 0
----------------
AaronBallman wrote:

Remove dead code?

https://github.com/llvm/llvm-project/pull/116843


More information about the cfe-commits mailing list