blob: 9955f30d401e0258504d2313b381015a3eb971a5 [file] [log] [blame]
Ian Rogers96faf5b2013-08-09 22:05:32 -07001/*
2 * Copyright (C) 2013 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_RUNTIME_MAPPING_TABLE_H_
18#define ART_RUNTIME_MAPPING_TABLE_H_
19
20#include "base/logging.h"
21#include "leb128.h"
22
23namespace art {
24
25// A utility for processing the raw uleb128 encoded mapping table created by the quick compiler.
26class MappingTable {
27 public:
28 explicit MappingTable(const uint8_t* encoded_map) : encoded_table_(encoded_map) {
29 }
30
31 uint32_t TotalSize() const PURE {
32 const uint8_t* table = encoded_table_;
33 if (table == NULL) {
34 return 0;
35 } else {
36 return DecodeUnsignedLeb128(&table);
37 }
38 }
39
40 uint32_t DexToPcSize() const PURE {
41 const uint8_t* table = encoded_table_;
42 if (table == NULL) {
43 return 0;
44 } else {
45 uint32_t total_size = DecodeUnsignedLeb128(&table);
46 uint32_t pc_to_dex_size = DecodeUnsignedLeb128(&table);
47 return total_size - pc_to_dex_size;
48 }
49 }
50
51 const uint8_t* FirstDexToPcPtr() const {
52 const uint8_t* table = encoded_table_;
53 if (table != NULL) {
54 DecodeUnsignedLeb128(&table); // Total_size, unused.
55 uint32_t pc_to_dex_size = DecodeUnsignedLeb128(&table);
56 for (uint32_t i = 0; i < pc_to_dex_size; ++i) {
57 DecodeUnsignedLeb128(&table); // Move ptr past native PC.
58 DecodeUnsignedLeb128(&table); // Move ptr past dex PC.
59 }
60 }
61 return table;
62 }
63
64 class DexToPcIterator {
65 public:
66 DexToPcIterator(const MappingTable* table, uint32_t element) :
67 table_(table), element_(element), end_(table_->DexToPcSize()), encoded_table_ptr_(NULL),
68 native_pc_offset_(0), dex_pc_(0) {
69 if (element == 0) {
70 encoded_table_ptr_ = table_->FirstDexToPcPtr();
Ian Rogers20ab6c82013-11-13 23:31:08 -080071 if (end_ > 0) {
72 native_pc_offset_ = DecodeUnsignedLeb128(&encoded_table_ptr_);
73 dex_pc_ = DecodeUnsignedLeb128(&encoded_table_ptr_);
74 }
Ian Rogers96faf5b2013-08-09 22:05:32 -070075 } else {
76 DCHECK_EQ(table_->DexToPcSize(), element);
77 }
78 }
79 uint32_t NativePcOffset() const {
80 return native_pc_offset_;
81 }
82 uint32_t DexPc() const {
83 return dex_pc_;
84 }
85 void operator++() {
86 ++element_;
87 if (element_ != end_) { // Avoid reading beyond the end of the table.
88 native_pc_offset_ = DecodeUnsignedLeb128(&encoded_table_ptr_);
89 dex_pc_ = DecodeUnsignedLeb128(&encoded_table_ptr_);
90 }
91 }
92 bool operator==(const DexToPcIterator& rhs) const {
93 CHECK(table_ == rhs.table_);
94 return element_ == rhs.element_;
95 }
96 bool operator!=(const DexToPcIterator& rhs) const {
97 CHECK(table_ == rhs.table_);
98 return element_ != rhs.element_;
99 }
100
101 private:
102 const MappingTable* const table_; // The original table.
103 uint32_t element_; // A value in the range 0 to end_.
104 const uint32_t end_; // Equal to table_->DexToPcSize().
105 const uint8_t* encoded_table_ptr_; // Either NULL or points to encoded data after this entry.
106 uint32_t native_pc_offset_; // The current value of native pc offset.
107 uint32_t dex_pc_; // The current value of dex pc.
108 };
109
110 DexToPcIterator DexToPcBegin() const {
111 return DexToPcIterator(this, 0);
112 }
113
114 DexToPcIterator DexToPcEnd() const {
115 uint32_t size = DexToPcSize();
116 return DexToPcIterator(this, size);
117 }
118
119 uint32_t PcToDexSize() const PURE {
120 const uint8_t* table = encoded_table_;
121 if (table == NULL) {
122 return 0;
123 } else {
124 DecodeUnsignedLeb128(&table); // Total_size, unused.
125 uint32_t pc_to_dex_size = DecodeUnsignedLeb128(&table);
126 return pc_to_dex_size;
127 }
128 }
129
130 const uint8_t* FirstPcToDexPtr() const {
131 const uint8_t* table = encoded_table_;
132 if (table != NULL) {
133 DecodeUnsignedLeb128(&table); // Total_size, unused.
134 DecodeUnsignedLeb128(&table); // PC to Dex size, unused.
135 }
136 return table;
137 }
138
139 class PcToDexIterator {
140 public:
141 PcToDexIterator(const MappingTable* table, uint32_t element) :
142 table_(table), element_(element), end_(table_->PcToDexSize()), encoded_table_ptr_(NULL),
143 native_pc_offset_(0), dex_pc_(0) {
144 if (element == 0) {
145 encoded_table_ptr_ = table_->FirstPcToDexPtr();
Ian Rogers20ab6c82013-11-13 23:31:08 -0800146 if (end_ > 0) {
147 native_pc_offset_ = DecodeUnsignedLeb128(&encoded_table_ptr_);
148 dex_pc_ = DecodeUnsignedLeb128(&encoded_table_ptr_);
149 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700150 } else {
151 DCHECK_EQ(table_->PcToDexSize(), element);
152 }
153 }
154 uint32_t NativePcOffset() const {
155 return native_pc_offset_;
156 }
157 uint32_t DexPc() const {
158 return dex_pc_;
159 }
160 void operator++() {
161 ++element_;
162 if (element_ != end_) { // Avoid reading beyond the end of the table.
163 native_pc_offset_ = DecodeUnsignedLeb128(&encoded_table_ptr_);
164 dex_pc_ = DecodeUnsignedLeb128(&encoded_table_ptr_);
165 }
166 }
167 bool operator==(const PcToDexIterator& rhs) const {
168 CHECK(table_ == rhs.table_);
169 return element_ == rhs.element_;
170 }
171 bool operator!=(const PcToDexIterator& rhs) const {
172 CHECK(table_ == rhs.table_);
173 return element_ != rhs.element_;
174 }
175
176 private:
177 const MappingTable* const table_; // The original table.
178 uint32_t element_; // A value in the range 0 to PcToDexSize.
179 const uint32_t end_; // Equal to table_->PcToDexSize().
180 const uint8_t* encoded_table_ptr_; // Either NULL or points to encoded data after this entry.
181 uint32_t native_pc_offset_; // The current value of native pc offset.
182 uint32_t dex_pc_; // The current value of dex pc.
183 };
184
185 PcToDexIterator PcToDexBegin() const {
186 return PcToDexIterator(this, 0);
187 }
188
189 PcToDexIterator PcToDexEnd() const {
190 uint32_t size = PcToDexSize();
191 return PcToDexIterator(this, size);
192 }
193
194 private:
195 const uint8_t* const encoded_table_;
196};
197
198} // namespace art
199
200#endif // ART_RUNTIME_MAPPING_TABLE_H_