annotate include/llvm/Support/LEB128.h @ 128:c347d3398279 default tip

fix
author mir3636
date Wed, 06 Dec 2017 14:37:17 +0900
parents 803732b1fca8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- llvm/Support/LEB128.h - [SU]LEB128 utility functions -----*- C++ -*-===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 // This file declares some utility functions for encoding SLEB128 and
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 // ULEB128 values.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 #ifndef LLVM_SUPPORT_LEB128_H
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #define LLVM_SUPPORT_LEB128_H
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 #include "llvm/Support/raw_ostream.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 namespace llvm {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 /// Utility function to encode a SLEB128 value to an output stream.
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
23 inline void encodeSLEB128(int64_t Value, raw_ostream &OS,
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
24 unsigned PadTo = 0) {
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
25 bool More;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
26 unsigned Count = 0;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
27 do {
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
28 uint8_t Byte = Value & 0x7f;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
29 // NOTE: this assumes that this signed shift is an arithmetic right shift.
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
30 Value >>= 7;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
31 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
32 ((Value == -1) && ((Byte & 0x40) != 0))));
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
33 Count++;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
34 if (More || Count < PadTo)
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
35 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
36 OS << char(Byte);
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
37 } while (More);
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
38
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
39 // Pad with 0x80 and emit a terminating byte at the end.
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
40 if (Count < PadTo) {
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
41 uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
42 for (; Count < PadTo - 1; ++Count)
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
43 OS << char(PadValue | 0x80);
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
44 OS << char(PadValue);
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
45 }
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
46 }
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
47
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
48 /// Utility function to encode a SLEB128 value to a buffer. Returns
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
49 /// the length in bytes of the encoded value.
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
50 inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned PadTo = 0) {
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
51 uint8_t *orig_p = p;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
52 unsigned Count = 0;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 bool More;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 do {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 uint8_t Byte = Value & 0x7f;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 // NOTE: this assumes that this signed shift is an arithmetic right shift.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 Value >>= 7;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 ((Value == -1) && ((Byte & 0x40) != 0))));
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
60 Count++;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
61 if (More || Count < PadTo)
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
63 *p++ = Byte;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 } while (More);
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
65
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
66 // Pad with 0x80 and emit a terminating byte at the end.
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
67 if (Count < PadTo) {
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
68 uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
69 for (; Count < PadTo - 1; ++Count)
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
70 *p++ = (PadValue | 0x80);
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
71 *p++ = PadValue;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
72 }
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
73 return (unsigned)(p - orig_p);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 /// Utility function to encode a ULEB128 value to an output stream.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 inline void encodeULEB128(uint64_t Value, raw_ostream &OS,
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
78 unsigned PadTo = 0) {
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
79 unsigned Count = 0;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 do {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 uint8_t Byte = Value & 0x7f;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 Value >>= 7;
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
83 Count++;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
84 if (Value != 0 || Count < PadTo)
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 OS << char(Byte);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 } while (Value != 0);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 // Pad with 0x80 and emit a null byte at the end.
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
90 if (Count < PadTo) {
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
91 for (; Count < PadTo - 1; ++Count)
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 OS << '\x80';
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 OS << '\x00';
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
94 Count++;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 /// Utility function to encode a ULEB128 value to a buffer. Returns
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 /// the length in bytes of the encoded value.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 inline unsigned encodeULEB128(uint64_t Value, uint8_t *p,
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
101 unsigned PadTo = 0) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 uint8_t *orig_p = p;
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
103 unsigned Count = 0;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 do {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 uint8_t Byte = Value & 0x7f;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 Value >>= 7;
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
107 Count++;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
108 if (Value != 0 || Count < PadTo)
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 *p++ = Byte;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 } while (Value != 0);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 // Pad with 0x80 and emit a null byte at the end.
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
114 if (Count < PadTo) {
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
115 for (; Count < PadTo - 1; ++Count)
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 *p++ = '\x80';
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 *p++ = '\x00';
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 }
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
119
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 return (unsigned)(p - orig_p);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 /// Utility function to decode a ULEB128 value.
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
124 inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
125 const uint8_t *end = nullptr,
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
126 const char **error = nullptr) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 const uint8_t *orig_p = p;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 uint64_t Value = 0;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 unsigned Shift = 0;
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
130 if (error)
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
131 *error = nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 do {
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
133 if (end && p == end) {
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
134 if (error)
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
135 *error = "malformed uleb128, extends past end";
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
136 if (n)
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
137 *n = (unsigned)(p - orig_p);
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
138 return 0;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
139 }
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
140 uint64_t Slice = *p & 0x7f;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
141 if (Shift >= 64 || Slice << Shift >> Shift != Slice) {
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
142 if (error)
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
143 *error = "uleb128 too big for uint64";
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
144 if (n)
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
145 *n = (unsigned)(p - orig_p);
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
146 return 0;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
147 }
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
148 Value += uint64_t(*p & 0x7f) << Shift;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 Shift += 7;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 } while (*p++ >= 128);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 if (n)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 *n = (unsigned)(p - orig_p);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 return Value;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
156 /// Utility function to decode a SLEB128 value.
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
157 inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
158 const uint8_t *end = nullptr,
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
159 const char **error = nullptr) {
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
160 const uint8_t *orig_p = p;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
161 int64_t Value = 0;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
162 unsigned Shift = 0;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
163 uint8_t Byte;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
164 do {
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
165 if (end && p == end) {
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
166 if (error)
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
167 *error = "malformed sleb128, extends past end";
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
168 if (n)
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
169 *n = (unsigned)(p - orig_p);
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
170 return 0;
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
171 }
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
172 Byte = *p++;
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
173 Value |= (int64_t(Byte & 0x7f) << Shift);
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
174 Shift += 7;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
175 } while (Byte >= 128);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
176 // Sign extend negative numbers.
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
177 if (Byte & 0x40)
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
178 Value |= (-1ULL) << Shift;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
179 if (n)
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
180 *n = (unsigned)(p - orig_p);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
181 return Value;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
182 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
183
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
184 /// Utility function to get the size of the ULEB128-encoded value.
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
185 extern unsigned getULEB128Size(uint64_t Value);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
186
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
187 /// Utility function to get the size of the SLEB128-encoded value.
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
188 extern unsigned getSLEB128Size(int64_t Value);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
189
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
190 } // namespace llvm
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191
121
803732b1fca8 LLVM 5.0
kono
parents: 83
diff changeset
192 #endif // LLVM_SYSTEM_LEB128_H