Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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_QUICKEN_INFO_H_ |
| 18 | #define ART_RUNTIME_QUICKEN_INFO_H_ |
| 19 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 20 | #include "base/array_ref.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 21 | #include "base/leb128.h" |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 22 | #include "dex/compact_offset_table.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 23 | #include "dex/dex_instruction.h" |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 27 | // QuickenInfoTable is a table of 16 bit dex indices. There is one slot for every instruction that |
| 28 | // is possibly dequickenable. |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 29 | class QuickenInfoTable { |
| 30 | public: |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 31 | class Builder { |
| 32 | public: |
| 33 | Builder(std::vector<uint8_t>* out_data, size_t num_elements) : out_data_(out_data) { |
| 34 | EncodeUnsignedLeb128(out_data_, num_elements); |
| 35 | } |
| 36 | |
| 37 | void AddIndex(uint16_t index) { |
| 38 | out_data_->push_back(static_cast<uint8_t>(index)); |
| 39 | out_data_->push_back(static_cast<uint8_t>(index >> 8)); |
| 40 | } |
| 41 | |
| 42 | private: |
| 43 | std::vector<uint8_t>* const out_data_; |
| 44 | }; |
| 45 | |
| 46 | explicit QuickenInfoTable(ArrayRef<const uint8_t> data) |
| 47 | : data_(data.data()), |
| 48 | num_elements_(!data.empty() ? DecodeUnsignedLeb128(&data_) : 0u) {} |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 49 | |
| 50 | bool IsNull() const { |
| 51 | return data_ == nullptr; |
| 52 | } |
| 53 | |
| 54 | uint16_t GetData(size_t index) const { |
| 55 | return data_[index * 2] | (static_cast<uint16_t>(data_[index * 2 + 1]) << 8); |
| 56 | } |
| 57 | |
| 58 | // Returns true if the dex instruction has an index in the table. (maybe dequickenable). |
| 59 | static bool NeedsIndexForInstruction(const Instruction* inst) { |
| 60 | return inst->IsQuickened() || inst->Opcode() == Instruction::NOP; |
| 61 | } |
| 62 | |
| 63 | static size_t NumberOfIndices(size_t bytes) { |
| 64 | return bytes / sizeof(uint16_t); |
| 65 | } |
| 66 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 67 | static size_t SizeInBytes(ArrayRef<const uint8_t> data) { |
| 68 | QuickenInfoTable table(data); |
| 69 | return table.data_ + table.NumIndices() * 2 - data.data(); |
| 70 | } |
| 71 | |
| 72 | uint32_t NumIndices() const { |
| 73 | return num_elements_; |
| 74 | } |
| 75 | |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 76 | private: |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 77 | const uint8_t* data_; |
| 78 | const uint32_t num_elements_; |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 79 | |
| 80 | DISALLOW_COPY_AND_ASSIGN(QuickenInfoTable); |
| 81 | }; |
| 82 | |
| 83 | } // namespace art |
| 84 | |
| 85 | #endif // ART_RUNTIME_QUICKEN_INFO_H_ |