blob: faa196f28e67b33059316c6237e3a0de66f47606 [file] [log] [blame]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001/*
2 * Copyright (C) 2014 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#ifndef ART_RUNTIME_STACK_MAP_H_
18#define ART_RUNTIME_STACK_MAP_H_
19
Andreas Gampe69489fa2017-06-08 18:03:25 -070020#include <limits>
21
David Sehr1ce2b3b2018-04-05 11:02:03 -070022#include "base/bit_memory_region.h"
David Srbecky052f8ca2018-04-26 15:42:54 +010023#include "base/bit_table.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010024#include "base/bit_utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "base/bit_vector.h"
David Sehr67bf42e2018-02-26 16:43:04 -080026#include "base/leb128.h"
David Sehr1ce2b3b2018-04-05 11:02:03 -070027#include "base/memory_region.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_file_types.h"
David Srbecky71ec1cc2018-05-18 15:57:25 +010029#include "dex_register_location.h"
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070030#include "method_info.h"
David Srbecky052f8ca2018-04-26 15:42:54 +010031#include "oat_quick_method_header.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010032
33namespace art {
34
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010035class VariableIndentationOutputStream;
36
Roland Levillaina2d8ec62015-03-12 15:25:29 +000037// Size of a frame slot, in bytes. This constant is a signed value,
38// to please the compiler in arithmetic operations involving int32_t
39// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000040static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000041
David Srbecky6de88332018-06-03 12:00:11 +010042// The delta compression of dex register maps means we need to scan the stackmaps backwards.
43// We compress the data in such a way so that there is an upper bound on the search distance.
44// Max distance 0 means each stack map must be fully defined and no scanning back is allowed.
45// If this value is changed, the oat file version should be incremented (for DCHECK to pass).
46static constexpr size_t kMaxDexRegisterMapSearchDistance = 32;
47
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000048class ArtMethod;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000049class CodeInfo;
David Srbecky86decb62018-06-05 06:41:10 +010050class Stats;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000051
David Srbecky71ec1cc2018-05-18 15:57:25 +010052std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation& reg);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010053
David Srbecky71ec1cc2018-05-18 15:57:25 +010054// Information on Dex register locations for a specific PC.
55// Effectively just a convenience wrapper for DexRegisterLocation vector.
56// If the size is small enough, it keeps the data on the stack.
Roland Levillaina552e1c2015-03-26 15:01:03 +000057class DexRegisterMap {
58 public:
David Srbecky6de88332018-06-03 12:00:11 +010059 using iterator = DexRegisterLocation*;
60
61 // Create map for given number of registers and initialize them to the given value.
62 DexRegisterMap(size_t count, DexRegisterLocation value) : count_(count), regs_small_{} {
David Srbecky71ec1cc2018-05-18 15:57:25 +010063 if (count_ <= kSmallCount) {
David Srbecky6de88332018-06-03 12:00:11 +010064 std::fill_n(regs_small_.begin(), count, value);
David Srbecky71ec1cc2018-05-18 15:57:25 +010065 } else {
David Srbecky6de88332018-06-03 12:00:11 +010066 regs_large_.resize(count, value);
David Srbecky71ec1cc2018-05-18 15:57:25 +010067 }
Roland Levillaina552e1c2015-03-26 15:01:03 +000068 }
69
David Srbecky71ec1cc2018-05-18 15:57:25 +010070 DexRegisterLocation* data() {
71 return count_ <= kSmallCount ? regs_small_.data() : regs_large_.data();
72 }
Roland Levillaina552e1c2015-03-26 15:01:03 +000073
David Srbecky6de88332018-06-03 12:00:11 +010074 iterator begin() { return data(); }
75 iterator end() { return data() + count_; }
76
David Srbecky71ec1cc2018-05-18 15:57:25 +010077 size_t size() const { return count_; }
78
David Srbeckyfd89b072018-06-03 12:00:22 +010079 bool empty() const { return count_ == 0; }
David Srbecky71ec1cc2018-05-18 15:57:25 +010080
81 DexRegisterLocation Get(size_t index) const {
82 DCHECK_LT(index, count_);
83 return count_ <= kSmallCount ? regs_small_[index] : regs_large_[index];
84 }
85
86 DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number) const {
87 return Get(dex_register_number).GetKind();
88 }
89
90 // TODO: Remove.
91 DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number) const {
92 return Get(dex_register_number).GetKind();
93 }
94
95 DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number) const {
96 return Get(dex_register_number);
97 }
Roland Levillaina552e1c2015-03-26 15:01:03 +000098
David Srbecky21d45b42018-05-30 06:35:05 +010099 int32_t GetStackOffsetInBytes(uint16_t dex_register_number) const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100100 DexRegisterLocation location = Get(dex_register_number);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000101 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000102 return location.GetValue();
103 }
104
David Srbecky21d45b42018-05-30 06:35:05 +0100105 int32_t GetConstant(uint16_t dex_register_number) const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100106 DexRegisterLocation location = Get(dex_register_number);
107 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kConstant);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000108 return location.GetValue();
109 }
110
David Srbecky21d45b42018-05-30 06:35:05 +0100111 int32_t GetMachineRegister(uint16_t dex_register_number) const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100112 DexRegisterLocation location = Get(dex_register_number);
113 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInRegister ||
114 location.GetKind() == DexRegisterLocation::Kind::kInRegisterHigh ||
115 location.GetKind() == DexRegisterLocation::Kind::kInFpuRegister ||
116 location.GetKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000117 return location.GetValue();
118 }
119
Mingyao Yang01b47b02017-02-03 12:09:57 -0800120 ALWAYS_INLINE bool IsDexRegisterLive(uint16_t dex_register_number) const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100121 return Get(dex_register_number).IsLive();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000122 }
123
David Srbecky71ec1cc2018-05-18 15:57:25 +0100124 size_t GetNumberOfLiveDexRegisters() const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000125 size_t number_of_live_dex_registers = 0;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100126 for (size_t i = 0; i < count_; ++i) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000127 if (IsDexRegisterLive(i)) {
128 ++number_of_live_dex_registers;
129 }
130 }
131 return number_of_live_dex_registers;
132 }
133
David Srbecky71ec1cc2018-05-18 15:57:25 +0100134 bool HasAnyLiveDexRegisters() const {
135 for (size_t i = 0; i < count_; ++i) {
136 if (IsDexRegisterLive(i)) {
137 return true;
138 }
139 }
140 return false;
David Srbecky21d45b42018-05-30 06:35:05 +0100141 }
142
Roland Levillaina552e1c2015-03-26 15:01:03 +0000143 private:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100144 // Store the data inline if the number of registers is small to avoid memory allocations.
145 // If count_ <= kSmallCount, we use the regs_small_ array, and regs_large_ otherwise.
146 static constexpr size_t kSmallCount = 16;
147 size_t count_;
148 std::array<DexRegisterLocation, kSmallCount> regs_small_;
149 dchecked_vector<DexRegisterLocation> regs_large_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000150};
151
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100152/**
153 * A Stack Map holds compilation information for a specific PC necessary for:
154 * - Mapping it to a dex PC,
155 * - Knowing which stack entries are objects,
156 * - Knowing which registers hold objects,
157 * - Knowing the inlining information,
158 * - Knowing the values of dex registers.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100159 */
David Srbecky50fac062018-06-13 18:55:35 +0100160class StackMap : public BitTable<8>::Accessor {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100161 public:
David Srbecky50fac062018-06-13 18:55:35 +0100162 enum Kind {
163 Default = -1,
164 Catch = 0,
165 OSR = 1,
166 Debug = 2,
167 };
David Srbeckyd97e0822018-06-03 12:00:24 +0100168 BIT_TABLE_HEADER()
David Srbecky50fac062018-06-13 18:55:35 +0100169 BIT_TABLE_COLUMN(0, Kind)
170 BIT_TABLE_COLUMN(1, PackedNativePc)
171 BIT_TABLE_COLUMN(2, DexPc)
172 BIT_TABLE_COLUMN(3, RegisterMaskIndex)
173 BIT_TABLE_COLUMN(4, StackMaskIndex)
174 BIT_TABLE_COLUMN(5, InlineInfoIndex)
175 BIT_TABLE_COLUMN(6, DexRegisterMaskIndex)
176 BIT_TABLE_COLUMN(7, DexRegisterMapIndex)
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100177
David Srbecky052f8ca2018-04-26 15:42:54 +0100178 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100179 return UnpackNativePc(Get<kPackedNativePc>(), instruction_set);
David Brazdilf677ebf2015-05-29 16:29:43 +0100180 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100181
David Srbeckyd97e0822018-06-03 12:00:24 +0100182 ALWAYS_INLINE bool HasInlineInfo() const {
183 return HasInlineInfoIndex();
184 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100185
David Srbeckyd97e0822018-06-03 12:00:24 +0100186 ALWAYS_INLINE bool HasDexRegisterMap() const {
187 return HasDexRegisterMapIndex();
188 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100189
David Srbeckyd02b23f2018-05-29 23:27:22 +0100190 static uint32_t PackNativePc(uint32_t native_pc, InstructionSet isa) {
David Srbeckyd775f962018-05-30 18:12:52 +0100191 DCHECK_ALIGNED_PARAM(native_pc, GetInstructionSetInstructionAlignment(isa));
David Srbeckyd02b23f2018-05-29 23:27:22 +0100192 return native_pc / GetInstructionSetInstructionAlignment(isa);
193 }
194
195 static uint32_t UnpackNativePc(uint32_t packed_native_pc, InstructionSet isa) {
196 uint32_t native_pc = packed_native_pc * GetInstructionSetInstructionAlignment(isa);
197 DCHECK_EQ(native_pc / GetInstructionSetInstructionAlignment(isa), packed_native_pc);
198 return native_pc;
199 }
200
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100201 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100202 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700203 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100204 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100205 InstructionSet instruction_set) const;
David Srbecky61b28a12016-02-25 21:55:03 +0000206};
207
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100208/**
David Srbecky052f8ca2018-04-26 15:42:54 +0100209 * Inline information for a specific PC.
210 * The row referenced from the StackMap holds information at depth 0.
211 * Following rows hold information for further depths.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100212 */
David Srbecky6de88332018-06-03 12:00:11 +0100213class InlineInfo : public BitTable<6>::Accessor {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100214 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100215 BIT_TABLE_HEADER()
216 BIT_TABLE_COLUMN(0, IsLast) // Determines if there are further rows for further depths.
217 BIT_TABLE_COLUMN(1, DexPc)
218 BIT_TABLE_COLUMN(2, MethodInfoIndex)
219 BIT_TABLE_COLUMN(3, ArtMethodHi) // High bits of ArtMethod*.
220 BIT_TABLE_COLUMN(4, ArtMethodLo) // Low bits of ArtMethod*.
David Srbecky6de88332018-06-03 12:00:11 +0100221 BIT_TABLE_COLUMN(5, NumberOfDexRegisters) // Includes outer levels and the main method.
David Srbeckyd97e0822018-06-03 12:00:24 +0100222 BIT_TABLE_COLUMN(6, DexRegisterMapIndex)
223
David Srbecky052f8ca2018-04-26 15:42:54 +0100224 static constexpr uint32_t kLast = -1;
225 static constexpr uint32_t kMore = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100226
David Srbecky6e69e522018-06-03 12:00:14 +0100227 uint32_t GetMethodIndex(const MethodInfo& method_info) const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100228 return method_info.GetMethodIndex(GetMethodInfoIndex());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100229 }
230
David Srbecky6e69e522018-06-03 12:00:14 +0100231 bool EncodesArtMethod() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100232 return HasArtMethodLo();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100233 }
234
David Srbecky6e69e522018-06-03 12:00:14 +0100235 ArtMethod* GetArtMethod() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100236 uint64_t lo = GetArtMethodLo();
237 uint64_t hi = GetArtMethodHi();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100238 return reinterpret_cast<ArtMethod*>((hi << 32) | lo);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100239 }
240
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100241 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +0000242 const CodeInfo& info,
David Srbecky6e69e522018-06-03 12:00:14 +0100243 const StackMap& stack_map,
David Srbeckyfd89b072018-06-03 12:00:22 +0100244 const MethodInfo& method_info) const;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800245};
246
David Srbecky052f8ca2018-04-26 15:42:54 +0100247class InvokeInfo : public BitTable<3>::Accessor {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800248 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100249 BIT_TABLE_HEADER()
250 BIT_TABLE_COLUMN(0, PackedNativePc)
251 BIT_TABLE_COLUMN(1, InvokeType)
252 BIT_TABLE_COLUMN(2, MethodInfoIndex)
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800253
David Srbecky052f8ca2018-04-26 15:42:54 +0100254 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100255 return StackMap::UnpackNativePc(Get<kPackedNativePc>(), instruction_set);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800256 }
257
David Srbecky052f8ca2018-04-26 15:42:54 +0100258 uint32_t GetMethodIndex(MethodInfo method_info) const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100259 return method_info.GetMethodIndex(GetMethodInfoIndex());
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800260 }
David Srbecky09ed0982016-02-12 21:58:43 +0000261};
262
David Srbecky86decb62018-06-05 06:41:10 +0100263class MaskInfo : public BitTable<1>::Accessor {
264 public:
265 BIT_TABLE_HEADER()
266 BIT_TABLE_COLUMN(0, Mask)
267};
268
269class DexRegisterMapInfo : public BitTable<1>::Accessor {
270 public:
271 BIT_TABLE_HEADER()
272 BIT_TABLE_COLUMN(0, CatalogueIndex)
273};
274
David Srbecky71ec1cc2018-05-18 15:57:25 +0100275class DexRegisterInfo : public BitTable<2>::Accessor {
276 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100277 BIT_TABLE_HEADER()
278 BIT_TABLE_COLUMN(0, Kind)
279 BIT_TABLE_COLUMN(1, PackedValue)
David Srbecky71ec1cc2018-05-18 15:57:25 +0100280
281 ALWAYS_INLINE DexRegisterLocation GetLocation() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100282 DexRegisterLocation::Kind kind = static_cast<DexRegisterLocation::Kind>(GetKind());
283 return DexRegisterLocation(kind, UnpackValue(kind, GetPackedValue()));
David Srbecky71ec1cc2018-05-18 15:57:25 +0100284 }
285
286 static uint32_t PackValue(DexRegisterLocation::Kind kind, uint32_t value) {
287 uint32_t packed_value = value;
288 if (kind == DexRegisterLocation::Kind::kInStack) {
289 DCHECK(IsAligned<kFrameSlotSize>(packed_value));
290 packed_value /= kFrameSlotSize;
291 }
292 return packed_value;
293 }
294
295 static uint32_t UnpackValue(DexRegisterLocation::Kind kind, uint32_t packed_value) {
296 uint32_t value = packed_value;
297 if (kind == DexRegisterLocation::Kind::kInStack) {
298 value *= kFrameSlotSize;
299 }
300 return value;
301 }
302};
303
David Srbecky4b59d102018-05-29 21:46:10 +0000304// Register masks tend to have many trailing zero bits (caller-saves are usually not encoded),
305// therefore it is worth encoding the mask as value+shift.
306class RegisterMask : public BitTable<2>::Accessor {
307 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100308 BIT_TABLE_HEADER()
309 BIT_TABLE_COLUMN(0, Value)
310 BIT_TABLE_COLUMN(1, Shift)
David Srbecky4b59d102018-05-29 21:46:10 +0000311
312 ALWAYS_INLINE uint32_t GetMask() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100313 return GetValue() << GetShift();
David Srbecky4b59d102018-05-29 21:46:10 +0000314 }
315};
316
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100317/**
318 * Wrapper around all compiler information collected for a method.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100319 * See the Decode method at the end for the precise binary format.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100320 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100321class CodeInfo {
322 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100323 explicit CodeInfo(const void* data) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100324 Decode(reinterpret_cast<const uint8_t*>(data));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100325 }
326
David Srbecky052f8ca2018-04-26 15:42:54 +0100327 explicit CodeInfo(MemoryRegion region) : CodeInfo(region.begin()) {
328 DCHECK_EQ(size_, region.size());
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100329 }
330
David Srbecky052f8ca2018-04-26 15:42:54 +0100331 explicit CodeInfo(const OatQuickMethodHeader* header)
332 : CodeInfo(header->GetOptimizedCodeInfoPtr()) {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100333 }
334
David Srbecky052f8ca2018-04-26 15:42:54 +0100335 size_t Size() const {
336 return size_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000337 }
338
David Srbecky052f8ca2018-04-26 15:42:54 +0100339 bool HasInlineInfo() const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100340 return inline_infos_.NumRows() > 0;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100341 }
342
David Srbecky052f8ca2018-04-26 15:42:54 +0100343 ALWAYS_INLINE StackMap GetStackMapAt(size_t index) const {
344 return StackMap(&stack_maps_, index);
David Srbecky45aa5982016-03-18 02:15:09 +0000345 }
346
David Srbecky052f8ca2018-04-26 15:42:54 +0100347 BitMemoryRegion GetStackMask(size_t index) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000348 return stack_masks_.GetBitMemoryRegion(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800349 }
350
David Srbecky052f8ca2018-04-26 15:42:54 +0100351 BitMemoryRegion GetStackMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000352 uint32_t index = stack_map.GetStackMaskIndex();
353 return (index == StackMap::kNoValue) ? BitMemoryRegion() : GetStackMask(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800354 }
355
David Srbecky052f8ca2018-04-26 15:42:54 +0100356 uint32_t GetRegisterMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000357 uint32_t index = stack_map.GetRegisterMaskIndex();
358 return (index == StackMap::kNoValue) ? 0 : RegisterMask(&register_masks_, index).GetMask();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100359 }
360
David Srbecky052f8ca2018-04-26 15:42:54 +0100361 uint32_t GetNumberOfLocationCatalogEntries() const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100362 return dex_register_catalog_.NumRows();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000363 }
364
David Srbecky71ec1cc2018-05-18 15:57:25 +0100365 ALWAYS_INLINE DexRegisterLocation GetDexRegisterCatalogEntry(size_t index) const {
David Srbecky6de88332018-06-03 12:00:11 +0100366 return (index == StackMap::kNoValue)
367 ? DexRegisterLocation::None()
368 : DexRegisterInfo(&dex_register_catalog_, index).GetLocation();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100369 }
370
David Srbecky052f8ca2018-04-26 15:42:54 +0100371 uint32_t GetNumberOfStackMaps() const {
372 return stack_maps_.NumRows();
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100373 }
374
David Srbecky052f8ca2018-04-26 15:42:54 +0100375 InvokeInfo GetInvokeInfo(size_t index) const {
376 return InvokeInfo(&invoke_infos_, index);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800377 }
378
David Srbeckyfd89b072018-06-03 12:00:22 +0100379 ALWAYS_INLINE DexRegisterMap GetDexRegisterMapOf(StackMap stack_map) const {
David Srbecky6de88332018-06-03 12:00:11 +0100380 if (stack_map.HasDexRegisterMap()) {
381 DexRegisterMap map(number_of_dex_registers_, DexRegisterLocation::Invalid());
382 DecodeDexRegisterMap(stack_map.Row(), /* first_dex_register */ 0, &map);
383 return map;
384 }
385 return DexRegisterMap(0, DexRegisterLocation::None());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100386 }
387
David Srbeckyfd89b072018-06-03 12:00:22 +0100388 ALWAYS_INLINE DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth, StackMap stack_map) const {
David Srbecky6de88332018-06-03 12:00:11 +0100389 if (stack_map.HasDexRegisterMap()) {
390 // The register counts are commutative and include all outer levels.
391 // This allows us to determine the range [first, last) in just two lookups.
392 // If we are at depth 0 (the first inlinee), the count from the main method is used.
393 uint32_t first = (depth == 0) ? number_of_dex_registers_
394 : GetInlineInfoAtDepth(stack_map, depth - 1).GetNumberOfDexRegisters();
395 uint32_t last = GetInlineInfoAtDepth(stack_map, depth).GetNumberOfDexRegisters();
396 DexRegisterMap map(last - first, DexRegisterLocation::Invalid());
397 DecodeDexRegisterMap(stack_map.Row(), first, &map);
398 return map;
399 }
400 return DexRegisterMap(0, DexRegisterLocation::None());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100401 }
402
David Srbecky052f8ca2018-04-26 15:42:54 +0100403 InlineInfo GetInlineInfo(size_t index) const {
404 return InlineInfo(&inline_infos_, index);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800405 }
406
David Srbecky6e69e522018-06-03 12:00:14 +0100407 uint32_t GetInlineDepthOf(StackMap stack_map) const {
408 uint32_t depth = 0;
David Srbecky052f8ca2018-04-26 15:42:54 +0100409 uint32_t index = stack_map.GetInlineInfoIndex();
David Srbecky6e69e522018-06-03 12:00:14 +0100410 if (index != StackMap::kNoValue) {
411 while (GetInlineInfo(index + depth++).GetIsLast() == InlineInfo::kMore) { }
412 }
413 return depth;
414 }
415
416 InlineInfo GetInlineInfoAtDepth(StackMap stack_map, uint32_t depth) const {
417 DCHECK(stack_map.HasInlineInfo());
418 DCHECK_LT(depth, GetInlineDepthOf(stack_map));
419 return GetInlineInfo(stack_map.GetInlineInfoIndex() + depth);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100420 }
421
David Srbecky052f8ca2018-04-26 15:42:54 +0100422 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
423 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
424 StackMap stack_map = GetStackMapAt(i);
David Srbecky50fac062018-06-13 18:55:35 +0100425 if (stack_map.GetDexPc() == dex_pc && stack_map.GetKind() != StackMap::Kind::Debug) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100426 return stack_map;
427 }
428 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100429 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100430 }
431
David Srbecky50fac062018-06-13 18:55:35 +0100432 // Searches the stack map list backwards because catch stack maps are stored at the end.
David Srbecky052f8ca2018-04-26 15:42:54 +0100433 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc) const {
434 for (size_t i = GetNumberOfStackMaps(); i > 0; --i) {
435 StackMap stack_map = GetStackMapAt(i - 1);
David Srbecky50fac062018-06-13 18:55:35 +0100436 if (stack_map.GetDexPc() == dex_pc && stack_map.GetKind() == StackMap::Kind::Catch) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000437 return stack_map;
438 }
439 }
440 return StackMap();
441 }
442
David Srbecky052f8ca2018-04-26 15:42:54 +0100443 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc) const {
David Srbecky50fac062018-06-13 18:55:35 +0100444 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100445 StackMap stack_map = GetStackMapAt(i);
David Srbecky50fac062018-06-13 18:55:35 +0100446 if (stack_map.GetDexPc() == dex_pc && stack_map.GetKind() == StackMap::Kind::OSR) {
447 return stack_map;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000448 }
449 }
450 return StackMap();
451 }
452
David Srbecky50fac062018-06-13 18:55:35 +0100453 StackMap GetStackMapForNativePcOffset(uint32_t pc, InstructionSet isa = kRuntimeISA) const {
David Brazdil77a48ae2015-09-15 12:34:04 +0000454 // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack
455 // maps are not. If we knew that the method does not have try/catch,
456 // we could do binary search.
David Srbecky052f8ca2018-04-26 15:42:54 +0100457 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
458 StackMap stack_map = GetStackMapAt(i);
David Srbecky50fac062018-06-13 18:55:35 +0100459 if (stack_map.GetNativePcOffset(isa) == pc) {
460 StackMap::Kind kind = static_cast<StackMap::Kind>(stack_map.GetKind());
461 if (kind == StackMap::Kind::Default || kind == StackMap::Kind::OSR) {
462 return stack_map;
463 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100464 }
465 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100466 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100467 }
468
David Srbecky052f8ca2018-04-26 15:42:54 +0100469 InvokeInfo GetInvokeInfoForNativePcOffset(uint32_t native_pc_offset) {
470 for (size_t index = 0; index < invoke_infos_.NumRows(); index++) {
471 InvokeInfo item = GetInvokeInfo(index);
472 if (item.GetNativePcOffset(kRuntimeISA) == native_pc_offset) {
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800473 return item;
474 }
475 }
David Srbeckyd97e0822018-06-03 12:00:24 +0100476 return InvokeInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800477 }
478
David Srbecky71ec1cc2018-05-18 15:57:25 +0100479 // Dump this CodeInfo object on `vios`.
480 // `code_offset` is the (absolute) native PC of the compiled method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100481 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100482 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100483 bool verbose,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700484 InstructionSet instruction_set,
485 const MethodInfo& method_info) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000486
David Srbecky86decb62018-06-05 06:41:10 +0100487 // Accumulate code info size statistics into the given Stats tree.
488 void AddSizeStats(/*out*/ Stats* parent) const;
489
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100490 private:
David Srbecky6de88332018-06-03 12:00:11 +0100491 // Scan backward to determine dex register locations at given stack map.
492 void DecodeDexRegisterMap(uint32_t stack_map_index,
493 uint32_t first_dex_register,
494 /*out*/ DexRegisterMap* map) const;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000495
David Srbecky052f8ca2018-04-26 15:42:54 +0100496 void Decode(const uint8_t* data) {
497 size_t non_header_size = DecodeUnsignedLeb128(&data);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100498 BitMemoryRegion region(MemoryRegion(const_cast<uint8_t*>(data), non_header_size));
David Srbecky052f8ca2018-04-26 15:42:54 +0100499 size_t bit_offset = 0;
500 size_ = UnsignedLeb128Size(non_header_size) + non_header_size;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100501 stack_maps_.Decode(region, &bit_offset);
502 register_masks_.Decode(region, &bit_offset);
503 stack_masks_.Decode(region, &bit_offset);
504 invoke_infos_.Decode(region, &bit_offset);
505 inline_infos_.Decode(region, &bit_offset);
506 dex_register_masks_.Decode(region, &bit_offset);
507 dex_register_maps_.Decode(region, &bit_offset);
508 dex_register_catalog_.Decode(region, &bit_offset);
David Srbecky6de88332018-06-03 12:00:11 +0100509 number_of_dex_registers_ = DecodeVarintBits(region, &bit_offset);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100510 CHECK_EQ(non_header_size, BitsToBytesRoundUp(bit_offset)) << "Invalid CodeInfo";
David Srbecky052f8ca2018-04-26 15:42:54 +0100511 }
512
513 size_t size_;
David Srbeckyd97e0822018-06-03 12:00:24 +0100514 BitTable<StackMap::kCount> stack_maps_;
515 BitTable<RegisterMask::kCount> register_masks_;
David Srbecky86decb62018-06-05 06:41:10 +0100516 BitTable<MaskInfo::kCount> stack_masks_;
David Srbeckyd97e0822018-06-03 12:00:24 +0100517 BitTable<InvokeInfo::kCount> invoke_infos_;
518 BitTable<InlineInfo::kCount> inline_infos_;
David Srbecky86decb62018-06-05 06:41:10 +0100519 BitTable<MaskInfo::kCount> dex_register_masks_;
520 BitTable<DexRegisterMapInfo::kCount> dex_register_maps_;
David Srbeckyd97e0822018-06-03 12:00:24 +0100521 BitTable<DexRegisterInfo::kCount> dex_register_catalog_;
David Srbecky6de88332018-06-03 12:00:11 +0100522 uint32_t number_of_dex_registers_; // Excludes any inlined methods.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100523};
524
Roland Levillain1c1da432015-07-16 11:54:44 +0100525#undef ELEMENT_BYTE_OFFSET_AFTER
526#undef ELEMENT_BIT_OFFSET_AFTER
527
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100528} // namespace art
529
530#endif // ART_RUNTIME_STACK_MAP_H_