[clang] [clang][bytecode] Handle bitcasts involving bitfields (PR #116843)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 20 07:06:42 PST 2024
================
@@ -0,0 +1,66 @@
+//===--------------------- BitcastBuffer.h ----------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_CLANG_AST_INTERP_BITCAST_BUFFER_H
+#define LLVM_CLANG_AST_INTERP_BITCAST_BUFFER_H
+
+#include <cassert>
+#include <cstddef>
+#include <memory>
+
+namespace clang {
+namespace interp {
+
+enum class Endian { Little, Big };
+
+/// Returns the value of the bit in the given sequence of bytes.
+static inline bool bitof(const std::byte *B, unsigned BitIndex) {
+ return (B[BitIndex / 8] & (std::byte{1} << (BitIndex % 8))) != std::byte{0};
+}
+
+/// Returns whether \p N is a full byte offset or size.
+static inline bool fullByte(unsigned N) { return N % 8 == 0; }
+
+/// Track what bits have been initialized to known values and which ones
+/// have indeterminate value.
+/// All offsets are in bits.
----------------
tbaederr wrote:
Yeah I was wondering the same thing at some point, we could just have a POD struct wrapping a `size_t` or similar and use that for bits, we already have `CharUnits` for bytes although that's more "bytes for the target system", which might also be _another_ distinction we have to make _if_ the host and target byte size aren't the same... But I don't even want to think about that.
I can imagine just having
```c++
struct Bytes { size_t N; Bits toBits();}
struct Bits { size_t N; }
```
and use those.
https://github.com/llvm/llvm-project/pull/116843
More information about the cfe-commits
mailing list