blob: 67666831f0b0e19ecf5a200228a39e5178e1ea07 [file] [log] [blame]
Ian Rogers96faf5b2013-08-09 22:05:32 -07001/*
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 */
16
17#ifndef ART_COMPILER_LEB128_ENCODER_H_
18#define ART_COMPILER_LEB128_ENCODER_H_
19
20#include "base/macros.h"
Vladimir Marko1e6cb632013-11-28 16:27:29 +000021#include "leb128.h"
Ian Rogers96faf5b2013-08-09 22:05:32 -070022
23namespace art {
24
Vladimir Marko06606b92013-12-02 15:31:08 +000025static inline uint8_t* EncodeUnsignedLeb128(uint8_t* dest, uint32_t value) {
26 uint8_t out = value & 0x7f;
27 value >>= 7;
28 while (value != 0) {
29 *dest++ = out | 0x80;
30 out = value & 0x7f;
31 value >>= 7;
32 }
33 *dest++ = out;
34 return dest;
35}
36
37static inline uint8_t* EncodeSignedLeb128(uint8_t* dest, int32_t value) {
38 uint32_t extra_bits = static_cast<uint32_t>(value ^ (value >> 31)) >> 6;
39 uint8_t out = value & 0x7f;
40 while (extra_bits != 0u) {
41 *dest++ = out | 0x80;
42 value >>= 7;
43 out = value & 0x7f;
44 extra_bits >>= 7;
45 }
46 *dest++ = out;
47 return dest;
48}
49
Ian Rogers96faf5b2013-08-09 22:05:32 -070050// An encoder with an API similar to vector<uint32_t> where the data is captured in ULEB128 format.
Vladimir Marko1e6cb632013-11-28 16:27:29 +000051class Leb128EncodingVector {
Ian Rogers96faf5b2013-08-09 22:05:32 -070052 public:
Vladimir Marko1e6cb632013-11-28 16:27:29 +000053 Leb128EncodingVector() {
Ian Rogers96faf5b2013-08-09 22:05:32 -070054 }
55
Vladimir Marko1e6cb632013-11-28 16:27:29 +000056 void Reserve(uint32_t size) {
57 data_.reserve(size);
58 }
59
60 void PushBackUnsigned(uint32_t value) {
61 uint8_t out = value & 0x7f;
62 value >>= 7;
63 while (value != 0) {
64 data_.push_back(out | 0x80);
65 out = value & 0x7f;
66 value >>= 7;
67 }
68 data_.push_back(out);
Ian Rogers96faf5b2013-08-09 22:05:32 -070069 }
70
71 template<typename It>
Vladimir Marko1e6cb632013-11-28 16:27:29 +000072 void InsertBackUnsigned(It cur, It end) {
Ian Rogers96faf5b2013-08-09 22:05:32 -070073 for (; cur != end; ++cur) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +000074 PushBackUnsigned(*cur);
75 }
76 }
77
78 void PushBackSigned(int32_t value) {
79 uint32_t extra_bits = static_cast<uint32_t>(value ^ (value >> 31)) >> 6;
80 uint8_t out = value & 0x7f;
81 while (extra_bits != 0u) {
82 data_.push_back(out | 0x80);
83 value >>= 7;
84 out = value & 0x7f;
85 extra_bits >>= 7;
86 }
87 data_.push_back(out);
88 }
89
90 template<typename It>
91 void InsertBackSigned(It cur, It end) {
92 for (; cur != end; ++cur) {
93 PushBackSigned(*cur);
Ian Rogers96faf5b2013-08-09 22:05:32 -070094 }
95 }
96
97 const std::vector<uint8_t>& GetData() const {
98 return data_;
99 }
100
101 private:
102 std::vector<uint8_t> data_;
103
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000104 DISALLOW_COPY_AND_ASSIGN(Leb128EncodingVector);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700105};
106
107} // namespace art
108
109#endif // ART_COMPILER_LEB128_ENCODER_H_