1 //===- llvm/Support/LEB128.h - [SU]LEB128 utility functions -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file declares some utility functions for encoding SLEB128 and
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_LEB128_H
15 #define LLVM_SUPPORT_LEB128_H
17 #include "llvm/Support/raw_ostream.h"
21 /// Utility function to encode a SLEB128 value to an output stream. Returns
22 /// the length in bytes of the encoded value.
23 inline unsigned encodeSLEB128(int64_t Value, raw_ostream &OS,
28 uint8_t Byte = Value & 0x7f;
29 // NOTE: this assumes that this signed shift is an arithmetic right shift.
31 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
32 ((Value == -1) && ((Byte & 0x40) != 0))));
34 if (More || Count < PadTo)
35 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
39 // Pad with 0x80 and emit a terminating byte at the end.
41 uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
42 for (; Count < PadTo - 1; ++Count)
43 OS << char(PadValue | 0x80);
50 /// Utility function to encode a SLEB128 value to a buffer. Returns
51 /// the length in bytes of the encoded value.
52 inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned PadTo = 0) {
57 uint8_t Byte = Value & 0x7f;
58 // NOTE: this assumes that this signed shift is an arithmetic right shift.
60 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
61 ((Value == -1) && ((Byte & 0x40) != 0))));
63 if (More || Count < PadTo)
64 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
68 // Pad with 0x80 and emit a terminating byte at the end.
70 uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
71 for (; Count < PadTo - 1; ++Count)
72 *p++ = (PadValue | 0x80);
75 return (unsigned)(p - orig_p);
78 /// Utility function to encode a ULEB128 value to an output stream. Returns
79 /// the length in bytes of the encoded value.
80 inline unsigned encodeULEB128(uint64_t Value, raw_ostream &OS,
84 uint8_t Byte = Value & 0x7f;
87 if (Value != 0 || Count < PadTo)
88 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
92 // Pad with 0x80 and emit a null byte at the end.
94 for (; Count < PadTo - 1; ++Count)
102 /// Utility function to encode a ULEB128 value to a buffer. Returns
103 /// the length in bytes of the encoded value.
104 inline unsigned encodeULEB128(uint64_t Value, uint8_t *p,
105 unsigned PadTo = 0) {
109 uint8_t Byte = Value & 0x7f;
112 if (Value != 0 || Count < PadTo)
113 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
115 } while (Value != 0);
117 // Pad with 0x80 and emit a null byte at the end.
119 for (; Count < PadTo - 1; ++Count)
124 return (unsigned)(p - orig_p);
127 /// Utility function to decode a ULEB128 value.
128 inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
129 const uint8_t *end = nullptr,
130 const char **error = nullptr) {
131 const uint8_t *orig_p = p;
137 if (end && p == end) {
139 *error = "malformed uleb128, extends past end";
141 *n = (unsigned)(p - orig_p);
144 uint64_t Slice = *p & 0x7f;
145 if (Shift >= 64 || Slice << Shift >> Shift != Slice) {
147 *error = "uleb128 too big for uint64";
149 *n = (unsigned)(p - orig_p);
152 Value += uint64_t(*p & 0x7f) << Shift;
154 } while (*p++ >= 128);
156 *n = (unsigned)(p - orig_p);
160 /// Utility function to decode a SLEB128 value.
161 inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
162 const uint8_t *end = nullptr,
163 const char **error = nullptr) {
164 const uint8_t *orig_p = p;
171 if (end && p == end) {
173 *error = "malformed sleb128, extends past end";
175 *n = (unsigned)(p - orig_p);
179 Value |= (uint64_t(Byte & 0x7f) << Shift);
181 } while (Byte >= 128);
182 // Sign extend negative numbers if needed.
183 if (Shift < 64 && (Byte & 0x40))
184 Value |= (-1ULL) << Shift;
186 *n = (unsigned)(p - orig_p);
190 /// Utility function to get the size of the ULEB128-encoded value.
191 extern unsigned getULEB128Size(uint64_t Value);
193 /// Utility function to get the size of the SLEB128-encoded value.
194 extern unsigned getSLEB128Size(int64_t Value);
198 #endif // LLVM_SYSTEM_LEB128_H