LLVM: include/llvm/Support/LEB128.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14#ifndef LLVM_SUPPORT_LEB128_H

15#define LLVM_SUPPORT_LEB128_H

16

19

20namespace llvm {

21

22

23

25 unsigned PadTo = 0) {

26 bool More;

27 unsigned Count = 0;

28 do {

30

32 More = Value != ((Byte & 0x40) ? -1 : 0);

34 if (More || Count < PadTo)

35 Byte |= 0x80;

36 OS << char(Byte);

37 } while (More);

38

39

40 if (Count < PadTo) {

43 OS << char(PadValue | 0x80);

44 OS << char(PadValue);

46 }

48}

49

50

51

54 unsigned Count = 0;

55 bool More;

56 do {

58

60 More = Value != ((Byte & 0x40) ? -1 : 0);

62 if (More || Count < PadTo)

63 Byte |= 0x80;

64 *p++ = Byte;

65 } while (More);

66

67

68 if (Count < PadTo) {

71 *p++ = (PadValue | 0x80);

72 *p++ = PadValue;

73 }

74 return (unsigned)(p - orig_p);

75}

76

77

78

80 unsigned PadTo = 0) {

81 unsigned Count = 0;

82 do {

87 Byte |= 0x80;

88 OS << char(Byte);

89 } while (Value != 0);

90

91

92 if (Count < PadTo) {

94 OS << '\x80';

95 OS << '\x00';

97 }

99}

100

101

102

104 unsigned PadTo = 0) {

106 unsigned Count = 0;

107 do {

112 Byte |= 0x80;

113 *p++ = Byte;

114 } while (Value != 0);

115

116

117 if (Count < PadTo) {

119 *p++ = '\x80';

120 *p++ = '\x00';

121 }

122

123 return (unsigned)(p - orig_p);

124}

125

126

127

128

129

131 const uint8_t *end = nullptr,

132 const char **error = nullptr) {

133 const uint8_t *orig_p = p;

135 unsigned Shift = 0;

136 do {

139 *error = "malformed uleb128, extends past end";

141 break;

142 }

145 ((Shift == 63 && (Slice << Shift >> Shift) != Slice) ||

146 (Shift > 63 && Slice != 0))) {

148 *error = "uleb128 too big for uint64";

150 break;

151 }

152 Value += Slice << Shift;

153 Shift += 7;

154 } while (*p++ >= 128);

155 if (n)

158}

159

160

161

162

163

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;

171 do {

174 *error = "malformed sleb128, extends past end";

175 if (n)

177 return 0;

178 }

179 Byte = *p;

180 uint64_t Slice = Byte & 0x7f;

182 ((Shift == 63 && Slice != 0 && Slice != 0x7f) ||

183 (Shift > 63 && Slice != (Value < 0 ? 0x7f : 0x00)))) {

185 *error = "sleb128 too big for int64";

186 if (n)

188 return 0;

189 }

190 Value |= Slice << Shift;

191 Shift += 7;

192 ++p;

193 } while (Byte >= 128);

194

195 if (Shift < 64 && (Byte & 0x40))

197 if (n)

200}

201

203 const char **error = nullptr) {

204 unsigned n;

206 p += n;

207 return ret;

208}

209

211 const char **error = nullptr) {

212 unsigned n;

214 p += n;

215 return ret;

216}

217

221

222

224 while (*bufLoc & 0x80) {

225 *bufLoc++ = 0x80 | (val & 0x7f);

226 val >>= 7;

227 }

228 *bufLoc = val;

229 return val;

230}

231

233

235 unsigned MaxLEB128SizeBytes = 16>

237 static_assert(sizeof(U) == 1, "Expected buffer of bytes");

238 unsigned LEB128ValueSize;

239 U TmpBuffer[MaxLEB128SizeBytes];

241 LEB128ValueSize =

243 else

244 LEB128ValueSize =

246 Buffer.append(TmpBuffer, TmpBuffer + LEB128ValueSize);

247}

248

249

251

252

254

255}

256

257#endif

#define LLVM_UNLIKELY(EXPR)

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.

This class implements an extremely fast bulk output stream that can only output to a stream.

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

LLVM_ABI unsigned getULEB128Size(uint64_t Value)

Utility function to get the size of the ULEB128-encoded value.

LEB128Sign

Definition LEB128.h:232

@ Signed

Definition LEB128.h:232

@ Unsigned

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.