Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [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 | */ |
| 16 | |
| 17 | #include "stack_map.h" |
| 18 | |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 21 | #include "indenter.h" |
| 22 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 23 | namespace art { |
| 24 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 25 | constexpr size_t DexRegisterLocationCatalog::kNoLocationEntryIndex; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 26 | constexpr uint32_t StackMap::kNoDexRegisterMap; |
| 27 | constexpr uint32_t StackMap::kNoInlineInfo; |
| 28 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 29 | DexRegisterLocation::Kind DexRegisterMap::GetLocationInternalKind(uint16_t dex_register_number, |
| 30 | uint16_t number_of_dex_registers, |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 31 | const CodeInfo& code_info, |
| 32 | const StackMapEncoding& enc) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 33 | DexRegisterLocationCatalog dex_register_location_catalog = |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 34 | code_info.GetDexRegisterLocationCatalog(enc); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 35 | size_t location_catalog_entry_index = GetLocationCatalogEntryIndex( |
| 36 | dex_register_number, |
| 37 | number_of_dex_registers, |
| 38 | code_info.GetNumberOfDexRegisterLocationCatalogEntries()); |
| 39 | return dex_register_location_catalog.GetLocationInternalKind(location_catalog_entry_index); |
| 40 | } |
| 41 | |
| 42 | DexRegisterLocation DexRegisterMap::GetDexRegisterLocation(uint16_t dex_register_number, |
| 43 | uint16_t number_of_dex_registers, |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 44 | const CodeInfo& code_info, |
| 45 | const StackMapEncoding& enc) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 46 | DexRegisterLocationCatalog dex_register_location_catalog = |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 47 | code_info.GetDexRegisterLocationCatalog(enc); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 48 | size_t location_catalog_entry_index = GetLocationCatalogEntryIndex( |
| 49 | dex_register_number, |
| 50 | number_of_dex_registers, |
| 51 | code_info.GetNumberOfDexRegisterLocationCatalogEntries()); |
| 52 | return dex_register_location_catalog.GetDexRegisterLocation(location_catalog_entry_index); |
| 53 | } |
| 54 | |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 55 | uint32_t StackMap::LoadAt(size_t number_of_bytes, size_t offset, bool check_max) const { |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 56 | if (number_of_bytes == 0u) { |
| 57 | DCHECK(!check_max); |
| 58 | return 0; |
| 59 | } else if (number_of_bytes == 1u) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 60 | uint8_t value = region_.LoadUnaligned<uint8_t>(offset); |
| 61 | return (check_max && value == 0xFF) ? -1 : value; |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 62 | } else if (number_of_bytes == 2u) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 63 | uint16_t value = region_.LoadUnaligned<uint16_t>(offset); |
| 64 | return (check_max && value == 0xFFFF) ? -1 : value; |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 65 | } else if (number_of_bytes == 3u) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 66 | uint16_t low = region_.LoadUnaligned<uint16_t>(offset); |
| 67 | uint16_t high = region_.LoadUnaligned<uint8_t>(offset + sizeof(uint16_t)); |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 68 | uint32_t value = (high << 16) + low; |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 69 | return (check_max && value == 0xFFFFFF) ? -1 : value; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 70 | } else { |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 71 | DCHECK_EQ(number_of_bytes, 4u); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 72 | return region_.LoadUnaligned<uint32_t>(offset); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 76 | void StackMap::StoreAt(size_t number_of_bytes, size_t offset, uint32_t value) const { |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 77 | if (number_of_bytes == 0u) { |
| 78 | DCHECK_EQ(value, 0u); |
| 79 | } else if (number_of_bytes == 1u) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 80 | region_.StoreUnaligned<uint8_t>(offset, value); |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 81 | } else if (number_of_bytes == 2u) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 82 | region_.StoreUnaligned<uint16_t>(offset, value); |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 83 | } else if (number_of_bytes == 3u) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 84 | region_.StoreUnaligned<uint16_t>(offset, Low16Bits(value)); |
| 85 | region_.StoreUnaligned<uint8_t>(offset + sizeof(uint16_t), High16Bits(value)); |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 86 | } else { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 87 | region_.StoreUnaligned<uint32_t>(offset, value); |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 88 | DCHECK_EQ(number_of_bytes, 4u); |
| 89 | } |
| 90 | } |
| 91 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 92 | static void DumpRegisterMapping(std::ostream& os, |
| 93 | size_t dex_register_num, |
| 94 | DexRegisterLocation location, |
| 95 | const std::string& prefix = "v", |
| 96 | const std::string& suffix = "") { |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 97 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 98 | std::ostream indented_os(&indent_filter); |
| 99 | indented_os << prefix << dex_register_num << ": " |
| 100 | << DexRegisterLocation::PrettyDescriptor(location.GetInternalKind()) |
| 101 | << " (" << location.GetValue() << ")" << suffix << '\n'; |
| 102 | } |
| 103 | |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 104 | void CodeInfo::Dump(std::ostream& os, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 105 | uint32_t code_offset, |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 106 | uint16_t number_of_dex_registers, |
| 107 | bool dump_stack_maps) const { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 108 | StackMapEncoding encoding = ExtractEncoding(); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 109 | uint32_t code_info_size = GetOverallSize(); |
| 110 | size_t number_of_stack_maps = GetNumberOfStackMaps(); |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 111 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 112 | std::ostream indented_os(&indent_filter); |
| 113 | indented_os << "Optimized CodeInfo (size=" << code_info_size |
| 114 | << ", number_of_dex_registers=" << number_of_dex_registers |
| 115 | << ", number_of_stack_maps=" << number_of_stack_maps |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 116 | << ", has_inline_info=" << encoding.HasInlineInfo() |
| 117 | << ", number_of_bytes_for_inline_info=" << encoding.NumberOfBytesForInlineInfo() |
| 118 | << ", number_of_bytes_for_dex_register_map=" |
| 119 | << encoding.NumberOfBytesForDexRegisterMap() |
| 120 | << ", number_of_bytes_for_dex_pc=" << encoding.NumberOfBytesForDexPc() |
| 121 | << ", number_of_bytes_for_native_pc=" << encoding.NumberOfBytesForNativePc() |
| 122 | << ", number_of_bytes_for_register_mask=" << encoding.NumberOfBytesForRegisterMask() |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 123 | << ")\n"; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 124 | // Display the Dex register location catalog. |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 125 | GetDexRegisterLocationCatalog(encoding).Dump(indented_os, *this); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 126 | // Display stack maps along with (live) Dex register maps. |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 127 | if (dump_stack_maps) { |
| 128 | for (size_t i = 0; i < number_of_stack_maps; ++i) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 129 | StackMap stack_map = GetStackMapAt(i, encoding); |
| 130 | stack_map.Dump(indented_os, |
| 131 | *this, |
| 132 | encoding, |
| 133 | code_offset, |
| 134 | number_of_dex_registers, |
| 135 | " " + std::to_string(i)); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 138 | // TODO: Dump the stack map's inline information? We need to know more from the caller: |
| 139 | // we need to know the number of dex registers for each inlined method. |
| 140 | } |
| 141 | |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 142 | void DexRegisterLocationCatalog::Dump(std::ostream& os, const CodeInfo& code_info) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 143 | StackMapEncoding encoding = code_info.ExtractEncoding(); |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 144 | size_t number_of_location_catalog_entries = |
| 145 | code_info.GetNumberOfDexRegisterLocationCatalogEntries(); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 146 | size_t location_catalog_size_in_bytes = code_info.GetDexRegisterLocationCatalogSize(encoding); |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 147 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 148 | std::ostream indented_os(&indent_filter); |
| 149 | indented_os |
| 150 | << "DexRegisterLocationCatalog (number_of_entries=" << number_of_location_catalog_entries |
| 151 | << ", size_in_bytes=" << location_catalog_size_in_bytes << ")\n"; |
| 152 | for (size_t i = 0; i < number_of_location_catalog_entries; ++i) { |
| 153 | DexRegisterLocation location = GetDexRegisterLocation(i); |
| 154 | DumpRegisterMapping(indented_os, i, location, "entry "); |
| 155 | } |
| 156 | } |
| 157 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 158 | void DexRegisterMap::Dump(std::ostream& os, |
| 159 | const CodeInfo& code_info, |
| 160 | uint16_t number_of_dex_registers) const { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 161 | StackMapEncoding encoding = code_info.ExtractEncoding(); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 162 | size_t number_of_location_catalog_entries = |
| 163 | code_info.GetNumberOfDexRegisterLocationCatalogEntries(); |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 164 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 165 | std::ostream indented_os(&indent_filter); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 166 | // TODO: Display the bit mask of live Dex registers. |
| 167 | for (size_t j = 0; j < number_of_dex_registers; ++j) { |
| 168 | if (IsDexRegisterLive(j)) { |
| 169 | size_t location_catalog_entry_index = GetLocationCatalogEntryIndex( |
| 170 | j, number_of_dex_registers, number_of_location_catalog_entries); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 171 | DexRegisterLocation location = GetDexRegisterLocation(j, |
| 172 | number_of_dex_registers, |
| 173 | code_info, |
| 174 | encoding); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 175 | DumpRegisterMapping( |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 176 | indented_os, j, location, "v", |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 177 | "\t[entry " + std::to_string(static_cast<int>(location_catalog_entry_index)) + "]"); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 182 | void StackMap::Dump(std::ostream& os, |
| 183 | const CodeInfo& code_info, |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 184 | const StackMapEncoding& encoding, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 185 | uint32_t code_offset, |
| 186 | uint16_t number_of_dex_registers, |
| 187 | const std::string& header_suffix) const { |
| 188 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 189 | std::ostream indented_os(&indent_filter); |
| 190 | indented_os << "StackMap" << header_suffix |
| 191 | << std::hex |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 192 | << " [native_pc=0x" << code_offset + GetNativePcOffset(encoding) << "]" |
| 193 | << " (dex_pc=0x" << GetDexPc(encoding) |
| 194 | << ", native_pc_offset=0x" << GetNativePcOffset(encoding) |
| 195 | << ", dex_register_map_offset=0x" << GetDexRegisterMapOffset(encoding) |
| 196 | << ", inline_info_offset=0x" << GetInlineDescriptorOffset(encoding) |
| 197 | << ", register_mask=0x" << GetRegisterMask(encoding) |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 198 | << std::dec |
| 199 | << ", stack_mask=0b"; |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 200 | MemoryRegion stack_mask = GetStackMask(encoding); |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 201 | for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) { |
| 202 | indented_os << stack_mask.LoadBit(e - i - 1); |
| 203 | } |
| 204 | indented_os << ")\n"; |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 205 | if (HasDexRegisterMap(encoding)) { |
| 206 | DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf( |
| 207 | *this, encoding, number_of_dex_registers); |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 208 | dex_register_map.Dump(os, code_info, number_of_dex_registers); |
| 209 | } |
| 210 | } |
| 211 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 212 | void InlineInfo::Dump(std::ostream& os, |
| 213 | const CodeInfo& code_info, |
| 214 | uint16_t number_of_dex_registers[]) const { |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 215 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 216 | std::ostream indented_os(&indent_filter); |
| 217 | indented_os << "InlineInfo with depth " << static_cast<uint32_t>(GetDepth()) << "\n"; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 218 | |
| 219 | for (size_t i = 0; i < GetDepth(); ++i) { |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 220 | indented_os << " At depth " << i |
| 221 | << std::hex |
| 222 | << " (dex_pc=0x" << GetDexPcAtDepth(i) |
| 223 | << ", method_index=0x" << GetMethodIndexAtDepth(i) |
| 224 | << ")\n"; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 225 | if (HasDexRegisterMapAtDepth(i)) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 226 | StackMapEncoding encoding = code_info.ExtractEncoding(); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 227 | DexRegisterMap dex_register_map = |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 228 | code_info.GetDexRegisterMapAtDepth(i, *this, encoding, number_of_dex_registers[i]); |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 229 | dex_register_map.Dump(indented_os, code_info, number_of_dex_registers[i]); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 230 | } |
| 231 | } |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | } // namespace art |