| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2015 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 | */ | 
| Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 16 |  | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 17 | #include "stack_map_stream.h" | 
|  | 18 |  | 
| Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 19 | #include "art_method.h" | 
|  | 20 | #include "runtime.h" | 
|  | 21 | #include "scoped_thread_state_change-inl.h" | 
|  | 22 |  | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 23 | namespace art { | 
|  | 24 |  | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 25 | void StackMapStream::BeginStackMapEntry(uint32_t dex_pc, | 
|  | 26 | uint32_t native_pc_offset, | 
|  | 27 | uint32_t register_mask, | 
|  | 28 | BitVector* sp_mask, | 
|  | 29 | uint32_t num_dex_registers, | 
|  | 30 | uint8_t inlining_depth) { | 
|  | 31 | DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry"; | 
| Calin Juravle | b5c4693 | 2015-10-06 17:09:49 +0100 | [diff] [blame] | 32 | DCHECK_NE(dex_pc, static_cast<uint32_t>(-1)) << "invalid dex_pc"; | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 33 | current_entry_.dex_pc = dex_pc; | 
| Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 34 | current_entry_.native_pc_code_offset = CodeOffset::FromOffset(native_pc_offset, instruction_set_); | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 35 | current_entry_.register_mask = register_mask; | 
|  | 36 | current_entry_.sp_mask = sp_mask; | 
|  | 37 | current_entry_.num_dex_registers = num_dex_registers; | 
|  | 38 | current_entry_.inlining_depth = inlining_depth; | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 39 | current_entry_.dex_register_locations_start_index = dex_register_locations_.size(); | 
|  | 40 | current_entry_.inline_infos_start_index = inline_infos_.size(); | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 41 | current_entry_.dex_register_map_hash = 0; | 
|  | 42 | current_entry_.same_dex_register_map_as_ = kNoSameDexMapFound; | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 43 | if (num_dex_registers != 0) { | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 44 | current_entry_.live_dex_registers_mask = | 
| Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 45 | ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 46 | } else { | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 47 | current_entry_.live_dex_registers_mask = nullptr; | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 48 | } | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 49 |  | 
|  | 50 | if (sp_mask != nullptr) { | 
|  | 51 | stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet()); | 
|  | 52 | } | 
|  | 53 | if (inlining_depth > 0) { | 
|  | 54 | number_of_stack_maps_with_inline_info_++; | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | dex_pc_max_ = std::max(dex_pc_max_, dex_pc); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 58 | register_mask_max_ = std::max(register_mask_max_, register_mask); | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 59 | current_dex_register_ = 0; | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 62 | void StackMapStream::EndStackMapEntry() { | 
|  | 63 | current_entry_.same_dex_register_map_as_ = FindEntryWithTheSameDexMap(); | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 64 | stack_maps_.push_back(current_entry_); | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 65 | current_entry_ = StackMapEntry(); | 
|  | 66 | } | 
|  | 67 |  | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 68 | void StackMapStream::AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) { | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 69 | if (kind != DexRegisterLocation::Kind::kNone) { | 
|  | 70 | // Ensure we only use non-compressed location kind at this stage. | 
| David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 71 | DCHECK(DexRegisterLocation::IsShortLocationKind(kind)) << kind; | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 72 | DexRegisterLocation location(kind, value); | 
|  | 73 |  | 
|  | 74 | // Look for Dex register `location` in the location catalog (using the | 
|  | 75 | // companion hash map of locations to indices).  Use its index if it | 
|  | 76 | // is already in the location catalog.  If not, insert it (in the | 
|  | 77 | // location catalog and the hash map) and use the newly created index. | 
|  | 78 | auto it = location_catalog_entries_indices_.Find(location); | 
|  | 79 | if (it != location_catalog_entries_indices_.end()) { | 
|  | 80 | // Retrieve the index from the hash map. | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 81 | dex_register_locations_.push_back(it->second); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 82 | } else { | 
|  | 83 | // Create a new entry in the location catalog and the hash map. | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 84 | size_t index = location_catalog_entries_.size(); | 
|  | 85 | location_catalog_entries_.push_back(location); | 
|  | 86 | dex_register_locations_.push_back(index); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 87 | location_catalog_entries_indices_.Insert(std::make_pair(location, index)); | 
|  | 88 | } | 
|  | 89 |  | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 90 | if (in_inline_frame_) { | 
|  | 91 | // TODO: Support sharing DexRegisterMap across InlineInfo. | 
|  | 92 | DCHECK_LT(current_dex_register_, current_inline_info_.num_dex_registers); | 
|  | 93 | current_inline_info_.live_dex_registers_mask->SetBit(current_dex_register_); | 
|  | 94 | } else { | 
|  | 95 | DCHECK_LT(current_dex_register_, current_entry_.num_dex_registers); | 
|  | 96 | current_entry_.live_dex_registers_mask->SetBit(current_dex_register_); | 
|  | 97 | current_entry_.dex_register_map_hash += (1 << | 
|  | 98 | (current_dex_register_ % (sizeof(current_entry_.dex_register_map_hash) * kBitsPerByte))); | 
|  | 99 | current_entry_.dex_register_map_hash += static_cast<uint32_t>(value); | 
|  | 100 | current_entry_.dex_register_map_hash += static_cast<uint32_t>(kind); | 
|  | 101 | } | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 102 | } | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 103 | current_dex_register_++; | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
| Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 106 | static bool EncodeArtMethodInInlineInfo(ArtMethod* method ATTRIBUTE_UNUSED) { | 
|  | 107 | // Note: the runtime is null only for unit testing. | 
|  | 108 | return Runtime::Current() == nullptr || !Runtime::Current()->IsAotCompiler(); | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | void StackMapStream::BeginInlineInfoEntry(ArtMethod* method, | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 112 | uint32_t dex_pc, | 
| Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 113 | uint32_t num_dex_registers, | 
|  | 114 | const DexFile* outer_dex_file) { | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 115 | DCHECK(!in_inline_frame_); | 
|  | 116 | in_inline_frame_ = true; | 
| Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 117 | if (EncodeArtMethodInInlineInfo(method)) { | 
|  | 118 | current_inline_info_.method = method; | 
|  | 119 | } else { | 
|  | 120 | if (dex_pc != static_cast<uint32_t>(-1) && kIsDebugBuild) { | 
|  | 121 | ScopedObjectAccess soa(Thread::Current()); | 
|  | 122 | DCHECK(IsSameDexFile(*outer_dex_file, *method->GetDexFile())); | 
|  | 123 | } | 
|  | 124 | current_inline_info_.method_index = method->GetDexMethodIndexUnchecked(); | 
|  | 125 | } | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 126 | current_inline_info_.dex_pc = dex_pc; | 
|  | 127 | current_inline_info_.num_dex_registers = num_dex_registers; | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 128 | current_inline_info_.dex_register_locations_start_index = dex_register_locations_.size(); | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 129 | if (num_dex_registers != 0) { | 
|  | 130 | current_inline_info_.live_dex_registers_mask = | 
| Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 131 | ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream); | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 132 | } else { | 
|  | 133 | current_inline_info_.live_dex_registers_mask = nullptr; | 
|  | 134 | } | 
|  | 135 | current_dex_register_ = 0; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | void StackMapStream::EndInlineInfoEntry() { | 
|  | 139 | DCHECK(in_inline_frame_); | 
|  | 140 | DCHECK_EQ(current_dex_register_, current_inline_info_.num_dex_registers) | 
|  | 141 | << "Inline information contains less registers than expected"; | 
|  | 142 | in_inline_frame_ = false; | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 143 | inline_infos_.push_back(current_inline_info_); | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 144 | current_inline_info_ = InlineInfoEntry(); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
| Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 147 | CodeOffset StackMapStream::ComputeMaxNativePcCodeOffset() const { | 
|  | 148 | CodeOffset max_native_pc_offset; | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 149 | for (const StackMapEntry& entry : stack_maps_) { | 
| Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 150 | max_native_pc_offset = std::max(max_native_pc_offset, entry.native_pc_code_offset); | 
| Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 151 | } | 
|  | 152 | return max_native_pc_offset; | 
|  | 153 | } | 
|  | 154 |  | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 155 | size_t StackMapStream::PrepareForFillIn() { | 
|  | 156 | int stack_mask_number_of_bits = stack_mask_max_ + 1;  // Need room for max element too. | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 157 | dex_register_maps_size_ = ComputeDexRegisterMapsSize(); | 
| David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 158 | ComputeInlineInfoEncoding();  // needs dex_register_maps_size_. | 
|  | 159 | inline_info_size_ = inline_infos_.size() * inline_info_encoding_.GetEntrySize(); | 
| Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 160 | CodeOffset max_native_pc_offset = ComputeMaxNativePcCodeOffset(); | 
|  | 161 | // The stack map contains compressed native offsets. | 
|  | 162 | size_t stack_map_size = stack_map_encoding_.SetFromSizes(max_native_pc_offset.CompressedValue(), | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 163 | dex_pc_max_, | 
|  | 164 | dex_register_maps_size_, | 
|  | 165 | inline_info_size_, | 
|  | 166 | register_mask_max_, | 
|  | 167 | stack_mask_number_of_bits); | 
|  | 168 | stack_maps_size_ = stack_maps_.size() * stack_map_size; | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 169 | dex_register_location_catalog_size_ = ComputeDexRegisterLocationCatalogSize(); | 
|  | 170 |  | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 171 | size_t non_header_size = | 
|  | 172 | stack_maps_size_ + | 
|  | 173 | dex_register_location_catalog_size_ + | 
|  | 174 | dex_register_maps_size_ + | 
|  | 175 | inline_info_size_; | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 176 |  | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 177 | // Prepare the CodeInfo variable-sized encoding. | 
|  | 178 | CodeInfoEncoding code_info_encoding; | 
|  | 179 | code_info_encoding.non_header_size = non_header_size; | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 180 | code_info_encoding.number_of_stack_maps = stack_maps_.size(); | 
|  | 181 | code_info_encoding.stack_map_size_in_bytes = stack_map_size; | 
| David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 182 | code_info_encoding.stack_map_encoding = stack_map_encoding_; | 
|  | 183 | code_info_encoding.inline_info_encoding = inline_info_encoding_; | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 184 | code_info_encoding.number_of_location_catalog_entries = location_catalog_entries_.size(); | 
|  | 185 | code_info_encoding.Compress(&code_info_encoding_); | 
|  | 186 |  | 
| Nicolas Geoffray | 6530baf | 2015-05-26 15:22:58 +0100 | [diff] [blame] | 187 | // TODO: Move the catalog at the end. It is currently too expensive at runtime | 
|  | 188 | // to compute its size (note that we do not encode that size in the CodeInfo). | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 189 | dex_register_location_catalog_start_ = code_info_encoding_.size() + stack_maps_size_; | 
| Nicolas Geoffray | 6530baf | 2015-05-26 15:22:58 +0100 | [diff] [blame] | 190 | dex_register_maps_start_ = | 
|  | 191 | dex_register_location_catalog_start_ + dex_register_location_catalog_size_; | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 192 | inline_infos_start_ = dex_register_maps_start_ + dex_register_maps_size_; | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 193 |  | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 194 | needed_size_ = code_info_encoding_.size() + non_header_size; | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 195 | return needed_size_; | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
|  | 198 | size_t StackMapStream::ComputeDexRegisterLocationCatalogSize() const { | 
|  | 199 | size_t size = DexRegisterLocationCatalog::kFixedSize; | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 200 | for (const DexRegisterLocation& dex_register_location : location_catalog_entries_) { | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 201 | size += DexRegisterLocationCatalog::EntrySize(dex_register_location); | 
|  | 202 | } | 
|  | 203 | return size; | 
|  | 204 | } | 
|  | 205 |  | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 206 | size_t StackMapStream::ComputeDexRegisterMapSize(uint32_t num_dex_registers, | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 207 | const BitVector* live_dex_registers_mask) const { | 
|  | 208 | // For num_dex_registers == 0u live_dex_registers_mask may be null. | 
|  | 209 | if (num_dex_registers == 0u) { | 
|  | 210 | return 0u;  // No register map will be emitted. | 
|  | 211 | } | 
|  | 212 | DCHECK(live_dex_registers_mask != nullptr); | 
|  | 213 |  | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 214 | // Size of the map in bytes. | 
|  | 215 | size_t size = DexRegisterMap::kFixedSize; | 
|  | 216 | // Add the live bit mask for the Dex register liveness. | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 217 | size += DexRegisterMap::GetLiveBitMaskSize(num_dex_registers); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 218 | // Compute the size of the set of live Dex register entries. | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 219 | size_t number_of_live_dex_registers = live_dex_registers_mask->NumSetBits(); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 220 | size_t map_entries_size_in_bits = | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 221 | DexRegisterMap::SingleEntrySizeInBits(location_catalog_entries_.size()) | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 222 | * number_of_live_dex_registers; | 
|  | 223 | size_t map_entries_size_in_bytes = | 
|  | 224 | RoundUp(map_entries_size_in_bits, kBitsPerByte) / kBitsPerByte; | 
|  | 225 | size += map_entries_size_in_bytes; | 
|  | 226 | return size; | 
|  | 227 | } | 
|  | 228 |  | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 229 | size_t StackMapStream::ComputeDexRegisterMapsSize() const { | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 230 | size_t size = 0; | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 231 | size_t inline_info_index = 0; | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 232 | for (const StackMapEntry& entry : stack_maps_) { | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 233 | if (entry.same_dex_register_map_as_ == kNoSameDexMapFound) { | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 234 | size += ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask); | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 235 | } else { | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 236 | // Entries with the same dex map will have the same offset. | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 237 | } | 
|  | 238 | for (size_t j = 0; j < entry.inlining_depth; ++j) { | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 239 | InlineInfoEntry inline_entry = inline_infos_[inline_info_index++]; | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 240 | size += ComputeDexRegisterMapSize(inline_entry.num_dex_registers, | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 241 | inline_entry.live_dex_registers_mask); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 242 | } | 
|  | 243 | } | 
|  | 244 | return size; | 
|  | 245 | } | 
|  | 246 |  | 
| David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 247 | void StackMapStream::ComputeInlineInfoEncoding() { | 
|  | 248 | uint32_t method_index_max = 0; | 
| Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 249 | uint32_t dex_pc_max = DexFile::kDexNoIndex; | 
| Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 250 | uint32_t extra_data_max = 0; | 
| David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 251 |  | 
|  | 252 | uint32_t inline_info_index = 0; | 
|  | 253 | for (const StackMapEntry& entry : stack_maps_) { | 
|  | 254 | for (size_t j = 0; j < entry.inlining_depth; ++j) { | 
|  | 255 | InlineInfoEntry inline_entry = inline_infos_[inline_info_index++]; | 
| Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 256 | if (inline_entry.method == nullptr) { | 
|  | 257 | method_index_max = std::max(method_index_max, inline_entry.method_index); | 
|  | 258 | extra_data_max = std::max(extra_data_max, 1u); | 
|  | 259 | } else { | 
|  | 260 | method_index_max = std::max( | 
|  | 261 | method_index_max, High32Bits(reinterpret_cast<uintptr_t>(inline_entry.method))); | 
|  | 262 | extra_data_max = std::max( | 
|  | 263 | extra_data_max, Low32Bits(reinterpret_cast<uintptr_t>(inline_entry.method))); | 
|  | 264 | } | 
| Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 265 | if (inline_entry.dex_pc != DexFile::kDexNoIndex && | 
|  | 266 | (dex_pc_max == DexFile::kDexNoIndex || dex_pc_max < inline_entry.dex_pc)) { | 
|  | 267 | dex_pc_max = inline_entry.dex_pc; | 
|  | 268 | } | 
| David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 269 | } | 
|  | 270 | } | 
|  | 271 | DCHECK_EQ(inline_info_index, inline_infos_.size()); | 
|  | 272 |  | 
|  | 273 | inline_info_encoding_.SetFromSizes(method_index_max, | 
|  | 274 | dex_pc_max, | 
| Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 275 | extra_data_max, | 
| David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 276 | dex_register_maps_size_); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 277 | } | 
|  | 278 |  | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 279 | void StackMapStream::FillIn(MemoryRegion region) { | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 280 | DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry"; | 
|  | 281 | DCHECK_NE(0u, needed_size_) << "PrepareForFillIn not called before FillIn"; | 
|  | 282 |  | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 283 | DCHECK_EQ(region.size(), needed_size_); | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 284 |  | 
|  | 285 | // Note that the memory region does not have to be zeroed when we JIT code | 
|  | 286 | // because we do not use the arena allocator there. | 
|  | 287 |  | 
|  | 288 | // Write the CodeInfo header. | 
|  | 289 | region.CopyFrom(0, MemoryRegion(code_info_encoding_.data(), code_info_encoding_.size())); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 290 |  | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 291 | MemoryRegion dex_register_locations_region = region.Subregion( | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 292 | dex_register_maps_start_, dex_register_maps_size_); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 293 |  | 
|  | 294 | MemoryRegion inline_infos_region = region.Subregion( | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 295 | inline_infos_start_, inline_info_size_); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 296 |  | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 297 | CodeInfo code_info(region); | 
|  | 298 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); | 
|  | 299 | DCHECK_EQ(code_info.GetStackMapsSize(encoding), stack_maps_size_); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 300 |  | 
|  | 301 | // Set the Dex register location catalog. | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 302 | MemoryRegion dex_register_location_catalog_region = region.Subregion( | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 303 | dex_register_location_catalog_start_, dex_register_location_catalog_size_); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 304 | DexRegisterLocationCatalog dex_register_location_catalog(dex_register_location_catalog_region); | 
|  | 305 | // Offset in `dex_register_location_catalog` where to store the next | 
|  | 306 | // register location. | 
|  | 307 | size_t location_catalog_offset = DexRegisterLocationCatalog::kFixedSize; | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 308 | for (DexRegisterLocation dex_register_location : location_catalog_entries_) { | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 309 | dex_register_location_catalog.SetRegisterInfo(location_catalog_offset, dex_register_location); | 
|  | 310 | location_catalog_offset += DexRegisterLocationCatalog::EntrySize(dex_register_location); | 
|  | 311 | } | 
|  | 312 | // Ensure we reached the end of the Dex registers location_catalog. | 
|  | 313 | DCHECK_EQ(location_catalog_offset, dex_register_location_catalog_region.size()); | 
|  | 314 |  | 
| Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 315 | ArenaBitVector empty_bitmask(allocator_, 0, /* expandable */ false, kArenaAllocStackMapStream); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 316 | uintptr_t next_dex_register_map_offset = 0; | 
|  | 317 | uintptr_t next_inline_info_offset = 0; | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 318 | for (size_t i = 0, e = stack_maps_.size(); i < e; ++i) { | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 319 | StackMap stack_map = code_info.GetStackMapAt(i, encoding); | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 320 | StackMapEntry entry = stack_maps_[i]; | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 321 |  | 
| David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 322 | stack_map.SetDexPc(stack_map_encoding_, entry.dex_pc); | 
| Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 323 | stack_map.SetNativePcCodeOffset(stack_map_encoding_, entry.native_pc_code_offset); | 
| David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 324 | stack_map.SetRegisterMask(stack_map_encoding_, entry.register_mask); | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 325 | size_t number_of_stack_mask_bits = stack_map.GetNumberOfStackMaskBits(stack_map_encoding_); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 326 | if (entry.sp_mask != nullptr) { | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 327 | for (size_t bit = 0; bit < number_of_stack_mask_bits; bit++) { | 
|  | 328 | stack_map.SetStackMaskBit(stack_map_encoding_, bit, entry.sp_mask->IsBitSet(bit)); | 
|  | 329 | } | 
| David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 330 | } else { | 
|  | 331 | // The MemoryRegion does not have to be zeroed, so make sure we clear the bits. | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 332 | for (size_t bit = 0; bit < number_of_stack_mask_bits; bit++) { | 
|  | 333 | stack_map.SetStackMaskBit(stack_map_encoding_, bit, false); | 
|  | 334 | } | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 335 | } | 
|  | 336 |  | 
| Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 337 | if (entry.num_dex_registers == 0 || (entry.live_dex_registers_mask->NumSetBits() == 0)) { | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 338 | // No dex map available. | 
| David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 339 | stack_map.SetDexRegisterMapOffset(stack_map_encoding_, StackMap::kNoDexRegisterMap); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 340 | } else { | 
|  | 341 | // Search for an entry with the same dex map. | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 342 | if (entry.same_dex_register_map_as_ != kNoSameDexMapFound) { | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 343 | // If we have a hit reuse the offset. | 
| David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 344 | stack_map.SetDexRegisterMapOffset( | 
|  | 345 | stack_map_encoding_, | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 346 | code_info.GetStackMapAt(entry.same_dex_register_map_as_, encoding) | 
| David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 347 | .GetDexRegisterMapOffset(stack_map_encoding_)); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 348 | } else { | 
|  | 349 | // New dex registers maps should be added to the stack map. | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 350 | MemoryRegion register_region = dex_register_locations_region.Subregion( | 
|  | 351 | next_dex_register_map_offset, | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 352 | ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask)); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 353 | next_dex_register_map_offset += register_region.size(); | 
|  | 354 | DexRegisterMap dex_register_map(register_region); | 
|  | 355 | stack_map.SetDexRegisterMapOffset( | 
| David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 356 | stack_map_encoding_, register_region.start() - dex_register_locations_region.start()); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 357 |  | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 358 | // Set the dex register location. | 
|  | 359 | FillInDexRegisterMap(dex_register_map, | 
|  | 360 | entry.num_dex_registers, | 
|  | 361 | *entry.live_dex_registers_mask, | 
|  | 362 | entry.dex_register_locations_start_index); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 363 | } | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | // Set the inlining info. | 
|  | 367 | if (entry.inlining_depth != 0) { | 
|  | 368 | MemoryRegion inline_region = inline_infos_region.Subregion( | 
|  | 369 | next_inline_info_offset, | 
| David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 370 | entry.inlining_depth * inline_info_encoding_.GetEntrySize()); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 371 | next_inline_info_offset += inline_region.size(); | 
|  | 372 | InlineInfo inline_info(inline_region); | 
|  | 373 |  | 
|  | 374 | // Currently relative to the dex register map. | 
|  | 375 | stack_map.SetInlineDescriptorOffset( | 
| David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 376 | stack_map_encoding_, inline_region.start() - dex_register_locations_region.start()); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 377 |  | 
| David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 378 | inline_info.SetDepth(inline_info_encoding_, entry.inlining_depth); | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 379 | DCHECK_LE(entry.inline_infos_start_index + entry.inlining_depth, inline_infos_.size()); | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 380 | for (size_t depth = 0; depth < entry.inlining_depth; ++depth) { | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 381 | InlineInfoEntry inline_entry = inline_infos_[depth + entry.inline_infos_start_index]; | 
| Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 382 | if (inline_entry.method != nullptr) { | 
|  | 383 | inline_info.SetMethodIndexAtDepth( | 
|  | 384 | inline_info_encoding_, | 
|  | 385 | depth, | 
|  | 386 | High32Bits(reinterpret_cast<uintptr_t>(inline_entry.method))); | 
|  | 387 | inline_info.SetExtraDataAtDepth( | 
|  | 388 | inline_info_encoding_, | 
|  | 389 | depth, | 
|  | 390 | Low32Bits(reinterpret_cast<uintptr_t>(inline_entry.method))); | 
|  | 391 | } else { | 
|  | 392 | inline_info.SetMethodIndexAtDepth(inline_info_encoding_, depth, inline_entry.method_index); | 
|  | 393 | inline_info.SetExtraDataAtDepth(inline_info_encoding_, depth, 1); | 
|  | 394 | } | 
| David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 395 | inline_info.SetDexPcAtDepth(inline_info_encoding_, depth, inline_entry.dex_pc); | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 396 | if (inline_entry.num_dex_registers == 0) { | 
|  | 397 | // No dex map available. | 
| David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 398 | inline_info.SetDexRegisterMapOffsetAtDepth(inline_info_encoding_, | 
|  | 399 | depth, | 
|  | 400 | StackMap::kNoDexRegisterMap); | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 401 | DCHECK(inline_entry.live_dex_registers_mask == nullptr); | 
|  | 402 | } else { | 
|  | 403 | MemoryRegion register_region = dex_register_locations_region.Subregion( | 
|  | 404 | next_dex_register_map_offset, | 
|  | 405 | ComputeDexRegisterMapSize(inline_entry.num_dex_registers, | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 406 | inline_entry.live_dex_registers_mask)); | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 407 | next_dex_register_map_offset += register_region.size(); | 
|  | 408 | DexRegisterMap dex_register_map(register_region); | 
|  | 409 | inline_info.SetDexRegisterMapOffsetAtDepth( | 
| David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 410 | inline_info_encoding_, | 
|  | 411 | depth, register_region.start() - dex_register_locations_region.start()); | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 412 |  | 
|  | 413 | FillInDexRegisterMap(dex_register_map, | 
|  | 414 | inline_entry.num_dex_registers, | 
|  | 415 | *inline_entry.live_dex_registers_mask, | 
|  | 416 | inline_entry.dex_register_locations_start_index); | 
|  | 417 | } | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 418 | } | 
|  | 419 | } else { | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 420 | if (inline_info_size_ != 0) { | 
| David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 421 | stack_map.SetInlineDescriptorOffset(stack_map_encoding_, StackMap::kNoInlineInfo); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 422 | } | 
|  | 423 | } | 
|  | 424 | } | 
| David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 425 |  | 
|  | 426 | // Verify all written data in debug build. | 
|  | 427 | if (kIsDebugBuild) { | 
|  | 428 | CheckCodeInfo(region); | 
|  | 429 | } | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 430 | } | 
|  | 431 |  | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 432 | void StackMapStream::FillInDexRegisterMap(DexRegisterMap dex_register_map, | 
|  | 433 | uint32_t num_dex_registers, | 
|  | 434 | const BitVector& live_dex_registers_mask, | 
|  | 435 | uint32_t start_index_in_dex_register_locations) const { | 
|  | 436 | dex_register_map.SetLiveBitMask(num_dex_registers, live_dex_registers_mask); | 
|  | 437 | // Set the dex register location mapping data. | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 438 | size_t number_of_live_dex_registers = live_dex_registers_mask.NumSetBits(); | 
|  | 439 | DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size()); | 
|  | 440 | DCHECK_LE(start_index_in_dex_register_locations, | 
|  | 441 | dex_register_locations_.size() - number_of_live_dex_registers); | 
|  | 442 | for (size_t index_in_dex_register_locations = 0; | 
|  | 443 | index_in_dex_register_locations != number_of_live_dex_registers; | 
|  | 444 | ++index_in_dex_register_locations) { | 
|  | 445 | size_t location_catalog_entry_index = dex_register_locations_[ | 
|  | 446 | start_index_in_dex_register_locations + index_in_dex_register_locations]; | 
|  | 447 | dex_register_map.SetLocationCatalogEntryIndex( | 
|  | 448 | index_in_dex_register_locations, | 
|  | 449 | location_catalog_entry_index, | 
|  | 450 | num_dex_registers, | 
|  | 451 | location_catalog_entries_.size()); | 
| Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 452 | } | 
|  | 453 | } | 
|  | 454 |  | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 455 | size_t StackMapStream::FindEntryWithTheSameDexMap() { | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 456 | size_t current_entry_index = stack_maps_.size(); | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 457 | auto entries_it = dex_map_hash_to_stack_map_indices_.find(current_entry_.dex_register_map_hash); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 458 | if (entries_it == dex_map_hash_to_stack_map_indices_.end()) { | 
|  | 459 | // We don't have a perfect hash functions so we need a list to collect all stack maps | 
|  | 460 | // which might have the same dex register map. | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 461 | ArenaVector<uint32_t> stack_map_indices(allocator_->Adapter(kArenaAllocStackMapStream)); | 
|  | 462 | stack_map_indices.push_back(current_entry_index); | 
|  | 463 | dex_map_hash_to_stack_map_indices_.Put(current_entry_.dex_register_map_hash, | 
|  | 464 | std::move(stack_map_indices)); | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 465 | return kNoSameDexMapFound; | 
|  | 466 | } | 
|  | 467 |  | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 468 | // We might have collisions, so we need to check whether or not we really have a match. | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 469 | for (uint32_t test_entry_index : entries_it->second) { | 
|  | 470 | if (HaveTheSameDexMaps(GetStackMap(test_entry_index), current_entry_)) { | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 471 | return test_entry_index; | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 472 | } | 
|  | 473 | } | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 474 | entries_it->second.push_back(current_entry_index); | 
| Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 475 | return kNoSameDexMapFound; | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 476 | } | 
|  | 477 |  | 
|  | 478 | bool StackMapStream::HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const { | 
|  | 479 | if (a.live_dex_registers_mask == nullptr && b.live_dex_registers_mask == nullptr) { | 
|  | 480 | return true; | 
|  | 481 | } | 
|  | 482 | if (a.live_dex_registers_mask == nullptr || b.live_dex_registers_mask == nullptr) { | 
|  | 483 | return false; | 
|  | 484 | } | 
|  | 485 | if (a.num_dex_registers != b.num_dex_registers) { | 
|  | 486 | return false; | 
|  | 487 | } | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 488 | if (a.num_dex_registers != 0u) { | 
|  | 489 | DCHECK(a.live_dex_registers_mask != nullptr); | 
|  | 490 | DCHECK(b.live_dex_registers_mask != nullptr); | 
|  | 491 | if (!a.live_dex_registers_mask->Equal(b.live_dex_registers_mask)) { | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 492 | return false; | 
|  | 493 | } | 
| Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 494 | size_t number_of_live_dex_registers = a.live_dex_registers_mask->NumSetBits(); | 
|  | 495 | DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size()); | 
|  | 496 | DCHECK_LE(a.dex_register_locations_start_index, | 
|  | 497 | dex_register_locations_.size() - number_of_live_dex_registers); | 
|  | 498 | DCHECK_LE(b.dex_register_locations_start_index, | 
|  | 499 | dex_register_locations_.size() - number_of_live_dex_registers); | 
|  | 500 | auto a_begin = dex_register_locations_.begin() + a.dex_register_locations_start_index; | 
|  | 501 | auto b_begin = dex_register_locations_.begin() + b.dex_register_locations_start_index; | 
|  | 502 | if (!std::equal(a_begin, a_begin + number_of_live_dex_registers, b_begin)) { | 
|  | 503 | return false; | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 504 | } | 
|  | 505 | } | 
|  | 506 | return true; | 
|  | 507 | } | 
|  | 508 |  | 
| David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 509 | // Helper for CheckCodeInfo - check that register map has the expected content. | 
|  | 510 | void StackMapStream::CheckDexRegisterMap(const CodeInfo& code_info, | 
|  | 511 | const DexRegisterMap& dex_register_map, | 
|  | 512 | size_t num_dex_registers, | 
|  | 513 | BitVector* live_dex_registers_mask, | 
|  | 514 | size_t dex_register_locations_index) const { | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 515 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); | 
| David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 516 | for (size_t reg = 0; reg < num_dex_registers; reg++) { | 
|  | 517 | // Find the location we tried to encode. | 
|  | 518 | DexRegisterLocation expected = DexRegisterLocation::None(); | 
|  | 519 | if (live_dex_registers_mask->IsBitSet(reg)) { | 
|  | 520 | size_t catalog_index = dex_register_locations_[dex_register_locations_index++]; | 
|  | 521 | expected = location_catalog_entries_[catalog_index]; | 
|  | 522 | } | 
|  | 523 | // Compare to the seen location. | 
|  | 524 | if (expected.GetKind() == DexRegisterLocation::Kind::kNone) { | 
|  | 525 | DCHECK(!dex_register_map.IsValid() || !dex_register_map.IsDexRegisterLive(reg)); | 
|  | 526 | } else { | 
|  | 527 | DCHECK(dex_register_map.IsDexRegisterLive(reg)); | 
|  | 528 | DexRegisterLocation seen = dex_register_map.GetDexRegisterLocation( | 
|  | 529 | reg, num_dex_registers, code_info, encoding); | 
|  | 530 | DCHECK_EQ(expected.GetKind(), seen.GetKind()); | 
|  | 531 | DCHECK_EQ(expected.GetValue(), seen.GetValue()); | 
|  | 532 | } | 
|  | 533 | } | 
|  | 534 | if (num_dex_registers == 0) { | 
|  | 535 | DCHECK(!dex_register_map.IsValid()); | 
|  | 536 | } | 
|  | 537 | } | 
|  | 538 |  | 
|  | 539 | // Check that all StackMapStream inputs are correctly encoded by trying to read them back. | 
|  | 540 | void StackMapStream::CheckCodeInfo(MemoryRegion region) const { | 
|  | 541 | CodeInfo code_info(region); | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 542 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); | 
|  | 543 | DCHECK_EQ(code_info.GetNumberOfStackMaps(encoding), stack_maps_.size()); | 
| David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 544 | for (size_t s = 0; s < stack_maps_.size(); ++s) { | 
|  | 545 | const StackMap stack_map = code_info.GetStackMapAt(s, encoding); | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 546 | const StackMapEncoding& stack_map_encoding = encoding.stack_map_encoding; | 
| David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 547 | StackMapEntry entry = stack_maps_[s]; | 
|  | 548 |  | 
|  | 549 | // Check main stack map fields. | 
| Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 550 | DCHECK_EQ(stack_map.GetNativePcOffset(stack_map_encoding, instruction_set_), | 
|  | 551 | entry.native_pc_code_offset.Uint32Value(instruction_set_)); | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 552 | DCHECK_EQ(stack_map.GetDexPc(stack_map_encoding), entry.dex_pc); | 
|  | 553 | DCHECK_EQ(stack_map.GetRegisterMask(stack_map_encoding), entry.register_mask); | 
|  | 554 | size_t num_stack_mask_bits = stack_map.GetNumberOfStackMaskBits(stack_map_encoding); | 
| David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 555 | if (entry.sp_mask != nullptr) { | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 556 | DCHECK_GE(num_stack_mask_bits, entry.sp_mask->GetNumberOfBits()); | 
|  | 557 | for (size_t b = 0; b < num_stack_mask_bits; b++) { | 
|  | 558 | DCHECK_EQ(stack_map.GetStackMaskBit(stack_map_encoding, b), entry.sp_mask->IsBitSet(b)); | 
| David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 559 | } | 
|  | 560 | } else { | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 561 | for (size_t b = 0; b < num_stack_mask_bits; b++) { | 
|  | 562 | DCHECK_EQ(stack_map.GetStackMaskBit(stack_map_encoding, b), 0u); | 
| David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 563 | } | 
|  | 564 | } | 
|  | 565 |  | 
|  | 566 | CheckDexRegisterMap(code_info, | 
|  | 567 | code_info.GetDexRegisterMapOf( | 
|  | 568 | stack_map, encoding, entry.num_dex_registers), | 
|  | 569 | entry.num_dex_registers, | 
|  | 570 | entry.live_dex_registers_mask, | 
|  | 571 | entry.dex_register_locations_start_index); | 
|  | 572 |  | 
|  | 573 | // Check inline info. | 
| David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 574 | DCHECK_EQ(stack_map.HasInlineInfo(stack_map_encoding), (entry.inlining_depth != 0)); | 
| David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 575 | if (entry.inlining_depth != 0) { | 
|  | 576 | InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding); | 
| David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 577 | DCHECK_EQ(inline_info.GetDepth(encoding.inline_info_encoding), entry.inlining_depth); | 
| David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 578 | for (size_t d = 0; d < entry.inlining_depth; ++d) { | 
|  | 579 | size_t inline_info_index = entry.inline_infos_start_index + d; | 
|  | 580 | DCHECK_LT(inline_info_index, inline_infos_.size()); | 
|  | 581 | InlineInfoEntry inline_entry = inline_infos_[inline_info_index]; | 
| David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 582 | DCHECK_EQ(inline_info.GetDexPcAtDepth(encoding.inline_info_encoding, d), | 
|  | 583 | inline_entry.dex_pc); | 
| Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 584 | if (inline_info.EncodesArtMethodAtDepth(encoding.inline_info_encoding, d)) { | 
|  | 585 | DCHECK_EQ(inline_info.GetArtMethodAtDepth(encoding.inline_info_encoding, d), | 
|  | 586 | inline_entry.method); | 
|  | 587 | } else { | 
|  | 588 | DCHECK_EQ(inline_info.GetMethodIndexAtDepth(encoding.inline_info_encoding, d), | 
|  | 589 | inline_entry.method_index); | 
|  | 590 | } | 
| David Srbecky | 1bbdfd7 | 2016-02-24 16:39:26 +0000 | [diff] [blame] | 591 |  | 
|  | 592 | CheckDexRegisterMap(code_info, | 
|  | 593 | code_info.GetDexRegisterMapAtDepth( | 
|  | 594 | d, inline_info, encoding, inline_entry.num_dex_registers), | 
|  | 595 | inline_entry.num_dex_registers, | 
|  | 596 | inline_entry.live_dex_registers_mask, | 
|  | 597 | inline_entry.dex_register_locations_start_index); | 
|  | 598 | } | 
|  | 599 | } | 
|  | 600 | } | 
|  | 601 | } | 
|  | 602 |  | 
| Calin Juravle | c416d33 | 2015-04-23 16:01:43 +0100 | [diff] [blame] | 603 | }  // namespace art |