blob: 9bc14329ffe16af2541142aeac08543513ac6e7e [file] [log] [blame]
Vladimir Markof3c52b42017-11-17 17:32:12 +00001/*
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_DEX2OAT_LINKER_INDEX_BSS_MAPPING_ENCODER_H_
18#define ART_DEX2OAT_LINKER_INDEX_BSS_MAPPING_ENCODER_H_
19
20#include "base/bit_utils.h"
21#include "base/bit_vector-inl.h"
22#include "base/logging.h"
23#include "index_bss_mapping.h"
24
25namespace art {
26namespace linker {
27
28// Helper class for encoding compressed IndexBssMapping.
29class IndexBssMappingEncoder {
30 public:
31 IndexBssMappingEncoder(size_t number_of_indexes, size_t slot_size)
32 : index_bits_(IndexBssMappingEntry::IndexBits(number_of_indexes)),
33 slot_size_(slot_size) {
34 entry_.index_and_mask = static_cast<uint32_t>(-1);
35 entry_.bss_offset = static_cast<uint32_t>(-1);
36 DCHECK_NE(number_of_indexes, 0u);
37 }
38
39 // Try to merge the next index -> bss_offset mapping into the current entry.
40 // Return true on success, false on failure.
41 bool TryMerge(uint32_t index, uint32_t bss_offset) {
42 DCHECK_LE(MinimumBitsToStore(index), index_bits_);
43 DCHECK_NE(index, entry_.GetIndex(index_bits_));
44 if (entry_.bss_offset + slot_size_ != bss_offset) {
45 return false;
46 }
47 uint32_t diff = index - entry_.GetIndex(index_bits_);
48 if (diff > 32u - index_bits_) {
49 return false;
50 }
51 uint32_t mask = entry_.GetMask(index_bits_);
52 if ((mask & ~(static_cast<uint32_t>(-1) << diff)) != 0u) {
53 return false;
54 }
55 // Insert the bit indicating the index we've just overwritten
56 // and shift bits indicating indexes before that.
57 mask = ((mask << index_bits_) >> diff) | (static_cast<uint32_t>(1u) << (32 - diff));
58 entry_.index_and_mask = mask | index;
59 entry_.bss_offset = bss_offset;
60 return true;
61 }
62
63 void Reset(uint32_t method_index, uint32_t bss_offset) {
64 DCHECK_LE(MinimumBitsToStore(method_index), index_bits_);
65 entry_.index_and_mask = method_index; // Mask bits set to 0.
66 entry_.bss_offset = bss_offset;
67 }
68
69 IndexBssMappingEntry GetEntry() {
70 return entry_;
71 }
72
73 size_t GetIndexBits() const {
74 return index_bits_;
75 }
76
77 private:
78 const size_t index_bits_;
79 const size_t slot_size_;
80 IndexBssMappingEntry entry_;
81};
82
83} // namespace linker
84} // namespace art
85
86#endif // ART_DEX2OAT_LINKER_INDEX_BSS_MAPPING_ENCODER_H_