LLVM  16.0.0git
BinaryStreamWriter.cpp
Go to the documentation of this file.
1 //===- BinaryStreamWriter.cpp - Writes objects to a BinaryStream ----------===//
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 
10 
13 #include "llvm/Support/LEB128.h"
14 
15 using namespace llvm;
16 
18  : Stream(Ref) {}
19 
21  : Stream(Stream) {}
22 
25  : Stream(Data, Endian) {}
26 
28  if (auto EC = Stream.writeBytes(Offset, Buffer))
29  return EC;
30  Offset += Buffer.size();
31  return Error::success();
32 }
33 
35  uint8_t EncodedBytes[10] = {0};
36  unsigned Size = encodeULEB128(Value, &EncodedBytes[0]);
37  return writeBytes({EncodedBytes, Size});
38 }
39 
41  uint8_t EncodedBytes[10] = {0};
42  unsigned Size = encodeSLEB128(Value, &EncodedBytes[0]);
43  return writeBytes({EncodedBytes, Size});
44 }
45 
47  if (auto EC = writeFixedString(Str))
48  return EC;
49  if (auto EC = writeObject('\0'))
50  return EC;
51 
52  return Error::success();
53 }
54 
56 
57  return writeBytes(arrayRefFromStringRef(Str));
58 }
59 
61  return writeStreamRef(Ref, Ref.getLength());
62 }
63 
65  BinaryStreamReader SrcReader(Ref.slice(0, Length));
66  // This is a bit tricky. If we just call readBytes, we are requiring that it
67  // return us the entire stream as a contiguous buffer. There is no guarantee
68  // this can be satisfied by returning a reference straight from the buffer, as
69  // an implementation may not store all data in a single contiguous buffer. So
70  // we iterate over each contiguous chunk, writing each one in succession.
71  while (SrcReader.bytesRemaining() > 0) {
72  ArrayRef<uint8_t> Chunk;
73  if (auto EC = SrcReader.readLongestContiguousChunk(Chunk))
74  return EC;
75  if (auto EC = writeBytes(Chunk))
76  return EC;
77  }
78  return Error::success();
79 }
80 
81 std::pair<BinaryStreamWriter, BinaryStreamWriter>
83  assert(getLength() >= Off);
84 
86 
87  WritableBinaryStreamRef Second = First.drop_front(Off);
88  First = First.keep_front(Off);
90  BinaryStreamWriter W2{Second};
91  return std::make_pair(W1, W2);
92 }
93 
95  uint64_t NewOffset = alignTo(Offset, Align);
96  const uint64_t ZerosSize = 64;
97  static constexpr char Zeros[ZerosSize] = {};
98  while (Offset < NewOffset)
99  if (auto E = writeArray(
100  ArrayRef<char>(Zeros, std::min(ZerosSize, NewOffset - Offset))))
101  return E;
102  return Error::success();
103 }
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:156
BinaryStreamReader.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::BinaryStreamWriter::Stream
WritableBinaryStreamRef Stream
Definition: BinaryStreamWriter.h:184
llvm::BinaryStreamWriter::writeFixedString
Error writeFixedString(StringRef Str)
Write the string Str to the underlying stream without a null terminator.
Definition: BinaryStreamWriter.cpp:55
llvm::BinaryStreamWriter
Provides write only access to a subclass of WritableBinaryStream.
Definition: BinaryStreamWriter.h:30
llvm::BinaryStreamReader::readLongestContiguousChunk
Error readLongestContiguousChunk(ArrayRef< uint8_t > &Buffer)
Read as much as possible from the underlying string at the current offset without invoking a copy,...
Definition: BinaryStreamReader.cpp:29
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::BinaryStreamWriter::writeStreamRef
Error writeStreamRef(BinaryStreamRef Ref)
Efficiently reads all data from Ref, and writes it to this stream.
Definition: BinaryStreamWriter.cpp:60
llvm::BinaryStreamReader::bytesRemaining
uint64_t bytesRemaining() const
Definition: BinaryStreamReader.h:250
llvm::BinaryStreamWriter::writeArray
Error writeArray(ArrayRef< T > Array)
Writes an array of objects of type T to the underlying stream, as if by using memcpy.
Definition: BinaryStreamWriter.h:145
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::MutableArrayRef< uint8_t >
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
llvm::encodeSLEB128
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
First
into llvm powi allowing the code generator to produce balanced multiplication trees First
Definition: README.txt:54
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::BinaryStreamReader
Provides read only access to a subclass of BinaryStream.
Definition: BinaryStreamReader.h:29
llvm::BinaryStreamWriter::Offset
uint64_t Offset
Definition: BinaryStreamWriter.h:185
uint64_t
LEB128.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::BinaryStreamWriter::writeSLEB128
Error writeSLEB128(int64_t Value)
Write the unsigned integer Value to the underlying stream using ULEB128 encoding.
Definition: BinaryStreamWriter.cpp:40
llvm::BinaryStreamWriter::writeCString
Error writeCString(StringRef Str)
Write the string Str to the underlying stream followed by a null terminator.
Definition: BinaryStreamWriter.cpp:46
llvm::BinaryStreamWriter::writeULEB128
Error writeULEB128(uint64_t Value)
Write the unsigned integer Value to the underlying stream using ULEB128 encoding.
Definition: BinaryStreamWriter.cpp:34
llvm::ArrayRef< uint8_t >
llvm::BinaryStreamWriter::writeObject
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
Definition: BinaryStreamWriter.h:129
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:357
llvm::BinaryStreamWriter::writeBytes
Error writeBytes(ArrayRef< uint8_t > Buffer)
Write the bytes specified in Buffer to the underlying stream.
Definition: BinaryStreamWriter.cpp:27
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
BinaryStreamRef.h
uint32_t
llvm::BinaryStreamWriter::padToAlignment
Error padToAlignment(uint32_t Align)
Definition: BinaryStreamWriter.cpp:94
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::BinaryStreamWriter::split
std::pair< BinaryStreamWriter, BinaryStreamWriter > split(uint64_t Off) const
Splits the Writer into two Writers at a given offset.
Definition: BinaryStreamWriter.cpp:82
llvm::WritableBinaryStreamRef::writeBytes
Error writeBytes(uint64_t Offset, ArrayRef< uint8_t > Data) const
Given an Offset into this WritableBinaryStreamRef and some input data, writes the data to the underly...
Definition: BinaryStreamRef.cpp:116
llvm::BinaryStreamWriter::getLength
uint64_t getLength() const
Definition: BinaryStreamWriter.h:179
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:164
llvm::encodeULEB128
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80
llvm::support::endianness
endianness
Definition: Endian.h:27
BinaryStreamWriter.h
llvm::BinaryStreamWriter::BinaryStreamWriter
BinaryStreamWriter()=default
llvm::WritableBinaryStream
A BinaryStream which can be read from as well as written to.
Definition: BinaryStream.h:73
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::WritableBinaryStreamRef
Definition: BinaryStreamRef.h:220
llvm::BinaryStreamRefBase::drop_front
RefType drop_front(uint64_t N) const
Return a new BinaryStreamRef with the first N elements removed.
Definition: BinaryStreamRef.h:60
llvm::BinaryStreamRef
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Definition: BinaryStreamRef.h:155