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" |
Nicolas Geoffray | 12bdb72 | 2015-06-17 09:44:43 +0100 | [diff] [blame] | 22 | #include "invoke_type.h" |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 23 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 24 | namespace art { |
| 25 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 26 | constexpr size_t DexRegisterLocationCatalog::kNoLocationEntryIndex; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 27 | constexpr uint32_t StackMap::kNoDexRegisterMap; |
| 28 | constexpr uint32_t StackMap::kNoInlineInfo; |
| 29 | |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 30 | std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation::Kind& kind) { |
| 31 | using Kind = DexRegisterLocation::Kind; |
| 32 | switch (kind) { |
| 33 | case Kind::kNone: |
| 34 | return stream << "none"; |
| 35 | case Kind::kInStack: |
| 36 | return stream << "in stack"; |
| 37 | case Kind::kInRegister: |
| 38 | return stream << "in register"; |
| 39 | case Kind::kInRegisterHigh: |
| 40 | return stream << "in register high"; |
| 41 | case Kind::kInFpuRegister: |
| 42 | return stream << "in fpu register"; |
| 43 | case Kind::kInFpuRegisterHigh: |
| 44 | return stream << "in fpu register high"; |
| 45 | case Kind::kConstant: |
| 46 | return stream << "as constant"; |
| 47 | case Kind::kInStackLargeOffset: |
| 48 | return stream << "in stack (large offset)"; |
| 49 | case Kind::kConstantLargeValue: |
| 50 | return stream << "as constant (large value)"; |
| 51 | } |
| 52 | return stream << "Kind<" << static_cast<uint32_t>(kind) << ">"; |
| 53 | } |
| 54 | |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 55 | DexRegisterLocation::Kind DexRegisterMap::GetLocationInternalKind( |
| 56 | uint16_t dex_register_number, |
| 57 | uint16_t number_of_dex_registers, |
| 58 | const CodeInfo& code_info, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 59 | const CodeInfoEncoding& enc) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 60 | DexRegisterLocationCatalog dex_register_location_catalog = |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 61 | code_info.GetDexRegisterLocationCatalog(enc); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 62 | size_t location_catalog_entry_index = GetLocationCatalogEntryIndex( |
| 63 | dex_register_number, |
| 64 | number_of_dex_registers, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 65 | code_info.GetNumberOfLocationCatalogEntries(enc)); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 66 | return dex_register_location_catalog.GetLocationInternalKind(location_catalog_entry_index); |
| 67 | } |
| 68 | |
| 69 | DexRegisterLocation DexRegisterMap::GetDexRegisterLocation(uint16_t dex_register_number, |
| 70 | uint16_t number_of_dex_registers, |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 71 | const CodeInfo& code_info, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 72 | const CodeInfoEncoding& enc) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 73 | DexRegisterLocationCatalog dex_register_location_catalog = |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 74 | code_info.GetDexRegisterLocationCatalog(enc); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 75 | size_t location_catalog_entry_index = GetLocationCatalogEntryIndex( |
| 76 | dex_register_number, |
| 77 | number_of_dex_registers, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 78 | code_info.GetNumberOfLocationCatalogEntries(enc)); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 79 | return dex_register_location_catalog.GetDexRegisterLocation(location_catalog_entry_index); |
| 80 | } |
| 81 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 82 | static void DumpRegisterMapping(std::ostream& os, |
| 83 | size_t dex_register_num, |
| 84 | DexRegisterLocation location, |
| 85 | const std::string& prefix = "v", |
| 86 | const std::string& suffix = "") { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 87 | os << prefix << dex_register_num << ": " |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 88 | << location.GetInternalKind() |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 89 | << " (" << location.GetValue() << ")" << suffix << '\n'; |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 90 | } |
| 91 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 92 | void StackMapEncoding::Dump(VariableIndentationOutputStream* vios) const { |
| 93 | vios->Stream() |
| 94 | << "StackMapEncoding" |
| 95 | << " (native_pc_bit_offset=" << static_cast<uint32_t>(kNativePcBitOffset) |
| 96 | << ", dex_pc_bit_offset=" << static_cast<uint32_t>(dex_pc_bit_offset_) |
| 97 | << ", dex_register_map_bit_offset=" << static_cast<uint32_t>(dex_register_map_bit_offset_) |
| 98 | << ", inline_info_bit_offset=" << static_cast<uint32_t>(inline_info_bit_offset_) |
| 99 | << ", register_mask_bit_offset=" << static_cast<uint32_t>(register_mask_bit_offset_) |
| 100 | << ", stack_mask_bit_offset=" << static_cast<uint32_t>(stack_mask_bit_offset_) |
| 101 | << ")\n"; |
| 102 | } |
| 103 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 104 | void CodeInfo::Dump(VariableIndentationOutputStream* vios, |
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 Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 108 | CodeInfoEncoding encoding = ExtractEncoding(); |
| 109 | size_t number_of_stack_maps = GetNumberOfStackMaps(encoding); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 110 | vios->Stream() |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 111 | << "Optimized CodeInfo (number_of_dex_registers=" << number_of_dex_registers |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 112 | << ", number_of_stack_maps=" << number_of_stack_maps |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 113 | << ")\n"; |
| 114 | ScopedIndentation indent1(vios); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 115 | encoding.stack_map_encoding.Dump(vios); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 116 | // Display the Dex register location catalog. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 117 | GetDexRegisterLocationCatalog(encoding).Dump(vios, *this); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 118 | // Display stack maps along with (live) Dex register maps. |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 119 | if (dump_stack_maps) { |
| 120 | for (size_t i = 0; i < number_of_stack_maps; ++i) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 121 | StackMap stack_map = GetStackMapAt(i, encoding); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 122 | stack_map.Dump(vios, |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 123 | *this, |
| 124 | encoding, |
| 125 | code_offset, |
| 126 | number_of_dex_registers, |
| 127 | " " + std::to_string(i)); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 130 | // TODO: Dump the stack map's inline information? We need to know more from the caller: |
| 131 | // we need to know the number of dex registers for each inlined method. |
| 132 | } |
| 133 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 134 | void DexRegisterLocationCatalog::Dump(VariableIndentationOutputStream* vios, |
| 135 | const CodeInfo& code_info) { |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 136 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
| 137 | size_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries(encoding); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 138 | size_t location_catalog_size_in_bytes = code_info.GetDexRegisterLocationCatalogSize(encoding); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 139 | vios->Stream() |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 140 | << "DexRegisterLocationCatalog (number_of_entries=" << number_of_location_catalog_entries |
| 141 | << ", size_in_bytes=" << location_catalog_size_in_bytes << ")\n"; |
| 142 | for (size_t i = 0; i < number_of_location_catalog_entries; ++i) { |
| 143 | DexRegisterLocation location = GetDexRegisterLocation(i); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 144 | ScopedIndentation indent1(vios); |
| 145 | DumpRegisterMapping(vios->Stream(), i, location, "entry "); |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 149 | void DexRegisterMap::Dump(VariableIndentationOutputStream* vios, |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 150 | const CodeInfo& code_info, |
| 151 | uint16_t number_of_dex_registers) const { |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 152 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
| 153 | size_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries(encoding); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 154 | // TODO: Display the bit mask of live Dex registers. |
| 155 | for (size_t j = 0; j < number_of_dex_registers; ++j) { |
| 156 | if (IsDexRegisterLive(j)) { |
| 157 | size_t location_catalog_entry_index = GetLocationCatalogEntryIndex( |
| 158 | j, number_of_dex_registers, number_of_location_catalog_entries); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 159 | DexRegisterLocation location = GetDexRegisterLocation(j, |
| 160 | number_of_dex_registers, |
| 161 | code_info, |
| 162 | encoding); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 163 | ScopedIndentation indent1(vios); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 164 | DumpRegisterMapping( |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 165 | vios->Stream(), j, location, "v", |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 166 | "\t[entry " + std::to_string(static_cast<int>(location_catalog_entry_index)) + "]"); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 171 | void StackMap::Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 172 | const CodeInfo& code_info, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 173 | const CodeInfoEncoding& encoding, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 174 | uint32_t code_offset, |
| 175 | uint16_t number_of_dex_registers, |
| 176 | const std::string& header_suffix) const { |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 177 | StackMapEncoding stack_map_encoding = encoding.stack_map_encoding; |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 178 | vios->Stream() |
| 179 | << "StackMap" << header_suffix |
| 180 | << std::hex |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 181 | << " [native_pc=0x" << code_offset + GetNativePcOffset(stack_map_encoding) << "]" |
| 182 | << " (dex_pc=0x" << GetDexPc(stack_map_encoding) |
| 183 | << ", native_pc_offset=0x" << GetNativePcOffset(stack_map_encoding) |
| 184 | << ", dex_register_map_offset=0x" << GetDexRegisterMapOffset(stack_map_encoding) |
| 185 | << ", inline_info_offset=0x" << GetInlineDescriptorOffset(stack_map_encoding) |
| 186 | << ", register_mask=0x" << GetRegisterMask(stack_map_encoding) |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 187 | << std::dec |
| 188 | << ", stack_mask=0b"; |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 189 | for (size_t i = 0, e = GetNumberOfStackMaskBits(stack_map_encoding); i < e; ++i) { |
| 190 | vios->Stream() << GetStackMaskBit(stack_map_encoding, e - i - 1); |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 191 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 192 | vios->Stream() << ")\n"; |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 193 | if (HasDexRegisterMap(stack_map_encoding)) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 194 | DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf( |
| 195 | *this, encoding, number_of_dex_registers); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 196 | dex_register_map.Dump(vios, code_info, number_of_dex_registers); |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 197 | } |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 198 | if (HasInlineInfo(stack_map_encoding)) { |
Nicolas Geoffray | 12bdb72 | 2015-06-17 09:44:43 +0100 | [diff] [blame] | 199 | InlineInfo inline_info = code_info.GetInlineInfoOf(*this, encoding); |
| 200 | // We do not know the length of the dex register maps of inlined frames |
| 201 | // at this level, so we just pass null to `InlineInfo::Dump` to tell |
| 202 | // it not to look at these maps. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 203 | inline_info.Dump(vios, code_info, nullptr); |
Nicolas Geoffray | 12bdb72 | 2015-06-17 09:44:43 +0100 | [diff] [blame] | 204 | } |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 207 | void InlineInfo::Dump(VariableIndentationOutputStream* vios, |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 208 | const CodeInfo& code_info, |
| 209 | uint16_t number_of_dex_registers[]) const { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 210 | vios->Stream() << "InlineInfo with depth " << static_cast<uint32_t>(GetDepth()) << "\n"; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 211 | |
| 212 | for (size_t i = 0; i < GetDepth(); ++i) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 213 | vios->Stream() |
| 214 | << " At depth " << i |
| 215 | << std::hex |
| 216 | << " (dex_pc=0x" << GetDexPcAtDepth(i) |
| 217 | << std::dec |
| 218 | << ", method_index=" << GetMethodIndexAtDepth(i) |
| 219 | << ", invoke_type=" << static_cast<InvokeType>(GetInvokeTypeAtDepth(i)) |
| 220 | << ")\n"; |
Nicolas Geoffray | 12bdb72 | 2015-06-17 09:44:43 +0100 | [diff] [blame] | 221 | if (HasDexRegisterMapAtDepth(i) && (number_of_dex_registers != nullptr)) { |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame^] | 222 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 223 | DexRegisterMap dex_register_map = |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 224 | code_info.GetDexRegisterMapAtDepth(i, *this, encoding, number_of_dex_registers[i]); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 225 | ScopedIndentation indent1(vios); |
| 226 | dex_register_map.Dump(vios, code_info, number_of_dex_registers[i]); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 227 | } |
| 228 | } |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | } // namespace art |