LLVM 23.0.0git
LEB128.h
Go to the documentation of this file.
1//===- llvm/Support/LEB128.h - [SU]LEB128 utility functions -----*- C++ -*-===//
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// This file declares some utility functions for encoding SLEB128 and
10// ULEB128 values.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_LEB128_H
15#define LLVM_SUPPORT_LEB128_H
16
19
20namespace llvm {
21
22/// Utility function to encode a SLEB128 value to an output stream. Returns
23/// the length in bytes of the encoded value.
24inline unsigned encodeSLEB128(int64_t Value, raw_ostream &OS,
25 unsigned PadTo = 0) {
26 bool More;
27 unsigned Count = 0;
28 do {
29 uint8_t Byte = Value & 0x7f;
30 // NOTE: this assumes that this signed shift is an arithmetic right shift.
31 Value >>= 7;
32 More = Value != ((Byte & 0x40) ? -1 : 0);
33 Count++;
34 if (More || Count < PadTo)
35 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
36 OS << char(Byte);
37 } while (More);
38
39 // Pad with 0x80 and emit a terminating byte at the end.
40 if (Count < PadTo) {
41 uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
42 for (; Count < PadTo - 1; ++Count)
43 OS << char(PadValue | 0x80);
44 OS << char(PadValue);
45 Count++;
46 }
47 return Count;
48}
49
50/// Utility function to encode a SLEB128 value to a buffer. Returns
51/// the length in bytes of the encoded value.
52inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned PadTo = 0) {
53 uint8_t *orig_p = p;
54 unsigned Count = 0;
55 bool More;
56 do {
57 uint8_t Byte = Value & 0x7f;
58 // NOTE: this assumes that this signed shift is an arithmetic right shift.
59 Value >>= 7;
60 More = Value != ((Byte & 0x40) ? -1 : 0);
61 Count++;
62 if (More || Count < PadTo)
63 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
64 *p++ = Byte;
65 } while (More);
66
67 // Pad with 0x80 and emit a terminating byte at the end.
68 if (Count < PadTo) {
69 uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
70 for (; Count < PadTo - 1; ++Count)
71 *p++ = (PadValue | 0x80);
72 *p++ = PadValue;
73 }
74 return (unsigned)(p - orig_p);
75}
76
77/// Utility function to encode a ULEB128 value to an output stream. Returns
78/// the length in bytes of the encoded value.
80 unsigned PadTo = 0) {
81 unsigned Count = 0;
82 do {
83 uint8_t Byte = Value & 0x7f;
84 Value >>= 7;
85 Count++;
86 if (Value != 0 || Count < PadTo)
87 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
88 OS << char(Byte);
89 } while (Value != 0);
90
91 // Pad with 0x80 and emit a null byte at the end.
92 if (Count < PadTo) {
93 for (; Count < PadTo - 1; ++Count)
94 OS << '\x80';
95 OS << '\x00';
96 Count++;
97 }
98 return Count;
99}
100
101/// Utility function to encode a ULEB128 value to a buffer. Returns
102/// the length in bytes of the encoded value.
104 unsigned PadTo = 0) {
105 uint8_t *orig_p = p;
106 unsigned Count = 0;
107 do {
108 uint8_t Byte = Value & 0x7f;
109 Value >>= 7;
110 Count++;
111 if (Value != 0 || Count < PadTo)
112 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
113 *p++ = Byte;
114 } while (Value != 0);
115
116 // Pad with 0x80 and emit a null byte at the end.
117 if (Count < PadTo) {
118 for (; Count < PadTo - 1; ++Count)
119 *p++ = '\x80';
120 *p++ = '\x00';
121 }
122
123 return (unsigned)(p - orig_p);
124}
125
126/// Utility function to decode a ULEB128 value.
127///
128/// If \p error is non-null, it will point to a static error message,
129/// if an error occurred. It will not be modified on success.
130inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
131 const uint8_t *end = nullptr,
132 const char **error = nullptr) {
133 const uint8_t *orig_p = p;
134 uint64_t Value = 0;
135 unsigned Shift = 0;
136 do {
137 if (LLVM_UNLIKELY(p == end)) {
138 if (error)
139 *error = "malformed uleb128, extends past end";
140 Value = 0;
141 break;
142 }
143 uint64_t Slice = *p & 0x7f;
144 if (LLVM_UNLIKELY(Shift >= 63) &&
145 ((Shift == 63 && (Slice << Shift >> Shift) != Slice) ||
146 (Shift > 63 && Slice != 0))) {
147 if (error)
148 *error = "uleb128 too big for uint64";
149 Value = 0;
150 break;
151 }
152 // Once Shift reaches 64 the remaining bytes have already been validated
153 // above to be pure zero-extension, so they contribute nothing. Performing
154 // "Slice << Shift" with Shift >= 64 would be undefined behavior, so skip
155 // it.
156 if (LLVM_LIKELY(Shift < 64))
157 Value += Slice << Shift;
158 Shift += 7;
159 } while (*p++ >= 128);
160 if (n)
161 *n = (unsigned)(p - orig_p);
162 return Value;
163}
164
165/// Utility function to decode a SLEB128 value.
166///
167/// If \p error is non-null, it will point to a static error message,
168/// if an error occurred. It will not be modified on success.
169inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
170 const uint8_t *end = nullptr,
171 const char **error = nullptr) {
172 const uint8_t *orig_p = p;
173 int64_t Value = 0;
174 unsigned Shift = 0;
175 uint8_t Byte;
176 do {
177 if (LLVM_UNLIKELY(p == end)) {
178 if (error)
179 *error = "malformed sleb128, extends past end";
180 if (n)
181 *n = (unsigned)(p - orig_p);
182 return 0;
183 }
184 Byte = *p;
185 uint64_t Slice = Byte & 0x7f;
186 if (LLVM_UNLIKELY(Shift >= 63) &&
187 ((Shift == 63 && Slice != 0 && Slice != 0x7f) ||
188 (Shift > 63 && Slice != (Value < 0 ? 0x7f : 0x00)))) {
189 if (error)
190 *error = "sleb128 too big for int64";
191 if (n)
192 *n = (unsigned)(p - orig_p);
193 return 0;
194 }
195 // Once Shift reaches 64 the remaining bytes have already been validated
196 // above to be pure sign-extension, so they contribute nothing. Performing
197 // "Slice << Shift" with Shift >= 64 would be undefined behavior, so skip
198 // it.
199 if (LLVM_LIKELY(Shift < 64))
200 Value |= Slice << Shift;
201 Shift += 7;
202 ++p;
203 } while (Byte >= 128);
204 // Sign extend negative numbers if needed.
205 if (Shift < 64 && (Byte & 0x40))
206 Value |= UINT64_MAX << Shift;
207 if (n)
208 *n = (unsigned)(p - orig_p);
209 return Value;
210}
211
212inline uint64_t decodeULEB128AndInc(const uint8_t *&p, const uint8_t *end,
213 const char **error = nullptr) {
214 unsigned n;
215 auto ret = decodeULEB128(p, &n, end, error);
216 p += n;
217 return ret;
218}
219
220inline int64_t decodeSLEB128AndInc(const uint8_t *&p, const uint8_t *end,
221 const char **error = nullptr) {
222 unsigned n;
223 auto ret = decodeSLEB128(p, &n, end, error);
224 p += n;
225 return ret;
226}
227
229 return decodeULEB128AndInc(p, nullptr);
230}
231
232/// Overwrite a ULEB128 value and keep the original length.
234 while (*bufLoc & 0x80) {
235 *bufLoc++ = 0x80 | (val & 0x7f);
236 val >>= 7;
237 }
238 *bufLoc = val;
239 return val;
240}
241
242enum class LEB128Sign { Unsigned, Signed };
243
244template <LEB128Sign Sign, typename T, typename U = char,
245 unsigned MaxLEB128SizeBytes = 16>
246inline void appendLEB128(SmallVectorImpl<U> &Buffer, T Value) {
247 static_assert(sizeof(U) == 1, "Expected buffer of bytes");
248 unsigned LEB128ValueSize;
249 U TmpBuffer[MaxLEB128SizeBytes];
250 if constexpr (Sign == LEB128Sign::Signed)
251 LEB128ValueSize =
252 encodeSLEB128(Value, reinterpret_cast<uint8_t *>(TmpBuffer));
253 else
254 LEB128ValueSize =
255 encodeULEB128(Value, reinterpret_cast<uint8_t *>(TmpBuffer));
256 Buffer.append(TmpBuffer, TmpBuffer + LEB128ValueSize);
257}
258
259/// Utility function to get the size of the ULEB128-encoded value.
260LLVM_ABI extern unsigned getULEB128Size(uint64_t Value);
261
262/// Utility function to get the size of the SLEB128-encoded value.
263LLVM_ABI extern unsigned getSLEB128Size(int64_t Value);
264
265} // namespace llvm
266
267#endif // LLVM_SUPPORT_LEB128_H
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:338
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:337
#define T
#define error(X)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define UINT64_MAX
Definition DataTypes.h:77
This is an optimization pass for GlobalISel generic memory operations.
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
Definition LEB128.h:130
int64_t decodeSLEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a SLEB128 value.
Definition LEB128.h:169
int64_t decodeSLEB128AndInc(const uint8_t *&p, const uint8_t *end, const char **error=nullptr)
Definition LEB128.h:220
uint64_t decodeULEB128AndInc(const uint8_t *&p, const uint8_t *end, const char **error=nullptr)
Definition LEB128.h:212
uint64_t overwriteULEB128(uint8_t *bufLoc, uint64_t val)
Overwrite a ULEB128 value and keep the original length.
Definition LEB128.h:233
LLVM_ABI unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
Definition LEB128.cpp:19
LEB128Sign
Definition LEB128.h:242
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
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
void appendLEB128(SmallVectorImpl< U > &Buffer, T Value)
Definition LEB128.h:246
uint64_t decodeULEB128AndIncUnsafe(const uint8_t *&p)
Definition LEB128.h:228
LLVM_ABI unsigned getSLEB128Size(int64_t Value)
Utility function to get the size of the SLEB128-encoded value.
Definition LEB128.cpp:29