blob: 6da002138c004d9d0721396fad7bd2d90afa499c [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
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000042class ArtMethod;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000043class CodeInfo;
44
David Srbecky71ec1cc2018-05-18 15:57:25 +010045std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation& reg);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010046
David Srbecky71ec1cc2018-05-18 15:57:25 +010047// Information on Dex register locations for a specific PC.
48// Effectively just a convenience wrapper for DexRegisterLocation vector.
49// If the size is small enough, it keeps the data on the stack.
Roland Levillaina552e1c2015-03-26 15:01:03 +000050class DexRegisterMap {
51 public:
David Srbecky71ec1cc2018-05-18 15:57:25 +010052 // Create map for given number of registers and initialize all locations to None.
53 explicit DexRegisterMap(size_t count) : count_(count), regs_small_{} {
54 if (count_ <= kSmallCount) {
55 std::fill_n(regs_small_.begin(), count, DexRegisterLocation::None());
56 } else {
57 regs_large_.resize(count, DexRegisterLocation::None());
58 }
Roland Levillaina552e1c2015-03-26 15:01:03 +000059 }
60
David Srbecky71ec1cc2018-05-18 15:57:25 +010061 DexRegisterLocation* data() {
62 return count_ <= kSmallCount ? regs_small_.data() : regs_large_.data();
63 }
Roland Levillaina552e1c2015-03-26 15:01:03 +000064
David Srbecky71ec1cc2018-05-18 15:57:25 +010065 size_t size() const { return count_; }
66
67 bool IsValid() const { return count_ != 0; }
68
69 DexRegisterLocation Get(size_t index) const {
70 DCHECK_LT(index, count_);
71 return count_ <= kSmallCount ? regs_small_[index] : regs_large_[index];
72 }
73
74 DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number) const {
75 return Get(dex_register_number).GetKind();
76 }
77
78 // TODO: Remove.
79 DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number) const {
80 return Get(dex_register_number).GetKind();
81 }
82
83 DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number) const {
84 return Get(dex_register_number);
85 }
Roland Levillaina552e1c2015-03-26 15:01:03 +000086
David Srbecky21d45b42018-05-30 06:35:05 +010087 int32_t GetStackOffsetInBytes(uint16_t dex_register_number) const {
David Srbecky71ec1cc2018-05-18 15:57:25 +010088 DexRegisterLocation location = Get(dex_register_number);
Roland Levillaina552e1c2015-03-26 15:01:03 +000089 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
Roland Levillaina552e1c2015-03-26 15:01:03 +000090 return location.GetValue();
91 }
92
David Srbecky21d45b42018-05-30 06:35:05 +010093 int32_t GetConstant(uint16_t dex_register_number) const {
David Srbecky71ec1cc2018-05-18 15:57:25 +010094 DexRegisterLocation location = Get(dex_register_number);
95 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kConstant);
Roland Levillaina552e1c2015-03-26 15:01:03 +000096 return location.GetValue();
97 }
98
David Srbecky21d45b42018-05-30 06:35:05 +010099 int32_t GetMachineRegister(uint16_t dex_register_number) const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100100 DexRegisterLocation location = Get(dex_register_number);
101 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInRegister ||
102 location.GetKind() == DexRegisterLocation::Kind::kInRegisterHigh ||
103 location.GetKind() == DexRegisterLocation::Kind::kInFpuRegister ||
104 location.GetKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000105 return location.GetValue();
106 }
107
Mingyao Yang01b47b02017-02-03 12:09:57 -0800108 ALWAYS_INLINE bool IsDexRegisterLive(uint16_t dex_register_number) const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100109 return Get(dex_register_number).IsLive();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000110 }
111
David Srbecky71ec1cc2018-05-18 15:57:25 +0100112 size_t GetNumberOfLiveDexRegisters() const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000113 size_t number_of_live_dex_registers = 0;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100114 for (size_t i = 0; i < count_; ++i) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000115 if (IsDexRegisterLive(i)) {
116 ++number_of_live_dex_registers;
117 }
118 }
119 return number_of_live_dex_registers;
120 }
121
David Srbecky71ec1cc2018-05-18 15:57:25 +0100122 bool HasAnyLiveDexRegisters() const {
123 for (size_t i = 0; i < count_; ++i) {
124 if (IsDexRegisterLive(i)) {
125 return true;
126 }
127 }
128 return false;
David Srbecky21d45b42018-05-30 06:35:05 +0100129 }
130
Roland Levillaina552e1c2015-03-26 15:01:03 +0000131 private:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100132 // Store the data inline if the number of registers is small to avoid memory allocations.
133 // If count_ <= kSmallCount, we use the regs_small_ array, and regs_large_ otherwise.
134 static constexpr size_t kSmallCount = 16;
135 size_t count_;
136 std::array<DexRegisterLocation, kSmallCount> regs_small_;
137 dchecked_vector<DexRegisterLocation> regs_large_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000138};
139
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100140/**
141 * A Stack Map holds compilation information for a specific PC necessary for:
142 * - Mapping it to a dex PC,
143 * - Knowing which stack entries are objects,
144 * - Knowing which registers hold objects,
145 * - Knowing the inlining information,
146 * - Knowing the values of dex registers.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100147 */
David Srbecky71ec1cc2018-05-18 15:57:25 +0100148class StackMap : public BitTable<7>::Accessor {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100149 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100150 BIT_TABLE_HEADER()
151 BIT_TABLE_COLUMN(0, PackedNativePc)
152 BIT_TABLE_COLUMN(1, DexPc)
153 BIT_TABLE_COLUMN(2, RegisterMaskIndex)
154 BIT_TABLE_COLUMN(3, StackMaskIndex)
155 BIT_TABLE_COLUMN(4, InlineInfoIndex)
156 BIT_TABLE_COLUMN(5, DexRegisterMaskIndex)
157 BIT_TABLE_COLUMN(6, DexRegisterMapIndex)
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100158
David Srbecky052f8ca2018-04-26 15:42:54 +0100159 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100160 return UnpackNativePc(Get<kPackedNativePc>(), instruction_set);
David Brazdilf677ebf2015-05-29 16:29:43 +0100161 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100162
David Srbeckyd97e0822018-06-03 12:00:24 +0100163 ALWAYS_INLINE bool HasInlineInfo() const {
164 return HasInlineInfoIndex();
165 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100166
David Srbeckyd97e0822018-06-03 12:00:24 +0100167 ALWAYS_INLINE bool HasDexRegisterMap() const {
168 return HasDexRegisterMapIndex();
169 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100170
David Srbeckyd02b23f2018-05-29 23:27:22 +0100171 static uint32_t PackNativePc(uint32_t native_pc, InstructionSet isa) {
David Srbeckyd775f962018-05-30 18:12:52 +0100172 DCHECK_ALIGNED_PARAM(native_pc, GetInstructionSetInstructionAlignment(isa));
David Srbeckyd02b23f2018-05-29 23:27:22 +0100173 return native_pc / GetInstructionSetInstructionAlignment(isa);
174 }
175
176 static uint32_t UnpackNativePc(uint32_t packed_native_pc, InstructionSet isa) {
177 uint32_t native_pc = packed_native_pc * GetInstructionSetInstructionAlignment(isa);
178 DCHECK_EQ(native_pc / GetInstructionSetInstructionAlignment(isa), packed_native_pc);
179 return native_pc;
180 }
181
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100182 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100183 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700184 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100185 uint32_t code_offset,
186 uint16_t number_of_dex_registers,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100187 InstructionSet instruction_set) const;
David Srbecky61b28a12016-02-25 21:55:03 +0000188};
189
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100190/**
David Srbecky052f8ca2018-04-26 15:42:54 +0100191 * Inline information for a specific PC.
192 * The row referenced from the StackMap holds information at depth 0.
193 * Following rows hold information for further depths.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100194 */
David Srbecky71ec1cc2018-05-18 15:57:25 +0100195class InlineInfo : public BitTable<7>::Accessor {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100196 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100197 BIT_TABLE_HEADER()
198 BIT_TABLE_COLUMN(0, IsLast) // Determines if there are further rows for further depths.
199 BIT_TABLE_COLUMN(1, DexPc)
200 BIT_TABLE_COLUMN(2, MethodInfoIndex)
201 BIT_TABLE_COLUMN(3, ArtMethodHi) // High bits of ArtMethod*.
202 BIT_TABLE_COLUMN(4, ArtMethodLo) // Low bits of ArtMethod*.
203 BIT_TABLE_COLUMN(5, DexRegisterMaskIndex)
204 BIT_TABLE_COLUMN(6, DexRegisterMapIndex)
205
David Srbecky052f8ca2018-04-26 15:42:54 +0100206 static constexpr uint32_t kLast = -1;
207 static constexpr uint32_t kMore = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100208
David Srbecky6e69e522018-06-03 12:00:14 +0100209 uint32_t GetMethodIndex(const MethodInfo& method_info) const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100210 return method_info.GetMethodIndex(GetMethodInfoIndex());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100211 }
212
David Srbecky6e69e522018-06-03 12:00:14 +0100213 bool EncodesArtMethod() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100214 return HasArtMethodLo();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100215 }
216
David Srbecky6e69e522018-06-03 12:00:14 +0100217 ArtMethod* GetArtMethod() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100218 uint64_t lo = GetArtMethodLo();
219 uint64_t hi = GetArtMethodHi();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100220 return reinterpret_cast<ArtMethod*>((hi << 32) | lo);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100221 }
222
David Srbeckyd97e0822018-06-03 12:00:24 +0100223 ALWAYS_INLINE bool HasDexRegisterMap() const {
224 return HasDexRegisterMapIndex();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100225 }
226
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100227 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +0000228 const CodeInfo& info,
David Srbecky6e69e522018-06-03 12:00:14 +0100229 const StackMap& stack_map,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700230 const MethodInfo& method_info,
David Srbecky6e69e522018-06-03 12:00:14 +0100231 uint16_t number_of_dex_registers) const;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800232};
233
David Srbecky052f8ca2018-04-26 15:42:54 +0100234class InvokeInfo : public BitTable<3>::Accessor {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800235 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100236 BIT_TABLE_HEADER()
237 BIT_TABLE_COLUMN(0, PackedNativePc)
238 BIT_TABLE_COLUMN(1, InvokeType)
239 BIT_TABLE_COLUMN(2, MethodInfoIndex)
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800240
David Srbecky052f8ca2018-04-26 15:42:54 +0100241 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100242 return StackMap::UnpackNativePc(Get<kPackedNativePc>(), instruction_set);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800243 }
244
David Srbecky052f8ca2018-04-26 15:42:54 +0100245 uint32_t GetMethodIndex(MethodInfo method_info) const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100246 return method_info.GetMethodIndex(GetMethodInfoIndex());
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800247 }
David Srbecky09ed0982016-02-12 21:58:43 +0000248};
249
David Srbecky71ec1cc2018-05-18 15:57:25 +0100250class DexRegisterInfo : public BitTable<2>::Accessor {
251 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100252 BIT_TABLE_HEADER()
253 BIT_TABLE_COLUMN(0, Kind)
254 BIT_TABLE_COLUMN(1, PackedValue)
David Srbecky71ec1cc2018-05-18 15:57:25 +0100255
256 ALWAYS_INLINE DexRegisterLocation GetLocation() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100257 DexRegisterLocation::Kind kind = static_cast<DexRegisterLocation::Kind>(GetKind());
258 return DexRegisterLocation(kind, UnpackValue(kind, GetPackedValue()));
David Srbecky71ec1cc2018-05-18 15:57:25 +0100259 }
260
261 static uint32_t PackValue(DexRegisterLocation::Kind kind, uint32_t value) {
262 uint32_t packed_value = value;
263 if (kind == DexRegisterLocation::Kind::kInStack) {
264 DCHECK(IsAligned<kFrameSlotSize>(packed_value));
265 packed_value /= kFrameSlotSize;
266 }
267 return packed_value;
268 }
269
270 static uint32_t UnpackValue(DexRegisterLocation::Kind kind, uint32_t packed_value) {
271 uint32_t value = packed_value;
272 if (kind == DexRegisterLocation::Kind::kInStack) {
273 value *= kFrameSlotSize;
274 }
275 return value;
276 }
277};
278
David Srbecky4b59d102018-05-29 21:46:10 +0000279// Register masks tend to have many trailing zero bits (caller-saves are usually not encoded),
280// therefore it is worth encoding the mask as value+shift.
281class RegisterMask : public BitTable<2>::Accessor {
282 public:
David Srbeckyd97e0822018-06-03 12:00:24 +0100283 BIT_TABLE_HEADER()
284 BIT_TABLE_COLUMN(0, Value)
285 BIT_TABLE_COLUMN(1, Shift)
David Srbecky4b59d102018-05-29 21:46:10 +0000286
287 ALWAYS_INLINE uint32_t GetMask() const {
David Srbeckyd97e0822018-06-03 12:00:24 +0100288 return GetValue() << GetShift();
David Srbecky4b59d102018-05-29 21:46:10 +0000289 }
290};
291
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100292/**
293 * Wrapper around all compiler information collected for a method.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100294 * See the Decode method at the end for the precise binary format.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100295 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100296class CodeInfo {
297 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100298 explicit CodeInfo(const void* data) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100299 Decode(reinterpret_cast<const uint8_t*>(data));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100300 }
301
David Srbecky052f8ca2018-04-26 15:42:54 +0100302 explicit CodeInfo(MemoryRegion region) : CodeInfo(region.begin()) {
303 DCHECK_EQ(size_, region.size());
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100304 }
305
David Srbecky052f8ca2018-04-26 15:42:54 +0100306 explicit CodeInfo(const OatQuickMethodHeader* header)
307 : CodeInfo(header->GetOptimizedCodeInfoPtr()) {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100308 }
309
David Srbecky052f8ca2018-04-26 15:42:54 +0100310 size_t Size() const {
311 return size_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000312 }
313
David Srbecky052f8ca2018-04-26 15:42:54 +0100314 bool HasInlineInfo() const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100315 return inline_infos_.NumRows() > 0;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100316 }
317
David Srbecky052f8ca2018-04-26 15:42:54 +0100318 ALWAYS_INLINE StackMap GetStackMapAt(size_t index) const {
319 return StackMap(&stack_maps_, index);
David Srbecky45aa5982016-03-18 02:15:09 +0000320 }
321
David Srbecky052f8ca2018-04-26 15:42:54 +0100322 BitMemoryRegion GetStackMask(size_t index) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000323 return stack_masks_.GetBitMemoryRegion(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800324 }
325
David Srbecky052f8ca2018-04-26 15:42:54 +0100326 BitMemoryRegion GetStackMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000327 uint32_t index = stack_map.GetStackMaskIndex();
328 return (index == StackMap::kNoValue) ? BitMemoryRegion() : GetStackMask(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800329 }
330
David Srbecky052f8ca2018-04-26 15:42:54 +0100331 uint32_t GetRegisterMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000332 uint32_t index = stack_map.GetRegisterMaskIndex();
333 return (index == StackMap::kNoValue) ? 0 : RegisterMask(&register_masks_, index).GetMask();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100334 }
335
David Srbecky052f8ca2018-04-26 15:42:54 +0100336 uint32_t GetNumberOfLocationCatalogEntries() const {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100337 return dex_register_catalog_.NumRows();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000338 }
339
David Srbecky71ec1cc2018-05-18 15:57:25 +0100340 ALWAYS_INLINE DexRegisterLocation GetDexRegisterCatalogEntry(size_t index) const {
341 return DexRegisterInfo(&dex_register_catalog_, index).GetLocation();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100342 }
343
David Srbecky052f8ca2018-04-26 15:42:54 +0100344 uint32_t GetNumberOfStackMaps() const {
345 return stack_maps_.NumRows();
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100346 }
347
David Srbecky052f8ca2018-04-26 15:42:54 +0100348 InvokeInfo GetInvokeInfo(size_t index) const {
349 return InvokeInfo(&invoke_infos_, index);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800350 }
351
David Srbecky71ec1cc2018-05-18 15:57:25 +0100352 ALWAYS_INLINE DexRegisterMap GetDexRegisterMapOf(StackMap stack_map,
353 size_t num_dex_registers) const {
354 return DecodeDexRegisterMap(stack_map.GetDexRegisterMaskIndex(),
355 stack_map.GetDexRegisterMapIndex(),
356 num_dex_registers);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100357 }
358
David Srbecky71ec1cc2018-05-18 15:57:25 +0100359 ALWAYS_INLINE DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
David Srbecky6e69e522018-06-03 12:00:14 +0100360 StackMap stack_map,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100361 size_t num_dex_registers) const {
David Srbecky6e69e522018-06-03 12:00:14 +0100362 InlineInfo inline_info = GetInlineInfoAtDepth(stack_map, depth);
363 return DecodeDexRegisterMap(inline_info.GetDexRegisterMaskIndex(),
364 inline_info.GetDexRegisterMapIndex(),
David Srbecky71ec1cc2018-05-18 15:57:25 +0100365 num_dex_registers);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100366 }
367
David Srbecky052f8ca2018-04-26 15:42:54 +0100368 InlineInfo GetInlineInfo(size_t index) const {
369 return InlineInfo(&inline_infos_, index);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800370 }
371
David Srbecky6e69e522018-06-03 12:00:14 +0100372 uint32_t GetInlineDepthOf(StackMap stack_map) const {
373 uint32_t depth = 0;
David Srbecky052f8ca2018-04-26 15:42:54 +0100374 uint32_t index = stack_map.GetInlineInfoIndex();
David Srbecky6e69e522018-06-03 12:00:14 +0100375 if (index != StackMap::kNoValue) {
376 while (GetInlineInfo(index + depth++).GetIsLast() == InlineInfo::kMore) { }
377 }
378 return depth;
379 }
380
381 InlineInfo GetInlineInfoAtDepth(StackMap stack_map, uint32_t depth) const {
382 DCHECK(stack_map.HasInlineInfo());
383 DCHECK_LT(depth, GetInlineDepthOf(stack_map));
384 return GetInlineInfo(stack_map.GetInlineInfoIndex() + depth);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100385 }
386
David Srbecky052f8ca2018-04-26 15:42:54 +0100387 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
388 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
389 StackMap stack_map = GetStackMapAt(i);
390 if (stack_map.GetDexPc() == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100391 return stack_map;
392 }
393 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100394 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100395 }
396
David Brazdil77a48ae2015-09-15 12:34:04 +0000397 // Searches the stack map list backwards because catch stack maps are stored
398 // at the end.
David Srbecky052f8ca2018-04-26 15:42:54 +0100399 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc) const {
400 for (size_t i = GetNumberOfStackMaps(); i > 0; --i) {
401 StackMap stack_map = GetStackMapAt(i - 1);
402 if (stack_map.GetDexPc() == dex_pc) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000403 return stack_map;
404 }
405 }
406 return StackMap();
407 }
408
David Srbecky052f8ca2018-04-26 15:42:54 +0100409 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc) const {
410 size_t e = GetNumberOfStackMaps();
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000411 if (e == 0) {
412 // There cannot be OSR stack map if there is no stack map.
413 return StackMap();
414 }
415 // Walk over all stack maps. If two consecutive stack maps are identical, then we
416 // have found a stack map suitable for OSR.
417 for (size_t i = 0; i < e - 1; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100418 StackMap stack_map = GetStackMapAt(i);
419 if (stack_map.GetDexPc() == dex_pc) {
420 StackMap other = GetStackMapAt(i + 1);
421 if (other.GetDexPc() == dex_pc &&
422 other.GetNativePcOffset(kRuntimeISA) ==
423 stack_map.GetNativePcOffset(kRuntimeISA)) {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100424 DCHECK_EQ(other.GetDexRegisterMapIndex(),
425 stack_map.GetDexRegisterMapIndex());
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000426 if (i < e - 2) {
427 // Make sure there are not three identical stack maps following each other.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800428 DCHECK_NE(
David Srbecky052f8ca2018-04-26 15:42:54 +0100429 stack_map.GetNativePcOffset(kRuntimeISA),
430 GetStackMapAt(i + 2).GetNativePcOffset(kRuntimeISA));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000431 }
432 return stack_map;
433 }
434 }
435 }
436 return StackMap();
437 }
438
David Srbecky052f8ca2018-04-26 15:42:54 +0100439 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const {
David Brazdil77a48ae2015-09-15 12:34:04 +0000440 // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack
441 // maps are not. If we knew that the method does not have try/catch,
442 // we could do binary search.
David Srbecky052f8ca2018-04-26 15:42:54 +0100443 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
444 StackMap stack_map = GetStackMapAt(i);
445 if (stack_map.GetNativePcOffset(kRuntimeISA) == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100446 return stack_map;
447 }
448 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100449 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100450 }
451
David Srbecky052f8ca2018-04-26 15:42:54 +0100452 InvokeInfo GetInvokeInfoForNativePcOffset(uint32_t native_pc_offset) {
453 for (size_t index = 0; index < invoke_infos_.NumRows(); index++) {
454 InvokeInfo item = GetInvokeInfo(index);
455 if (item.GetNativePcOffset(kRuntimeISA) == native_pc_offset) {
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800456 return item;
457 }
458 }
David Srbeckyd97e0822018-06-03 12:00:24 +0100459 return InvokeInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800460 }
461
David Srbecky71ec1cc2018-05-18 15:57:25 +0100462 // Dump this CodeInfo object on `vios`.
463 // `code_offset` is the (absolute) native PC of the compiled method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100464 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100465 uint32_t code_offset,
466 uint16_t number_of_dex_registers,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100467 bool verbose,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700468 InstructionSet instruction_set,
469 const MethodInfo& method_info) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000470
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100471 private:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100472 ALWAYS_INLINE DexRegisterMap DecodeDexRegisterMap(uint32_t mask_index,
473 uint32_t map_index,
474 uint32_t num_dex_registers) const {
475 DexRegisterMap map(map_index == StackMap::kNoValue ? 0 : num_dex_registers);
476 if (mask_index != StackMap::kNoValue) {
477 BitMemoryRegion mask = dex_register_masks_.GetBitMemoryRegion(mask_index);
478 num_dex_registers = std::min<uint32_t>(num_dex_registers, mask.size_in_bits());
479 DexRegisterLocation* regs = map.data();
480 for (uint32_t r = 0; r < mask.size_in_bits(); r++) {
481 if (mask.LoadBit(r) /* is_live */) {
482 DCHECK_LT(r, map.size());
483 regs[r] = GetDexRegisterCatalogEntry(dex_register_maps_.Get(map_index++));
484 }
485 }
486 }
487 return map;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000488 }
489
David Srbecky052f8ca2018-04-26 15:42:54 +0100490 void Decode(const uint8_t* data) {
491 size_t non_header_size = DecodeUnsignedLeb128(&data);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100492 BitMemoryRegion region(MemoryRegion(const_cast<uint8_t*>(data), non_header_size));
David Srbecky052f8ca2018-04-26 15:42:54 +0100493 size_t bit_offset = 0;
494 size_ = UnsignedLeb128Size(non_header_size) + non_header_size;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100495 stack_maps_.Decode(region, &bit_offset);
496 register_masks_.Decode(region, &bit_offset);
497 stack_masks_.Decode(region, &bit_offset);
498 invoke_infos_.Decode(region, &bit_offset);
499 inline_infos_.Decode(region, &bit_offset);
500 dex_register_masks_.Decode(region, &bit_offset);
501 dex_register_maps_.Decode(region, &bit_offset);
502 dex_register_catalog_.Decode(region, &bit_offset);
503 CHECK_EQ(non_header_size, BitsToBytesRoundUp(bit_offset)) << "Invalid CodeInfo";
David Srbecky052f8ca2018-04-26 15:42:54 +0100504 }
505
506 size_t size_;
David Srbeckyd97e0822018-06-03 12:00:24 +0100507 BitTable<StackMap::kCount> stack_maps_;
508 BitTable<RegisterMask::kCount> register_masks_;
David Srbecky4b59d102018-05-29 21:46:10 +0000509 BitTable<1> stack_masks_;
David Srbeckyd97e0822018-06-03 12:00:24 +0100510 BitTable<InvokeInfo::kCount> invoke_infos_;
511 BitTable<InlineInfo::kCount> inline_infos_;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100512 BitTable<1> dex_register_masks_;
513 BitTable<1> dex_register_maps_;
David Srbeckyd97e0822018-06-03 12:00:24 +0100514 BitTable<DexRegisterInfo::kCount> dex_register_catalog_;
David Srbecky052f8ca2018-04-26 15:42:54 +0100515
516 friend class OatDumper;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100517};
518
Roland Levillain1c1da432015-07-16 11:54:44 +0100519#undef ELEMENT_BYTE_OFFSET_AFTER
520#undef ELEMENT_BIT_OFFSET_AFTER
521
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100522} // namespace art
523
524#endif // ART_RUNTIME_STACK_MAP_H_