blob: b51baf1b0cfadecc25b01da9c5280433d3f71673 [file] [log] [blame]
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001/*
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 Geoffray896f8f72015-03-30 15:44:25 +010019#include <stdint.h>
20
Roland Levillain0396ed72015-05-27 15:12:19 +010021#include "indenter.h"
Nicolas Geoffray12bdb722015-06-17 09:44:43 +010022#include "invoke_type.h"
Roland Levillain0396ed72015-05-27 15:12:19 +010023
Nicolas Geoffray004c2302015-03-20 10:06:38 +000024namespace art {
25
Roland Levillaina552e1c2015-03-26 15:01:03 +000026constexpr size_t DexRegisterLocationCatalog::kNoLocationEntryIndex;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000027constexpr uint32_t StackMap::kNoDexRegisterMap;
28constexpr uint32_t StackMap::kNoInlineInfo;
29
David Srbecky7dc11782016-02-25 13:23:56 +000030std::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 Levillain1c1da432015-07-16 11:54:44 +010055DexRegisterLocation::Kind DexRegisterMap::GetLocationInternalKind(
56 uint16_t dex_register_number,
57 uint16_t number_of_dex_registers,
58 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +000059 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +000060 DexRegisterLocationCatalog dex_register_location_catalog =
David Brazdilf677ebf2015-05-29 16:29:43 +010061 code_info.GetDexRegisterLocationCatalog(enc);
Roland Levillaina552e1c2015-03-26 15:01:03 +000062 size_t location_catalog_entry_index = GetLocationCatalogEntryIndex(
63 dex_register_number,
64 number_of_dex_registers,
David Srbecky09ed0982016-02-12 21:58:43 +000065 code_info.GetNumberOfLocationCatalogEntries(enc));
Roland Levillaina552e1c2015-03-26 15:01:03 +000066 return dex_register_location_catalog.GetLocationInternalKind(location_catalog_entry_index);
67}
68
69DexRegisterLocation DexRegisterMap::GetDexRegisterLocation(uint16_t dex_register_number,
70 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +010071 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +000072 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +000073 DexRegisterLocationCatalog dex_register_location_catalog =
David Brazdilf677ebf2015-05-29 16:29:43 +010074 code_info.GetDexRegisterLocationCatalog(enc);
Roland Levillaina552e1c2015-03-26 15:01:03 +000075 size_t location_catalog_entry_index = GetLocationCatalogEntryIndex(
76 dex_register_number,
77 number_of_dex_registers,
David Srbecky09ed0982016-02-12 21:58:43 +000078 code_info.GetNumberOfLocationCatalogEntries(enc));
Roland Levillaina552e1c2015-03-26 15:01:03 +000079 return dex_register_location_catalog.GetDexRegisterLocation(location_catalog_entry_index);
80}
81
Roland Levillaina552e1c2015-03-26 15:01:03 +000082static 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 Marko8f1e08a2015-06-26 12:06:30 +010087 os << prefix << dex_register_num << ": "
David Srbecky7dc11782016-02-25 13:23:56 +000088 << location.GetInternalKind()
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010089 << " (" << location.GetValue() << ")" << suffix << '\n';
Roland Levillain0396ed72015-05-27 15:12:19 +010090}
91
David Srbecky09ed0982016-02-12 21:58:43 +000092void 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 Marko8f1e08a2015-06-26 12:06:30 +0100104void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100105 uint32_t code_offset,
Roland Levillain0396ed72015-05-27 15:12:19 +0100106 uint16_t number_of_dex_registers,
107 bool dump_stack_maps) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000108 CodeInfoEncoding encoding = ExtractEncoding();
109 size_t number_of_stack_maps = GetNumberOfStackMaps(encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100110 vios->Stream()
David Srbecky09ed0982016-02-12 21:58:43 +0000111 << "Optimized CodeInfo (number_of_dex_registers=" << number_of_dex_registers
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100112 << ", number_of_stack_maps=" << number_of_stack_maps
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100113 << ")\n";
114 ScopedIndentation indent1(vios);
David Srbecky09ed0982016-02-12 21:58:43 +0000115 encoding.stack_map_encoding.Dump(vios);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000116 // Display the Dex register location catalog.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100117 GetDexRegisterLocationCatalog(encoding).Dump(vios, *this);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000118 // Display stack maps along with (live) Dex register maps.
Roland Levillain0396ed72015-05-27 15:12:19 +0100119 if (dump_stack_maps) {
120 for (size_t i = 0; i < number_of_stack_maps; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100121 StackMap stack_map = GetStackMapAt(i, encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100122 stack_map.Dump(vios,
David Brazdilf677ebf2015-05-29 16:29:43 +0100123 *this,
124 encoding,
125 code_offset,
126 number_of_dex_registers,
127 " " + std::to_string(i));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000128 }
129 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100130 // 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 Marko8f1e08a2015-06-26 12:06:30 +0100134void DexRegisterLocationCatalog::Dump(VariableIndentationOutputStream* vios,
135 const CodeInfo& code_info) {
David Srbecky09ed0982016-02-12 21:58:43 +0000136 CodeInfoEncoding encoding = code_info.ExtractEncoding();
137 size_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries(encoding);
David Brazdilf677ebf2015-05-29 16:29:43 +0100138 size_t location_catalog_size_in_bytes = code_info.GetDexRegisterLocationCatalogSize(encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100139 vios->Stream()
Roland Levillain0396ed72015-05-27 15:12:19 +0100140 << "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 Marko8f1e08a2015-06-26 12:06:30 +0100144 ScopedIndentation indent1(vios);
145 DumpRegisterMapping(vios->Stream(), i, location, "entry ");
Roland Levillain0396ed72015-05-27 15:12:19 +0100146 }
147}
148
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100149void DexRegisterMap::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100150 const CodeInfo& code_info,
151 uint16_t number_of_dex_registers) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000152 CodeInfoEncoding encoding = code_info.ExtractEncoding();
153 size_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries(encoding);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100154 // 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 Brazdilf677ebf2015-05-29 16:29:43 +0100159 DexRegisterLocation location = GetDexRegisterLocation(j,
160 number_of_dex_registers,
161 code_info,
162 encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100163 ScopedIndentation indent1(vios);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100164 DumpRegisterMapping(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100165 vios->Stream(), j, location, "v",
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100166 "\t[entry " + std::to_string(static_cast<int>(location_catalog_entry_index)) + "]");
167 }
168 }
169}
170
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100171void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100172 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000173 const CodeInfoEncoding& encoding,
Roland Levillainf2650d12015-05-28 14:53:28 +0100174 uint32_t code_offset,
175 uint16_t number_of_dex_registers,
176 const std::string& header_suffix) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000177 StackMapEncoding stack_map_encoding = encoding.stack_map_encoding;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100178 vios->Stream()
179 << "StackMap" << header_suffix
180 << std::hex
David Srbecky09ed0982016-02-12 21:58:43 +0000181 << " [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 Marko8f1e08a2015-06-26 12:06:30 +0100187 << std::dec
188 << ", stack_mask=0b";
David Srbecky09ed0982016-02-12 21:58:43 +0000189 for (size_t i = 0, e = GetNumberOfStackMaskBits(stack_map_encoding); i < e; ++i) {
190 vios->Stream() << GetStackMaskBit(stack_map_encoding, e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100191 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100192 vios->Stream() << ")\n";
David Srbecky09ed0982016-02-12 21:58:43 +0000193 if (HasDexRegisterMap(stack_map_encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100194 DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(
195 *this, encoding, number_of_dex_registers);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100196 dex_register_map.Dump(vios, code_info, number_of_dex_registers);
Roland Levillainf2650d12015-05-28 14:53:28 +0100197 }
David Srbecky09ed0982016-02-12 21:58:43 +0000198 if (HasInlineInfo(stack_map_encoding)) {
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100199 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 Marko8f1e08a2015-06-26 12:06:30 +0100203 inline_info.Dump(vios, code_info, nullptr);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100204 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100205}
206
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100207void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100208 const CodeInfo& code_info,
209 uint16_t number_of_dex_registers[]) const {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100210 vios->Stream() << "InlineInfo with depth " << static_cast<uint32_t>(GetDepth()) << "\n";
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100211
212 for (size_t i = 0; i < GetDepth(); ++i) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100213 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 Geoffray12bdb722015-06-17 09:44:43 +0100221 if (HasDexRegisterMapAtDepth(i) && (number_of_dex_registers != nullptr)) {
David Srbecky09ed0982016-02-12 21:58:43 +0000222 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100223 DexRegisterMap dex_register_map =
David Brazdilf677ebf2015-05-29 16:29:43 +0100224 code_info.GetDexRegisterMapAtDepth(i, *this, encoding, number_of_dex_registers[i]);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100225 ScopedIndentation indent1(vios);
226 dex_register_map.Dump(vios, code_info, number_of_dex_registers[i]);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100227 }
228 }
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000229}
230
231} // namespace art