LLVM 24.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
14#include "llvm/Support/LEB128.h"
15
16using namespace llvm;
17
20
23
27
29 if (auto EC = Stream.writeBytes(Offset, Buffer))
30 return EC;
31 Offset += Buffer.size();
32 return Error::success();
33}
34
36 assert(Value.getBitWidth() == 128 && "expected a 128-bit value");
37 // Regardless of host endianness APInt always stores the low words first.
38 const uint64_t *Words = Value.getRawData(); // [low, high]
39 if (Stream.getEndian() == endianness::big) {
40 if (auto EC = writeInteger(Words[1]))
41 return EC;
42 return writeInteger(Words[0]);
43 }
44
45 if (auto EC = writeInteger(Words[0]))
46 return EC;
47 return writeInteger(Words[1]);
48}
49
51 uint8_t EncodedBytes[10] = {0};
52 unsigned Size = encodeULEB128(Value, &EncodedBytes[0]);
53 return writeBytes({EncodedBytes, Size});
54}
55
57 uint8_t EncodedBytes[10] = {0};
58 unsigned Size = encodeSLEB128(Value, &EncodedBytes[0]);
59 return writeBytes({EncodedBytes, Size});
60}
61
63 if (auto EC = writeFixedString(Str))
64 return EC;
65 if (auto EC = writeObject('\0'))
66 return EC;
67
68 return Error::success();
69}
70
75
79
81 BinaryStreamReader SrcReader(Ref.slice(0, Length));
82 // This is a bit tricky. If we just call readBytes, we are requiring that it
83 // return us the entire stream as a contiguous buffer. There is no guarantee
84 // this can be satisfied by returning a reference straight from the buffer, as
85 // an implementation may not store all data in a single contiguous buffer. So
86 // we iterate over each contiguous chunk, writing each one in succession.
87 while (SrcReader.bytesRemaining() > 0) {
89 if (auto EC = SrcReader.readLongestContiguousChunk(Chunk))
90 return EC;
91 if (auto EC = writeBytes(Chunk))
92 return EC;
93 }
94 return Error::success();
95}
96
97std::pair<BinaryStreamWriter, BinaryStreamWriter>
99 assert(getLength() >= Off);
100
102
103 WritableBinaryStreamRef Second = First.drop_front(Off);
104 First = First.keep_front(Off);
106 BinaryStreamWriter W2{Second};
107 return {W1, W2};
108}
109
111 uint64_t NewOffset = alignTo(Offset, Align);
112 const uint64_t ZerosSize = 64;
113 static constexpr char Zeros[ZerosSize] = {};
114 while (Offset < NewOffset)
115 if (auto E = writeArray(
116 ArrayRef<char>(Zeros, std::min(ZerosSize, NewOffset - Offset))))
117 return E;
118 return Error::success();
119}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains some functions that are useful when dealing with strings.
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
Provides read only access to a subclass of BinaryStream.
LLVM_ABI Error readLongestContiguousChunk(ArrayRef< uint8_t > &Buffer)
Read as much as possible from the underlying string at the current offset without invoking a copy,...
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
LLVM_ABI Error writeCString(StringRef Str)
Write the string Str to the underlying stream followed by a null terminator.
Error writeArray(ArrayRef< T > Array)
Writes an array of objects of type T to the underlying stream, as if by using memcpy.
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
LLVM_ABI Error writeSLEB128(int64_t Value)
Write the unsigned integer Value to the underlying stream using ULEB128 encoding.
LLVM_ABI Error writeInt128(const APSInt &Value)
Write Value as a 128 bit integer (possibly truncated).
LLVM_ABI std::pair< BinaryStreamWriter, BinaryStreamWriter > split(uint64_t Off) const
Splits the Writer into two Writers at a given offset.
LLVM_ABI Error writeStreamRef(BinaryStreamRef Ref)
Efficiently reads all data from Ref, and writes it to this stream.
LLVM_ABI Error writeBytes(ArrayRef< uint8_t > Buffer)
Write the bytes specified in Buffer to the underlying stream.
LLVM_ABI Error writeFixedString(StringRef Str)
Write the string Str to the underlying stream without a null terminator.
LLVM_ABI Error writeULEB128(uint64_t Value)
Write the unsigned integer Value to the underlying stream using ULEB128 encoding.
WritableBinaryStreamRef Stream
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
LLVM_ABI Error padToAlignment(uint32_t Align)
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
A BinaryStream which can be read from as well as written to.
This is an optimization pass for GlobalISel generic memory operations.
@ Length
Definition DWP.cpp:577
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct an array ref of bytes from a string ref.
@ Off
Use window algorithm after SMS algorithm fails.
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:149
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
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:24
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:79
endianness
Definition bit.h:71
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39