blob: fe38c2f5cdbfb29c1e4c0d5a00fe83f765358cca [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
25// 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 +000026class Leb128EncodingVector {
Ian Rogers96faf5b2013-08-09 22:05:32 -070027 public:
Vladimir Marko1e6cb632013-11-28 16:27:29 +000028 Leb128EncodingVector() {
Ian Rogers96faf5b2013-08-09 22:05:32 -070029 }
30
Vladimir Marko1e6cb632013-11-28 16:27:29 +000031 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 Rogers96faf5b2013-08-09 22:05:32 -070044 }
45
46 template<typename It>
Vladimir Marko1e6cb632013-11-28 16:27:29 +000047 void InsertBackUnsigned(It cur, It end) {
Ian Rogers96faf5b2013-08-09 22:05:32 -070048 for (; cur != end; ++cur) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +000049 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 Rogers96faf5b2013-08-09 22:05:32 -070069 }
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 Marko1e6cb632013-11-28 16:27:29 +000079 DISALLOW_COPY_AND_ASSIGN(Leb128EncodingVector);
Ian Rogers96faf5b2013-08-09 22:05:32 -070080};
81
82} // namespace art
83
84#endif // ART_COMPILER_LEB128_ENCODER_H_