blob: a7e7c21a42d12f037f08cf70d289cf4b3190fe4e [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
David Srbecky61b28a12016-02-25 21:55:03 +0000104void InlineInfoEncoding::Dump(VariableIndentationOutputStream* vios) const {
105 vios->Stream()
106 << "InlineInfoEncoding"
107 << " (method_index_bit_offset=" << static_cast<uint32_t>(kMethodIndexBitOffset)
108 << ", dex_pc_bit_offset=" << static_cast<uint32_t>(dex_pc_bit_offset_)
109 << ", invoke_type_bit_offset=" << static_cast<uint32_t>(invoke_type_bit_offset_)
110 << ", dex_register_map_bit_offset=" << static_cast<uint32_t>(dex_register_map_bit_offset_)
111 << ", total_bit_size=" << static_cast<uint32_t>(total_bit_size_)
112 << ")\n";
113}
114
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100115void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100116 uint32_t code_offset,
Roland Levillain0396ed72015-05-27 15:12:19 +0100117 uint16_t number_of_dex_registers,
118 bool dump_stack_maps) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000119 CodeInfoEncoding encoding = ExtractEncoding();
120 size_t number_of_stack_maps = GetNumberOfStackMaps(encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100121 vios->Stream()
David Srbecky09ed0982016-02-12 21:58:43 +0000122 << "Optimized CodeInfo (number_of_dex_registers=" << number_of_dex_registers
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100123 << ", number_of_stack_maps=" << number_of_stack_maps
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100124 << ")\n";
125 ScopedIndentation indent1(vios);
David Srbecky09ed0982016-02-12 21:58:43 +0000126 encoding.stack_map_encoding.Dump(vios);
David Srbecky61b28a12016-02-25 21:55:03 +0000127 if (HasInlineInfo(encoding)) {
128 encoding.inline_info_encoding.Dump(vios);
129 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000130 // Display the Dex register location catalog.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100131 GetDexRegisterLocationCatalog(encoding).Dump(vios, *this);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000132 // Display stack maps along with (live) Dex register maps.
Roland Levillain0396ed72015-05-27 15:12:19 +0100133 if (dump_stack_maps) {
134 for (size_t i = 0; i < number_of_stack_maps; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100135 StackMap stack_map = GetStackMapAt(i, encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100136 stack_map.Dump(vios,
David Brazdilf677ebf2015-05-29 16:29:43 +0100137 *this,
138 encoding,
139 code_offset,
140 number_of_dex_registers,
141 " " + std::to_string(i));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000142 }
143 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100144 // TODO: Dump the stack map's inline information? We need to know more from the caller:
145 // we need to know the number of dex registers for each inlined method.
146}
147
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100148void DexRegisterLocationCatalog::Dump(VariableIndentationOutputStream* vios,
149 const CodeInfo& code_info) {
David Srbecky09ed0982016-02-12 21:58:43 +0000150 CodeInfoEncoding encoding = code_info.ExtractEncoding();
151 size_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries(encoding);
David Brazdilf677ebf2015-05-29 16:29:43 +0100152 size_t location_catalog_size_in_bytes = code_info.GetDexRegisterLocationCatalogSize(encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100153 vios->Stream()
Roland Levillain0396ed72015-05-27 15:12:19 +0100154 << "DexRegisterLocationCatalog (number_of_entries=" << number_of_location_catalog_entries
155 << ", size_in_bytes=" << location_catalog_size_in_bytes << ")\n";
156 for (size_t i = 0; i < number_of_location_catalog_entries; ++i) {
157 DexRegisterLocation location = GetDexRegisterLocation(i);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100158 ScopedIndentation indent1(vios);
159 DumpRegisterMapping(vios->Stream(), i, location, "entry ");
Roland Levillain0396ed72015-05-27 15:12:19 +0100160 }
161}
162
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100163void DexRegisterMap::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100164 const CodeInfo& code_info,
165 uint16_t number_of_dex_registers) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000166 CodeInfoEncoding encoding = code_info.ExtractEncoding();
167 size_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries(encoding);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100168 // TODO: Display the bit mask of live Dex registers.
169 for (size_t j = 0; j < number_of_dex_registers; ++j) {
170 if (IsDexRegisterLive(j)) {
171 size_t location_catalog_entry_index = GetLocationCatalogEntryIndex(
172 j, number_of_dex_registers, number_of_location_catalog_entries);
David Brazdilf677ebf2015-05-29 16:29:43 +0100173 DexRegisterLocation location = GetDexRegisterLocation(j,
174 number_of_dex_registers,
175 code_info,
176 encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100177 ScopedIndentation indent1(vios);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100178 DumpRegisterMapping(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100179 vios->Stream(), j, location, "v",
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100180 "\t[entry " + std::to_string(static_cast<int>(location_catalog_entry_index)) + "]");
181 }
182 }
183}
184
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100185void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100186 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000187 const CodeInfoEncoding& encoding,
Roland Levillainf2650d12015-05-28 14:53:28 +0100188 uint32_t code_offset,
189 uint16_t number_of_dex_registers,
190 const std::string& header_suffix) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000191 StackMapEncoding stack_map_encoding = encoding.stack_map_encoding;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100192 vios->Stream()
193 << "StackMap" << header_suffix
194 << std::hex
David Srbecky09ed0982016-02-12 21:58:43 +0000195 << " [native_pc=0x" << code_offset + GetNativePcOffset(stack_map_encoding) << "]"
196 << " (dex_pc=0x" << GetDexPc(stack_map_encoding)
197 << ", native_pc_offset=0x" << GetNativePcOffset(stack_map_encoding)
198 << ", dex_register_map_offset=0x" << GetDexRegisterMapOffset(stack_map_encoding)
199 << ", inline_info_offset=0x" << GetInlineDescriptorOffset(stack_map_encoding)
200 << ", register_mask=0x" << GetRegisterMask(stack_map_encoding)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100201 << std::dec
202 << ", stack_mask=0b";
David Srbecky09ed0982016-02-12 21:58:43 +0000203 for (size_t i = 0, e = GetNumberOfStackMaskBits(stack_map_encoding); i < e; ++i) {
204 vios->Stream() << GetStackMaskBit(stack_map_encoding, e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100205 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100206 vios->Stream() << ")\n";
David Srbecky09ed0982016-02-12 21:58:43 +0000207 if (HasDexRegisterMap(stack_map_encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100208 DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(
209 *this, encoding, number_of_dex_registers);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100210 dex_register_map.Dump(vios, code_info, number_of_dex_registers);
Roland Levillainf2650d12015-05-28 14:53:28 +0100211 }
David Srbecky09ed0982016-02-12 21:58:43 +0000212 if (HasInlineInfo(stack_map_encoding)) {
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100213 InlineInfo inline_info = code_info.GetInlineInfoOf(*this, encoding);
214 // We do not know the length of the dex register maps of inlined frames
215 // at this level, so we just pass null to `InlineInfo::Dump` to tell
216 // it not to look at these maps.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100217 inline_info.Dump(vios, code_info, nullptr);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100218 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100219}
220
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100221void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100222 const CodeInfo& code_info,
223 uint16_t number_of_dex_registers[]) const {
David Srbecky61b28a12016-02-25 21:55:03 +0000224 InlineInfoEncoding inline_info_encoding = code_info.ExtractEncoding().inline_info_encoding;
225 vios->Stream() << "InlineInfo with depth "
226 << static_cast<uint32_t>(GetDepth(inline_info_encoding))
227 << "\n";
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100228
David Srbecky61b28a12016-02-25 21:55:03 +0000229 for (size_t i = 0; i < GetDepth(inline_info_encoding); ++i) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100230 vios->Stream()
231 << " At depth " << i
232 << std::hex
David Srbecky61b28a12016-02-25 21:55:03 +0000233 << " (dex_pc=0x" << GetDexPcAtDepth(inline_info_encoding, i)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100234 << std::dec
David Srbecky61b28a12016-02-25 21:55:03 +0000235 << ", method_index=" << GetMethodIndexAtDepth(inline_info_encoding, i)
236 << ", invoke_type=" << static_cast<InvokeType>(GetInvokeTypeAtDepth(inline_info_encoding,
237 i))
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100238 << ")\n";
David Srbecky61b28a12016-02-25 21:55:03 +0000239 if (HasDexRegisterMapAtDepth(inline_info_encoding, i) && (number_of_dex_registers != nullptr)) {
David Srbecky09ed0982016-02-12 21:58:43 +0000240 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100241 DexRegisterMap dex_register_map =
David Brazdilf677ebf2015-05-29 16:29:43 +0100242 code_info.GetDexRegisterMapAtDepth(i, *this, encoding, number_of_dex_registers[i]);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100243 ScopedIndentation indent1(vios);
244 dex_register_map.Dump(vios, code_info, number_of_dex_registers[i]);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100245 }
246 }
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000247}
248
249} // namespace art