clang 20.0.0git
BitcastBuffer.h
Go to the documentation of this file.
1//===--------------------- BitcastBuffer.h ----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8#ifndef LLVM_CLANG_AST_INTERP_BITCAST_BUFFER_H
9#define LLVM_CLANG_AST_INTERP_BITCAST_BUFFER_H
10
11#include "llvm/ADT/SmallVector.h"
12#include <cassert>
13#include <cstddef>
14#include <memory>
15
16namespace clang {
17namespace interp {
18
19enum class Endian { Little, Big };
20
21struct Bytes;
22
23/// A quantity in bits.
24struct Bits {
25 size_t N = 0;
26 Bits() = default;
27 static Bits zero() { return Bits(0); }
28 explicit Bits(size_t Quantity) : N(Quantity) {}
29 size_t getQuantity() const { return N; }
30 size_t roundToBytes() const { return N / 8; }
31 size_t getOffsetInByte() const { return N % 8; }
32 bool isFullByte() const { return N % 8 == 0; }
33 bool nonZero() const { return N != 0; }
34 bool isZero() const { return N == 0; }
35 Bytes toBytes() const;
36
37 Bits operator-(Bits Other) const { return Bits(N - Other.N); }
38 Bits operator+(Bits Other) const { return Bits(N + Other.N); }
39 Bits operator+=(size_t O) {
40 N += O;
41 return *this;
42 }
44 N += O.N;
45 return *this;
46 }
47
48 bool operator>=(Bits Other) const { return N >= Other.N; }
49 bool operator<=(Bits Other) const { return N <= Other.N; }
50 bool operator==(Bits Other) const { return N == Other.N; }
51 bool operator!=(Bits Other) const { return N != Other.N; }
52};
53
54/// A quantity in bytes.
55struct Bytes {
56 size_t N;
57 explicit Bytes(size_t Quantity) : N(Quantity) {}
58 size_t getQuantity() const { return N; }
59 Bits toBits() const { return Bits(N * 8); }
60};
61
62inline Bytes Bits::toBytes() const {
63 assert(isFullByte());
64 return Bytes(N / 8);
65}
66
67/// A bit range. Both Start and End are inclusive.
68struct BitRange {
71
73 Bits size() const { return End - Start + Bits(1); }
74 bool operator<(BitRange Other) const { return Start.N < Other.Start.N; }
75
76 bool contains(Bits B) { return Start <= B && End >= B; }
77};
78
79/// Track what bits have been initialized to known values and which ones
80/// have indeterminate value.
83 std::unique_ptr<std::byte[]> Data;
85
87 assert(FinalBitSize.isFullByte());
88 unsigned ByteSize = FinalBitSize.roundToBytes();
89 Data = std::make_unique<std::byte[]>(ByteSize);
90 }
91
92 /// Returns the buffer size in bits.
93 Bits size() const { return FinalBitSize; }
94 Bytes byteSize() const { return FinalBitSize.toBytes(); }
95
96 /// Returns \c true if all bits in the buffer have been initialized.
97 bool allInitialized() const;
98 /// Marks the bits in the given range as initialized.
99 /// FIXME: Can we do this automatically in pushData()?
100 void markInitialized(Bits Start, Bits Length);
101 bool rangeInitialized(Bits Offset, Bits Length) const;
102
103 /// Push \p BitWidth bits at \p BitOffset from \p In into the buffer.
104 /// \p TargetEndianness is the endianness of the target we're compiling for.
105 /// \p In must hold at least \p BitWidth many bits.
106 void pushData(const std::byte *In, Bits BitOffset, Bits BitWidth,
107 Endian TargetEndianness);
108
109 /// Copy \p BitWidth bits at offset \p BitOffset from the buffer.
110 /// \p TargetEndianness is the endianness of the target we're compiling for.
111 ///
112 /// The returned output holds exactly (\p FullBitWidth / 8) bytes.
113 std::unique_ptr<std::byte[]> copyBits(Bits BitOffset, Bits BitWidth,
114 Bits FullBitWidth,
115 Endian TargetEndianness) const;
116};
117
118} // namespace interp
119} // namespace clang
120#endif
The JSON file list parser is used to communicate input to InstallAPI.
@ Other
Other implicit parameter.
A bit range. Both Start and End are inclusive.
Definition: BitcastBuffer.h:68
bool operator<(BitRange Other) const
Definition: BitcastBuffer.h:74
BitRange(Bits Start, Bits End)
Definition: BitcastBuffer.h:72
Track what bits have been initialized to known values and which ones have indeterminate value.
Definition: BitcastBuffer.h:81
std::unique_ptr< std::byte[]> copyBits(Bits BitOffset, Bits BitWidth, Bits FullBitWidth, Endian TargetEndianness) const
Copy BitWidth bits at offset BitOffset from the buffer.
void markInitialized(Bits Start, Bits Length)
Marks the bits in the given range as initialized.
llvm::SmallVector< BitRange > InitializedBits
Definition: BitcastBuffer.h:84
bool allInitialized() const
Returns true if all bits in the buffer have been initialized.
BitcastBuffer(Bits FinalBitSize)
Definition: BitcastBuffer.h:86
bool rangeInitialized(Bits Offset, Bits Length) const
Bits size() const
Returns the buffer size in bits.
Definition: BitcastBuffer.h:93
void pushData(const std::byte *In, Bits BitOffset, Bits BitWidth, Endian TargetEndianness)
Push BitWidth bits at BitOffset from In into the buffer.
std::unique_ptr< std::byte[]> Data
Definition: BitcastBuffer.h:83
A quantity in bits.
Definition: BitcastBuffer.h:24
bool operator!=(Bits Other) const
Definition: BitcastBuffer.h:51
bool nonZero() const
Definition: BitcastBuffer.h:33
Bytes toBytes() const
Definition: BitcastBuffer.h:62
bool operator==(Bits Other) const
Definition: BitcastBuffer.h:50
bool isZero() const
Definition: BitcastBuffer.h:34
size_t roundToBytes() const
Definition: BitcastBuffer.h:30
Bits operator+=(size_t O)
Definition: BitcastBuffer.h:39
bool operator<=(Bits Other) const
Definition: BitcastBuffer.h:49
bool isFullByte() const
Definition: BitcastBuffer.h:32
static Bits zero()
Definition: BitcastBuffer.h:27
Bits(size_t Quantity)
Definition: BitcastBuffer.h:28
Bits operator-(Bits Other) const
Definition: BitcastBuffer.h:37
Bits operator+(Bits Other) const
Definition: BitcastBuffer.h:38
size_t getOffsetInByte() const
Definition: BitcastBuffer.h:31
Bits operator+=(Bits O)
Definition: BitcastBuffer.h:43
bool operator>=(Bits Other) const
Definition: BitcastBuffer.h:48
size_t getQuantity() const
Definition: BitcastBuffer.h:29
A quantity in bytes.
Definition: BitcastBuffer.h:55
size_t getQuantity() const
Definition: BitcastBuffer.h:58
Bits toBits() const
Definition: BitcastBuffer.h:59
Bytes(size_t Quantity)
Definition: BitcastBuffer.h:57