|
| 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 | + |
| 10 | +using namespace clang; |
| 11 | +using namespace clang::interp; |
| 12 | + |
| 13 | +/// Returns the value of the bit in the given sequence of bytes. |
| 14 | +static inline bool bitof(const std::byte *B, Bits BitIndex) { |
| 15 | + return (B[BitIndex.roundToBytes()] & |
| 16 | + (std::byte{1} << BitIndex.getOffsetInByte())) != std::byte{0}; |
| 17 | +} |
| 18 | + |
| 19 | +void BitcastBuffer::pushData(const std::byte *In, Bits BitOffset, Bits BitWidth, |
| 20 | + Endian TargetEndianness) { |
| 21 | + for (unsigned It = 0; It != BitWidth.getQuantity(); ++It) { |
| 22 | + bool BitValue = bitof(In, Bits(It)); |
| 23 | + if (!BitValue) |
| 24 | + continue; |
| 25 | + |
| 26 | + Bits DstBit; |
| 27 | + if (TargetEndianness == Endian::Little) |
| 28 | + DstBit = BitOffset + Bits(It); |
| 29 | + else |
| 30 | + DstBit = size() - BitOffset - BitWidth + Bits(It); |
| 31 | + |
| 32 | + size_t DstByte = DstBit.roundToBytes(); |
| 33 | + Data[DstByte] |= std::byte{1} << DstBit.getOffsetInByte(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +std::unique_ptr<std::byte[]> |
| 38 | +BitcastBuffer::copyBits(Bits BitOffset, Bits BitWidth, Bits FullBitWidth, |
| 39 | + Endian TargetEndianness) const { |
| 40 | + assert(BitWidth.getQuantity() <= FullBitWidth.getQuantity()); |
| 41 | + assert(FullBitWidth.isFullByte()); |
| 42 | + auto Out = std::make_unique<std::byte[]>(FullBitWidth.roundToBytes()); |
| 43 | + |
| 44 | + for (unsigned It = 0; It != BitWidth.getQuantity(); ++It) { |
| 45 | + Bits BitIndex; |
| 46 | + if (TargetEndianness == Endian::Little) |
| 47 | + BitIndex = BitOffset + Bits(It); |
| 48 | + else |
| 49 | + BitIndex = size() - BitWidth - BitOffset + Bits(It); |
| 50 | + |
| 51 | + bool BitValue = bitof(Data.get(), BitIndex); |
| 52 | + if (!BitValue) |
| 53 | + continue; |
| 54 | + |
| 55 | + Bits DstBit = Bits(It); |
| 56 | + size_t DstByte = DstBit.roundToBytes(); |
| 57 | + Out[DstByte] |= std::byte{1} << DstBit.getOffsetInByte(); |
| 58 | + } |
| 59 | + |
| 60 | + return Out; |
| 61 | +} |
| 62 | + |
| 63 | +#if 0 |
| 64 | + template<typename T> |
| 65 | + static std::string hex(T t) { |
| 66 | + std::stringstream stream; |
| 67 | + stream << std::hex << (int)t; |
| 68 | + return std::string(stream.str()); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + void BitcastBuffer::dump(bool AsHex = true) const { |
| 73 | + llvm::errs() << "LSB\n "; |
| 74 | + unsigned LineLength = 0; |
| 75 | + for (unsigned I = 0; I != (FinalBitSize / 8); ++I) { |
| 76 | + std::byte B = Data[I]; |
| 77 | + if (AsHex) { |
| 78 | + std::stringstream stream; |
| 79 | + stream << std::hex << (int)B; |
| 80 | + llvm::errs() << stream.str(); |
| 81 | + LineLength += stream.str().size() + 1; |
| 82 | + } else { |
| 83 | + llvm::errs() << std::bitset<8>((int)B).to_string(); |
| 84 | + LineLength += 8 + 1; |
| 85 | + // llvm::errs() << (int)B; |
| 86 | + } |
| 87 | + llvm::errs() << ' '; |
| 88 | + } |
| 89 | + llvm::errs() << '\n'; |
| 90 | + |
| 91 | + for (unsigned I = 0; I != LineLength; ++I) |
| 92 | + llvm::errs() << ' '; |
| 93 | + llvm::errs() << "MSB\n"; |
| 94 | + } |
| 95 | +#endif |
0 commit comments