1*700637cbSDimitry Andric //===-------------------- Bitcastbuffer.cpp ---------------------*- C++ -*-===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric #include "BitcastBuffer.h"
9*700637cbSDimitry Andric #include "llvm/ADT/STLExtras.h"
10*700637cbSDimitry Andric
11*700637cbSDimitry Andric using namespace clang;
12*700637cbSDimitry Andric using namespace clang::interp;
13*700637cbSDimitry Andric
14*700637cbSDimitry Andric /// Returns the value of the bit in the given sequence of bytes.
bitof(const std::byte * B,Bits BitIndex)15*700637cbSDimitry Andric static inline bool bitof(const std::byte *B, Bits BitIndex) {
16*700637cbSDimitry Andric return (B[BitIndex.roundToBytes()] &
17*700637cbSDimitry Andric (std::byte{1} << BitIndex.getOffsetInByte())) != std::byte{0};
18*700637cbSDimitry Andric }
19*700637cbSDimitry Andric
pushData(const std::byte * In,Bits BitOffset,Bits BitWidth,Endian TargetEndianness)20*700637cbSDimitry Andric void BitcastBuffer::pushData(const std::byte *In, Bits BitOffset, Bits BitWidth,
21*700637cbSDimitry Andric Endian TargetEndianness) {
22*700637cbSDimitry Andric for (unsigned It = 0; It != BitWidth.getQuantity(); ++It) {
23*700637cbSDimitry Andric bool BitValue = bitof(In, Bits(It));
24*700637cbSDimitry Andric if (!BitValue)
25*700637cbSDimitry Andric continue;
26*700637cbSDimitry Andric
27*700637cbSDimitry Andric Bits DstBit;
28*700637cbSDimitry Andric if (TargetEndianness == Endian::Little)
29*700637cbSDimitry Andric DstBit = BitOffset + Bits(It);
30*700637cbSDimitry Andric else
31*700637cbSDimitry Andric DstBit = size() - BitOffset - BitWidth + Bits(It);
32*700637cbSDimitry Andric
33*700637cbSDimitry Andric size_t DstByte = DstBit.roundToBytes();
34*700637cbSDimitry Andric Data[DstByte] |= std::byte{1} << DstBit.getOffsetInByte();
35*700637cbSDimitry Andric }
36*700637cbSDimitry Andric }
37*700637cbSDimitry Andric
38*700637cbSDimitry Andric std::unique_ptr<std::byte[]>
copyBits(Bits BitOffset,Bits BitWidth,Bits FullBitWidth,Endian TargetEndianness) const39*700637cbSDimitry Andric BitcastBuffer::copyBits(Bits BitOffset, Bits BitWidth, Bits FullBitWidth,
40*700637cbSDimitry Andric Endian TargetEndianness) const {
41*700637cbSDimitry Andric assert(BitWidth.getQuantity() <= FullBitWidth.getQuantity());
42*700637cbSDimitry Andric assert(FullBitWidth.isFullByte());
43*700637cbSDimitry Andric auto Out = std::make_unique<std::byte[]>(FullBitWidth.roundToBytes());
44*700637cbSDimitry Andric
45*700637cbSDimitry Andric for (unsigned It = 0; It != BitWidth.getQuantity(); ++It) {
46*700637cbSDimitry Andric Bits BitIndex;
47*700637cbSDimitry Andric if (TargetEndianness == Endian::Little)
48*700637cbSDimitry Andric BitIndex = BitOffset + Bits(It);
49*700637cbSDimitry Andric else
50*700637cbSDimitry Andric BitIndex = size() - BitWidth - BitOffset + Bits(It);
51*700637cbSDimitry Andric
52*700637cbSDimitry Andric bool BitValue = bitof(Data.get(), BitIndex);
53*700637cbSDimitry Andric if (!BitValue)
54*700637cbSDimitry Andric continue;
55*700637cbSDimitry Andric
56*700637cbSDimitry Andric Bits DstBit = Bits(It);
57*700637cbSDimitry Andric size_t DstByte = DstBit.roundToBytes();
58*700637cbSDimitry Andric Out[DstByte] |= std::byte{1} << DstBit.getOffsetInByte();
59*700637cbSDimitry Andric }
60*700637cbSDimitry Andric
61*700637cbSDimitry Andric return Out;
62*700637cbSDimitry Andric }
63*700637cbSDimitry Andric
allInitialized() const64*700637cbSDimitry Andric bool BitcastBuffer::allInitialized() const {
65*700637cbSDimitry Andric return rangeInitialized(Bits::zero(), FinalBitSize);
66*700637cbSDimitry Andric }
67*700637cbSDimitry Andric
markInitialized(Bits Offset,Bits Length)68*700637cbSDimitry Andric void BitcastBuffer::markInitialized(Bits Offset, Bits Length) {
69*700637cbSDimitry Andric if (Length.isZero())
70*700637cbSDimitry Andric return;
71*700637cbSDimitry Andric
72*700637cbSDimitry Andric BitRange Element(Offset, Offset + Length - Bits(1));
73*700637cbSDimitry Andric if (InitializedBits.empty()) {
74*700637cbSDimitry Andric InitializedBits.push_back(Element);
75*700637cbSDimitry Andric return;
76*700637cbSDimitry Andric }
77*700637cbSDimitry Andric
78*700637cbSDimitry Andric assert(InitializedBits.size() >= 1);
79*700637cbSDimitry Andric // Common case of just appending.
80*700637cbSDimitry Andric Bits End = InitializedBits.back().End;
81*700637cbSDimitry Andric if (End <= Offset) {
82*700637cbSDimitry Andric // Merge this range with the last one.
83*700637cbSDimitry Andric // In the best-case scenario, this means we only ever have
84*700637cbSDimitry Andric // one single bit range covering all bits.
85*700637cbSDimitry Andric if (End == (Offset - Bits(1))) {
86*700637cbSDimitry Andric InitializedBits.back().End = Element.End;
87*700637cbSDimitry Andric return;
88*700637cbSDimitry Andric }
89*700637cbSDimitry Andric
90*700637cbSDimitry Andric // Otherwise, we can simply append.
91*700637cbSDimitry Andric InitializedBits.push_back(Element);
92*700637cbSDimitry Andric } else {
93*700637cbSDimitry Andric // Insert sorted.
94*700637cbSDimitry Andric auto It = llvm::upper_bound(InitializedBits, Element);
95*700637cbSDimitry Andric InitializedBits.insert(It, Element);
96*700637cbSDimitry Andric }
97*700637cbSDimitry Andric
98*700637cbSDimitry Andric #ifndef NDEBUG
99*700637cbSDimitry Andric // Ensure ranges are sorted and non-overlapping.
100*700637cbSDimitry Andric assert(llvm::is_sorted(InitializedBits));
101*700637cbSDimitry Andric for (unsigned I = 1; I != InitializedBits.size(); ++I) {
102*700637cbSDimitry Andric [[maybe_unused]] auto Prev = InitializedBits[I - 1];
103*700637cbSDimitry Andric [[maybe_unused]] auto Cur = InitializedBits[I];
104*700637cbSDimitry Andric assert(Prev.End.N < Cur.Start.N);
105*700637cbSDimitry Andric }
106*700637cbSDimitry Andric #endif
107*700637cbSDimitry Andric }
108*700637cbSDimitry Andric
rangeInitialized(Bits Offset,Bits Length) const109*700637cbSDimitry Andric bool BitcastBuffer::rangeInitialized(Bits Offset, Bits Length) const {
110*700637cbSDimitry Andric if (Length.isZero())
111*700637cbSDimitry Andric return true;
112*700637cbSDimitry Andric
113*700637cbSDimitry Andric BitRange Range(Offset, Offset + Length - Bits(1));
114*700637cbSDimitry Andric Bits Sum;
115*700637cbSDimitry Andric bool FoundStart = false;
116*700637cbSDimitry Andric for (BitRange BR : InitializedBits) {
117*700637cbSDimitry Andric if (FoundStart) {
118*700637cbSDimitry Andric if (BR.contains(Range.End)) {
119*700637cbSDimitry Andric Sum += (Range.End - BR.Start + Bits(1));
120*700637cbSDimitry Andric break;
121*700637cbSDimitry Andric }
122*700637cbSDimitry Andric
123*700637cbSDimitry Andric // Else, BR is completely inside Range.
124*700637cbSDimitry Andric Sum += BR.size();
125*700637cbSDimitry Andric }
126*700637cbSDimitry Andric if (BR.contains(Range.Start)) {
127*700637cbSDimitry Andric Sum += (BR.End - Range.Start + Bits(1));
128*700637cbSDimitry Andric FoundStart = true;
129*700637cbSDimitry Andric }
130*700637cbSDimitry Andric }
131*700637cbSDimitry Andric
132*700637cbSDimitry Andric // Note that Sum can be larger than Range, e.g. when Range is fully
133*700637cbSDimitry Andric // contained in one range.
134*700637cbSDimitry Andric return Sum >= Range.size();
135*700637cbSDimitry Andric }
136*700637cbSDimitry Andric
137*700637cbSDimitry Andric #if 0
138*700637cbSDimitry Andric template<typename T>
139*700637cbSDimitry Andric static std::string hex(T t) {
140*700637cbSDimitry Andric std::stringstream stream;
141*700637cbSDimitry Andric stream << std::hex << (int)t;
142*700637cbSDimitry Andric return std::string(stream.str());
143*700637cbSDimitry Andric }
144*700637cbSDimitry Andric
145*700637cbSDimitry Andric
146*700637cbSDimitry Andric void BitcastBuffer::dump(bool AsHex = true) const {
147*700637cbSDimitry Andric llvm::errs() << "LSB\n ";
148*700637cbSDimitry Andric unsigned LineLength = 0;
149*700637cbSDimitry Andric for (unsigned I = 0; I != (FinalBitSize / 8); ++I) {
150*700637cbSDimitry Andric std::byte B = Data[I];
151*700637cbSDimitry Andric if (AsHex) {
152*700637cbSDimitry Andric std::stringstream stream;
153*700637cbSDimitry Andric stream << std::hex << (int)B;
154*700637cbSDimitry Andric llvm::errs() << stream.str();
155*700637cbSDimitry Andric LineLength += stream.str().size() + 1;
156*700637cbSDimitry Andric } else {
157*700637cbSDimitry Andric llvm::errs() << std::bitset<8>((int)B).to_string();
158*700637cbSDimitry Andric LineLength += 8 + 1;
159*700637cbSDimitry Andric // llvm::errs() << (int)B;
160*700637cbSDimitry Andric }
161*700637cbSDimitry Andric llvm::errs() << ' ';
162*700637cbSDimitry Andric }
163*700637cbSDimitry Andric llvm::errs() << '\n';
164*700637cbSDimitry Andric
165*700637cbSDimitry Andric for (unsigned I = 0; I != LineLength; ++I)
166*700637cbSDimitry Andric llvm::errs() << ' ';
167*700637cbSDimitry Andric llvm::errs() << "MSB\n";
168*700637cbSDimitry Andric }
169*700637cbSDimitry Andric #endif
170