xref: /llvm-project/clang/lib/AST/ByteCode/BitcastBuffer.cpp (revision 2f9cd43a736008bdecdd920f84c702209ddbd20f)
1 //===-------------------- Bitcastbuffer.cpp ---------------------*- 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 #include "BitcastBuffer.h"
9 #include "llvm/ADT/STLExtras.h"
10 
11 using namespace clang;
12 using namespace clang::interp;
13 
14 /// Returns the value of the bit in the given sequence of bytes.
15 static inline bool bitof(const std::byte *B, Bits BitIndex) {
16   return (B[BitIndex.roundToBytes()] &
17           (std::byte{1} << BitIndex.getOffsetInByte())) != std::byte{0};
18 }
19 
20 void BitcastBuffer::pushData(const std::byte *In, Bits BitOffset, Bits BitWidth,
21                              Endian TargetEndianness) {
22   for (unsigned It = 0; It != BitWidth.getQuantity(); ++It) {
23     bool BitValue = bitof(In, Bits(It));
24     if (!BitValue)
25       continue;
26 
27     Bits DstBit;
28     if (TargetEndianness == Endian::Little)
29       DstBit = BitOffset + Bits(It);
30     else
31       DstBit = size() - BitOffset - BitWidth + Bits(It);
32 
33     size_t DstByte = DstBit.roundToBytes();
34     Data[DstByte] |= std::byte{1} << DstBit.getOffsetInByte();
35   }
36 }
37 
38 std::unique_ptr<std::byte[]>
39 BitcastBuffer::copyBits(Bits BitOffset, Bits BitWidth, Bits FullBitWidth,
40                         Endian TargetEndianness) const {
41   assert(BitWidth.getQuantity() <= FullBitWidth.getQuantity());
42   assert(FullBitWidth.isFullByte());
43   auto Out = std::make_unique<std::byte[]>(FullBitWidth.roundToBytes());
44 
45   for (unsigned It = 0; It != BitWidth.getQuantity(); ++It) {
46     Bits BitIndex;
47     if (TargetEndianness == Endian::Little)
48       BitIndex = BitOffset + Bits(It);
49     else
50       BitIndex = size() - BitWidth - BitOffset + Bits(It);
51 
52     bool BitValue = bitof(Data.get(), BitIndex);
53     if (!BitValue)
54       continue;
55 
56     Bits DstBit = Bits(It);
57     size_t DstByte = DstBit.roundToBytes();
58     Out[DstByte] |= std::byte{1} << DstBit.getOffsetInByte();
59   }
60 
61   return Out;
62 }
63 
64 bool BitcastBuffer::allInitialized() const {
65   Bits Sum;
66   for (BitRange BR : InitializedBits)
67     Sum += BR.size();
68 
69   return Sum == FinalBitSize;
70 }
71 
72 void BitcastBuffer::markInitialized(Bits Offset, Bits Length) {
73   if (Length.isZero())
74     return;
75 
76   BitRange Element(Offset, Offset + Length - Bits(1));
77   if (InitializedBits.empty()) {
78     InitializedBits.push_back(Element);
79     return;
80   }
81 
82   assert(InitializedBits.size() >= 1);
83   // Common case of just appending.
84   Bits End = InitializedBits.back().End;
85   if (End <= Offset) {
86     // Merge this range with the last one.
87     // In the best-case scenario, this means we only ever have
88     // one single bit range covering all bits.
89     if (End == (Offset - Bits(1))) {
90       InitializedBits.back().End = Element.End;
91       return;
92     }
93 
94     // Otherwise, we can simply append.
95     InitializedBits.push_back(Element);
96   } else {
97     // Insert sorted.
98     auto It = std::upper_bound(InitializedBits.begin(), InitializedBits.end(),
99                                Element);
100     InitializedBits.insert(It, Element);
101   }
102 
103 #ifndef NDEBUG
104   // Ensure ranges are sorted and non-overlapping.
105   assert(llvm::is_sorted(InitializedBits));
106   for (unsigned I = 1; I != InitializedBits.size(); ++I) {
107     [[maybe_unused]] auto Prev = InitializedBits[I - 1];
108     [[maybe_unused]] auto Cur = InitializedBits[I];
109     assert(Prev.End.N < Cur.Start.N);
110   }
111 #endif
112 }
113 
114 #if 0
115   template<typename T>
116   static std::string hex(T t) {
117     std::stringstream stream;
118     stream << std::hex << (int)t;
119     return std::string(stream.str());
120   }
121 
122 
123   void BitcastBuffer::dump(bool AsHex = true) const {
124     llvm::errs() << "LSB\n  ";
125     unsigned LineLength = 0;
126     for (unsigned I = 0; I != (FinalBitSize / 8); ++I) {
127       std::byte B = Data[I];
128       if (AsHex) {
129         std::stringstream stream;
130         stream << std::hex << (int)B;
131         llvm::errs() << stream.str();
132         LineLength += stream.str().size() + 1;
133       } else {
134         llvm::errs() << std::bitset<8>((int)B).to_string();
135         LineLength += 8 + 1;
136         // llvm::errs() << (int)B;
137       }
138       llvm::errs() << ' ';
139     }
140     llvm::errs() << '\n';
141 
142     for (unsigned I = 0; I != LineLength; ++I)
143       llvm::errs() << ' ';
144     llvm::errs() << "MSB\n";
145   }
146 #endif
147