blob: 363884a21bbaf7ea6e8070a11ebdfcbc51481364 [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:
448 explicit DexRegisterMap(MemoryRegion region) : region_(region) {}
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000449 DexRegisterMap() {}
450
David Srbecky68fefac2018-05-10 17:49:33 +0100451 bool IsValid() const { return region_.IsValid(); }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000452
453 // Get the surface kind of Dex register `dex_register_number`.
454 DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number,
455 uint16_t number_of_dex_registers,
David Srbecky052f8ca2018-04-26 15:42:54 +0100456 const CodeInfo& code_info) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000457 return DexRegisterLocation::ConvertToSurfaceKind(
David Srbecky052f8ca2018-04-26 15:42:54 +0100458 GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000459 }
460
461 // Get the internal kind of Dex register `dex_register_number`.
462 DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number,
463 uint16_t number_of_dex_registers,
David Srbecky052f8ca2018-04-26 15:42:54 +0100464 const CodeInfo& code_info) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000465
466 // Get the Dex register location `dex_register_number`.
467 DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number,
468 uint16_t number_of_dex_registers,
David Srbecky052f8ca2018-04-26 15:42:54 +0100469 const CodeInfo& code_info) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000470
471 int32_t GetStackOffsetInBytes(uint16_t dex_register_number,
472 uint16_t number_of_dex_registers,
David Srbecky052f8ca2018-04-26 15:42:54 +0100473 const CodeInfo& code_info) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000474 DexRegisterLocation location =
David Srbecky052f8ca2018-04-26 15:42:54 +0100475 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000476 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
477 // GetDexRegisterLocation returns the offset in bytes.
478 return location.GetValue();
479 }
480
481 int32_t GetConstant(uint16_t dex_register_number,
482 uint16_t number_of_dex_registers,
David Srbecky052f8ca2018-04-26 15:42:54 +0100483 const CodeInfo& code_info) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000484 DexRegisterLocation location =
David Srbecky052f8ca2018-04-26 15:42:54 +0100485 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
David Srbecky7dc11782016-02-25 13:23:56 +0000486 DCHECK_EQ(location.GetKind(), DexRegisterLocation::Kind::kConstant);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000487 return location.GetValue();
488 }
489
490 int32_t GetMachineRegister(uint16_t dex_register_number,
491 uint16_t number_of_dex_registers,
David Srbecky052f8ca2018-04-26 15:42:54 +0100492 const CodeInfo& code_info) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000493 DexRegisterLocation location =
David Srbecky052f8ca2018-04-26 15:42:54 +0100494 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
David Brazdild9cb68e2015-08-25 13:52:43 +0100495 DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister ||
496 location.GetInternalKind() == DexRegisterLocation::Kind::kInRegisterHigh ||
497 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister ||
498 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh)
David Srbecky7dc11782016-02-25 13:23:56 +0000499 << location.GetInternalKind();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000500 return location.GetValue();
501 }
502
503 // Get the index of the entry in the Dex register location catalog
504 // corresponding to `dex_register_number`.
505 size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number,
506 uint16_t number_of_dex_registers,
507 size_t number_of_location_catalog_entries) const {
508 if (!IsDexRegisterLive(dex_register_number)) {
509 return DexRegisterLocationCatalog::kNoLocationEntryIndex;
510 }
511
512 if (number_of_location_catalog_entries == 1) {
513 // We do not allocate space for location maps in the case of a
514 // single-entry location catalog, as it is useless. The only valid
515 // entry index is 0;
516 return 0;
517 }
518
519 // The bit offset of the beginning of the map locations.
520 size_t map_locations_offset_in_bits =
521 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
522 size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number);
523 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
524 // The bit size of an entry.
525 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
526 // The bit offset where `index_in_dex_register_map` is located.
527 size_t entry_offset_in_bits =
528 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
529 size_t location_catalog_entry_index =
530 region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits);
531 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
532 return location_catalog_entry_index;
533 }
534
535 // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`.
536 void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map,
537 size_t location_catalog_entry_index,
538 uint16_t number_of_dex_registers,
539 size_t number_of_location_catalog_entries) {
540 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
541 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
542
543 if (number_of_location_catalog_entries == 1) {
544 // We do not allocate space for location maps in the case of a
545 // single-entry location catalog, as it is useless.
546 return;
547 }
548
549 // The bit offset of the beginning of the map locations.
550 size_t map_locations_offset_in_bits =
551 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
552 // The bit size of an entry.
553 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
554 // The bit offset where `index_in_dex_register_map` is located.
555 size_t entry_offset_in_bits =
556 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
557 region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits);
558 }
559
560 void SetLiveBitMask(uint16_t number_of_dex_registers,
561 const BitVector& live_dex_registers_mask) {
562 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
563 for (uint16_t i = 0; i < number_of_dex_registers; ++i) {
564 region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i));
565 }
566 }
567
Mingyao Yang01b47b02017-02-03 12:09:57 -0800568 ALWAYS_INLINE bool IsDexRegisterLive(uint16_t dex_register_number) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000569 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
570 return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number);
571 }
572
573 size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const {
574 size_t number_of_live_dex_registers = 0;
575 for (size_t i = 0; i < number_of_dex_registers; ++i) {
576 if (IsDexRegisterLive(i)) {
577 ++number_of_live_dex_registers;
578 }
579 }
580 return number_of_live_dex_registers;
581 }
582
583 static size_t GetLiveBitMaskOffset() {
584 return kFixedSize;
585 }
586
587 // Compute the size of the live register bit mask (in bytes), for a
588 // method having `number_of_dex_registers` Dex registers.
589 static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) {
590 return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte;
591 }
592
593 static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) {
594 return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers);
595 }
596
597 size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers,
598 size_t number_of_location_catalog_entries) const {
599 size_t location_mapping_data_size_in_bits =
600 GetNumberOfLiveDexRegisters(number_of_dex_registers)
601 * SingleEntrySizeInBits(number_of_location_catalog_entries);
602 return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
603 }
604
605 // Return the size of a map entry in bits. Note that if
606 // `number_of_location_catalog_entries` equals 1, this function returns 0,
607 // which is fine, as there is no need to allocate a map for a
608 // single-entry location catalog; the only valid location catalog entry index
609 // for a live register in this case is 0 and there is no need to
610 // store it.
611 static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) {
612 // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2.
613 return number_of_location_catalog_entries == 0
614 ? 0u
615 : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries));
616 }
617
618 // Return the size of the DexRegisterMap object, in bytes.
619 size_t Size() const {
David Srbecky68fefac2018-05-10 17:49:33 +0100620 return BitsToBytesRoundUp(region_.size_in_bits());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000621 }
622
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100623 void Dump(VariableIndentationOutputStream* vios,
624 const CodeInfo& code_info, uint16_t number_of_dex_registers) const;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100625
Roland Levillaina552e1c2015-03-26 15:01:03 +0000626 private:
627 // Return the index in the Dex register map corresponding to the Dex
628 // register number `dex_register_number`.
629 size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const {
630 if (!IsDexRegisterLive(dex_register_number)) {
631 return kInvalidIndexInDexRegisterMap;
632 }
633 return GetNumberOfLiveDexRegisters(dex_register_number);
634 }
635
636 // Special (invalid) Dex register map entry index meaning that there
637 // is no index in the map for a given Dex register (i.e., it must
638 // have been mapped to a DexRegisterLocation::Kind::kNone location).
639 static constexpr size_t kInvalidIndexInDexRegisterMap = -1;
640
641 static constexpr int kFixedSize = 0;
642
David Srbecky68fefac2018-05-10 17:49:33 +0100643 BitMemoryRegion region_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000644
645 friend class CodeInfo;
646 friend class StackMapStream;
647};
648
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100649/**
650 * A Stack Map holds compilation information for a specific PC necessary for:
651 * - Mapping it to a dex PC,
652 * - Knowing which stack entries are objects,
653 * - Knowing which registers hold objects,
654 * - Knowing the inlining information,
655 * - Knowing the values of dex registers.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100656 */
David Srbecky052f8ca2018-04-26 15:42:54 +0100657class StackMap : public BitTable<6>::Accessor {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100658 public:
David Srbecky052f8ca2018-04-26 15:42:54 +0100659 enum Field {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100660 kPackedNativePc,
David Srbecky052f8ca2018-04-26 15:42:54 +0100661 kDexPc,
662 kDexRegisterMapOffset,
663 kInlineInfoIndex,
664 kRegisterMaskIndex,
665 kStackMaskIndex,
666 kCount,
667 };
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100668
David Srbecky052f8ca2018-04-26 15:42:54 +0100669 StackMap() : BitTable<kCount>::Accessor(nullptr, -1) {}
670 StackMap(const BitTable<kCount>* table, uint32_t row)
671 : BitTable<kCount>::Accessor(table, row) {}
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100672
David Srbecky052f8ca2018-04-26 15:42:54 +0100673 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100674 return UnpackNativePc(Get<kPackedNativePc>(), instruction_set);
David Brazdilf677ebf2015-05-29 16:29:43 +0100675 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100676
David Srbecky052f8ca2018-04-26 15:42:54 +0100677 uint32_t GetDexPc() const { return Get<kDexPc>(); }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100678
David Srbecky052f8ca2018-04-26 15:42:54 +0100679 uint32_t GetDexRegisterMapOffset() const { return Get<kDexRegisterMapOffset>(); }
680 bool HasDexRegisterMap() const { return GetDexRegisterMapOffset() != kNoValue; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100681
David Srbecky052f8ca2018-04-26 15:42:54 +0100682 uint32_t GetInlineInfoIndex() const { return Get<kInlineInfoIndex>(); }
683 bool HasInlineInfo() const { return GetInlineInfoIndex() != kNoValue; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100684
David Srbecky052f8ca2018-04-26 15:42:54 +0100685 uint32_t GetRegisterMaskIndex() const { return Get<kRegisterMaskIndex>(); }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100686
David Srbecky052f8ca2018-04-26 15:42:54 +0100687 uint32_t GetStackMaskIndex() const { return Get<kStackMaskIndex>(); }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100688
David Srbeckyd02b23f2018-05-29 23:27:22 +0100689 static uint32_t PackNativePc(uint32_t native_pc, InstructionSet isa) {
690 // TODO: DCHECK_ALIGNED_PARAM(native_pc, GetInstructionSetInstructionAlignment(isa));
691 return native_pc / GetInstructionSetInstructionAlignment(isa);
692 }
693
694 static uint32_t UnpackNativePc(uint32_t packed_native_pc, InstructionSet isa) {
695 uint32_t native_pc = packed_native_pc * GetInstructionSetInstructionAlignment(isa);
696 DCHECK_EQ(native_pc / GetInstructionSetInstructionAlignment(isa), packed_native_pc);
697 return native_pc;
698 }
699
David Srbecky052f8ca2018-04-26 15:42:54 +0100700 static void DumpEncoding(const BitTable<6>& table, VariableIndentationOutputStream* vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100701 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100702 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700703 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100704 uint32_t code_offset,
705 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800706 InstructionSet instruction_set,
Roland Levillainf2650d12015-05-28 14:53:28 +0100707 const std::string& header_suffix = "") const;
David Srbecky61b28a12016-02-25 21:55:03 +0000708};
709
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100710/**
David Srbecky052f8ca2018-04-26 15:42:54 +0100711 * Inline information for a specific PC.
712 * The row referenced from the StackMap holds information at depth 0.
713 * Following rows hold information for further depths.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100714 */
David Srbecky052f8ca2018-04-26 15:42:54 +0100715class InlineInfo : public BitTable<5>::Accessor {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100716 public:
David Srbecky052f8ca2018-04-26 15:42:54 +0100717 enum Field {
718 kIsLast, // Determines if there are further rows for further depths.
719 kMethodIndexIdx, // Method index or ArtMethod high bits.
720 kDexPc,
721 kExtraData, // ArtMethod low bits or 1.
722 kDexRegisterMapOffset,
723 kCount,
724 };
725 static constexpr uint32_t kLast = -1;
726 static constexpr uint32_t kMore = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100727
David Srbecky052f8ca2018-04-26 15:42:54 +0100728 InlineInfo(const BitTable<kCount>* table, uint32_t row)
729 : BitTable<kCount>::Accessor(table, row) {}
730
731 ALWAYS_INLINE InlineInfo AtDepth(uint32_t depth) const {
732 return InlineInfo(table_, this->row_ + depth);
733 }
734
735 uint32_t GetDepth() const {
David Srbecky61b28a12016-02-25 21:55:03 +0000736 size_t depth = 0;
David Srbecky052f8ca2018-04-26 15:42:54 +0100737 while (AtDepth(depth++).Get<kIsLast>() == kMore) { }
David Srbecky61b28a12016-02-25 21:55:03 +0000738 return depth;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100739 }
740
David Srbecky052f8ca2018-04-26 15:42:54 +0100741 uint32_t GetMethodIndexIdxAtDepth(uint32_t depth) const {
742 DCHECK(!EncodesArtMethodAtDepth(depth));
743 return AtDepth(depth).Get<kMethodIndexIdx>();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100744 }
745
David Srbecky052f8ca2018-04-26 15:42:54 +0100746 uint32_t GetMethodIndexAtDepth(const MethodInfo& method_info, uint32_t depth) const {
747 return method_info.GetMethodIndex(GetMethodIndexIdxAtDepth(depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100748 }
749
David Srbecky052f8ca2018-04-26 15:42:54 +0100750 uint32_t GetDexPcAtDepth(uint32_t depth) const {
751 return AtDepth(depth).Get<kDexPc>();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700752 }
753
David Srbecky052f8ca2018-04-26 15:42:54 +0100754 bool EncodesArtMethodAtDepth(uint32_t depth) const {
755 return (AtDepth(depth).Get<kExtraData>() & 1) == 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100756 }
757
David Srbecky052f8ca2018-04-26 15:42:54 +0100758 ArtMethod* GetArtMethodAtDepth(uint32_t depth) const {
759 uint32_t low_bits = AtDepth(depth).Get<kExtraData>();
760 uint32_t high_bits = AtDepth(depth).Get<kMethodIndexIdx>();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000761 if (high_bits == 0) {
762 return reinterpret_cast<ArtMethod*>(low_bits);
763 } else {
764 uint64_t address = high_bits;
765 address = address << 32;
766 return reinterpret_cast<ArtMethod*>(address | low_bits);
767 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100768 }
769
David Srbecky052f8ca2018-04-26 15:42:54 +0100770 uint32_t GetDexRegisterMapOffsetAtDepth(uint32_t depth) const {
771 return AtDepth(depth).Get<kDexRegisterMapOffset>();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100772 }
773
David Srbecky052f8ca2018-04-26 15:42:54 +0100774 bool HasDexRegisterMapAtDepth(uint32_t depth) const {
775 return GetDexRegisterMapOffsetAtDepth(depth) != StackMap::kNoValue;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100776 }
777
David Srbecky052f8ca2018-04-26 15:42:54 +0100778 static void DumpEncoding(const BitTable<5>& table, VariableIndentationOutputStream* vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100779 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +0000780 const CodeInfo& info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700781 const MethodInfo& method_info,
David Srbecky61b28a12016-02-25 21:55:03 +0000782 uint16_t* number_of_dex_registers) const;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800783};
784
David Srbecky052f8ca2018-04-26 15:42:54 +0100785class InvokeInfo : public BitTable<3>::Accessor {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800786 public:
David Srbecky052f8ca2018-04-26 15:42:54 +0100787 enum Field {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100788 kPackedNativePc,
David Srbecky052f8ca2018-04-26 15:42:54 +0100789 kInvokeType,
790 kMethodIndexIdx,
791 kCount,
792 };
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800793
David Srbecky052f8ca2018-04-26 15:42:54 +0100794 InvokeInfo(const BitTable<kCount>* table, uint32_t row)
795 : BitTable<kCount>::Accessor(table, row) {}
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800796
David Srbecky052f8ca2018-04-26 15:42:54 +0100797 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100798 return StackMap::UnpackNativePc(Get<kPackedNativePc>(), instruction_set);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800799 }
800
David Srbecky052f8ca2018-04-26 15:42:54 +0100801 uint32_t GetInvokeType() const { return Get<kInvokeType>(); }
802
803 uint32_t GetMethodIndexIdx() const { return Get<kMethodIndexIdx>(); }
804
805 uint32_t GetMethodIndex(MethodInfo method_info) const {
806 return method_info.GetMethodIndex(GetMethodIndexIdx());
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800807 }
David Srbecky09ed0982016-02-12 21:58:43 +0000808};
809
David Srbecky4b59d102018-05-29 21:46:10 +0000810// Register masks tend to have many trailing zero bits (caller-saves are usually not encoded),
811// therefore it is worth encoding the mask as value+shift.
812class RegisterMask : public BitTable<2>::Accessor {
813 public:
814 enum Field {
815 kValue,
816 kShift,
817 kCount,
818 };
819
820 RegisterMask(const BitTable<kCount>* table, uint32_t row)
821 : BitTable<kCount>::Accessor(table, row) {}
822
823 ALWAYS_INLINE uint32_t GetMask() const {
824 return Get<kValue>() << Get<kShift>();
825 }
826};
827
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100828/**
829 * Wrapper around all compiler information collected for a method.
830 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100831 *
David Srbecky052f8ca2018-04-26 15:42:54 +0100832 * [BitTable<Header>, BitTable<StackMap>, BitTable<RegisterMask>, BitTable<InlineInfo>,
833 * BitTable<InvokeInfo>, BitTable<StackMask>, DexRegisterMap, DexLocationCatalog]
Mathieu Chartierc420a802017-02-14 15:16:19 -0800834 *
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100835 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100836class CodeInfo {
837 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100838 explicit CodeInfo(const void* data) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100839 Decode(reinterpret_cast<const uint8_t*>(data));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100840 }
841
David Srbecky052f8ca2018-04-26 15:42:54 +0100842 explicit CodeInfo(MemoryRegion region) : CodeInfo(region.begin()) {
843 DCHECK_EQ(size_, region.size());
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100844 }
845
David Srbecky052f8ca2018-04-26 15:42:54 +0100846 explicit CodeInfo(const OatQuickMethodHeader* header)
847 : CodeInfo(header->GetOptimizedCodeInfoPtr()) {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100848 }
849
David Srbecky052f8ca2018-04-26 15:42:54 +0100850 size_t Size() const {
851 return size_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000852 }
853
David Srbecky052f8ca2018-04-26 15:42:54 +0100854 bool HasInlineInfo() const {
855 return stack_maps_.NumColumnBits(StackMap::kInlineInfoIndex) != 0;
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800856 }
857
David Srbecky052f8ca2018-04-26 15:42:54 +0100858 DexRegisterLocationCatalog GetDexRegisterLocationCatalog() const {
859 return DexRegisterLocationCatalog(location_catalog_);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100860 }
861
David Srbecky052f8ca2018-04-26 15:42:54 +0100862 ALWAYS_INLINE StackMap GetStackMapAt(size_t index) const {
863 return StackMap(&stack_maps_, index);
David Srbecky45aa5982016-03-18 02:15:09 +0000864 }
865
David Srbecky052f8ca2018-04-26 15:42:54 +0100866 BitMemoryRegion GetStackMask(size_t index) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000867 return stack_masks_.GetBitMemoryRegion(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800868 }
869
David Srbecky052f8ca2018-04-26 15:42:54 +0100870 BitMemoryRegion GetStackMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000871 uint32_t index = stack_map.GetStackMaskIndex();
872 return (index == StackMap::kNoValue) ? BitMemoryRegion() : GetStackMask(index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800873 }
874
David Srbecky052f8ca2018-04-26 15:42:54 +0100875 uint32_t GetRegisterMaskOf(const StackMap& stack_map) const {
David Srbecky4b59d102018-05-29 21:46:10 +0000876 uint32_t index = stack_map.GetRegisterMaskIndex();
877 return (index == StackMap::kNoValue) ? 0 : RegisterMask(&register_masks_, index).GetMask();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100878 }
879
David Srbecky052f8ca2018-04-26 15:42:54 +0100880 uint32_t GetNumberOfLocationCatalogEntries() const {
881 return location_catalog_entries_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000882 }
883
David Srbecky052f8ca2018-04-26 15:42:54 +0100884 uint32_t GetDexRegisterLocationCatalogSize() const {
885 return location_catalog_.size();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100886 }
887
David Srbecky052f8ca2018-04-26 15:42:54 +0100888 uint32_t GetNumberOfStackMaps() const {
889 return stack_maps_.NumRows();
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100890 }
891
David Srbecky052f8ca2018-04-26 15:42:54 +0100892 InvokeInfo GetInvokeInfo(size_t index) const {
893 return InvokeInfo(&invoke_infos_, index);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800894 }
895
David Brazdilf677ebf2015-05-29 16:29:43 +0100896 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800897 size_t number_of_dex_registers) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100898 if (!stack_map.HasDexRegisterMap()) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000899 return DexRegisterMap();
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000900 }
David Srbecky052f8ca2018-04-26 15:42:54 +0100901 const uint32_t offset = stack_map.GetDexRegisterMapOffset();
902 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
903 return DexRegisterMap(dex_register_maps_.Subregion(offset, size));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100904 }
905
David Srbecky052f8ca2018-04-26 15:42:54 +0100906 size_t GetDexRegisterMapsSize(uint32_t number_of_dex_registers) const {
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800907 size_t total = 0;
David Srbecky052f8ca2018-04-26 15:42:54 +0100908 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
909 StackMap stack_map = GetStackMapAt(i);
910 DexRegisterMap map(GetDexRegisterMapOf(stack_map, number_of_dex_registers));
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800911 total += map.Size();
912 }
913 return total;
914 }
915
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100916 // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`.
917 DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
918 InlineInfo inline_info,
919 uint32_t number_of_dex_registers) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100920 if (!inline_info.HasDexRegisterMapAtDepth(depth)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000921 return DexRegisterMap();
922 } else {
David Srbecky052f8ca2018-04-26 15:42:54 +0100923 uint32_t offset = inline_info.GetDexRegisterMapOffsetAtDepth(depth);
924 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
925 return DexRegisterMap(dex_register_maps_.Subregion(offset, size));
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000926 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100927 }
928
David Srbecky052f8ca2018-04-26 15:42:54 +0100929 InlineInfo GetInlineInfo(size_t index) const {
930 return InlineInfo(&inline_infos_, index);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800931 }
932
David Srbecky052f8ca2018-04-26 15:42:54 +0100933 InlineInfo GetInlineInfoOf(StackMap stack_map) const {
934 DCHECK(stack_map.HasInlineInfo());
935 uint32_t index = stack_map.GetInlineInfoIndex();
936 return GetInlineInfo(index);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100937 }
938
David Srbecky052f8ca2018-04-26 15:42:54 +0100939 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
940 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
941 StackMap stack_map = GetStackMapAt(i);
942 if (stack_map.GetDexPc() == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100943 return stack_map;
944 }
945 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100946 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100947 }
948
David Brazdil77a48ae2015-09-15 12:34:04 +0000949 // Searches the stack map list backwards because catch stack maps are stored
950 // at the end.
David Srbecky052f8ca2018-04-26 15:42:54 +0100951 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc) const {
952 for (size_t i = GetNumberOfStackMaps(); i > 0; --i) {
953 StackMap stack_map = GetStackMapAt(i - 1);
954 if (stack_map.GetDexPc() == dex_pc) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000955 return stack_map;
956 }
957 }
958 return StackMap();
959 }
960
David Srbecky052f8ca2018-04-26 15:42:54 +0100961 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc) const {
962 size_t e = GetNumberOfStackMaps();
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000963 if (e == 0) {
964 // There cannot be OSR stack map if there is no stack map.
965 return StackMap();
966 }
967 // Walk over all stack maps. If two consecutive stack maps are identical, then we
968 // have found a stack map suitable for OSR.
969 for (size_t i = 0; i < e - 1; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100970 StackMap stack_map = GetStackMapAt(i);
971 if (stack_map.GetDexPc() == dex_pc) {
972 StackMap other = GetStackMapAt(i + 1);
973 if (other.GetDexPc() == dex_pc &&
974 other.GetNativePcOffset(kRuntimeISA) ==
975 stack_map.GetNativePcOffset(kRuntimeISA)) {
976 DCHECK_EQ(other.GetDexRegisterMapOffset(),
977 stack_map.GetDexRegisterMapOffset());
978 DCHECK(!stack_map.HasInlineInfo());
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000979 if (i < e - 2) {
980 // Make sure there are not three identical stack maps following each other.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800981 DCHECK_NE(
David Srbecky052f8ca2018-04-26 15:42:54 +0100982 stack_map.GetNativePcOffset(kRuntimeISA),
983 GetStackMapAt(i + 2).GetNativePcOffset(kRuntimeISA));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000984 }
985 return stack_map;
986 }
987 }
988 }
989 return StackMap();
990 }
991
David Srbecky052f8ca2018-04-26 15:42:54 +0100992 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const {
David Brazdil77a48ae2015-09-15 12:34:04 +0000993 // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack
994 // maps are not. If we knew that the method does not have try/catch,
995 // we could do binary search.
David Srbecky052f8ca2018-04-26 15:42:54 +0100996 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
997 StackMap stack_map = GetStackMapAt(i);
998 if (stack_map.GetNativePcOffset(kRuntimeISA) == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100999 return stack_map;
1000 }
1001 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001002 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001003 }
1004
David Srbecky052f8ca2018-04-26 15:42:54 +01001005 InvokeInfo GetInvokeInfoForNativePcOffset(uint32_t native_pc_offset) {
1006 for (size_t index = 0; index < invoke_infos_.NumRows(); index++) {
1007 InvokeInfo item = GetInvokeInfo(index);
1008 if (item.GetNativePcOffset(kRuntimeISA) == native_pc_offset) {
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001009 return item;
1010 }
1011 }
David Srbecky052f8ca2018-04-26 15:42:54 +01001012 return InvokeInfo(&invoke_infos_, -1);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001013 }
1014
Roland Levillainf2650d12015-05-28 14:53:28 +01001015 // Dump this CodeInfo object on `os`. `code_offset` is the (absolute)
1016 // native PC of the compiled method and `number_of_dex_registers` the
1017 // number of Dex virtual registers used in this method. If
1018 // `dump_stack_maps` is true, also dump the stack maps and the
1019 // associated Dex register maps.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001020 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001021 uint32_t code_offset,
1022 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001023 bool dump_stack_maps,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001024 InstructionSet instruction_set,
1025 const MethodInfo& method_info) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001026
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001027 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +00001028 // Compute the size of the Dex register map associated to the stack map at
1029 // `dex_register_map_offset_in_code_info`.
David Srbecky052f8ca2018-04-26 15:42:54 +01001030 size_t ComputeDexRegisterMapSizeOf(uint32_t dex_register_map_offset,
Roland Levillaina552e1c2015-03-26 15:01:03 +00001031 uint16_t number_of_dex_registers) const {
1032 // Offset where the actual mapping data starts within art::DexRegisterMap.
1033 size_t location_mapping_data_offset_in_dex_register_map =
1034 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1035 // Create a temporary art::DexRegisterMap to be able to call
1036 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1037 DexRegisterMap dex_register_map_without_locations(
David Srbecky052f8ca2018-04-26 15:42:54 +01001038 MemoryRegion(dex_register_maps_.Subregion(dex_register_map_offset,
1039 location_mapping_data_offset_in_dex_register_map)));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001040 size_t number_of_live_dex_registers =
1041 dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
1042 size_t location_mapping_data_size_in_bits =
David Srbecky052f8ca2018-04-26 15:42:54 +01001043 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries())
Roland Levillaina552e1c2015-03-26 15:01:03 +00001044 * number_of_live_dex_registers;
1045 size_t location_mapping_data_size_in_bytes =
1046 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1047 size_t dex_register_map_size =
1048 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1049 return dex_register_map_size;
1050 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001051
David Srbecky052f8ca2018-04-26 15:42:54 +01001052 MemoryRegion DecodeMemoryRegion(MemoryRegion& region, size_t* bit_offset) {
1053 size_t length = DecodeVarintBits(BitMemoryRegion(region), bit_offset);
1054 size_t offset = BitsToBytesRoundUp(*bit_offset);;
1055 *bit_offset = (offset + length) * kBitsPerByte;
1056 return region.Subregion(offset, length);
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001057 }
1058
David Srbecky052f8ca2018-04-26 15:42:54 +01001059 void Decode(const uint8_t* data) {
1060 size_t non_header_size = DecodeUnsignedLeb128(&data);
1061 MemoryRegion region(const_cast<uint8_t*>(data), non_header_size);
1062 BitMemoryRegion bit_region(region);
1063 size_t bit_offset = 0;
1064 size_ = UnsignedLeb128Size(non_header_size) + non_header_size;
1065 dex_register_maps_ = DecodeMemoryRegion(region, &bit_offset);
1066 location_catalog_entries_ = DecodeVarintBits(bit_region, &bit_offset);
1067 location_catalog_ = DecodeMemoryRegion(region, &bit_offset);
1068 stack_maps_.Decode(bit_region, &bit_offset);
1069 invoke_infos_.Decode(bit_region, &bit_offset);
1070 inline_infos_.Decode(bit_region, &bit_offset);
1071 register_masks_.Decode(bit_region, &bit_offset);
David Srbecky4b59d102018-05-29 21:46:10 +00001072 stack_masks_.Decode(bit_region, &bit_offset);
1073 CHECK_EQ(BitsToBytesRoundUp(bit_offset), non_header_size);
David Srbecky052f8ca2018-04-26 15:42:54 +01001074 }
1075
1076 size_t size_;
1077 MemoryRegion dex_register_maps_;
1078 uint32_t location_catalog_entries_;
1079 MemoryRegion location_catalog_;
1080 BitTable<StackMap::Field::kCount> stack_maps_;
1081 BitTable<InvokeInfo::Field::kCount> invoke_infos_;
1082 BitTable<InlineInfo::Field::kCount> inline_infos_;
David Srbecky4b59d102018-05-29 21:46:10 +00001083 BitTable<RegisterMask::Field::kCount> register_masks_;
1084 BitTable<1> stack_masks_;
David Srbecky052f8ca2018-04-26 15:42:54 +01001085
1086 friend class OatDumper;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001087};
1088
Roland Levillain1c1da432015-07-16 11:54:44 +01001089#undef ELEMENT_BYTE_OFFSET_AFTER
1090#undef ELEMENT_BIT_OFFSET_AFTER
1091
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001092} // namespace art
1093
1094#endif // ART_RUNTIME_STACK_MAP_H_