blob: 6c185905987ba22f220428d31f11c286e09fbd96 [file] [log] [blame]
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001/*
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 Chartier210531f2018-01-12 10:15:51 -080020#include "base/array_ref.h"
David Sehr67bf42e2018-02-26 16:43:04 -080021#include "base/leb128.h"
Mathieu Chartier2daa1342018-02-20 16:19:28 -080022#include "dex/compact_offset_table.h"
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_instruction.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070024
25namespace art {
26
Mathieu Chartier210531f2018-01-12 10:15:51 -080027// QuickenInfoTable is a table of 16 bit dex indices. There is one slot for every instruction that
28// is possibly dequickenable.
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070029class QuickenInfoTable {
30 public:
Mathieu Chartier210531f2018-01-12 10:15:51 -080031 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 Chartierde4b08f2017-07-10 14:13:41 -070049
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 Chartier210531f2018-01-12 10:15:51 -080067 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 Chartierde4b08f2017-07-10 14:13:41 -070076 private:
Mathieu Chartier210531f2018-01-12 10:15:51 -080077 const uint8_t* data_;
78 const uint32_t num_elements_;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070079
80 DISALLOW_COPY_AND_ASSIGN(QuickenInfoTable);
81};
82
83} // namespace art
84
85#endif // ART_RUNTIME_QUICKEN_INFO_H_