1 //===- llvm/Support/LEB128.h - [SU]LEB128 utility functions -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares some utility functions for encoding SLEB128 and
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_LEB128_H
16 #define LLVM_SUPPORT_LEB128_H
18 #include "llvm/Support/raw_ostream.h"
22 /// Utility function to encode a SLEB128 value to an output stream. Returns
23 /// the length in bytes of the encoded value.
24 inline unsigned encodeSLEB128(int64_t Value, raw_ostream &OS,
29 uint8_t Byte = Value & 0x7f;
30 // NOTE: this assumes that this signed shift is an arithmetic right shift.
32 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
33 ((Value == -1) && ((Byte & 0x40) != 0))));
35 if (More || Count < PadTo)
36 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
40 // Pad with 0x80 and emit a terminating byte at the end.
42 uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
43 for (; Count < PadTo - 1; ++Count)
44 OS << char(PadValue | 0x80);
51 /// Utility function to encode a SLEB128 value to a buffer. Returns
52 /// the length in bytes of the encoded value.
53 inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned PadTo = 0) {
58 uint8_t Byte = Value & 0x7f;
59 // NOTE: this assumes that this signed shift is an arithmetic right shift.
61 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
62 ((Value == -1) && ((Byte & 0x40) != 0))));
64 if (More || Count < PadTo)
65 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
69 // Pad with 0x80 and emit a terminating byte at the end.
71 uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
72 for (; Count < PadTo - 1; ++Count)
73 *p++ = (PadValue | 0x80);
76 return (unsigned)(p - orig_p);
79 /// Utility function to encode a ULEB128 value to an output stream. Returns
80 /// the length in bytes of the encoded value.
81 inline unsigned encodeULEB128(uint64_t Value, raw_ostream &OS,
85 uint8_t Byte = Value & 0x7f;
88 if (Value != 0 || Count < PadTo)
89 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
93 // Pad with 0x80 and emit a null byte at the end.
95 for (; Count < PadTo - 1; ++Count)
103 /// Utility function to encode a ULEB128 value to a buffer. Returns
104 /// the length in bytes of the encoded value.
105 inline unsigned encodeULEB128(uint64_t Value, uint8_t *p,
106 unsigned PadTo = 0) {
110 uint8_t Byte = Value & 0x7f;
113 if (Value != 0 || Count < PadTo)
114 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
116 } while (Value != 0);
118 // Pad with 0x80 and emit a null byte at the end.
120 for (; Count < PadTo - 1; ++Count)
125 return (unsigned)(p - orig_p);
128 /// Utility function to decode a ULEB128 value.
129 inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
130 const uint8_t *end = nullptr,
131 const char **error = nullptr) {
132 const uint8_t *orig_p = p;
138 if (end && p == end) {
140 *error = "malformed uleb128, extends past end";
142 *n = (unsigned)(p - orig_p);
145 uint64_t Slice = *p & 0x7f;
146 if (Shift >= 64 || Slice << Shift >> Shift != Slice) {
148 *error = "uleb128 too big for uint64";
150 *n = (unsigned)(p - orig_p);
153 Value += uint64_t(*p & 0x7f) << Shift;
155 } while (*p++ >= 128);
157 *n = (unsigned)(p - orig_p);
161 /// Utility function to decode a SLEB128 value.
162 inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
163 const uint8_t *end = nullptr,
164 const char **error = nullptr) {
165 const uint8_t *orig_p = p;
170 if (end && p == end) {
172 *error = "malformed sleb128, extends past end";
174 *n = (unsigned)(p - orig_p);
178 Value |= (int64_t(Byte & 0x7f) << Shift);
180 } while (Byte >= 128);
181 // Sign extend negative numbers.
183 Value |= (-1ULL) << Shift;
185 *n = (unsigned)(p - orig_p);
189 /// Utility function to get the size of the ULEB128-encoded value.
190 extern unsigned getULEB128Size(uint64_t Value);
192 /// Utility function to get the size of the SLEB128-encoded value.
193 extern unsigned getSLEB128Size(int64_t Value);
197 #endif // LLVM_SYSTEM_LEB128_H