LLVM 22.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 occured. 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 Value += Slice << Shift;
153 Shift += 7;
154 } while (*p++ >= 128);
155 if (n)
156 *n = (unsigned)(p - orig_p);
157 return Value;
158}
159
160/// Utility function to decode a SLEB128 value.
161///
162/// If \p error is non-null, it will point to a static error message,
163/// if an error occured. It will not be modified on success.
164inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
165 const uint8_t *end = nullptr,
166 const char **error = nullptr) {
167 const uint8_t *orig_p = p;
168 int64_t Value = 0;
169 unsigned Shift = 0;
170 uint8_t Byte;
171 do {
172 if (LLVM_UNLIKELY(p == end)) {
173 if (error)
174 *error = "malformed sleb128, extends past end";
175 if (n)
176 *n = (unsigned)(p - orig_p);
177 return 0;
178 }
179 Byte = *p;
180 uint64_t Slice = Byte & 0x7f;
181 if (LLVM_UNLIKELY(Shift >= 63) &&
182 ((Shift == 63 && Slice != 0 && Slice != 0x7f) ||
183 (Shift > 63 && Slice != (Value < 0 ? 0x7f : 0x00)))) {
184 if (error)
185 *error = "sleb128 too big for int64";
186 if (n)
187 *n = (unsigned)(p - orig_p);
188 return 0;
189 }
190 Value |= Slice << Shift;
191 Shift += 7;
192 ++p;
193 } while (Byte >= 128);
194 // Sign extend negative numbers if needed.
195 if (Shift < 64 && (Byte & 0x40))
196 Value |= UINT64_MAX << Shift;
197 if (n)
198 *n = (unsigned)(p - orig_p);
199 return Value;
200}
201
202inline uint64_t decodeULEB128AndInc(const uint8_t *&p, const uint8_t *end,
203 const char **error = nullptr) {
204 unsigned n;
205 auto ret = decodeULEB128(p, &n, end, error);
206 p += n;
207 return ret;
208}
209
210inline int64_t decodeSLEB128AndInc(const uint8_t *&p, const uint8_t *end,
211 const char **error = nullptr) {
212 unsigned n;
213 auto ret = decodeSLEB128(p, &n, end, error);
214 p += n;
215 return ret;
216}
217
219 return decodeULEB128AndInc(p, nullptr);
220}
221
222/// Overwrite a ULEB128 value and keep the original length.
224 while (*bufLoc & 0x80) {
225 *bufLoc++ = 0x80 | (val & 0x7f);
226 val >>= 7;
227 }
228 *bufLoc = val;
229 return val;
230}
231
232enum class LEB128Sign { Unsigned, Signed };
233
234template <LEB128Sign Sign, typename T, typename U = char,
235 unsigned MaxLEB128SizeBytes = 16>
236inline void appendLEB128(SmallVectorImpl<U> &Buffer, T Value) {
237 static_assert(sizeof(U) == 1, "Expected buffer of bytes");
238 unsigned LEB128ValueSize;
239 U TmpBuffer[MaxLEB128SizeBytes];
240 if constexpr (Sign == LEB128Sign::Signed)
241 LEB128ValueSize =
242 encodeSLEB128(Value, reinterpret_cast<uint8_t *>(TmpBuffer));
243 else
244 LEB128ValueSize =
245 encodeULEB128(Value, reinterpret_cast<uint8_t *>(TmpBuffer));
246 Buffer.append(TmpBuffer, TmpBuffer + LEB128ValueSize);
247}
248
249/// Utility function to get the size of the ULEB128-encoded value.
250LLVM_ABI extern unsigned getULEB128Size(uint64_t Value);
251
252/// Utility function to get the size of the SLEB128-encoded value.
253LLVM_ABI extern unsigned getSLEB128Size(int64_t Value);
254
255} // namespace llvm
256
257#endif // LLVM_SUPPORT_LEB128_H
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:336
#define LLVM_ABI
Definition Compiler.h:213
#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:164
int64_t decodeSLEB128AndInc(const uint8_t *&p, const uint8_t *end, const char **error=nullptr)
Definition LEB128.h:210
uint64_t decodeULEB128AndInc(const uint8_t *&p, const uint8_t *end, const char **error=nullptr)
Definition LEB128.h:202
uint64_t overwriteULEB128(uint8_t *bufLoc, uint64_t val)
Overwrite a ULEB128 value and keep the original length.
Definition LEB128.h:223
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
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:232
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:236
uint64_t decodeULEB128AndIncUnsafe(const uint8_t *&p)
Definition LEB128.h:218
LLVM_ABI unsigned getSLEB128Size(int64_t Value)
Utility function to get the size of the SLEB128-encoded value.
Definition LEB128.cpp:29