LLVM  10.0.0svn
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 
14 #include "llvm/Support/LEB128.h"
15 
16 using namespace llvm;
17 
19  : Stream(Ref) {}
20 
22  : Stream(Stream) {}
23 
26  : Stream(Data, Endian) {}
27 
29  if (auto EC = Stream.writeBytes(Offset, Buffer))
30  return EC;
31  Offset += Buffer.size();
32  return Error::success();
33 }
34 
36  uint8_t EncodedBytes[10] = {0};
37  unsigned Size = encodeULEB128(Value, &EncodedBytes[0]);
38  return writeBytes({EncodedBytes, Size});
39 }
40 
42  uint8_t EncodedBytes[10] = {0};
43  unsigned Size = encodeSLEB128(Value, &EncodedBytes[0]);
44  return writeBytes({EncodedBytes, Size});
45 }
46 
48  if (auto EC = writeFixedString(Str))
49  return EC;
50  if (auto EC = writeObject('\0'))
51  return EC;
52 
53  return Error::success();
54 }
55 
57 
58  return writeBytes(arrayRefFromStringRef(Str));
59 }
60 
62  return writeStreamRef(Ref, Ref.getLength());
63 }
64 
66  BinaryStreamReader SrcReader(Ref.slice(0, Length));
67  // This is a bit tricky. If we just call readBytes, we are requiring that it
68  // return us the entire stream as a contiguous buffer. There is no guarantee
69  // this can be satisfied by returning a reference straight from the buffer, as
70  // an implementation may not store all data in a single contiguous buffer. So
71  // we iterate over each contiguous chunk, writing each one in succession.
72  while (SrcReader.bytesRemaining() > 0) {
73  ArrayRef<uint8_t> Chunk;
74  if (auto EC = SrcReader.readLongestContiguousChunk(Chunk))
75  return EC;
76  if (auto EC = writeBytes(Chunk))
77  return EC;
78  }
79  return Error::success();
80 }
81 
82 std::pair<BinaryStreamWriter, BinaryStreamWriter>
84  assert(getLength() >= Off);
85 
87 
88  WritableBinaryStreamRef Second = First.drop_front(Off);
89  First = First.keep_front(Off);
90  BinaryStreamWriter W1{First};
91  BinaryStreamWriter W2{Second};
92  return std::make_pair(W1, W2);
93 }
94 
96  uint32_t NewOffset = alignTo(Offset, Align);
97  if (NewOffset > getLength())
98  return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
99  while (Offset < NewOffset)
100  if (auto EC = writeInteger('\0'))
101  return EC;
102  return Error::success();
103 }
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Error writeBytes(ArrayRef< uint8_t > Buffer)
Write the bytes specified in Buffer to the underlying stream.
RefType slice(uint32_t Offset, uint32_t Len) const
Return a new BinaryStreamRef with the first Offset elements removed, and retaining exactly Len elemen...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
ArrayRef< uint8_t > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
Definition: StringExtras.h:60
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:689
The access may reference the value stored in memory.
Error writeFixedString(StringRef Str)
Write the string Str to the underlying stream without a null terminator.
Error writeSLEB128(int64_t Value)
Write the unsigned integer Value to the underlying stream using ULEB128 encoding. ...
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
Error writeStreamRef(BinaryStreamRef Ref)
Efficiently reads all data from Ref, and writes it to this stream.
RefType drop_front(uint32_t N) const
Return a new BinaryStreamRef with the first N elements removed.
WritableBinaryStreamRef Stream
Provides write only access to a subclass of WritableBinaryStream.
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
Error writeCString(StringRef Str)
Write the string Str to the underlying stream followed by a null terminator.
uint32_t getLength() const
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
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
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
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
std::pair< BinaryStreamWriter, BinaryStreamWriter > split(uint32_t Off) const
Splits the Writer into two Writers at a given offset.
Error writeULEB128(uint64_t Value)
Write the unsigned integer Value to the underlying stream using ULEB128 encoding. ...
Error padToAlignment(uint32_t Align)
uint32_t Size
Definition: Profile.cpp:46
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:72
Error writeBytes(uint32_t Offset, ArrayRef< uint8_t > Data) const
Given an Offset into this WritableBinaryStreamRef and some input data, writes the data to the underly...
RefType keep_front(uint32_t N) const
Return a new BinaryStreamRef with only the first N elements remaining.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
A BinaryStream which can be read from as well as written to.
Definition: BinaryStream.h:73