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.
23 inline void encodeSLEB128(int64_t Value, raw_ostream &OS,
24 unsigned Padding = 0) {
27 uint8_t Byte = Value & 0x7f;
28 // NOTE: this assumes that this signed shift is an arithmetic right shift.
30 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
31 ((Value == -1) && ((Byte & 0x40) != 0))));
32 if (More || Padding != 0)
33 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
37 // Pad with 0x80 and emit a terminating byte at the end.
39 uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
40 for (; Padding != 1; --Padding)
41 OS << char(PadValue | 0x80);
46 /// Utility function to encode a SLEB128 value to a buffer. Returns
47 /// the length in bytes of the encoded value.
48 inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned Padding = 0) {
52 uint8_t Byte = Value & 0x7f;
53 // NOTE: this assumes that this signed shift is an arithmetic right shift.
55 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
56 ((Value == -1) && ((Byte & 0x40) != 0))));
57 if (More || Padding != 0)
58 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
62 // Pad with 0x80 and emit a terminating byte at the end.
64 uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
65 for (; Padding != 1; --Padding)
66 *p++ = (PadValue | 0x80);
69 return (unsigned)(p - orig_p);
72 /// Utility function to encode a ULEB128 value to an output stream.
73 inline void encodeULEB128(uint64_t Value, raw_ostream &OS,
74 unsigned Padding = 0) {
76 uint8_t Byte = Value & 0x7f;
78 if (Value != 0 || Padding != 0)
79 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
83 // Pad with 0x80 and emit a null byte at the end.
85 for (; Padding != 1; --Padding)
91 /// Utility function to encode a ULEB128 value to a buffer. Returns
92 /// the length in bytes of the encoded value.
93 inline unsigned encodeULEB128(uint64_t Value, uint8_t *p,
94 unsigned Padding = 0) {
97 uint8_t Byte = Value & 0x7f;
99 if (Value != 0 || Padding != 0)
100 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
102 } while (Value != 0);
104 // Pad with 0x80 and emit a null byte at the end.
106 for (; Padding != 1; --Padding)
110 return (unsigned)(p - orig_p);
113 /// Utility function to decode a ULEB128 value.
114 inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
115 const uint8_t *end = nullptr,
116 const char **error = nullptr) {
117 const uint8_t *orig_p = p;
123 if (end && p == end) {
125 *error = "malformed uleb128, extends past end";
127 *n = (unsigned)(p - orig_p);
130 uint64_t Slice = *p & 0x7f;
131 if (Shift >= 64 || Slice << Shift >> Shift != Slice) {
133 *error = "uleb128 too big for uint64";
135 *n = (unsigned)(p - orig_p);
138 Value += uint64_t(*p & 0x7f) << Shift;
140 } while (*p++ >= 128);
142 *n = (unsigned)(p - orig_p);
146 /// Utility function to decode a SLEB128 value.
147 inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
148 const uint8_t *end = nullptr,
149 const char **error = nullptr) {
150 const uint8_t *orig_p = p;
155 if (end && p == end) {
157 *error = "malformed sleb128, extends past end";
159 *n = (unsigned)(p - orig_p);
163 Value |= (int64_t(Byte & 0x7f) << Shift);
165 } while (Byte >= 128);
166 // Sign extend negative numbers.
168 Value |= (-1ULL) << Shift;
170 *n = (unsigned)(p - orig_p);
174 /// Utility function to get the size of the ULEB128-encoded value.
175 extern unsigned getULEB128Size(uint64_t Value);
177 /// Utility function to get the size of the SLEB128-encoded value.
178 extern unsigned getSLEB128Size(int64_t Value);
182 #endif // LLVM_SYSTEM_LEB128_H