LLVM 23.0.0git
ContiguousBlobAccumulator.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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///
9/// \file
10/// This file implements the ContiguousBlobAccumulator methods declared in
11/// ContiguousBlobAccumulator.h.
12///
13//===----------------------------------------------------------------------===//
14
17#include "llvm/Support/Errc.h"
18#include "llvm/Support/LEB128.h"
19
20using namespace llvm;
21using namespace llvm::yaml;
22
23bool ContiguousBlobAccumulator::checkLimit(uint64_t Size) {
24 uint64_t Off = getOffset();
25 if (!ReachedLimitErr && Off <= MaxSize && Size <= MaxSize - Off)
26 return true;
27 if (!ReachedLimitErr)
29 "reached the output size limit");
30 return false;
31}
32
34 uint64_t CurrentOffset = getOffset();
35 if (ReachedLimitErr)
36 return CurrentOffset;
37
38 uint64_t AlignedOffset = alignTo(CurrentOffset, Align == 0 ? 1 : Align);
39 uint64_t PaddingSize = AlignedOffset - CurrentOffset;
40 if (!checkLimit(PaddingSize))
41 return CurrentOffset;
42
43 writeZeros(PaddingSize);
44 return AlignedOffset;
45}
46
48 uint64_t N) {
49 // Only min(binary_size(), N) bytes are written.
50 if (!checkLimit(std::min<uint64_t>(Bin.binary_size(), N)))
51 return;
52 Bin.writeAsBinary(OS, N);
53}
54
56 if (!checkLimit(sizeof(uint64_t)))
57 return 0;
58 return encodeULEB128(Val, OS);
59}
60
62 if (!checkLimit(10))
63 return 0;
64 return encodeSLEB128(Val, OS);
65}
66
68 size_t Size) {
69 assert(Pos >= InitialOffset && Pos + Size <= getOffset());
70 memcpy(&Buf[Pos - InitialOffset], Data, Size);
71}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines ContiguousBlobAccumulator, the size-limited output buffer shared by the yaml2obj em...
Specialized YAMLIO scalar type for representing a binary blob.
Definition YAML.h:64
LLVM_ABI uint64_t padToAlignment(unsigned Align)
LLVM_ABI void writeAsBinary(const BinaryRef &Bin, uint64_t N=UINT64_MAX)
LLVM_ABI void updateDataAt(uint64_t Pos, const void *Data, size_t Size)
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
@ invalid_argument
Definition Errc.h:56
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
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
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39