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 | |
Vladimir Marko | 06606b9 | 2013-12-02 15:31:08 +0000 | [diff] [blame] | 25 | static 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 | |
| 37 | static 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 Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 50 | // 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] | 51 | class Leb128EncodingVector { |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 52 | public: |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 53 | Leb128EncodingVector() { |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 56 | 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 Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | template<typename It> |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 72 | void InsertBackUnsigned(It cur, It end) { |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 73 | for (; cur != end; ++cur) { |
Vladimir Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 74 | 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 Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 94 | } |
| 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 Marko | 1e6cb63 | 2013-11-28 16:27:29 +0000 | [diff] [blame] | 104 | DISALLOW_COPY_AND_ASSIGN(Leb128EncodingVector); |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace art |
| 108 | |
| 109 | #endif // ART_COMPILER_LEB128_ENCODER_H_ |