Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame^] | 21 | #include "leb128.h" |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | // An encoder with an API similar to vector<uint32_t> where the data is captured in ULEB128 format. |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame^] | 26 | class Leb128EncodingVector { |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 27 | public: |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame^] | 28 | Leb128EncodingVector() { |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame^] | 31 | void Reserve(uint32_t size) { |
| 32 | data_.reserve(size); |
| 33 | } |
| 34 | |
| 35 | void PushBackUnsigned(uint32_t value) { |
| 36 | uint8_t out = value & 0x7f; |
| 37 | value >>= 7; |
| 38 | while (value != 0) { |
| 39 | data_.push_back(out | 0x80); |
| 40 | out = value & 0x7f; |
| 41 | value >>= 7; |
| 42 | } |
| 43 | data_.push_back(out); |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | template<typename It> |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame^] | 47 | void InsertBackUnsigned(It cur, It end) { |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 48 | for (; cur != end; ++cur) { |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame^] | 49 | PushBackUnsigned(*cur); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void PushBackSigned(int32_t value) { |
| 54 | uint32_t extra_bits = static_cast<uint32_t>(value ^ (value >> 31)) >> 6; |
| 55 | uint8_t out = value & 0x7f; |
| 56 | while (extra_bits != 0u) { |
| 57 | data_.push_back(out | 0x80); |
| 58 | value >>= 7; |
| 59 | out = value & 0x7f; |
| 60 | extra_bits >>= 7; |
| 61 | } |
| 62 | data_.push_back(out); |
| 63 | } |
| 64 | |
| 65 | template<typename It> |
| 66 | void InsertBackSigned(It cur, It end) { |
| 67 | for (; cur != end; ++cur) { |
| 68 | PushBackSigned(*cur); |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | const std::vector<uint8_t>& GetData() const { |
| 73 | return data_; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | std::vector<uint8_t> data_; |
| 78 | |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame^] | 79 | DISALLOW_COPY_AND_ASSIGN(Leb128EncodingVector); |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | } // namespace art |
| 83 | |
| 84 | #endif // ART_COMPILER_LEB128_ENCODER_H_ |