blob: baf9da28f04c85b784823b278fc4f2a8910edfdb [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_LEB128_H_
18#define ART_RUNTIME_LEB128_H_
Carl Shapiro1fb86202011-06-27 17:43:13 -070019
Vladimir Marko80afd022015-05-19 18:08:00 +010020#include <vector>
21
22#include "base/bit_utils.h"
23#include "base/logging.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024#include "globals.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070025
26namespace art {
27
28// Reads an unsigned LEB128 value, updating the given pointer to point
29// just past the end of the read value. This function tolerates
30// non-zero high-order bits in the fifth encoded byte.
Ian Rogers96faf5b2013-08-09 22:05:32 -070031static inline uint32_t DecodeUnsignedLeb128(const uint8_t** data) {
32 const uint8_t* ptr = *data;
Carl Shapiro1fb86202011-06-27 17:43:13 -070033 int result = *(ptr++);
Ian Rogers1ff3c982014-08-12 02:30:58 -070034 if (UNLIKELY(result > 0x7f)) {
Carl Shapiro1fb86202011-06-27 17:43:13 -070035 int cur = *(ptr++);
36 result = (result & 0x7f) | ((cur & 0x7f) << 7);
37 if (cur > 0x7f) {
38 cur = *(ptr++);
39 result |= (cur & 0x7f) << 14;
40 if (cur > 0x7f) {
41 cur = *(ptr++);
42 result |= (cur & 0x7f) << 21;
43 if (cur > 0x7f) {
44 // Note: We don't check to see if cur is out of range here,
45 // meaning we tolerate garbage in the four high-order bits.
46 cur = *(ptr++);
47 result |= cur << 28;
48 }
49 }
50 }
51 }
52 *data = ptr;
buzbeecbd6d442012-11-17 14:11:25 -080053 return static_cast<uint32_t>(result);
Carl Shapiro1fb86202011-06-27 17:43:13 -070054}
55
Shih-wei Liao195487c2011-08-20 13:29:04 -070056// Reads an unsigned LEB128 + 1 value. updating the given pointer to point
57// just past the end of the read value. This function tolerates
58// non-zero high-order bits in the fifth encoded byte.
59// It is possible for this function to return -1.
Ian Rogers96faf5b2013-08-09 22:05:32 -070060static inline int32_t DecodeUnsignedLeb128P1(const uint8_t** data) {
Shih-wei Liao195487c2011-08-20 13:29:04 -070061 return DecodeUnsignedLeb128(data) - 1;
62}
63
Carl Shapiro1fb86202011-06-27 17:43:13 -070064// Reads a signed LEB128 value, updating the given pointer to point
65// just past the end of the read value. This function tolerates
66// non-zero high-order bits in the fifth encoded byte.
Ian Rogers96faf5b2013-08-09 22:05:32 -070067static inline int32_t DecodeSignedLeb128(const uint8_t** data) {
68 const uint8_t* ptr = *data;
Carl Shapiro1fb86202011-06-27 17:43:13 -070069 int32_t result = *(ptr++);
70 if (result <= 0x7f) {
71 result = (result << 25) >> 25;
72 } else {
73 int cur = *(ptr++);
74 result = (result & 0x7f) | ((cur & 0x7f) << 7);
75 if (cur <= 0x7f) {
76 result = (result << 18) >> 18;
77 } else {
78 cur = *(ptr++);
79 result |= (cur & 0x7f) << 14;
80 if (cur <= 0x7f) {
81 result = (result << 11) >> 11;
82 } else {
83 cur = *(ptr++);
84 result |= (cur & 0x7f) << 21;
85 if (cur <= 0x7f) {
86 result = (result << 4) >> 4;
87 } else {
88 // Note: We don't check to see if cur is out of range here,
89 // meaning we tolerate garbage in the four high-order bits.
90 cur = *(ptr++);
91 result |= cur << 28;
92 }
93 }
94 }
95 }
96 *data = ptr;
97 return result;
98}
99
jeffhaod1f0fde2011-09-08 17:25:33 -0700100// Returns the number of bytes needed to encode the value in unsigned LEB128.
101static inline uint32_t UnsignedLeb128Size(uint32_t data) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000102 // bits_to_encode = (data != 0) ? 32 - CLZ(x) : 1 // 32 - CLZ(data | 1)
103 // bytes = ceil(bits_to_encode / 7.0); // (6 + bits_to_encode) / 7
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000104 uint32_t x = 6 + 32 - CLZ(data | 1U);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000105 // Division by 7 is done by (x * 37) >> 8 where 37 = ceil(256 / 7).
106 // This works for 0 <= x < 256 / (7 * 37 - 256), i.e. 0 <= x <= 85.
107 return (x * 37) >> 8;
108}
109
110// Returns the number of bytes needed to encode the value in unsigned LEB128.
111static inline uint32_t SignedLeb128Size(int32_t data) {
112 // Like UnsignedLeb128Size(), but we need one bit beyond the highest bit that differs from sign.
113 data = data ^ (data >> 31);
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000114 uint32_t x = 1 /* we need to encode the sign bit */ + 6 + 32 - CLZ(data | 1U);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000115 return (x * 37) >> 8;
jeffhaod1f0fde2011-09-08 17:25:33 -0700116}
117
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800118static inline uint8_t* EncodeUnsignedLeb128(uint8_t* dest, uint32_t value) {
119 uint8_t out = value & 0x7f;
120 value >>= 7;
121 while (value != 0) {
122 *dest++ = out | 0x80;
123 out = value & 0x7f;
124 value >>= 7;
125 }
126 *dest++ = out;
127 return dest;
128}
129
David Srbecky15c19752015-03-31 14:53:55 +0000130template<typename Allocator>
131static inline void EncodeUnsignedLeb128(std::vector<uint8_t, Allocator>* dest, uint32_t value) {
132 uint8_t out = value & 0x7f;
133 value >>= 7;
134 while (value != 0) {
135 dest->push_back(out | 0x80);
136 out = value & 0x7f;
137 value >>= 7;
138 }
139 dest->push_back(out);
140}
141
David Srbeckyb5362472015-04-08 19:37:39 +0100142// Overwrite encoded Leb128 with a new value. The new value must be less than
143// or equal to the old value to ensure that it fits the allocated space.
144static inline void UpdateUnsignedLeb128(uint8_t* dest, uint32_t value) {
145 const uint8_t* old_end = dest;
146 uint32_t old_value = DecodeUnsignedLeb128(&old_end);
147 DCHECK_LE(value, old_value);
148 for (uint8_t* end = EncodeUnsignedLeb128(dest, value); end < old_end; end++) {
149 // Use longer encoding than necessary to fill the allocated space.
150 end[-1] |= 0x80;
151 end[0] = 0;
152 }
153}
154
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800155static inline uint8_t* EncodeSignedLeb128(uint8_t* dest, int32_t value) {
156 uint32_t extra_bits = static_cast<uint32_t>(value ^ (value >> 31)) >> 6;
157 uint8_t out = value & 0x7f;
158 while (extra_bits != 0u) {
159 *dest++ = out | 0x80;
160 value >>= 7;
161 out = value & 0x7f;
162 extra_bits >>= 7;
163 }
164 *dest++ = out;
165 return dest;
166}
167
David Srbecky15c19752015-03-31 14:53:55 +0000168template<typename Allocator>
169static inline void EncodeSignedLeb128(std::vector<uint8_t, Allocator>* dest, int32_t value) {
170 uint32_t extra_bits = static_cast<uint32_t>(value ^ (value >> 31)) >> 6;
171 uint8_t out = value & 0x7f;
172 while (extra_bits != 0u) {
173 dest->push_back(out | 0x80);
174 value >>= 7;
175 out = value & 0x7f;
176 extra_bits >>= 7;
177 }
178 dest->push_back(out);
179}
180
Vladimir Markof9f64412015-09-02 14:05:49 +0100181// An encoder that pushes int32_t/uint32_t data onto the given std::vector.
182template <typename Allocator = std::allocator<uint8_t>>
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700183class Leb128Encoder {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800184 public:
Vladimir Markof9f64412015-09-02 14:05:49 +0100185 explicit Leb128Encoder(std::vector<uint8_t, Allocator>* data) : data_(data) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700186 DCHECK(data != nullptr);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800187 }
188
189 void Reserve(uint32_t size) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700190 data_->reserve(size);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800191 }
192
193 void PushBackUnsigned(uint32_t value) {
David Srbecky15c19752015-03-31 14:53:55 +0000194 EncodeUnsignedLeb128(data_, value);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800195 }
196
197 template<typename It>
198 void InsertBackUnsigned(It cur, It end) {
199 for (; cur != end; ++cur) {
200 PushBackUnsigned(*cur);
201 }
202 }
203
204 void PushBackSigned(int32_t value) {
David Srbecky15c19752015-03-31 14:53:55 +0000205 EncodeSignedLeb128(data_, value);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800206 }
207
208 template<typename It>
209 void InsertBackSigned(It cur, It end) {
210 for (; cur != end; ++cur) {
211 PushBackSigned(*cur);
212 }
213 }
214
Vladimir Markof9f64412015-09-02 14:05:49 +0100215 const std::vector<uint8_t, Allocator>& GetData() const {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700216 return *data_;
217 }
218
219 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +0100220 std::vector<uint8_t, Allocator>* const data_;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700221
222 private:
223 DISALLOW_COPY_AND_ASSIGN(Leb128Encoder);
224};
225
226// An encoder with an API similar to vector<uint32_t> where the data is captured in ULEB128 format.
Vladimir Markof9f64412015-09-02 14:05:49 +0100227template <typename Allocator = std::allocator<uint8_t>>
228class Leb128EncodingVector FINAL : private std::vector<uint8_t, Allocator>,
229 public Leb128Encoder<Allocator> {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700230 public:
Vladimir Markof9f64412015-09-02 14:05:49 +0100231 Leb128EncodingVector() : Leb128Encoder<Allocator>(this) { }
232
233 explicit Leb128EncodingVector(const Allocator& alloc)
234 : std::vector<uint8_t, Allocator>(alloc),
235 Leb128Encoder<Allocator>(this) { }
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800236
237 private:
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800238 DISALLOW_COPY_AND_ASSIGN(Leb128EncodingVector);
239};
240
Carl Shapiro1fb86202011-06-27 17:43:13 -0700241} // namespace art
242
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700243#endif // ART_RUNTIME_LEB128_H_