LLVM 24.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/// Identifies why ULEB128 decoding failed.
128 /// No decoding error has been reported.
130 /// The encoding requires bytes beyond the supplied buffer.
132 /// The encoded value does not fit in uint64_t.
134};
135
136/// Utility function to decode a ULEB128 value and report a typed error.
137///
138/// \p p is the first byte of the encoding.
139/// If \p n is non-null, it receives the number of bytes consumed.
140/// If \p end is non-null, decoding will not read at or beyond that address.
141/// If \p error is non-null, it will point to a static error message if an error
142/// occurred. It will not be modified on success.
143/// If \p errorCode is non-null, it will identify the decoding outcome. It is
144/// set to \c None on entry and only changed when decoding fails.
145inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n, const uint8_t *end,
146 const char **error,
147 ULEB128DecodeError *errorCode) {
148 if (errorCode)
149 *errorCode = ULEB128DecodeError::None;
150
151 const uint8_t *orig_p = p;
152 uint64_t Value = 0;
153 unsigned Shift = 0;
154 do {
155 if (LLVM_UNLIKELY(p == end)) {
156 if (error)
157 *error = "malformed uleb128, extends past end";
158 if (errorCode)
160 Value = 0;
161 break;
162 }
163 uint64_t Slice = *p & 0x7f;
164 if (LLVM_UNLIKELY(Shift >= 63) &&
165 ((Shift == 63 && (Slice << Shift >> Shift) != Slice) ||
166 (Shift > 63 && Slice != 0))) {
167 if (error)
168 *error = "uleb128 too big for uint64";
169 if (errorCode)
170 *errorCode = ULEB128DecodeError::TooBig;
171 Value = 0;
172 break;
173 }
174 // Once Shift reaches 64 the remaining bytes have already been validated
175 // above to be pure zero-extension, so they contribute nothing. Performing
176 // "Slice << Shift" with Shift >= 64 would be undefined behavior, so skip
177 // it.
178 if (LLVM_LIKELY(Shift < 64))
179 Value += Slice << Shift;
180 Shift += 7;
181 } while (*p++ >= 128);
182 if (n)
183 *n = (unsigned)(p - orig_p);
184 return Value;
185}
186
187/// Utility function to decode a ULEB128 value.
188///
189/// If \p n is non-null, it receives the number of bytes consumed on success.
190/// If \p end is non-null, decoding will not read at or beyond that address.
191/// If \p error is non-null, it will point to a static error message if an error
192/// occurred. It will not be modified on success.
193inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
194 const uint8_t *end = nullptr,
195 const char **error = nullptr) {
196 return decodeULEB128(p, n, end, error, nullptr);
197}
198
199/// Utility function to decode a SLEB128 value.
200///
201/// If \p error is non-null, it will point to a static error message,
202/// if an error occurred. It will not be modified on success.
203inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
204 const uint8_t *end = nullptr,
205 const char **error = nullptr) {
206 const uint8_t *orig_p = p;
207 int64_t Value = 0;
208 unsigned Shift = 0;
209 uint8_t Byte;
210 do {
211 if (LLVM_UNLIKELY(p == end)) {
212 if (error)
213 *error = "malformed sleb128, extends past end";
214 if (n)
215 *n = (unsigned)(p - orig_p);
216 return 0;
217 }
218 Byte = *p;
219 uint64_t Slice = Byte & 0x7f;
220 if (LLVM_UNLIKELY(Shift >= 63) &&
221 ((Shift == 63 && Slice != 0 && Slice != 0x7f) ||
222 (Shift > 63 && Slice != (Value < 0 ? 0x7f : 0x00)))) {
223 if (error)
224 *error = "sleb128 too big for int64";
225 if (n)
226 *n = (unsigned)(p - orig_p);
227 return 0;
228 }
229 // Once Shift reaches 64 the remaining bytes have already been validated
230 // above to be pure sign-extension, so they contribute nothing. Performing
231 // "Slice << Shift" with Shift >= 64 would be undefined behavior, so skip
232 // it.
233 if (LLVM_LIKELY(Shift < 64))
234 Value |= Slice << Shift;
235 Shift += 7;
236 ++p;
237 } while (Byte >= 128);
238 // Sign extend negative numbers if needed.
239 if (Shift < 64 && (Byte & 0x40))
240 Value |= UINT64_MAX << Shift;
241 if (n)
242 *n = (unsigned)(p - orig_p);
243 return Value;
244}
245
246inline uint64_t decodeULEB128AndInc(const uint8_t *&p, const uint8_t *end,
247 const char **error = nullptr) {
248 unsigned n;
249 auto ret = decodeULEB128(p, &n, end, error);
250 p += n;
251 return ret;
252}
253
254inline int64_t decodeSLEB128AndInc(const uint8_t *&p, const uint8_t *end,
255 const char **error = nullptr) {
256 unsigned n;
257 auto ret = decodeSLEB128(p, &n, end, error);
258 p += n;
259 return ret;
260}
261
263 return decodeULEB128AndInc(p, nullptr);
264}
265
266/// Overwrite a ULEB128 value and keep the original length.
268 while (*bufLoc & 0x80) {
269 *bufLoc++ = 0x80 | (val & 0x7f);
270 val >>= 7;
271 }
272 *bufLoc = val;
273 return val;
274}
275
276enum class LEB128Sign { Unsigned, Signed };
277
278template <LEB128Sign Sign, typename T, typename U = char,
279 unsigned MaxLEB128SizeBytes = 16>
280inline void appendLEB128(SmallVectorImpl<U> &Buffer, T Value) {
281 static_assert(sizeof(U) == 1, "Expected buffer of bytes");
282 unsigned LEB128ValueSize;
283 U TmpBuffer[MaxLEB128SizeBytes];
284 if constexpr (Sign == LEB128Sign::Signed)
285 LEB128ValueSize =
286 encodeSLEB128(Value, reinterpret_cast<uint8_t *>(TmpBuffer));
287 else
288 LEB128ValueSize =
289 encodeULEB128(Value, reinterpret_cast<uint8_t *>(TmpBuffer));
290 Buffer.append(TmpBuffer, TmpBuffer + LEB128ValueSize);
291}
292
293/// Utility function to get the size of the ULEB128-encoded value.
294LLVM_ABI extern unsigned getULEB128Size(uint64_t Value);
295
296/// Utility function to get the size of the SLEB128-encoded value.
297LLVM_ABI extern unsigned getSLEB128Size(int64_t Value);
298
299} // namespace llvm
300
301#endif // LLVM_SUPPORT_LEB128_H
unsigned uint64_t
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:344
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:343
#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.
ULEB128DecodeError
Identifies why ULEB128 decoding failed.
Definition LEB128.h:127
@ UnexpectedEnd
The encoding requires bytes beyond the supplied buffer.
Definition LEB128.h:131
@ None
No decoding error has been reported.
Definition LEB128.h:129
@ TooBig
The encoded value does not fit in uint64_t.
Definition LEB128.h:133
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:203
int64_t decodeSLEB128AndInc(const uint8_t *&p, const uint8_t *end, const char **error=nullptr)
Definition LEB128.h:254
uint64_t decodeULEB128AndInc(const uint8_t *&p, const uint8_t *end, const char **error=nullptr)
Definition LEB128.h:246
uint64_t overwriteULEB128(uint8_t *bufLoc, uint64_t val)
Overwrite a ULEB128 value and keep the original length.
Definition LEB128.h:267
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:276
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:280
uint64_t decodeULEB128AndIncUnsafe(const uint8_t *&p)
Definition LEB128.h:262
uint64_t decodeULEB128(const uint8_t *p, unsigned *n, const uint8_t *end, const char **error, ULEB128DecodeError *errorCode)
Utility function to decode a ULEB128 value and report a typed error.
Definition LEB128.h:145
LLVM_ABI unsigned getSLEB128Size(int64_t Value)
Utility function to get the size of the SLEB128-encoded value.
Definition LEB128.cpp:29