blob: c558846bb35f7131a2da5d10b82ace7d17075820 [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"
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070029#include "method_info.h"
David Srbecky052f8ca2018-04-26 15:42:54 +010030#include "oat_quick_method_header.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010031
32namespace art {
33
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010034class VariableIndentationOutputStream;
35
Roland Levillaina2d8ec62015-03-12 15:25:29 +000036// Size of a frame slot, in bytes. This constant is a signed value,
37// to please the compiler in arithmetic operations involving int32_t
38// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000039static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000040
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000041class ArtMethod;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000042class CodeInfo;
43
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010044/**
45 * Classes in the following file are wrapper on stack map information backed
46 * by a MemoryRegion. As such they read and write to the region, they don't have
47 * their own fields.
48 */
49
Roland Levillaina2d8ec62015-03-12 15:25:29 +000050// Dex register location container used by DexRegisterMap and StackMapStream.
51class DexRegisterLocation {
52 public:
53 /*
54 * The location kind used to populate the Dex register information in a
55 * StackMapStream can either be:
David Brazdild9cb68e2015-08-25 13:52:43 +010056 * - kStack: vreg stored on the stack, value holds the stack offset;
57 * - kInRegister: vreg stored in low 32 bits of a core physical register,
58 * value holds the register number;
59 * - kInRegisterHigh: vreg stored in high 32 bits of a core physical register,
60 * value holds the register number;
61 * - kInFpuRegister: vreg stored in low 32 bits of an FPU register,
62 * value holds the register number;
63 * - kInFpuRegisterHigh: vreg stored in high 32 bits of an FPU register,
64 * value holds the register number;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000065 * - kConstant: value holds the constant;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000066 *
67 * In addition, DexRegisterMap also uses these values:
68 * - kInStackLargeOffset: value holds a "large" stack offset (greater than
Roland Levillaina552e1c2015-03-26 15:01:03 +000069 * or equal to 128 bytes);
70 * - kConstantLargeValue: value holds a "large" constant (lower than 0, or
David Brazdild9cb68e2015-08-25 13:52:43 +010071 * or greater than or equal to 32);
72 * - kNone: the register has no location, meaning it has not been set.
Roland Levillaina2d8ec62015-03-12 15:25:29 +000073 */
74 enum class Kind : uint8_t {
75 // Short location kinds, for entries fitting on one byte (3 bits
76 // for the kind, 5 bits for the value) in a DexRegisterMap.
David Brazdild9cb68e2015-08-25 13:52:43 +010077 kInStack = 0, // 0b000
78 kInRegister = 1, // 0b001
79 kInRegisterHigh = 2, // 0b010
Roland Levillaina2d8ec62015-03-12 15:25:29 +000080 kInFpuRegister = 3, // 0b011
David Brazdild9cb68e2015-08-25 13:52:43 +010081 kInFpuRegisterHigh = 4, // 0b100
82 kConstant = 5, // 0b101
Roland Levillaina2d8ec62015-03-12 15:25:29 +000083
84 // Large location kinds, requiring a 5-byte encoding (1 byte for the
85 // kind, 4 bytes for the value).
86
87 // Stack location at a large offset, meaning that the offset value
88 // divided by the stack frame slot size (4 bytes) cannot fit on a
89 // 5-bit unsigned integer (i.e., this offset value is greater than
90 // or equal to 2^5 * 4 = 128 bytes).
David Brazdild9cb68e2015-08-25 13:52:43 +010091 kInStackLargeOffset = 6, // 0b110
Roland Levillaina2d8ec62015-03-12 15:25:29 +000092
93 // Large constant, that cannot fit on a 5-bit signed integer (i.e.,
Roland Levillaina552e1c2015-03-26 15:01:03 +000094 // lower than 0, or greater than or equal to 2^5 = 32).
David Brazdild9cb68e2015-08-25 13:52:43 +010095 kConstantLargeValue = 7, // 0b111
96
97 // Entries with no location are not stored and do not need own marker.
98 kNone = static_cast<uint8_t>(-1),
Roland Levillaina2d8ec62015-03-12 15:25:29 +000099
100 kLastLocationKind = kConstantLargeValue
101 };
102
103 static_assert(
104 sizeof(Kind) == 1u,
105 "art::DexRegisterLocation::Kind has a size different from one byte.");
106
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000107 static bool IsShortLocationKind(Kind kind) {
108 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000109 case Kind::kInStack:
110 case Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100111 case Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000112 case Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100113 case Kind::kInFpuRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000114 case Kind::kConstant:
115 return true;
116
117 case Kind::kInStackLargeOffset:
118 case Kind::kConstantLargeValue:
119 return false;
120
David Brazdild9cb68e2015-08-25 13:52:43 +0100121 case Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000122 LOG(FATAL) << "Unexpected location kind";
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000123 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100124 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000125 }
126
127 // Convert `kind` to a "surface" kind, i.e. one that doesn't include
128 // any value with a "large" qualifier.
129 // TODO: Introduce another enum type for the surface kind?
130 static Kind ConvertToSurfaceKind(Kind kind) {
131 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000132 case Kind::kInStack:
133 case Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100134 case Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000135 case Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100136 case Kind::kInFpuRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000137 case Kind::kConstant:
138 return kind;
139
140 case Kind::kInStackLargeOffset:
141 return Kind::kInStack;
142
143 case Kind::kConstantLargeValue:
144 return Kind::kConstant;
145
David Brazdild9cb68e2015-08-25 13:52:43 +0100146 case Kind::kNone:
147 return kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000148 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100149 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000150 }
151
Roland Levillaina552e1c2015-03-26 15:01:03 +0000152 // Required by art::StackMapStream::LocationCatalogEntriesIndices.
153 DexRegisterLocation() : kind_(Kind::kNone), value_(0) {}
154
155 DexRegisterLocation(Kind kind, int32_t value) : kind_(kind), value_(value) {}
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000156
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000157 static DexRegisterLocation None() {
158 return DexRegisterLocation(Kind::kNone, 0);
159 }
160
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000161 // Get the "surface" kind of the location, i.e., the one that doesn't
162 // include any value with a "large" qualifier.
163 Kind GetKind() const {
164 return ConvertToSurfaceKind(kind_);
165 }
166
167 // Get the value of the location.
168 int32_t GetValue() const { return value_; }
169
170 // Get the actual kind of the location.
171 Kind GetInternalKind() const { return kind_; }
172
Calin Juravle6ae70962015-03-18 16:31:28 +0000173 bool operator==(DexRegisterLocation other) const {
174 return kind_ == other.kind_ && value_ == other.value_;
175 }
176
177 bool operator!=(DexRegisterLocation other) const {
178 return !(*this == other);
179 }
180
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000181 private:
182 Kind kind_;
183 int32_t value_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000184
185 friend class DexRegisterLocationHashFn;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000186};
187
David Srbecky7dc11782016-02-25 13:23:56 +0000188std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation::Kind& kind);
189
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100190/**
Roland Levillaina552e1c2015-03-26 15:01:03 +0000191 * Store information on unique Dex register locations used in a method.
192 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100193 *
194 * [DexRegisterLocation+].
195 *
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000196 * DexRegisterLocations are either 1- or 5-byte wide (see art::DexRegisterLocation::Kind).
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100197 */
Roland Levillaina552e1c2015-03-26 15:01:03 +0000198class DexRegisterLocationCatalog {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100199 public:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000200 explicit DexRegisterLocationCatalog(MemoryRegion region) : region_(region) {}
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100201
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000202 // Short (compressed) location, fitting on one byte.
203 typedef uint8_t ShortLocation;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100204
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000205 void SetRegisterInfo(size_t offset, const DexRegisterLocation& dex_register_location) {
206 DexRegisterLocation::Kind kind = ComputeCompressedKind(dex_register_location);
207 int32_t value = dex_register_location.GetValue();
208 if (DexRegisterLocation::IsShortLocationKind(kind)) {
209 // Short location. Compress the kind and the value as a single byte.
210 if (kind == DexRegisterLocation::Kind::kInStack) {
211 // Instead of storing stack offsets expressed in bytes for
212 // short stack locations, store slot offsets. A stack offset
213 // is a multiple of 4 (kFrameSlotSize). This means that by
214 // dividing it by 4, we can fit values from the [0, 128)
215 // interval in a short stack location, and not just values
216 // from the [0, 32) interval.
217 DCHECK_EQ(value % kFrameSlotSize, 0);
218 value /= kFrameSlotSize;
219 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000220 DCHECK(IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000221 region_.StoreUnaligned<ShortLocation>(offset, MakeShortLocation(kind, value));
222 } else {
223 // Large location. Write the location on one byte and the value
224 // on 4 bytes.
Roland Levillaina552e1c2015-03-26 15:01:03 +0000225 DCHECK(!IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000226 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
227 // Also divide large stack offsets by 4 for the sake of consistency.
228 DCHECK_EQ(value % kFrameSlotSize, 0);
229 value /= kFrameSlotSize;
230 }
231 // Data can be unaligned as the written Dex register locations can
232 // either be 1-byte or 5-byte wide. Use
233 // art::MemoryRegion::StoreUnaligned instead of
234 // art::MemoryRegion::Store to prevent unligned word accesses on ARM.
235 region_.StoreUnaligned<DexRegisterLocation::Kind>(offset, kind);
236 region_.StoreUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind), value);
Roland Levillain442b46a2015-02-18 16:54:21 +0000237 }
238 }
239
Roland Levillaina552e1c2015-03-26 15:01:03 +0000240 // Find the offset of the location catalog entry number `location_catalog_entry_index`.
241 size_t FindLocationOffset(size_t location_catalog_entry_index) const {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000242 size_t offset = kFixedSize;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000243 // Skip the first `location_catalog_entry_index - 1` entries.
244 for (uint16_t i = 0; i < location_catalog_entry_index; ++i) {
245 // Read the first next byte and inspect its first 3 bits to decide
246 // whether it is a short or a large location.
247 DexRegisterLocation::Kind kind = ExtractKindAtOffset(offset);
248 if (DexRegisterLocation::IsShortLocationKind(kind)) {
249 // Short location. Skip the current byte.
250 offset += SingleShortEntrySize();
251 } else {
252 // Large location. Skip the 5 next bytes.
253 offset += SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000254 }
255 }
256 return offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100257 }
258
Roland Levillaina552e1c2015-03-26 15:01:03 +0000259 // Get the internal kind of entry at `location_catalog_entry_index`.
260 DexRegisterLocation::Kind GetLocationInternalKind(size_t location_catalog_entry_index) const {
261 if (location_catalog_entry_index == kNoLocationEntryIndex) {
262 return DexRegisterLocation::Kind::kNone;
263 }
264 return ExtractKindAtOffset(FindLocationOffset(location_catalog_entry_index));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100265 }
266
Roland Levillaina552e1c2015-03-26 15:01:03 +0000267 // Get the (surface) kind and value of entry at `location_catalog_entry_index`.
268 DexRegisterLocation GetDexRegisterLocation(size_t location_catalog_entry_index) const {
269 if (location_catalog_entry_index == kNoLocationEntryIndex) {
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000270 return DexRegisterLocation::None();
271 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000272 size_t offset = FindLocationOffset(location_catalog_entry_index);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000273 // Read the first byte and inspect its first 3 bits to get the location.
274 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
275 DexRegisterLocation::Kind kind = ExtractKindFromShortLocation(first_byte);
276 if (DexRegisterLocation::IsShortLocationKind(kind)) {
277 // Short location. Extract the value from the remaining 5 bits.
278 int32_t value = ExtractValueFromShortLocation(first_byte);
279 if (kind == DexRegisterLocation::Kind::kInStack) {
280 // Convert the stack slot (short) offset to a byte offset value.
281 value *= kFrameSlotSize;
282 }
283 return DexRegisterLocation(kind, value);
284 } else {
285 // Large location. Read the four next bytes to get the value.
286 int32_t value = region_.LoadUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind));
287 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
288 // Convert the stack slot (large) offset to a byte offset value.
289 value *= kFrameSlotSize;
290 }
291 return DexRegisterLocation(kind, value);
292 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100293 }
294
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000295 // Compute the compressed kind of `location`.
296 static DexRegisterLocation::Kind ComputeCompressedKind(const DexRegisterLocation& location) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100297 DexRegisterLocation::Kind kind = location.GetInternalKind();
298 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000299 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000300 return IsShortStackOffsetValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000301 ? DexRegisterLocation::Kind::kInStack
302 : DexRegisterLocation::Kind::kInStackLargeOffset;
303
David Brazdild9cb68e2015-08-25 13:52:43 +0100304 case DexRegisterLocation::Kind::kInRegister:
305 case DexRegisterLocation::Kind::kInRegisterHigh:
306 DCHECK_GE(location.GetValue(), 0);
307 DCHECK_LT(location.GetValue(), 1 << kValueBits);
308 return kind;
309
310 case DexRegisterLocation::Kind::kInFpuRegister:
311 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
312 DCHECK_GE(location.GetValue(), 0);
313 DCHECK_LT(location.GetValue(), 1 << kValueBits);
314 return kind;
315
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000316 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000317 return IsShortConstantValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000318 ? DexRegisterLocation::Kind::kConstant
319 : DexRegisterLocation::Kind::kConstantLargeValue;
320
David Brazdild9cb68e2015-08-25 13:52:43 +0100321 case DexRegisterLocation::Kind::kConstantLargeValue:
322 case DexRegisterLocation::Kind::kInStackLargeOffset:
323 case DexRegisterLocation::Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000324 LOG(FATAL) << "Unexpected location kind " << kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000325 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100326 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000327 }
328
329 // Can `location` be turned into a short location?
330 static bool CanBeEncodedAsShortLocation(const DexRegisterLocation& location) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100331 DexRegisterLocation::Kind kind = location.GetInternalKind();
332 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000333 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000334 return IsShortStackOffsetValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000335
David Brazdild9cb68e2015-08-25 13:52:43 +0100336 case DexRegisterLocation::Kind::kInRegister:
337 case DexRegisterLocation::Kind::kInRegisterHigh:
338 case DexRegisterLocation::Kind::kInFpuRegister:
339 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
340 return true;
341
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000342 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000343 return IsShortConstantValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000344
David Brazdild9cb68e2015-08-25 13:52:43 +0100345 case DexRegisterLocation::Kind::kConstantLargeValue:
346 case DexRegisterLocation::Kind::kInStackLargeOffset:
347 case DexRegisterLocation::Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000348 LOG(FATAL) << "Unexpected location kind " << kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000349 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100350 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000351 }
352
353 static size_t EntrySize(const DexRegisterLocation& location) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000354 return CanBeEncodedAsShortLocation(location) ? SingleShortEntrySize() : SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000355 }
356
357 static size_t SingleShortEntrySize() {
358 return sizeof(ShortLocation);
359 }
360
361 static size_t SingleLargeEntrySize() {
362 return sizeof(DexRegisterLocation::Kind) + sizeof(int32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100363 }
364
Roland Levillain12baf472015-03-05 12:41:42 +0000365 size_t Size() const {
366 return region_.size();
367 }
368
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700369 void Dump(VariableIndentationOutputStream* vios,
370 const CodeInfo& code_info);
Roland Levillain0396ed72015-05-27 15:12:19 +0100371
Roland Levillaina552e1c2015-03-26 15:01:03 +0000372 // Special (invalid) Dex register location catalog entry index meaning
373 // that there is no location for a given Dex register (i.e., it is
374 // mapped to a DexRegisterLocation::Kind::kNone location).
375 static constexpr size_t kNoLocationEntryIndex = -1;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100376
Roland Levillain12baf472015-03-05 12:41:42 +0000377 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000378 static constexpr int kFixedSize = 0;
379
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000380 // Width of the kind "field" in a short location, in bits.
381 static constexpr size_t kKindBits = 3;
382 // Width of the value "field" in a short location, in bits.
383 static constexpr size_t kValueBits = 5;
384
385 static constexpr uint8_t kKindMask = (1 << kKindBits) - 1;
386 static constexpr int32_t kValueMask = (1 << kValueBits) - 1;
387 static constexpr size_t kKindOffset = 0;
388 static constexpr size_t kValueOffset = kKindBits;
389
Roland Levillaina552e1c2015-03-26 15:01:03 +0000390 static bool IsShortStackOffsetValue(int32_t value) {
391 DCHECK_EQ(value % kFrameSlotSize, 0);
392 return IsShortValue(value / kFrameSlotSize);
393 }
394
395 static bool IsShortConstantValue(int32_t value) {
396 return IsShortValue(value);
397 }
398
399 static bool IsShortValue(int32_t value) {
400 return IsUint<kValueBits>(value);
401 }
402
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000403 static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000404 uint8_t kind_integer_value = static_cast<uint8_t>(kind);
405 DCHECK(IsUint<kKindBits>(kind_integer_value)) << kind_integer_value;
406 DCHECK(IsShortValue(value)) << value;
407 return (kind_integer_value & kKindMask) << kKindOffset
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000408 | (value & kValueMask) << kValueOffset;
409 }
410
411 static DexRegisterLocation::Kind ExtractKindFromShortLocation(ShortLocation location) {
412 uint8_t kind = (location >> kKindOffset) & kKindMask;
413 DCHECK_LE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kLastLocationKind));
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000414 // We do not encode kNone locations in the stack map.
415 DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000416 return static_cast<DexRegisterLocation::Kind>(kind);
417 }
418
419 static int32_t ExtractValueFromShortLocation(ShortLocation location) {
420 return (location >> kValueOffset) & kValueMask;
421 }
422
423 // Extract a location kind from the byte at position `offset`.
424 DexRegisterLocation::Kind ExtractKindAtOffset(size_t offset) const {
425 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
426 return ExtractKindFromShortLocation(first_byte);
427 }
428
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100429 MemoryRegion region_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000430
431 friend class CodeInfo;
432 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100433};
434
Roland Levillaina552e1c2015-03-26 15:01:03 +0000435/* Information on Dex register locations for a specific PC, mapping a
436 * stack map's Dex register to a location entry in a DexRegisterLocationCatalog.
437 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100438 *
439 * [live_bit_mask, entries*]
440 *
Roland Levillaina552e1c2015-03-26 15:01:03 +0000441 * where entries are concatenated unsigned integer values encoded on a number
442 * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending
443 * on the number of entries in the Dex register location catalog
444 * (see DexRegisterMap::SingleEntrySizeInBits). The map is 1-byte aligned.
445 */
446class DexRegisterMap {
447 public:
David Srbecky21d45b42018-05-30 06:35:05 +0100448 DexRegisterMap(MemoryRegion region, uint16_t number_of_dex_registers, const CodeInfo& code_info)
449 : region_(region),
450 number_of_dex_registers_(number_of_dex_registers),
451 code_info_(code_info) {}
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000452
David Srbecky68fefac2018-05-10 17:49:33 +0100453 bool IsValid() const { return region_.IsValid(); }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000454
455 // Get the surface kind of Dex register `dex_register_number`.
David Srbecky21d45b42018-05-30 06:35:05 +0100456 DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number) const {
457 return DexRegisterLocation::ConvertToSurfaceKind(GetLocationInternalKind(dex_register_number));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000458 }
459
460 // Get the internal kind of Dex register `dex_register_number`.
David Srbecky21d45b42018-05-30 06:35:05 +0100461 DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000462
463 // Get the Dex register location `dex_register_number`.
David Srbecky21d45b42018-05-30 06:35:05 +0100464 DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000465
David Srbecky21d45b42018-05-30 06:35:05 +0100466 int32_t GetStackOffsetInBytes(uint16_t dex_register_number) const {
467 DexRegisterLocation location = GetDexRegisterLocation(dex_register_number);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000468 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
469 // GetDexRegisterLocation returns the offset in bytes.
470 return location.GetValue();
471 }
472
David Srbecky21d45b42018-05-30 06:35:05 +0100473 int32_t GetConstant(uint16_t dex_register_number) const {
474 DexRegisterLocation location = GetDexRegisterLocation(dex_register_number);
David Srbecky7dc11782016-02-25 13:23:56 +0000475 DCHECK_EQ(location.GetKind(), DexRegisterLocation::Kind::kConstant);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000476 return location.GetValue();
477 }
478
David Srbecky21d45b42018-05-30 06:35:05 +0100479 int32_t GetMachineRegister(uint16_t dex_register_number) const {
480 DexRegisterLocation location = GetDexRegisterLocation(dex_register_number);
David Brazdild9cb68e2015-08-25 13:52:43 +0100481 DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister ||
482 location.GetInternalKind() == DexRegisterLocation::Kind::kInRegisterHigh ||
483 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister ||
484 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh)
David Srbecky7dc11782016-02-25 13:23:56 +0000485 << location.GetInternalKind();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000486 return location.GetValue();
487 }
488
489 // Get the index of the entry in the Dex register location catalog
490 // corresponding to `dex_register_number`.
491 size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number,
Roland Levillaina552e1c2015-03-26 15:01:03 +0000492 size_t number_of_location_catalog_entries) const {
493 if (!IsDexRegisterLive(dex_register_number)) {
494 return DexRegisterLocationCatalog::kNoLocationEntryIndex;
495 }
496
497 if (number_of_location_catalog_entries == 1) {
498 // We do not allocate space for location maps in the case of a
499 // single-entry location catalog, as it is useless. The only valid
500 // entry index is 0;
501 return 0;
502 }
503
504 // The bit offset of the beginning of the map locations.
505 size_t map_locations_offset_in_bits =
David Srbecky21d45b42018-05-30 06:35:05 +0100506 GetLocationMappingDataOffset(number_of_dex_registers_) * kBitsPerByte;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000507 size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number);
David Srbecky21d45b42018-05-30 06:35:05 +0100508 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000509 // The bit size of an entry.
510 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
511 // The bit offset where `index_in_dex_register_map` is located.
512 size_t entry_offset_in_bits =
513 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
514 size_t location_catalog_entry_index =
515 region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits);
516 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
517 return location_catalog_entry_index;
518 }
519
520 // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`.
521 void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map,
522 size_t location_catalog_entry_index,
Roland Levillaina552e1c2015-03-26 15:01:03 +0000523 size_t number_of_location_catalog_entries) {
David Srbecky21d45b42018-05-30 06:35:05 +0100524 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000525 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
526
527 if (number_of_location_catalog_entries == 1) {
528 // We do not allocate space for location maps in the case of a
529 // single-entry location catalog, as it is useless.
530 return;
531 }
532
533 // The bit offset of the beginning of the map locations.
534 size_t map_locations_offset_in_bits =
David Srbecky21d45b42018-05-30 06:35:05 +0100535 GetLocationMappingDataOffset(number_of_dex_registers_) * kBitsPerByte;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000536 // The bit size of an entry.
537 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
538 // The bit offset where `index_in_dex_register_map` is located.
539 size_t entry_offset_in_bits =
540 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
541 region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits);
542 }
543
544 void SetLiveBitMask(uint16_t number_of_dex_registers,
545 const BitVector& live_dex_registers_mask) {
546 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
547 for (uint16_t i = 0; i < number_of_dex_registers; ++i) {
548 region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i));
549 }
550 }
551
Mingyao Yang01b47b02017-02-03 12:09:57 -0800552 ALWAYS_INLINE bool IsDexRegisterLive(uint16_t dex_register_number) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000553 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
554 return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number);
555 }
556
557 size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const {
558 size_t number_of_live_dex_registers = 0;
559 for (size_t i = 0; i < number_of_dex_registers; ++i) {
560 if (IsDexRegisterLive(i)) {
561 ++number_of_live_dex_registers;
562 }
563 }
564 return number_of_live_dex_registers;
565 }
566
David Srbecky21d45b42018-05-30 06:35:05 +0100567 size_t GetNumberOfLiveDexRegisters() const {
568 return GetNumberOfLiveDexRegisters(number_of_dex_registers_);
569 }
570
Roland Levillaina552e1c2015-03-26 15:01:03 +0000571 static size_t GetLiveBitMaskOffset() {
572 return kFixedSize;
573 }
574
575 // Compute the size of the live register bit mask (in bytes), for a
576 // method having `number_of_dex_registers` Dex registers.
577 static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) {
578 return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte;
579 }
580
581 static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) {
582 return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers);
583 }
584
David Srbecky21d45b42018-05-30 06:35:05 +0100585 size_t GetLocationMappingDataSize(size_t number_of_location_catalog_entries) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000586 size_t location_mapping_data_size_in_bits =
David Srbecky21d45b42018-05-30 06:35:05 +0100587 GetNumberOfLiveDexRegisters()
Roland Levillaina552e1c2015-03-26 15:01:03 +0000588 * SingleEntrySizeInBits(number_of_location_catalog_entries);
589 return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
590 }
591
592 // Return the size of a map entry in bits. Note that if
593 // `number_of_location_catalog_entries` equals 1, this function returns 0,
594 // which is fine, as there is no need to allocate a map for a
595 // single-entry location catalog; the only valid location catalog entry index
596 // for a live register in this case is 0 and there is no need to
597 // store it.
598 static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) {
599 // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2.
600 return number_of_location_catalog_entries == 0
601 ? 0u
602 : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries));
603 }
604
605 // Return the size of the DexRegisterMap object, in bytes.
606 size_t Size() const {
David Srbecky68fefac2018-05-10 17:49:33 +0100607 return BitsToBytesRoundUp(region_.size_in_bits());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000608 }
609
David Srbecky21d45b42018-05-30 06:35:05 +0100610 void Dump(VariableIndentationOutputStream* vios) const;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100611
Roland Levillaina552e1c2015-03-26 15:01:03 +0000612 private:
613 // Return the index in the Dex register map corresponding to the Dex
614 // register number `dex_register_number`.
615 size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const {
616 if (!IsDexRegisterLive(dex_register_number)) {
617 return kInvalidIndexInDexRegisterMap;
618 }
619 return GetNumberOfLiveDexRegisters(dex_register_number);
620 }
621
622 // Special (invalid) Dex register map entry index meaning that there
623 // is no index in the map for a given Dex register (i.e., it must
624 // have been mapped to a DexRegisterLocation::Kind::kNone location).
625 static constexpr size_t kInvalidIndexInDexRegisterMap = -1;
626
627 static constexpr int kFixedSize = 0;
628
David Srbecky68fefac2018-05-10 17:49:33 +0100629 BitMemoryRegion region_;
David Srbecky21d45b42018-05-30 06:35:05 +0100630 uint16_t number_of_dex_registers_;
631 const CodeInfo& code_info_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000632
633 friend class CodeInfo;
634 friend class StackMapStream;
635};
636
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100637/**
638 * A Stack Map holds compilation information for a specific PC necessary for:
639 * - Mapping it to a dex PC,
640 * - Knowing which stack entries are objects,
641 * - Knowing which registers hold objects,
642 * - Knowing the inlining information,
643 * - Knowing the values of dex registers.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100644 */
David Srbecky052f8ca2018-04-26 15:42:54 +0100645class StackMap : public BitTable<6>::Accessor {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100646 public:
David Srbecky052f8ca2018-04-26 15:42:54 +0100647 enum Field {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100648 kPackedNativePc,
David Srbecky052f8ca2018-04-26 15:42:54 +0100649 kDexPc,
650 kDexRegisterMapOffset,
651 kInlineInfoIndex,
652 kRegisterMaskIndex,
653 kStackMaskIndex,
654 kCount,
655 };
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100656
David Srbecky052f8ca2018-04-26 15:42:54 +0100657 StackMap() : BitTable<kCount>::Accessor(nullptr, -1) {}
658 StackMap(const BitTable<kCount>* table, uint32_t row)
659 : BitTable<kCount>::Accessor(table, row) {}
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100660
David Srbecky052f8ca2018-04-26 15:42:54 +0100661 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100662 return UnpackNativePc(Get<kPackedNativePc>(), instruction_set);
David Brazdilf677ebf2015-05-29 16:29:43 +0100663 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100664
David Srbecky052f8ca2018-04-26 15:42:54 +0100665 uint32_t GetDexPc() const { return Get<kDexPc>(); }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100666
David Srbecky052f8ca2018-04-26 15:42:54 +0100667 uint32_t GetDexRegisterMapOffset() const { return Get<kDexRegisterMapOffset>(); }
668 bool HasDexRegisterMap() const { return GetDexRegisterMapOffset() != kNoValue; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100669
David Srbecky052f8ca2018-04-26 15:42:54 +0100670 uint32_t GetInlineInfoIndex() const { return Get<kInlineInfoIndex>(); }
671 bool HasInlineInfo() const { return GetInlineInfoIndex() != kNoValue; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100672
David Srbecky052f8ca2018-04-26 15:42:54 +0100673 uint32_t GetRegisterMaskIndex() const { return Get<kRegisterMaskIndex>(); }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100674
David Srbecky052f8ca2018-04-26 15:42:54 +0100675 uint32_t GetStackMaskIndex() const { return Get<kStackMaskIndex>(); }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100676
David Srbeckyd02b23f2018-05-29 23:27:22 +0100677 static uint32_t PackNativePc(uint32_t native_pc, InstructionSet isa) {
David Srbeckyd775f962018-05-30 18:12:52 +0100678 DCHECK_ALIGNED_PARAM(native_pc, GetInstructionSetInstructionAlignment(isa));
David Srbeckyd02b23f2018-05-29 23:27:22 +0100679 return native_pc / GetInstructionSetInstructionAlignment(isa);
680 }
681
682 static uint32_t UnpackNativePc(uint32_t packed_native_pc, InstructionSet isa) {
683 uint32_t native_pc = packed_native_pc * GetInstructionSetInstructionAlignment(isa);
684 DCHECK_EQ(native_pc / GetInstructionSetInstructionAlignment(isa), packed_native_pc);
685 return native_pc;
686 }
687
David Srbecky052f8ca2018-04-26 15:42:54 +0100688 static void DumpEncoding(const BitTable<6>& table, VariableIndentationOutputStream* vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100689 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100690 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700691 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100692 uint32_t code_offset,
693 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800694 InstructionSet instruction_set,
Roland Levillainf2650d12015-05-28 14:53:28 +0100695 const std::string& header_suffix = "") const;
David Srbecky61b28a12016-02-25 21:55:03 +0000696};
697
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100698/**
David Srbecky052f8ca2018-04-26 15:42:54 +0100699 * Inline information for a specific PC.
700 * The row referenced from the StackMap holds information at depth 0.
701 * Following rows hold information for further depths.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100702 */
David Srbecky052f8ca2018-04-26 15:42:54 +0100703class InlineInfo : public BitTable<5>::Accessor {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100704 public:
David Srbecky052f8ca2018-04-26 15:42:54 +0100705 enum Field {
706 kIsLast, // Determines if there are further rows for further depths.
707 kMethodIndexIdx, // Method index or ArtMethod high bits.
708 kDexPc,
709 kExtraData, // ArtMethod low bits or 1.
710 kDexRegisterMapOffset,
711 kCount,
712 };
713 static constexpr uint32_t kLast = -1;
714 static constexpr uint32_t kMore = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100715
David Srbecky052f8ca2018-04-26 15:42:54 +0100716 InlineInfo(const BitTable<kCount>* table, uint32_t row)
717 : BitTable<kCount>::Accessor(table, row) {}
718
719 ALWAYS_INLINE InlineInfo AtDepth(uint32_t depth) const {
720 return InlineInfo(table_, this->row_ + depth);
721 }
722
723 uint32_t GetDepth() const {
David Srbecky61b28a12016-02-25 21:55:03 +0000724 size_t depth = 0;
David Srbecky052f8ca2018-04-26 15:42:54 +0100725 while (AtDepth(depth++).Get<kIsLast>() == kMore) { }
David Srbecky61b28a12016-02-25 21:55:03 +0000726 return depth;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100727 }
728
David Srbecky052f8ca2018-04-26 15:42:54 +0100729 uint32_t GetMethodIndexIdxAtDepth(uint32_t depth) const {
730 DCHECK(!EncodesArtMethodAtDepth(depth));
731 return AtDepth(depth).Get<kMethodIndexIdx>();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100732 }
733
David Srbecky052f8ca2018-04-26 15:42:54 +0100734 uint32_t GetMethodIndexAtDepth(const MethodInfo& method_info, uint32_t depth) const {
735 return method_info.GetMethodIndex(GetMethodIndexIdxAtDepth(depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100736 }
737
David Srbecky052f8ca2018-04-26 15:42:54 +0100738 uint32_t GetDexPcAtDepth(uint32_t depth) const {
739 return AtDepth(depth).Get<kDexPc>();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700740 }
741
David Srbecky052f8ca2018-04-26 15:42:54 +0100742 bool EncodesArtMethodAtDepth(uint32_t depth) const {
743 return (AtDepth(depth).Get<kExtraData>() & 1) == 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100744 }
745
David Srbecky052f8ca2018-04-26 15:42:54 +0100746 ArtMethod* GetArtMethodAtDepth(uint32_t depth) const {
747 uint32_t low_bits = AtDepth(depth).Get<kExtraData>();
748 uint32_t high_bits = AtDepth(depth).Get<kMethodIndexIdx>();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000749 if (high_bits == 0) {
750 return reinterpret_cast<ArtMethod*>(low_bits);
751 } else {
752 uint64_t address = high_bits;
753 address = address << 32;
754 return reinterpret_cast<ArtMethod*>(address | low_bits);
755 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100756 }
757
David Srbecky052f8ca2018-04-26 15:42:54 +0100758 uint32_t GetDexRegisterMapOffsetAtDepth(uint32_t depth) const {
759 return AtDepth(depth).Get<kDexRegisterMapOffset>();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100760 }
761
David Srbecky052f8ca2018-04-26 15:42:54 +0100762 bool HasDexRegisterMapAtDepth(uint32_t depth) const {
763 return GetDexRegisterMapOffsetAtDepth(depth) != StackMap::kNoValue;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100764 }
765
David Srbecky052f8ca2018-04-26 15:42:54 +0100766 static void DumpEncoding(const BitTable<5>& table, VariableIndentationOutputStream* vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100767 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +0000768 const CodeInfo& info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700769 const MethodInfo& method_info,
David Srbecky61b28a12016-02-25 21:55:03 +0000770 uint16_t* number_of_dex_registers) const;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800771};
772
David Srbecky052f8ca2018-04-26 15:42:54 +0100773class InvokeInfo : public BitTable<3>::Accessor {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800774 public:
David Srbecky052f8ca2018-04-26 15:42:54 +0100775 enum Field {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100776 kPackedNativePc,
David Srbecky052f8ca2018-04-26 15:42:54 +0100777 kInvokeType,
778 kMethodIndexIdx,
779 kCount,
780 };
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800781
David Srbecky052f8ca2018-04-26 15:42:54 +0100782 InvokeInfo(const BitTable<kCount>* table, uint32_t row)
783 : BitTable<kCount>::Accessor(table, row) {}
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800784
David Srbecky052f8ca2018-04-26 15:42:54 +0100785 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100786 return StackMap::UnpackNativePc(Get<kPackedNativePc>(), instruction_set);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800787 }
788
David Srbecky052f8ca2018-04-26 15:42:54 +0100789 uint32_t GetInvokeType() const { return Get<kInvokeType>(); }
790
791 uint32_t GetMethodIndexIdx() const { return Get<kMethodIndexIdx>(); }
792
793 uint32_t GetMethodIndex(MethodInfo method_info) const {
794 return method_info.GetMethodIndex(GetMethodIndexIdx());
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800795 }
David Srbecky09ed0982016-02-12 21:58:43 +0000796};
797
David Srbecky4b59d102018-05-29 21:46:10 +0000798// Register masks tend to have many trailing zero bits (caller-saves are usually not encoded),
799// therefore it is worth encoding the mask as value+shift.
800class RegisterMask : public BitTable<2>::Accessor {
801 public:
802 enum Field {
803 kValue,
804 kShift,
805 kCount,
806 };
807
808 RegisterMask(const BitTable<kCount>* table, uint32_t row)
809 : BitTable<kCount>::Accessor(table, row) {}
810
811 ALWAYS_INLINE uint32_t GetMask() const {
812 return Get<kValue>() << Get<kShift>();
813 }
814};
815
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100816/**
817 * Wrapper around all compiler information collected for a method.
818 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100819 *
David Srbecky052f8ca2018-04-26 15:42:54 +0100820 * [BitTable<Header>, BitTable<StackMap>, BitTable<RegisterMask>, BitTable<InlineInfo>,
821 * BitTable<InvokeInfo>, BitTable<StackMask>, DexRegisterMap, DexLocationCatalog]
Mathieu Chartierc420a802017-02-14 15:16:19 -0800822 *
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100823 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100824class CodeInfo {
825 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100826 explicit CodeInfo(const void* data) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100827 Decode(reinterpret_cast<const uint8_t*>(data));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100828 }
829
David Srbecky052f8ca2018-04-26 15:42:54 +0100830 explicit CodeInfo(MemoryRegion region) : CodeInfo(region.begin()) {
831 DCHECK_EQ(size_, region.size());
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100832 }
833
David Srbecky052f8ca2018-04-26 15:42:54 +0100834 explicit CodeInfo(const OatQuickMethodHeader* header)
835 : CodeInfo(header->GetOptimizedCodeInfoPtr()) {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100836 }
837
David Srbecky052f8ca2018-04-26 15:42:54 +0100838 size_t Size() const {
839 return size_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000840 }
841
David Srbecky052f8ca2018-04-26 15:42:54 +0100842 bool HasInlineInfo() const {
843 return stack_maps_.NumColumnBits(StackMap::kInlineInfoIndex) != 0;
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800844 }
845
David Srbecky052f8ca2018-04-26 15:42:54 +0100846 DexRegisterLocationCatalog GetDexRegisterLocationCatalog() const {
847 return DexRegisterLocationCatalog(location_catalog_);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100848 }
849
David Srbecky052f8ca2018-04-26 15:42:54 +0100850 ALWAYS_INLINE StackMap GetStackMapAt(size_t index) const {
851 return StackMap(&stack_maps_, index);
David Srbecky45aa5982016-03-18 02:15:09 +0000852 }
853
David Srbecky052f8ca2018-04-26 15:42:54 +0100854 BitMemoryRegion GetStackMask(size_t index) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000855 return stack_masks_.GetBitMemoryRegion(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800856 }
857
David Srbecky052f8ca2018-04-26 15:42:54 +0100858 BitMemoryRegion GetStackMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000859 uint32_t index = stack_map.GetStackMaskIndex();
860 return (index == StackMap::kNoValue) ? BitMemoryRegion() : GetStackMask(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800861 }
862
David Srbecky052f8ca2018-04-26 15:42:54 +0100863 uint32_t GetRegisterMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000864 uint32_t index = stack_map.GetRegisterMaskIndex();
865 return (index == StackMap::kNoValue) ? 0 : RegisterMask(&register_masks_, index).GetMask();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100866 }
867
David Srbecky052f8ca2018-04-26 15:42:54 +0100868 uint32_t GetNumberOfLocationCatalogEntries() const {
869 return location_catalog_entries_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000870 }
871
David Srbecky052f8ca2018-04-26 15:42:54 +0100872 uint32_t GetDexRegisterLocationCatalogSize() const {
873 return location_catalog_.size();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100874 }
875
David Srbecky052f8ca2018-04-26 15:42:54 +0100876 uint32_t GetNumberOfStackMaps() const {
877 return stack_maps_.NumRows();
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100878 }
879
David Srbecky052f8ca2018-04-26 15:42:54 +0100880 InvokeInfo GetInvokeInfo(size_t index) const {
881 return InvokeInfo(&invoke_infos_, index);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800882 }
883
David Brazdilf677ebf2015-05-29 16:29:43 +0100884 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800885 size_t number_of_dex_registers) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100886 if (!stack_map.HasDexRegisterMap()) {
David Srbecky21d45b42018-05-30 06:35:05 +0100887 return DexRegisterMap(MemoryRegion(), 0, *this);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000888 }
David Srbecky052f8ca2018-04-26 15:42:54 +0100889 const uint32_t offset = stack_map.GetDexRegisterMapOffset();
890 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
David Srbecky21d45b42018-05-30 06:35:05 +0100891 return DexRegisterMap(dex_register_maps_.Subregion(offset, size),
892 number_of_dex_registers,
893 *this);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100894 }
895
David Srbecky052f8ca2018-04-26 15:42:54 +0100896 size_t GetDexRegisterMapsSize(uint32_t number_of_dex_registers) const {
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800897 size_t total = 0;
David Srbecky052f8ca2018-04-26 15:42:54 +0100898 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
899 StackMap stack_map = GetStackMapAt(i);
900 DexRegisterMap map(GetDexRegisterMapOf(stack_map, number_of_dex_registers));
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800901 total += map.Size();
902 }
903 return total;
904 }
905
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100906 // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`.
907 DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
908 InlineInfo inline_info,
909 uint32_t number_of_dex_registers) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100910 if (!inline_info.HasDexRegisterMapAtDepth(depth)) {
David Srbecky21d45b42018-05-30 06:35:05 +0100911 return DexRegisterMap(MemoryRegion(), 0, *this);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000912 } else {
David Srbecky052f8ca2018-04-26 15:42:54 +0100913 uint32_t offset = inline_info.GetDexRegisterMapOffsetAtDepth(depth);
914 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
David Srbecky21d45b42018-05-30 06:35:05 +0100915 return DexRegisterMap(dex_register_maps_.Subregion(offset, size),
916 number_of_dex_registers,
917 *this);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000918 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100919 }
920
David Srbecky052f8ca2018-04-26 15:42:54 +0100921 InlineInfo GetInlineInfo(size_t index) const {
922 return InlineInfo(&inline_infos_, index);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800923 }
924
David Srbecky052f8ca2018-04-26 15:42:54 +0100925 InlineInfo GetInlineInfoOf(StackMap stack_map) const {
926 DCHECK(stack_map.HasInlineInfo());
927 uint32_t index = stack_map.GetInlineInfoIndex();
928 return GetInlineInfo(index);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100929 }
930
David Srbecky052f8ca2018-04-26 15:42:54 +0100931 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
932 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
933 StackMap stack_map = GetStackMapAt(i);
934 if (stack_map.GetDexPc() == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100935 return stack_map;
936 }
937 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100938 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100939 }
940
David Brazdil77a48ae2015-09-15 12:34:04 +0000941 // Searches the stack map list backwards because catch stack maps are stored
942 // at the end.
David Srbecky052f8ca2018-04-26 15:42:54 +0100943 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc) const {
944 for (size_t i = GetNumberOfStackMaps(); i > 0; --i) {
945 StackMap stack_map = GetStackMapAt(i - 1);
946 if (stack_map.GetDexPc() == dex_pc) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000947 return stack_map;
948 }
949 }
950 return StackMap();
951 }
952
David Srbecky052f8ca2018-04-26 15:42:54 +0100953 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc) const {
954 size_t e = GetNumberOfStackMaps();
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000955 if (e == 0) {
956 // There cannot be OSR stack map if there is no stack map.
957 return StackMap();
958 }
959 // Walk over all stack maps. If two consecutive stack maps are identical, then we
960 // have found a stack map suitable for OSR.
961 for (size_t i = 0; i < e - 1; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100962 StackMap stack_map = GetStackMapAt(i);
963 if (stack_map.GetDexPc() == dex_pc) {
964 StackMap other = GetStackMapAt(i + 1);
965 if (other.GetDexPc() == dex_pc &&
966 other.GetNativePcOffset(kRuntimeISA) ==
967 stack_map.GetNativePcOffset(kRuntimeISA)) {
968 DCHECK_EQ(other.GetDexRegisterMapOffset(),
969 stack_map.GetDexRegisterMapOffset());
970 DCHECK(!stack_map.HasInlineInfo());
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000971 if (i < e - 2) {
972 // Make sure there are not three identical stack maps following each other.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800973 DCHECK_NE(
David Srbecky052f8ca2018-04-26 15:42:54 +0100974 stack_map.GetNativePcOffset(kRuntimeISA),
975 GetStackMapAt(i + 2).GetNativePcOffset(kRuntimeISA));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000976 }
977 return stack_map;
978 }
979 }
980 }
981 return StackMap();
982 }
983
David Srbecky052f8ca2018-04-26 15:42:54 +0100984 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const {
David Brazdil77a48ae2015-09-15 12:34:04 +0000985 // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack
986 // maps are not. If we knew that the method does not have try/catch,
987 // we could do binary search.
David Srbecky052f8ca2018-04-26 15:42:54 +0100988 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
989 StackMap stack_map = GetStackMapAt(i);
990 if (stack_map.GetNativePcOffset(kRuntimeISA) == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100991 return stack_map;
992 }
993 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100994 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100995 }
996
David Srbecky052f8ca2018-04-26 15:42:54 +0100997 InvokeInfo GetInvokeInfoForNativePcOffset(uint32_t native_pc_offset) {
998 for (size_t index = 0; index < invoke_infos_.NumRows(); index++) {
999 InvokeInfo item = GetInvokeInfo(index);
1000 if (item.GetNativePcOffset(kRuntimeISA) == native_pc_offset) {
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001001 return item;
1002 }
1003 }
David Srbecky052f8ca2018-04-26 15:42:54 +01001004 return InvokeInfo(&invoke_infos_, -1);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001005 }
1006
Roland Levillainf2650d12015-05-28 14:53:28 +01001007 // Dump this CodeInfo object on `os`. `code_offset` is the (absolute)
1008 // native PC of the compiled method and `number_of_dex_registers` the
1009 // number of Dex virtual registers used in this method. If
1010 // `dump_stack_maps` is true, also dump the stack maps and the
1011 // associated Dex register maps.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001012 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001013 uint32_t code_offset,
1014 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001015 bool dump_stack_maps,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001016 InstructionSet instruction_set,
1017 const MethodInfo& method_info) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001018
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001019 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +00001020 // Compute the size of the Dex register map associated to the stack map at
1021 // `dex_register_map_offset_in_code_info`.
David Srbecky052f8ca2018-04-26 15:42:54 +01001022 size_t ComputeDexRegisterMapSizeOf(uint32_t dex_register_map_offset,
Roland Levillaina552e1c2015-03-26 15:01:03 +00001023 uint16_t number_of_dex_registers) const {
1024 // Offset where the actual mapping data starts within art::DexRegisterMap.
1025 size_t location_mapping_data_offset_in_dex_register_map =
1026 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1027 // Create a temporary art::DexRegisterMap to be able to call
1028 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1029 DexRegisterMap dex_register_map_without_locations(
David Srbecky052f8ca2018-04-26 15:42:54 +01001030 MemoryRegion(dex_register_maps_.Subregion(dex_register_map_offset,
David Srbecky21d45b42018-05-30 06:35:05 +01001031 location_mapping_data_offset_in_dex_register_map)),
1032 number_of_dex_registers,
1033 *this);
Roland Levillaina552e1c2015-03-26 15:01:03 +00001034 size_t number_of_live_dex_registers =
David Srbecky21d45b42018-05-30 06:35:05 +01001035 dex_register_map_without_locations.GetNumberOfLiveDexRegisters();
Roland Levillaina552e1c2015-03-26 15:01:03 +00001036 size_t location_mapping_data_size_in_bits =
David Srbecky052f8ca2018-04-26 15:42:54 +01001037 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries())
Roland Levillaina552e1c2015-03-26 15:01:03 +00001038 * number_of_live_dex_registers;
1039 size_t location_mapping_data_size_in_bytes =
1040 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1041 size_t dex_register_map_size =
1042 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1043 return dex_register_map_size;
1044 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001045
David Srbecky052f8ca2018-04-26 15:42:54 +01001046 MemoryRegion DecodeMemoryRegion(MemoryRegion& region, size_t* bit_offset) {
1047 size_t length = DecodeVarintBits(BitMemoryRegion(region), bit_offset);
1048 size_t offset = BitsToBytesRoundUp(*bit_offset);;
1049 *bit_offset = (offset + length) * kBitsPerByte;
1050 return region.Subregion(offset, length);
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001051 }
1052
David Srbecky052f8ca2018-04-26 15:42:54 +01001053 void Decode(const uint8_t* data) {
1054 size_t non_header_size = DecodeUnsignedLeb128(&data);
1055 MemoryRegion region(const_cast<uint8_t*>(data), non_header_size);
1056 BitMemoryRegion bit_region(region);
1057 size_t bit_offset = 0;
1058 size_ = UnsignedLeb128Size(non_header_size) + non_header_size;
1059 dex_register_maps_ = DecodeMemoryRegion(region, &bit_offset);
1060 location_catalog_entries_ = DecodeVarintBits(bit_region, &bit_offset);
1061 location_catalog_ = DecodeMemoryRegion(region, &bit_offset);
1062 stack_maps_.Decode(bit_region, &bit_offset);
1063 invoke_infos_.Decode(bit_region, &bit_offset);
1064 inline_infos_.Decode(bit_region, &bit_offset);
1065 register_masks_.Decode(bit_region, &bit_offset);
David Srbecky4b59d102018-05-29 21:46:10 +00001066 stack_masks_.Decode(bit_region, &bit_offset);
1067 CHECK_EQ(BitsToBytesRoundUp(bit_offset), non_header_size);
David Srbecky052f8ca2018-04-26 15:42:54 +01001068 }
1069
1070 size_t size_;
1071 MemoryRegion dex_register_maps_;
1072 uint32_t location_catalog_entries_;
1073 MemoryRegion location_catalog_;
1074 BitTable<StackMap::Field::kCount> stack_maps_;
1075 BitTable<InvokeInfo::Field::kCount> invoke_infos_;
1076 BitTable<InlineInfo::Field::kCount> inline_infos_;
David Srbecky4b59d102018-05-29 21:46:10 +00001077 BitTable<RegisterMask::Field::kCount> register_masks_;
1078 BitTable<1> stack_masks_;
David Srbecky052f8ca2018-04-26 15:42:54 +01001079
1080 friend class OatDumper;
David Srbecky21d45b42018-05-30 06:35:05 +01001081 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001082};
1083
Roland Levillain1c1da432015-07-16 11:54:44 +01001084#undef ELEMENT_BYTE_OFFSET_AFTER
1085#undef ELEMENT_BIT_OFFSET_AFTER
1086
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001087} // namespace art
1088
1089#endif // ART_RUNTIME_STACK_MAP_H_