[AMDGPU] Test codegen'ing True16 additions.
[llvm-project.git] / llvm / lib / Support / BinaryStreamRef.cpp
blob0ef00af4fcb7d5ae1f21c7532aee68e606cb02c3
1 //===- BinaryStreamRef.cpp - ----------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/BinaryStreamRef.h"
10 #include "llvm/Support/BinaryByteStream.h"
12 using namespace llvm;
13 using namespace llvm::support;
15 namespace {
17 class ArrayRefImpl : public BinaryStream {
18 public:
19 ArrayRefImpl(ArrayRef<uint8_t> Data, endianness Endian) : BBS(Data, Endian) {}
21 llvm::support::endianness getEndian() const override {
22 return BBS.getEndian();
24 Error readBytes(uint64_t Offset, uint64_t Size,
25 ArrayRef<uint8_t> &Buffer) override {
26 return BBS.readBytes(Offset, Size, Buffer);
28 Error readLongestContiguousChunk(uint64_t Offset,
29 ArrayRef<uint8_t> &Buffer) override {
30 return BBS.readLongestContiguousChunk(Offset, Buffer);
32 uint64_t getLength() override { return BBS.getLength(); }
34 private:
35 BinaryByteStream BBS;
38 class MutableArrayRefImpl : public WritableBinaryStream {
39 public:
40 MutableArrayRefImpl(MutableArrayRef<uint8_t> Data, endianness Endian)
41 : BBS(Data, Endian) {}
43 // Inherited via WritableBinaryStream
44 llvm::support::endianness getEndian() const override {
45 return BBS.getEndian();
47 Error readBytes(uint64_t Offset, uint64_t Size,
48 ArrayRef<uint8_t> &Buffer) override {
49 return BBS.readBytes(Offset, Size, Buffer);
51 Error readLongestContiguousChunk(uint64_t Offset,
52 ArrayRef<uint8_t> &Buffer) override {
53 return BBS.readLongestContiguousChunk(Offset, Buffer);
55 uint64_t getLength() override { return BBS.getLength(); }
57 Error writeBytes(uint64_t Offset, ArrayRef<uint8_t> Data) override {
58 return BBS.writeBytes(Offset, Data);
60 Error commit() override { return BBS.commit(); }
62 private:
63 MutableBinaryByteStream BBS;
65 } // namespace
67 BinaryStreamRef::BinaryStreamRef(BinaryStream &Stream)
68 : BinaryStreamRefBase(Stream) {}
69 BinaryStreamRef::BinaryStreamRef(BinaryStream &Stream, uint64_t Offset,
70 std::optional<uint64_t> Length)
71 : BinaryStreamRefBase(Stream, Offset, Length) {}
72 BinaryStreamRef::BinaryStreamRef(ArrayRef<uint8_t> Data, endianness Endian)
73 : BinaryStreamRefBase(std::make_shared<ArrayRefImpl>(Data, Endian), 0,
74 Data.size()) {}
75 BinaryStreamRef::BinaryStreamRef(StringRef Data, endianness Endian)
76 : BinaryStreamRef(ArrayRef(Data.bytes_begin(), Data.bytes_end()), Endian) {}
78 Error BinaryStreamRef::readBytes(uint64_t Offset, uint64_t Size,
79 ArrayRef<uint8_t> &Buffer) const {
80 if (auto EC = checkOffsetForRead(Offset, Size))
81 return EC;
82 return BorrowedImpl->readBytes(ViewOffset + Offset, Size, Buffer);
85 Error BinaryStreamRef::readLongestContiguousChunk(
86 uint64_t Offset, ArrayRef<uint8_t> &Buffer) const {
87 if (auto EC = checkOffsetForRead(Offset, 1))
88 return EC;
90 if (auto EC =
91 BorrowedImpl->readLongestContiguousChunk(ViewOffset + Offset, Buffer))
92 return EC;
93 // This StreamRef might refer to a smaller window over a larger stream. In
94 // that case we will have read out more bytes than we should return, because
95 // we should not read past the end of the current view.
96 uint64_t MaxLength = getLength() - Offset;
97 if (Buffer.size() > MaxLength)
98 Buffer = Buffer.slice(0, MaxLength);
99 return Error::success();
102 WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream &Stream)
103 : BinaryStreamRefBase(Stream) {}
105 WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream &Stream,
106 uint64_t Offset,
107 std::optional<uint64_t> Length)
108 : BinaryStreamRefBase(Stream, Offset, Length) {}
110 WritableBinaryStreamRef::WritableBinaryStreamRef(MutableArrayRef<uint8_t> Data,
111 endianness Endian)
112 : BinaryStreamRefBase(std::make_shared<MutableArrayRefImpl>(Data, Endian),
113 0, Data.size()) {}
115 Error WritableBinaryStreamRef::writeBytes(uint64_t Offset,
116 ArrayRef<uint8_t> Data) const {
117 if (auto EC = checkOffsetForWrite(Offset, Data.size()))
118 return EC;
120 return BorrowedImpl->writeBytes(ViewOffset + Offset, Data);
123 WritableBinaryStreamRef::operator BinaryStreamRef() const {
124 return BinaryStreamRef(*BorrowedImpl, ViewOffset, Length);
127 /// For buffered streams, commits changes to the backing store.
128 Error WritableBinaryStreamRef::commit() { return BorrowedImpl->commit(); }