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 == 0 ) && ((Byte & 0x40) == 0)) ||
33 ((Value == -1) && ((Byte & 0x40) != 0))));
34 Count++;
35 if (More || Count < PadTo)
36 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
37 OS << char(Byte);
38 } while (More);
39
40 // Pad with 0x80 and emit a terminating byte at the end.
41 if (Count < PadTo) {
42 uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
43 for (; Count < PadTo - 1; ++Count)
44 OS << char(PadValue | 0x80);
45 OS << char(PadValue);
46 Count++;
47 }
48 return Count;
49}
50
51/// Utility function to encode a SLEB128 value to a buffer. Returns
52/// the length in bytes of the encoded value.
53inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned PadTo = 0) {
54 uint8_t *orig_p = p;
55 unsigned Count = 0;
56 bool More;
57 do {
58 uint8_t Byte = Value & 0x7f;
59 // NOTE: this assumes that this signed shift is an arithmetic right shift.
60 Value >>= 7;
61 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
62 ((Value == -1) && ((Byte & 0x40) != 0))));
63 Count++;
64 if (More || Count < PadTo)
65 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
66 *p++ = Byte;
67 } while (More);
68
69 // Pad with 0x80 and emit a terminating byte at the end.
70 if (Count < PadTo) {
71 uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
72 for (; Count < PadTo - 1; ++Count)
73 *p++ = (PadValue | 0x80);
74 *p++ = PadValue;
75 }
76 return (unsigned)(p - orig_p);
77}
78
79/// Utility function to encode a ULEB128 value to an output stream. Returns
80/// the length in bytes of the encoded value.
82 unsigned PadTo = 0) {
83 unsigned Count = 0;
84 do {
85 uint8_t Byte = Value & 0x7f;
86 Value >>= 7;
87 Count++;
88 if (Value != 0 || Count < PadTo)
89 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
90 OS << char(Byte);
91 } while (Value != 0);
92
93 // Pad with 0x80 and emit a null byte at the end.
94 if (Count < PadTo) {
95 for (; Count < PadTo - 1; ++Count)
96 OS << '\x80';
97 OS << '\x00';
98 Count++;
99 }
100 return Count;
101}
102
103/// Utility function to encode a ULEB128 value to a buffer. Returns
104/// the length in bytes of the encoded value.
106 unsigned PadTo = 0) {
107 uint8_t *orig_p = p;
108 unsigned Count = 0;
109 do {
110 uint8_t Byte = Value & 0x7f;
111 Value >>= 7;
112 Count++;
113 if (Value != 0 || Count < PadTo)
114 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
115 *p++ = Byte;
116 } while (Value != 0);
117
118 // Pad with 0x80 and emit a null byte at the end.
119 if (Count < PadTo) {
120 for (; Count < PadTo - 1; ++Count)
121 *p++ = '\x80';
122 *p++ = '\x00';
123 }
124
125 return (unsigned)(p - orig_p);
126}
127
128/// Utility function to decode a ULEB128 value.
129///
130/// If \p error is non-null, it will point to a static error message,
131/// if an error occured. It will not be modified on success.
132inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
133 const uint8_t *end = nullptr,
134 const char **error = nullptr) {
135 const uint8_t *orig_p = p;
136 uint64_t Value = 0;
137 unsigned Shift = 0;
138 do {
139 if (LLVM_UNLIKELY(p == end)) {
140 if (error)
141 *error = "malformed uleb128, extends past end";
142 Value = 0;
143 break;
144 }
145 uint64_t Slice = *p & 0x7f;
146 if (LLVM_UNLIKELY(Shift >= 63) &&
147 ((Shift == 63 && (Slice << Shift >> Shift) != Slice) ||
148 (Shift > 63 && Slice != 0))) {
149 if (error)
150 *error = "uleb128 too big for uint64";
151 Value = 0;
152 break;
153 }
154 Value += Slice << Shift;
155 Shift += 7;
156 } while (*p++ >= 128);
157 if (n)
158 *n = (unsigned)(p - orig_p);
159 return Value;
160}
161
162/// Utility function to decode a SLEB128 value.
163///
164/// If \p error is non-null, it will point to a static error message,
165/// if an error occured. It will not be modified on success.
166inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
167 const uint8_t *end = nullptr,
168 const char **error = nullptr) {
169 const uint8_t *orig_p = p;
170 int64_t Value = 0;
171 unsigned Shift = 0;
172 uint8_t Byte;
173 do {
174 if (LLVM_UNLIKELY(p == end)) {
175 if (error)
176 *error = "malformed sleb128, extends past end";
177 if (n)
178 *n = (unsigned)(p - orig_p);
179 return 0;
180 }
181 Byte = *p;
182 uint64_t Slice = Byte & 0x7f;
183 if (LLVM_UNLIKELY(Shift >= 63) &&
184 ((Shift == 63 && Slice != 0 && Slice != 0x7f) ||
185 (Shift > 63 && Slice != (Value < 0 ? 0x7f : 0x00)))) {
186 if (error)
187 *error = "sleb128 too big for int64";
188 if (n)
189 *n = (unsigned)(p - orig_p);
190 return 0;
191 }
192 Value |= Slice << Shift;
193 Shift += 7;
194 ++p;
195 } while (Byte >= 128);
196 // Sign extend negative numbers if needed.
197 if (Shift < 64 && (Byte & 0x40))
198 Value |= UINT64_MAX << Shift;
199 if (n)
200 *n = (unsigned)(p - orig_p);
201 return Value;
202}
203
204inline uint64_t decodeULEB128AndInc(const uint8_t *&p, const uint8_t *end,
205 const char **error = nullptr) {
206 unsigned n;
207 auto ret = decodeULEB128(p, &n, end, error);
208 p += n;
209 return ret;
210}
211
212inline int64_t decodeSLEB128AndInc(const uint8_t *&p, const uint8_t *end,
213 const char **error = nullptr) {
214 unsigned n;
215 auto ret = decodeSLEB128(p, &n, end, error);
216 p += n;
217 return ret;
218}
219
221 return decodeULEB128AndInc(p, nullptr);
222}
223
224enum class LEB128Sign { Unsigned, Signed };
225
226template <LEB128Sign Sign, typename T, typename U = char,
227 unsigned MaxLEB128SizeBytes = 16>
228inline void appendLEB128(SmallVectorImpl<U> &Buffer, T Value) {
229 static_assert(sizeof(U) == 1, "Expected buffer of bytes");
230 unsigned LEB128ValueSize;
231 U TmpBuffer[MaxLEB128SizeBytes];
232 if constexpr (Sign == LEB128Sign::Signed)
233 LEB128ValueSize =
234 encodeSLEB128(Value, reinterpret_cast<uint8_t *>(TmpBuffer));
235 else
236 LEB128ValueSize =
237 encodeULEB128(Value, reinterpret_cast<uint8_t *>(TmpBuffer));
238 Buffer.append(TmpBuffer, TmpBuffer + LEB128ValueSize);
239}
240
241/// Utility function to get the size of the ULEB128-encoded value.
242LLVM_ABI extern unsigned getULEB128Size(uint64_t Value);
243
244/// Utility function to get the size of the SLEB128-encoded value.
245LLVM_ABI extern unsigned getSLEB128Size(int64_t Value);
246
247} // namespace llvm
248
249#endif // LLVM_SUPPORT_LEB128_H
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:336
#define LLVM_ABI
Definition: Compiler.h:213
#define T
raw_pwrite_stream & OS
#define error(X)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:684
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.
Definition: AddressRanges.h:18
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:132
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:166
int64_t decodeSLEB128AndInc(const uint8_t *&p, const uint8_t *end, const char **error=nullptr)
Definition: LEB128.h:212
uint64_t decodeULEB128AndInc(const uint8_t *&p, const uint8_t *end, const char **error=nullptr)
Definition: LEB128.h:204
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:224
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:81
void appendLEB128(SmallVectorImpl< U > &Buffer, T Value)
Definition: LEB128.h:228
uint64_t decodeULEB128AndIncUnsafe(const uint8_t *&p)
Definition: LEB128.h:220
LLVM_ABI unsigned getSLEB128Size(int64_t Value)
Utility function to get the size of the SLEB128-encoded value.
Definition: LEB128.cpp:29