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 20 #include "base/array_ref.h" 21 #include "base/leb128.h" 22 #include "dex/compact_offset_table.h" 23 #include "dex/dex_instruction.h" 24 25 namespace art { 26 27 // QuickenInfoTable is a table of 16 bit dex indices. There is one slot for every instruction that 28 // is possibly dequickenable. 29 class QuickenInfoTable { 30 public: 31 class Builder { 32 public: Builder(std::vector<uint8_t> * out_data,size_t num_elements)33 Builder(std::vector<uint8_t>* out_data, size_t num_elements) : out_data_(out_data) { 34 EncodeUnsignedLeb128(out_data_, num_elements); 35 } 36 AddIndex(uint16_t index)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 QuickenInfoTable(ArrayRef<const uint8_t> data)46 explicit QuickenInfoTable(ArrayRef<const uint8_t> data) 47 : data_(data.data()), 48 num_elements_(!data.empty() ? DecodeUnsignedLeb128(&data_) : 0u) {} 49 IsNull()50 bool IsNull() const { 51 return data_ == nullptr; 52 } 53 GetData(size_t index)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). NeedsIndexForInstruction(const Instruction * inst)59 static bool NeedsIndexForInstruction(const Instruction* inst) { 60 return inst->IsQuickened() || inst->Opcode() == Instruction::NOP; 61 } 62 NumberOfIndices(size_t bytes)63 static size_t NumberOfIndices(size_t bytes) { 64 return bytes / sizeof(uint16_t); 65 } 66 SizeInBytes(ArrayRef<const uint8_t> data)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 NumIndices()72 uint32_t NumIndices() const { 73 return num_elements_; 74 } 75 76 private: 77 const uint8_t* data_; 78 const uint32_t num_elements_; 79 80 DISALLOW_COPY_AND_ASSIGN(QuickenInfoTable); 81 }; 82 83 } // namespace art 84 85 #endif // ART_RUNTIME_QUICKEN_INFO_H_ 86