blob: cd9a3f04cffcc824c9acdd446289800cd9f9870d [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
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080020#include "arch/code_offset.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010021#include "base/bit_vector.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/bit_utils.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010023#include "dex_file.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010024#include "memory_region.h"
David Srbecky09ed0982016-02-12 21:58:43 +000025#include "leb128.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010026
27namespace art {
28
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010029class VariableIndentationOutputStream;
30
Roland Levillaina2d8ec62015-03-12 15:25:29 +000031// Size of a frame slot, in bytes. This constant is a signed value,
32// to please the compiler in arithmetic operations involving int32_t
33// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000034static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000035
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000036// Size of Dex virtual registers.
Roland Levillaina552e1c2015-03-26 15:01:03 +000037static constexpr size_t kVRegSize = 4;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000038
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000039class ArtMethod;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000040class CodeInfo;
David Brazdilf677ebf2015-05-29 16:29:43 +010041class StackMapEncoding;
David Srbecky09ed0982016-02-12 21:58:43 +000042struct CodeInfoEncoding;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000043
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
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100369 void Dump(VariableIndentationOutputStream* vios, const CodeInfo& code_info);
Roland Levillain0396ed72015-05-27 15:12:19 +0100370
Roland Levillaina552e1c2015-03-26 15:01:03 +0000371 // Special (invalid) Dex register location catalog entry index meaning
372 // that there is no location for a given Dex register (i.e., it is
373 // mapped to a DexRegisterLocation::Kind::kNone location).
374 static constexpr size_t kNoLocationEntryIndex = -1;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100375
Roland Levillain12baf472015-03-05 12:41:42 +0000376 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000377 static constexpr int kFixedSize = 0;
378
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000379 // Width of the kind "field" in a short location, in bits.
380 static constexpr size_t kKindBits = 3;
381 // Width of the value "field" in a short location, in bits.
382 static constexpr size_t kValueBits = 5;
383
384 static constexpr uint8_t kKindMask = (1 << kKindBits) - 1;
385 static constexpr int32_t kValueMask = (1 << kValueBits) - 1;
386 static constexpr size_t kKindOffset = 0;
387 static constexpr size_t kValueOffset = kKindBits;
388
Roland Levillaina552e1c2015-03-26 15:01:03 +0000389 static bool IsShortStackOffsetValue(int32_t value) {
390 DCHECK_EQ(value % kFrameSlotSize, 0);
391 return IsShortValue(value / kFrameSlotSize);
392 }
393
394 static bool IsShortConstantValue(int32_t value) {
395 return IsShortValue(value);
396 }
397
398 static bool IsShortValue(int32_t value) {
399 return IsUint<kValueBits>(value);
400 }
401
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000402 static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000403 uint8_t kind_integer_value = static_cast<uint8_t>(kind);
404 DCHECK(IsUint<kKindBits>(kind_integer_value)) << kind_integer_value;
405 DCHECK(IsShortValue(value)) << value;
406 return (kind_integer_value & kKindMask) << kKindOffset
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000407 | (value & kValueMask) << kValueOffset;
408 }
409
410 static DexRegisterLocation::Kind ExtractKindFromShortLocation(ShortLocation location) {
411 uint8_t kind = (location >> kKindOffset) & kKindMask;
412 DCHECK_LE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kLastLocationKind));
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000413 // We do not encode kNone locations in the stack map.
414 DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000415 return static_cast<DexRegisterLocation::Kind>(kind);
416 }
417
418 static int32_t ExtractValueFromShortLocation(ShortLocation location) {
419 return (location >> kValueOffset) & kValueMask;
420 }
421
422 // Extract a location kind from the byte at position `offset`.
423 DexRegisterLocation::Kind ExtractKindAtOffset(size_t offset) const {
424 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
425 return ExtractKindFromShortLocation(first_byte);
426 }
427
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100428 MemoryRegion region_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000429
430 friend class CodeInfo;
431 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100432};
433
Roland Levillaina552e1c2015-03-26 15:01:03 +0000434/* Information on Dex register locations for a specific PC, mapping a
435 * stack map's Dex register to a location entry in a DexRegisterLocationCatalog.
436 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100437 *
438 * [live_bit_mask, entries*]
439 *
Roland Levillaina552e1c2015-03-26 15:01:03 +0000440 * where entries are concatenated unsigned integer values encoded on a number
441 * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending
442 * on the number of entries in the Dex register location catalog
443 * (see DexRegisterMap::SingleEntrySizeInBits). The map is 1-byte aligned.
444 */
445class DexRegisterMap {
446 public:
447 explicit DexRegisterMap(MemoryRegion region) : region_(region) {}
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000448 DexRegisterMap() {}
449
450 bool IsValid() const { return region_.pointer() != nullptr; }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000451
452 // Get the surface kind of Dex register `dex_register_number`.
453 DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number,
454 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100455 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000456 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000457 return DexRegisterLocation::ConvertToSurfaceKind(
David Brazdilf677ebf2015-05-29 16:29:43 +0100458 GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info, enc));
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 Brazdilf677ebf2015-05-29 16:29:43 +0100464 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000465 const CodeInfoEncoding& enc) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000466
467 // Get the Dex register location `dex_register_number`.
468 DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number,
469 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100470 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000471 const CodeInfoEncoding& enc) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000472
473 int32_t GetStackOffsetInBytes(uint16_t dex_register_number,
474 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100475 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000476 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000477 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100478 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000479 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
480 // GetDexRegisterLocation returns the offset in bytes.
481 return location.GetValue();
482 }
483
484 int32_t GetConstant(uint16_t dex_register_number,
485 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100486 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000487 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000488 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100489 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
David Srbecky7dc11782016-02-25 13:23:56 +0000490 DCHECK_EQ(location.GetKind(), DexRegisterLocation::Kind::kConstant);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000491 return location.GetValue();
492 }
493
494 int32_t GetMachineRegister(uint16_t dex_register_number,
495 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100496 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000497 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000498 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100499 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
David Brazdild9cb68e2015-08-25 13:52:43 +0100500 DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister ||
501 location.GetInternalKind() == DexRegisterLocation::Kind::kInRegisterHigh ||
502 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister ||
503 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh)
David Srbecky7dc11782016-02-25 13:23:56 +0000504 << location.GetInternalKind();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000505 return location.GetValue();
506 }
507
508 // Get the index of the entry in the Dex register location catalog
509 // corresponding to `dex_register_number`.
510 size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number,
511 uint16_t number_of_dex_registers,
512 size_t number_of_location_catalog_entries) const {
513 if (!IsDexRegisterLive(dex_register_number)) {
514 return DexRegisterLocationCatalog::kNoLocationEntryIndex;
515 }
516
517 if (number_of_location_catalog_entries == 1) {
518 // We do not allocate space for location maps in the case of a
519 // single-entry location catalog, as it is useless. The only valid
520 // entry index is 0;
521 return 0;
522 }
523
524 // The bit offset of the beginning of the map locations.
525 size_t map_locations_offset_in_bits =
526 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
527 size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number);
528 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
529 // The bit size of an entry.
530 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
531 // The bit offset where `index_in_dex_register_map` is located.
532 size_t entry_offset_in_bits =
533 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
534 size_t location_catalog_entry_index =
535 region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits);
536 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
537 return location_catalog_entry_index;
538 }
539
540 // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`.
541 void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map,
542 size_t location_catalog_entry_index,
543 uint16_t number_of_dex_registers,
544 size_t number_of_location_catalog_entries) {
545 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
546 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
547
548 if (number_of_location_catalog_entries == 1) {
549 // We do not allocate space for location maps in the case of a
550 // single-entry location catalog, as it is useless.
551 return;
552 }
553
554 // The bit offset of the beginning of the map locations.
555 size_t map_locations_offset_in_bits =
556 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
557 // The bit size of an entry.
558 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
559 // The bit offset where `index_in_dex_register_map` is located.
560 size_t entry_offset_in_bits =
561 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
562 region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits);
563 }
564
565 void SetLiveBitMask(uint16_t number_of_dex_registers,
566 const BitVector& live_dex_registers_mask) {
567 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
568 for (uint16_t i = 0; i < number_of_dex_registers; ++i) {
569 region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i));
570 }
571 }
572
573 bool IsDexRegisterLive(uint16_t dex_register_number) const {
574 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
575 return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number);
576 }
577
578 size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const {
579 size_t number_of_live_dex_registers = 0;
580 for (size_t i = 0; i < number_of_dex_registers; ++i) {
581 if (IsDexRegisterLive(i)) {
582 ++number_of_live_dex_registers;
583 }
584 }
585 return number_of_live_dex_registers;
586 }
587
588 static size_t GetLiveBitMaskOffset() {
589 return kFixedSize;
590 }
591
592 // Compute the size of the live register bit mask (in bytes), for a
593 // method having `number_of_dex_registers` Dex registers.
594 static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) {
595 return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte;
596 }
597
598 static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) {
599 return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers);
600 }
601
602 size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers,
603 size_t number_of_location_catalog_entries) const {
604 size_t location_mapping_data_size_in_bits =
605 GetNumberOfLiveDexRegisters(number_of_dex_registers)
606 * SingleEntrySizeInBits(number_of_location_catalog_entries);
607 return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
608 }
609
610 // Return the size of a map entry in bits. Note that if
611 // `number_of_location_catalog_entries` equals 1, this function returns 0,
612 // which is fine, as there is no need to allocate a map for a
613 // single-entry location catalog; the only valid location catalog entry index
614 // for a live register in this case is 0 and there is no need to
615 // store it.
616 static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) {
617 // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2.
618 return number_of_location_catalog_entries == 0
619 ? 0u
620 : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries));
621 }
622
623 // Return the size of the DexRegisterMap object, in bytes.
624 size_t Size() const {
625 return region_.size();
626 }
627
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100628 void Dump(VariableIndentationOutputStream* vios,
629 const CodeInfo& code_info, uint16_t number_of_dex_registers) const;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100630
Roland Levillaina552e1c2015-03-26 15:01:03 +0000631 private:
632 // Return the index in the Dex register map corresponding to the Dex
633 // register number `dex_register_number`.
634 size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const {
635 if (!IsDexRegisterLive(dex_register_number)) {
636 return kInvalidIndexInDexRegisterMap;
637 }
638 return GetNumberOfLiveDexRegisters(dex_register_number);
639 }
640
641 // Special (invalid) Dex register map entry index meaning that there
642 // is no index in the map for a given Dex register (i.e., it must
643 // have been mapped to a DexRegisterLocation::Kind::kNone location).
644 static constexpr size_t kInvalidIndexInDexRegisterMap = -1;
645
646 static constexpr int kFixedSize = 0;
647
648 MemoryRegion region_;
649
650 friend class CodeInfo;
651 friend class StackMapStream;
652};
653
David Srbecky09ed0982016-02-12 21:58:43 +0000654// Represents bit range of bit-packed integer field.
655// We reuse the idea from ULEB128p1 to support encoding of -1 (aka 0xFFFFFFFF).
656// If min_value is set to -1, we implicitly subtract one from any loaded value,
657// and add one to any stored value. This is generalized to any negative values.
658// In other words, min_value acts as a base and the stored value is added to it.
659struct FieldEncoding {
660 FieldEncoding(size_t start_offset, size_t end_offset, int32_t min_value = 0)
661 : start_offset_(start_offset), end_offset_(end_offset), min_value_(min_value) {
662 DCHECK_LE(start_offset_, end_offset_);
663 DCHECK_LE(BitSize(), 32u);
664 }
665
666 ALWAYS_INLINE size_t BitSize() const { return end_offset_ - start_offset_; }
667
668 ALWAYS_INLINE int32_t Load(const MemoryRegion& region) const {
669 DCHECK_LE(end_offset_, region.size_in_bits());
670 const size_t bit_count = BitSize();
671 if (bit_count == 0) {
672 // Do not touch any memory if the range is empty.
673 return min_value_;
674 }
675 uint8_t* address = region.start() + start_offset_ / kBitsPerByte;
676 const uint32_t shift = start_offset_ & (kBitsPerByte - 1);
677 // Load the value (reading only the strictly needed bytes).
678 const uint32_t load_bit_count = shift + bit_count;
679 uint32_t value = *address++ >> shift;
680 if (load_bit_count > 8) {
681 value |= static_cast<uint32_t>(*address++) << (8 - shift);
682 if (load_bit_count > 16) {
683 value |= static_cast<uint32_t>(*address++) << (16 - shift);
684 if (load_bit_count > 24) {
685 value |= static_cast<uint32_t>(*address++) << (24 - shift);
686 if (load_bit_count > 32) {
687 value |= static_cast<uint32_t>(*address++) << (32 - shift);
688 }
689 }
690 }
691 }
692 // Clear unwanted most significant bits.
693 uint32_t clear_bit_count = 32 - bit_count;
694 value = (value << clear_bit_count) >> clear_bit_count;
695 return value + min_value_;
696 }
697
698 ALWAYS_INLINE void Store(MemoryRegion region, int32_t value) const {
699 region.StoreBits(start_offset_, value - min_value_, BitSize());
700 DCHECK_EQ(Load(region), value);
701 }
702
703 private:
704 size_t start_offset_;
705 size_t end_offset_;
706 int32_t min_value_;
707};
708
David Brazdilf677ebf2015-05-29 16:29:43 +0100709class StackMapEncoding {
710 public:
711 StackMapEncoding() {}
712
David Srbecky09ed0982016-02-12 21:58:43 +0000713 // Set stack map bit layout based on given sizes.
714 // Returns the size of stack map in bytes.
715 size_t SetFromSizes(size_t native_pc_max,
716 size_t dex_pc_max,
717 size_t dex_register_map_size,
718 size_t inline_info_size,
719 size_t register_mask_max,
720 size_t stack_mask_bit_size) {
721 size_t bit_offset = 0;
722 DCHECK_EQ(kNativePcBitOffset, bit_offset);
723 bit_offset += MinimumBitsToStore(native_pc_max);
David Brazdilf677ebf2015-05-29 16:29:43 +0100724
David Srbecky09ed0982016-02-12 21:58:43 +0000725 dex_pc_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
726 bit_offset += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max);
727
728 // We also need +1 for kNoDexRegisterMap, but since the size is strictly
729 // greater than any offset we might try to encode, we already implicitly have it.
730 dex_register_map_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
731 bit_offset += MinimumBitsToStore(dex_register_map_size);
732
733 // We also need +1 for kNoInlineInfo, but since the inline_info_size is strictly
734 // greater than the offset we might try to encode, we already implicitly have it.
735 // If inline_info_size is zero, we can encode only kNoInlineInfo (in zero bits).
736 inline_info_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
737 if (inline_info_size != 0) {
738 bit_offset += MinimumBitsToStore(dex_register_map_size + inline_info_size);
739 }
740
741 register_mask_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
742 bit_offset += MinimumBitsToStore(register_mask_max);
743
744 stack_mask_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
745 bit_offset += stack_mask_bit_size;
746
747 return RoundUp(bit_offset, kBitsPerByte) / kBitsPerByte;
David Brazdilf677ebf2015-05-29 16:29:43 +0100748 }
749
David Srbecky09ed0982016-02-12 21:58:43 +0000750 ALWAYS_INLINE FieldEncoding GetNativePcEncoding() const {
751 return FieldEncoding(kNativePcBitOffset, dex_pc_bit_offset_);
752 }
753 ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const {
754 return FieldEncoding(dex_pc_bit_offset_, dex_register_map_bit_offset_, -1 /* min_value */);
755 }
756 ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const {
757 return FieldEncoding(dex_register_map_bit_offset_, inline_info_bit_offset_, -1 /* min_value */);
758 }
759 ALWAYS_INLINE FieldEncoding GetInlineInfoEncoding() const {
760 return FieldEncoding(inline_info_bit_offset_, register_mask_bit_offset_, -1 /* min_value */);
761 }
762 ALWAYS_INLINE FieldEncoding GetRegisterMaskEncoding() const {
763 return FieldEncoding(register_mask_bit_offset_, stack_mask_bit_offset_);
764 }
765 ALWAYS_INLINE size_t GetStackMaskBitOffset() const {
766 // The end offset is not encoded. It is implicitly the end of stack map entry.
767 return stack_mask_bit_offset_;
David Brazdilf677ebf2015-05-29 16:29:43 +0100768 }
769
David Srbecky09ed0982016-02-12 21:58:43 +0000770 void Dump(VariableIndentationOutputStream* vios) const;
David Brazdilf677ebf2015-05-29 16:29:43 +0100771
772 private:
David Srbecky09ed0982016-02-12 21:58:43 +0000773 static constexpr size_t kNativePcBitOffset = 0;
774 uint8_t dex_pc_bit_offset_;
775 uint8_t dex_register_map_bit_offset_;
776 uint8_t inline_info_bit_offset_;
777 uint8_t register_mask_bit_offset_;
778 uint8_t stack_mask_bit_offset_;
David Brazdilf677ebf2015-05-29 16:29:43 +0100779};
780
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100781/**
782 * A Stack Map holds compilation information for a specific PC necessary for:
783 * - Mapping it to a dex PC,
784 * - Knowing which stack entries are objects,
785 * - Knowing which registers hold objects,
786 * - Knowing the inlining information,
787 * - Knowing the values of dex registers.
788 *
789 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100790 *
David Srbecky09ed0982016-02-12 21:58:43 +0000791 * [native_pc_offset, dex_pc, dex_register_map_offset, inlining_info_offset, register_mask,
Roland Levillain1c1da432015-07-16 11:54:44 +0100792 * stack_mask].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100793 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100794class StackMap {
795 public:
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100796 StackMap() {}
David Brazdilf677ebf2015-05-29 16:29:43 +0100797 explicit StackMap(MemoryRegion region) : region_(region) {}
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100798
David Srbecky09ed0982016-02-12 21:58:43 +0000799 ALWAYS_INLINE bool IsValid() const { return region_.pointer() != nullptr; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100800
David Srbecky09ed0982016-02-12 21:58:43 +0000801 ALWAYS_INLINE uint32_t GetDexPc(const StackMapEncoding& encoding) const {
802 return encoding.GetDexPcEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100803 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100804
David Srbecky09ed0982016-02-12 21:58:43 +0000805 ALWAYS_INLINE void SetDexPc(const StackMapEncoding& encoding, uint32_t dex_pc) {
806 encoding.GetDexPcEncoding().Store(region_, dex_pc);
David Brazdilf677ebf2015-05-29 16:29:43 +0100807 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100808
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800809 ALWAYS_INLINE uint32_t GetNativePcOffset(const StackMapEncoding& encoding,
810 InstructionSet instruction_set) const {
811 CodeOffset offset(
812 CodeOffset::FromCompressedOffset(encoding.GetNativePcEncoding().Load(region_)));
813 return offset.Uint32Value(instruction_set);
David Brazdilf677ebf2015-05-29 16:29:43 +0100814 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100815
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800816 ALWAYS_INLINE void SetNativePcCodeOffset(const StackMapEncoding& encoding,
817 CodeOffset native_pc_offset) {
818 encoding.GetNativePcEncoding().Store(region_, native_pc_offset.CompressedValue());
David Brazdilf677ebf2015-05-29 16:29:43 +0100819 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100820
David Srbecky09ed0982016-02-12 21:58:43 +0000821 ALWAYS_INLINE uint32_t GetDexRegisterMapOffset(const StackMapEncoding& encoding) const {
822 return encoding.GetDexRegisterMapEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100823 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100824
David Srbecky09ed0982016-02-12 21:58:43 +0000825 ALWAYS_INLINE void SetDexRegisterMapOffset(const StackMapEncoding& encoding, uint32_t offset) {
826 encoding.GetDexRegisterMapEncoding().Store(region_, offset);
David Brazdilf677ebf2015-05-29 16:29:43 +0100827 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100828
David Srbecky09ed0982016-02-12 21:58:43 +0000829 ALWAYS_INLINE uint32_t GetInlineDescriptorOffset(const StackMapEncoding& encoding) const {
830 return encoding.GetInlineInfoEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100831 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100832
David Srbecky09ed0982016-02-12 21:58:43 +0000833 ALWAYS_INLINE void SetInlineDescriptorOffset(const StackMapEncoding& encoding, uint32_t offset) {
834 encoding.GetInlineInfoEncoding().Store(region_, offset);
David Brazdilf677ebf2015-05-29 16:29:43 +0100835 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100836
David Srbecky09ed0982016-02-12 21:58:43 +0000837 ALWAYS_INLINE uint32_t GetRegisterMask(const StackMapEncoding& encoding) const {
838 return encoding.GetRegisterMaskEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100839 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100840
David Srbecky09ed0982016-02-12 21:58:43 +0000841 ALWAYS_INLINE void SetRegisterMask(const StackMapEncoding& encoding, uint32_t mask) {
842 encoding.GetRegisterMaskEncoding().Store(region_, mask);
David Brazdilf677ebf2015-05-29 16:29:43 +0100843 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100844
David Srbecky09ed0982016-02-12 21:58:43 +0000845 ALWAYS_INLINE size_t GetNumberOfStackMaskBits(const StackMapEncoding& encoding) const {
846 return region_.size_in_bits() - encoding.GetStackMaskBitOffset();
David Brazdilf677ebf2015-05-29 16:29:43 +0100847 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100848
David Srbecky09ed0982016-02-12 21:58:43 +0000849 ALWAYS_INLINE bool GetStackMaskBit(const StackMapEncoding& encoding, size_t index) const {
850 return region_.LoadBit(encoding.GetStackMaskBitOffset() + index);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100851 }
852
David Srbecky09ed0982016-02-12 21:58:43 +0000853 ALWAYS_INLINE void SetStackMaskBit(const StackMapEncoding& encoding, size_t index, bool value) {
854 region_.StoreBit(encoding.GetStackMaskBitOffset() + index, value);
855 }
856
857 ALWAYS_INLINE bool HasDexRegisterMap(const StackMapEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +0100858 return GetDexRegisterMapOffset(encoding) != kNoDexRegisterMap;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100859 }
860
David Srbecky09ed0982016-02-12 21:58:43 +0000861 ALWAYS_INLINE bool HasInlineInfo(const StackMapEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +0100862 return GetInlineDescriptorOffset(encoding) != kNoInlineInfo;
Roland Levillain442b46a2015-02-18 16:54:21 +0000863 }
864
David Srbecky09ed0982016-02-12 21:58:43 +0000865 ALWAYS_INLINE bool Equals(const StackMap& other) const {
866 return region_.pointer() == other.region_.pointer() && region_.size() == other.region_.size();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100867 }
868
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100869 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100870 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000871 const CodeInfoEncoding& encoding,
Roland Levillainf2650d12015-05-28 14:53:28 +0100872 uint32_t code_offset,
873 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800874 InstructionSet instruction_set,
Roland Levillainf2650d12015-05-28 14:53:28 +0100875 const std::string& header_suffix = "") const;
876
Roland Levillain442b46a2015-02-18 16:54:21 +0000877 // Special (invalid) offset for the DexRegisterMapOffset field meaning
878 // that there is no Dex register map for this stack map.
879 static constexpr uint32_t kNoDexRegisterMap = -1;
880
881 // Special (invalid) offset for the InlineDescriptorOffset field meaning
882 // that there is no inline info for this stack map.
883 static constexpr uint32_t kNoInlineInfo = -1;
884
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100885 private:
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100886 static constexpr int kFixedSize = 0;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100887
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100888 MemoryRegion region_;
889
Nicolas Geoffray39468442014-09-02 15:17:15 +0100890 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100891};
892
David Srbecky61b28a12016-02-25 21:55:03 +0000893class InlineInfoEncoding {
894 public:
895 void SetFromSizes(size_t method_index_max,
896 size_t dex_pc_max,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000897 size_t extra_data_max,
David Srbecky61b28a12016-02-25 21:55:03 +0000898 size_t dex_register_map_size) {
899 total_bit_size_ = kMethodIndexBitOffset;
900 total_bit_size_ += MinimumBitsToStore(method_index_max);
901
902 dex_pc_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100903 // Note: We're not encoding the dex pc if there is none. That's the case
904 // for an intrinsified native method, such as String.charAt().
905 if (dex_pc_max != DexFile::kDexNoIndex) {
906 total_bit_size_ += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max);
907 }
David Srbecky61b28a12016-02-25 21:55:03 +0000908
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000909 extra_data_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
910 total_bit_size_ += MinimumBitsToStore(extra_data_max);
David Srbecky61b28a12016-02-25 21:55:03 +0000911
912 // We also need +1 for kNoDexRegisterMap, but since the size is strictly
913 // greater than any offset we might try to encode, we already implicitly have it.
914 dex_register_map_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
915 total_bit_size_ += MinimumBitsToStore(dex_register_map_size);
916 }
917
918 ALWAYS_INLINE FieldEncoding GetMethodIndexEncoding() const {
919 return FieldEncoding(kMethodIndexBitOffset, dex_pc_bit_offset_);
920 }
921 ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000922 return FieldEncoding(dex_pc_bit_offset_, extra_data_bit_offset_, -1 /* min_value */);
David Srbecky61b28a12016-02-25 21:55:03 +0000923 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000924 ALWAYS_INLINE FieldEncoding GetExtraDataEncoding() const {
925 return FieldEncoding(extra_data_bit_offset_, dex_register_map_bit_offset_);
David Srbecky61b28a12016-02-25 21:55:03 +0000926 }
927 ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const {
928 return FieldEncoding(dex_register_map_bit_offset_, total_bit_size_, -1 /* min_value */);
929 }
930 ALWAYS_INLINE size_t GetEntrySize() const {
931 return RoundUp(total_bit_size_, kBitsPerByte) / kBitsPerByte;
932 }
933
934 void Dump(VariableIndentationOutputStream* vios) const;
935
936 private:
937 static constexpr uint8_t kIsLastBitOffset = 0;
938 static constexpr uint8_t kMethodIndexBitOffset = 1;
939 uint8_t dex_pc_bit_offset_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000940 uint8_t extra_data_bit_offset_;
David Srbecky61b28a12016-02-25 21:55:03 +0000941 uint8_t dex_register_map_bit_offset_;
942 uint8_t total_bit_size_;
943};
944
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100945/**
946 * Inline information for a specific PC. The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100947 *
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000948 * [is_last,
949 * method_index (or ArtMethod high bits),
950 * dex_pc,
951 * extra_data (ArtMethod low bits or 1),
952 * dex_register_map_offset]+.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100953 */
954class InlineInfo {
955 public:
David Srbecky61b28a12016-02-25 21:55:03 +0000956 explicit InlineInfo(MemoryRegion region) : region_(region) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100957 }
958
David Srbecky61b28a12016-02-25 21:55:03 +0000959 ALWAYS_INLINE uint32_t GetDepth(const InlineInfoEncoding& encoding) const {
960 size_t depth = 0;
961 while (!GetRegionAtDepth(encoding, depth++).LoadBit(0)) { } // Check is_last bit.
962 return depth;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100963 }
964
David Srbecky61b28a12016-02-25 21:55:03 +0000965 ALWAYS_INLINE void SetDepth(const InlineInfoEncoding& encoding, uint32_t depth) {
966 DCHECK_GT(depth, 0u);
967 for (size_t d = 0; d < depth; ++d) {
968 GetRegionAtDepth(encoding, d).StoreBit(0, d == depth - 1); // Set is_last bit.
969 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100970 }
971
David Srbecky61b28a12016-02-25 21:55:03 +0000972 ALWAYS_INLINE uint32_t GetMethodIndexAtDepth(const InlineInfoEncoding& encoding,
973 uint32_t depth) const {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000974 DCHECK(!EncodesArtMethodAtDepth(encoding, depth));
David Srbecky61b28a12016-02-25 21:55:03 +0000975 return encoding.GetMethodIndexEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100976 }
977
David Srbecky61b28a12016-02-25 21:55:03 +0000978 ALWAYS_INLINE void SetMethodIndexAtDepth(const InlineInfoEncoding& encoding,
979 uint32_t depth,
980 uint32_t index) {
981 encoding.GetMethodIndexEncoding().Store(GetRegionAtDepth(encoding, depth), index);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100982 }
983
David Srbecky61b28a12016-02-25 21:55:03 +0000984 ALWAYS_INLINE uint32_t GetDexPcAtDepth(const InlineInfoEncoding& encoding,
985 uint32_t depth) const {
986 return encoding.GetDexPcEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100987 }
988
David Srbecky61b28a12016-02-25 21:55:03 +0000989 ALWAYS_INLINE void SetDexPcAtDepth(const InlineInfoEncoding& encoding,
990 uint32_t depth,
991 uint32_t dex_pc) {
992 encoding.GetDexPcEncoding().Store(GetRegionAtDepth(encoding, depth), dex_pc);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100993 }
994
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000995 ALWAYS_INLINE bool EncodesArtMethodAtDepth(const InlineInfoEncoding& encoding,
996 uint32_t depth) const {
997 return (encoding.GetExtraDataEncoding().Load(GetRegionAtDepth(encoding, depth)) & 1) == 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100998 }
999
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001000 ALWAYS_INLINE void SetExtraDataAtDepth(const InlineInfoEncoding& encoding,
1001 uint32_t depth,
1002 uint32_t extra_data) {
1003 encoding.GetExtraDataEncoding().Store(GetRegionAtDepth(encoding, depth), extra_data);
1004 }
1005
1006 ALWAYS_INLINE ArtMethod* GetArtMethodAtDepth(const InlineInfoEncoding& encoding,
1007 uint32_t depth) const {
1008 uint32_t low_bits = encoding.GetExtraDataEncoding().Load(GetRegionAtDepth(encoding, depth));
1009 uint32_t high_bits = encoding.GetMethodIndexEncoding().Load(GetRegionAtDepth(encoding, depth));
1010 if (high_bits == 0) {
1011 return reinterpret_cast<ArtMethod*>(low_bits);
1012 } else {
1013 uint64_t address = high_bits;
1014 address = address << 32;
1015 return reinterpret_cast<ArtMethod*>(address | low_bits);
1016 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001017 }
1018
David Srbecky61b28a12016-02-25 21:55:03 +00001019 ALWAYS_INLINE uint32_t GetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding,
1020 uint32_t depth) const {
1021 return encoding.GetDexRegisterMapEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001022 }
1023
David Srbecky61b28a12016-02-25 21:55:03 +00001024 ALWAYS_INLINE void SetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding,
1025 uint32_t depth,
1026 uint32_t offset) {
1027 encoding.GetDexRegisterMapEncoding().Store(GetRegionAtDepth(encoding, depth), offset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001028 }
1029
David Srbecky61b28a12016-02-25 21:55:03 +00001030 ALWAYS_INLINE bool HasDexRegisterMapAtDepth(const InlineInfoEncoding& encoding,
1031 uint32_t depth) const {
1032 return GetDexRegisterMapOffsetAtDepth(encoding, depth) != StackMap::kNoDexRegisterMap;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001033 }
1034
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001035 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +00001036 const CodeInfo& info,
1037 uint16_t* number_of_dex_registers) const;
Roland Levillain1c1da432015-07-16 11:54:44 +01001038
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001039 private:
David Srbecky61b28a12016-02-25 21:55:03 +00001040 ALWAYS_INLINE MemoryRegion GetRegionAtDepth(const InlineInfoEncoding& encoding,
1041 uint32_t depth) const {
1042 size_t entry_size = encoding.GetEntrySize();
1043 DCHECK_GT(entry_size, 0u);
1044 return region_.Subregion(depth * entry_size, entry_size);
1045 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001046
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001047 MemoryRegion region_;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001048};
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001049
David Srbecky09ed0982016-02-12 21:58:43 +00001050// Most of the fields are encoded as ULEB128 to save space.
1051struct CodeInfoEncoding {
1052 uint32_t non_header_size;
1053 uint32_t number_of_stack_maps;
1054 uint32_t stack_map_size_in_bytes;
1055 uint32_t number_of_location_catalog_entries;
1056 StackMapEncoding stack_map_encoding;
David Srbecky61b28a12016-02-25 21:55:03 +00001057 InlineInfoEncoding inline_info_encoding;
David Srbecky09ed0982016-02-12 21:58:43 +00001058 uint8_t header_size;
1059
1060 CodeInfoEncoding() { }
1061
1062 explicit CodeInfoEncoding(const void* data) {
1063 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
1064 non_header_size = DecodeUnsignedLeb128(&ptr);
1065 number_of_stack_maps = DecodeUnsignedLeb128(&ptr);
1066 stack_map_size_in_bytes = DecodeUnsignedLeb128(&ptr);
1067 number_of_location_catalog_entries = DecodeUnsignedLeb128(&ptr);
David Srbecky61b28a12016-02-25 21:55:03 +00001068 static_assert(alignof(StackMapEncoding) == 1,
1069 "StackMapEncoding should not require alignment");
David Srbecky09ed0982016-02-12 21:58:43 +00001070 stack_map_encoding = *reinterpret_cast<const StackMapEncoding*>(ptr);
1071 ptr += sizeof(StackMapEncoding);
David Srbecky61b28a12016-02-25 21:55:03 +00001072 if (stack_map_encoding.GetInlineInfoEncoding().BitSize() > 0) {
1073 static_assert(alignof(InlineInfoEncoding) == 1,
1074 "InlineInfoEncoding should not require alignment");
1075 inline_info_encoding = *reinterpret_cast<const InlineInfoEncoding*>(ptr);
1076 ptr += sizeof(InlineInfoEncoding);
1077 } else {
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001078 inline_info_encoding = InlineInfoEncoding{}; // NOLINT.
David Srbecky61b28a12016-02-25 21:55:03 +00001079 }
David Srbecky09ed0982016-02-12 21:58:43 +00001080 header_size = dchecked_integral_cast<uint8_t>(ptr - reinterpret_cast<const uint8_t*>(data));
1081 }
1082
1083 template<typename Vector>
1084 void Compress(Vector* dest) const {
1085 EncodeUnsignedLeb128(dest, non_header_size);
1086 EncodeUnsignedLeb128(dest, number_of_stack_maps);
1087 EncodeUnsignedLeb128(dest, stack_map_size_in_bytes);
1088 EncodeUnsignedLeb128(dest, number_of_location_catalog_entries);
David Srbecky61b28a12016-02-25 21:55:03 +00001089 const uint8_t* stack_map_ptr = reinterpret_cast<const uint8_t*>(&stack_map_encoding);
1090 dest->insert(dest->end(), stack_map_ptr, stack_map_ptr + sizeof(StackMapEncoding));
1091 if (stack_map_encoding.GetInlineInfoEncoding().BitSize() > 0) {
1092 const uint8_t* inline_info_ptr = reinterpret_cast<const uint8_t*>(&inline_info_encoding);
1093 dest->insert(dest->end(), inline_info_ptr, inline_info_ptr + sizeof(InlineInfoEncoding));
1094 }
David Srbecky09ed0982016-02-12 21:58:43 +00001095 }
1096};
1097
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001098/**
1099 * Wrapper around all compiler information collected for a method.
1100 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +01001101 *
David Srbecky09ed0982016-02-12 21:58:43 +00001102 * [CodeInfoEncoding, StackMap+, DexRegisterLocationCatalog+, DexRegisterMap+, InlineInfo*]
Roland Levillain1c1da432015-07-16 11:54:44 +01001103 *
David Srbecky09ed0982016-02-12 21:58:43 +00001104 * where CodeInfoEncoding is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +01001105 *
David Srbecky09ed0982016-02-12 21:58:43 +00001106 * [non_header_size, number_of_stack_maps, stack_map_size_in_bytes,
1107 * number_of_location_catalog_entries, StackMapEncoding]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001108 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001109class CodeInfo {
1110 public:
David Srbecky09ed0982016-02-12 21:58:43 +00001111 explicit CodeInfo(MemoryRegion region) : region_(region) {
1112 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001113
Nicolas Geoffray39468442014-09-02 15:17:15 +01001114 explicit CodeInfo(const void* data) {
David Srbecky09ed0982016-02-12 21:58:43 +00001115 CodeInfoEncoding encoding = CodeInfoEncoding(data);
1116 region_ = MemoryRegion(const_cast<void*>(data),
1117 encoding.header_size + encoding.non_header_size);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001118 }
1119
David Srbecky09ed0982016-02-12 21:58:43 +00001120 CodeInfoEncoding ExtractEncoding() const {
Mathieu Chartier01c78142017-01-05 10:17:55 -08001121 CodeInfoEncoding encoding(region_.start());
1122 AssertValidStackMap(encoding);
1123 return encoding;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001124 }
1125
David Srbecky09ed0982016-02-12 21:58:43 +00001126 bool HasInlineInfo(const CodeInfoEncoding& encoding) const {
1127 return encoding.stack_map_encoding.GetInlineInfoEncoding().BitSize() > 0;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001128 }
1129
David Srbecky09ed0982016-02-12 21:58:43 +00001130 DexRegisterLocationCatalog GetDexRegisterLocationCatalog(const CodeInfoEncoding& encoding) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +00001131 return DexRegisterLocationCatalog(region_.Subregion(
David Brazdilf677ebf2015-05-29 16:29:43 +01001132 GetDexRegisterLocationCatalogOffset(encoding),
1133 GetDexRegisterLocationCatalogSize(encoding)));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001134 }
1135
Mingyao Yangccfa8852017-01-18 14:51:59 -08001136 ALWAYS_INLINE StackMap GetStackMapAt(size_t i, const CodeInfoEncoding& encoding) const {
David Srbecky09ed0982016-02-12 21:58:43 +00001137 size_t stack_map_size = encoding.stack_map_size_in_bytes;
David Brazdilf677ebf2015-05-29 16:29:43 +01001138 return StackMap(GetStackMaps(encoding).Subregion(i * stack_map_size, stack_map_size));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001139 }
1140
David Srbecky09ed0982016-02-12 21:58:43 +00001141 uint32_t GetNumberOfLocationCatalogEntries(const CodeInfoEncoding& encoding) const {
1142 return encoding.number_of_location_catalog_entries;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001143 }
1144
David Srbecky09ed0982016-02-12 21:58:43 +00001145 uint32_t GetDexRegisterLocationCatalogSize(const CodeInfoEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +01001146 return ComputeDexRegisterLocationCatalogSize(GetDexRegisterLocationCatalogOffset(encoding),
David Srbecky09ed0982016-02-12 21:58:43 +00001147 GetNumberOfLocationCatalogEntries(encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001148 }
1149
David Srbecky09ed0982016-02-12 21:58:43 +00001150 uint32_t GetNumberOfStackMaps(const CodeInfoEncoding& encoding) const {
1151 return encoding.number_of_stack_maps;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001152 }
1153
David Brazdil77a48ae2015-09-15 12:34:04 +00001154 // Get the size of all the stack maps of this CodeInfo object, in bytes.
David Srbecky09ed0982016-02-12 21:58:43 +00001155 size_t GetStackMapsSize(const CodeInfoEncoding& encoding) const {
1156 return encoding.stack_map_size_in_bytes * GetNumberOfStackMaps(encoding);
Roland Levillain29ba1b02015-03-13 11:45:07 +00001157 }
1158
David Srbecky09ed0982016-02-12 21:58:43 +00001159 uint32_t GetDexRegisterLocationCatalogOffset(const CodeInfoEncoding& encoding) const {
1160 return GetStackMapsOffset(encoding) + GetStackMapsSize(encoding);
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001161 }
1162
David Srbecky09ed0982016-02-12 21:58:43 +00001163 size_t GetDexRegisterMapsOffset(const CodeInfoEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +01001164 return GetDexRegisterLocationCatalogOffset(encoding)
1165 + GetDexRegisterLocationCatalogSize(encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001166 }
1167
David Srbecky09ed0982016-02-12 21:58:43 +00001168 uint32_t GetStackMapsOffset(const CodeInfoEncoding& encoding) const {
1169 return encoding.header_size;
Nicolas Geoffray6530baf2015-05-26 15:22:58 +01001170 }
1171
David Brazdilf677ebf2015-05-29 16:29:43 +01001172 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map,
David Srbecky09ed0982016-02-12 21:58:43 +00001173 const CodeInfoEncoding& encoding,
David Brazdilf677ebf2015-05-29 16:29:43 +01001174 uint32_t number_of_dex_registers) const {
David Srbecky09ed0982016-02-12 21:58:43 +00001175 if (!stack_map.HasDexRegisterMap(encoding.stack_map_encoding)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001176 return DexRegisterMap();
1177 } else {
1178 uint32_t offset = GetDexRegisterMapsOffset(encoding)
David Srbecky09ed0982016-02-12 21:58:43 +00001179 + stack_map.GetDexRegisterMapOffset(encoding.stack_map_encoding);
1180 size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001181 return DexRegisterMap(region_.Subregion(offset, size));
1182 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001183 }
1184
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001185 size_t GetDexRegisterMapsSize(const CodeInfoEncoding& encoding,
1186 uint32_t number_of_dex_registers) const {
1187 size_t total = 0;
1188 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
1189 StackMap stack_map = GetStackMapAt(i, encoding);
1190 DexRegisterMap map(GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers));
1191 total += map.Size();
1192 }
1193 return total;
1194 }
1195
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001196 // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`.
1197 DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
1198 InlineInfo inline_info,
David Srbecky09ed0982016-02-12 21:58:43 +00001199 const CodeInfoEncoding& encoding,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001200 uint32_t number_of_dex_registers) const {
David Srbecky61b28a12016-02-25 21:55:03 +00001201 if (!inline_info.HasDexRegisterMapAtDepth(encoding.inline_info_encoding, depth)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001202 return DexRegisterMap();
1203 } else {
David Srbecky61b28a12016-02-25 21:55:03 +00001204 uint32_t offset = GetDexRegisterMapsOffset(encoding) +
1205 inline_info.GetDexRegisterMapOffsetAtDepth(encoding.inline_info_encoding, depth);
David Srbecky09ed0982016-02-12 21:58:43 +00001206 size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001207 return DexRegisterMap(region_.Subregion(offset, size));
1208 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001209 }
1210
David Srbecky09ed0982016-02-12 21:58:43 +00001211 InlineInfo GetInlineInfoOf(StackMap stack_map, const CodeInfoEncoding& encoding) const {
1212 DCHECK(stack_map.HasInlineInfo(encoding.stack_map_encoding));
1213 uint32_t offset = stack_map.GetInlineDescriptorOffset(encoding.stack_map_encoding)
David Brazdilf677ebf2015-05-29 16:29:43 +01001214 + GetDexRegisterMapsOffset(encoding);
David Srbecky61b28a12016-02-25 21:55:03 +00001215 return InlineInfo(region_.Subregion(offset, region_.size() - offset));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001216 }
1217
David Srbecky09ed0982016-02-12 21:58:43 +00001218 StackMap GetStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1219 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +01001220 StackMap stack_map = GetStackMapAt(i, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001221 if (stack_map.GetDexPc(encoding.stack_map_encoding) == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001222 return stack_map;
1223 }
1224 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001225 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001226 }
1227
David Brazdil77a48ae2015-09-15 12:34:04 +00001228 // Searches the stack map list backwards because catch stack maps are stored
1229 // at the end.
David Srbecky09ed0982016-02-12 21:58:43 +00001230 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1231 for (size_t i = GetNumberOfStackMaps(encoding); i > 0; --i) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001232 StackMap stack_map = GetStackMapAt(i - 1, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001233 if (stack_map.GetDexPc(encoding.stack_map_encoding) == dex_pc) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001234 return stack_map;
1235 }
1236 }
1237 return StackMap();
1238 }
1239
David Srbecky09ed0982016-02-12 21:58:43 +00001240 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1241 size_t e = GetNumberOfStackMaps(encoding);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001242 if (e == 0) {
1243 // There cannot be OSR stack map if there is no stack map.
1244 return StackMap();
1245 }
1246 // Walk over all stack maps. If two consecutive stack maps are identical, then we
1247 // have found a stack map suitable for OSR.
David Srbecky09ed0982016-02-12 21:58:43 +00001248 const StackMapEncoding& stack_map_encoding = encoding.stack_map_encoding;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001249 for (size_t i = 0; i < e - 1; ++i) {
1250 StackMap stack_map = GetStackMapAt(i, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001251 if (stack_map.GetDexPc(stack_map_encoding) == dex_pc) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001252 StackMap other = GetStackMapAt(i + 1, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001253 if (other.GetDexPc(stack_map_encoding) == dex_pc &&
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001254 other.GetNativePcOffset(stack_map_encoding, kRuntimeISA) ==
1255 stack_map.GetNativePcOffset(stack_map_encoding, kRuntimeISA)) {
David Srbecky09ed0982016-02-12 21:58:43 +00001256 DCHECK_EQ(other.GetDexRegisterMapOffset(stack_map_encoding),
1257 stack_map.GetDexRegisterMapOffset(stack_map_encoding));
1258 DCHECK(!stack_map.HasInlineInfo(stack_map_encoding));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001259 if (i < e - 2) {
1260 // Make sure there are not three identical stack maps following each other.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001261 DCHECK_NE(
1262 stack_map.GetNativePcOffset(stack_map_encoding, kRuntimeISA),
1263 GetStackMapAt(i + 2, encoding).GetNativePcOffset(stack_map_encoding, kRuntimeISA));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001264 }
1265 return stack_map;
1266 }
1267 }
1268 }
1269 return StackMap();
1270 }
1271
David Brazdilf677ebf2015-05-29 16:29:43 +01001272 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset,
David Srbecky09ed0982016-02-12 21:58:43 +00001273 const CodeInfoEncoding& encoding) const {
David Brazdil77a48ae2015-09-15 12:34:04 +00001274 // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack
1275 // maps are not. If we knew that the method does not have try/catch,
1276 // we could do binary search.
David Srbecky09ed0982016-02-12 21:58:43 +00001277 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +01001278 StackMap stack_map = GetStackMapAt(i, encoding);
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001279 if (stack_map.GetNativePcOffset(encoding.stack_map_encoding, kRuntimeISA) ==
1280 native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001281 return stack_map;
1282 }
1283 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001284 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001285 }
1286
Roland Levillainf2650d12015-05-28 14:53:28 +01001287 // Dump this CodeInfo object on `os`. `code_offset` is the (absolute)
1288 // native PC of the compiled method and `number_of_dex_registers` the
1289 // number of Dex virtual registers used in this method. If
1290 // `dump_stack_maps` is true, also dump the stack maps and the
1291 // associated Dex register maps.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001292 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001293 uint32_t code_offset,
1294 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001295 bool dump_stack_maps,
1296 InstructionSet instruction_set) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001297
Mathieu Chartier01c78142017-01-05 10:17:55 -08001298 // Check that the code info has valid stack map and abort if it does not.
1299 void AssertValidStackMap(const CodeInfoEncoding& encoding) const {
1300 if (region_.size() != 0 && region_.size() < GetStackMapsSize(encoding)) {
1301 LOG(FATAL) << region_.size() << "\n"
1302 << encoding.header_size << "\n"
1303 << encoding.non_header_size << "\n"
1304 << encoding.number_of_location_catalog_entries << "\n"
1305 << encoding.number_of_stack_maps << "\n"
1306 << encoding.stack_map_size_in_bytes;
1307 }
1308 }
1309
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001310 private:
Mingyao Yangccfa8852017-01-18 14:51:59 -08001311 ALWAYS_INLINE MemoryRegion GetStackMaps(const CodeInfoEncoding& encoding) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001312 return region_.size() == 0
1313 ? MemoryRegion()
David Srbecky09ed0982016-02-12 21:58:43 +00001314 : region_.Subregion(GetStackMapsOffset(encoding), GetStackMapsSize(encoding));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001315 }
1316
Roland Levillaina552e1c2015-03-26 15:01:03 +00001317 // Compute the size of the Dex register map associated to the stack map at
1318 // `dex_register_map_offset_in_code_info`.
David Srbecky09ed0982016-02-12 21:58:43 +00001319 size_t ComputeDexRegisterMapSizeOf(const CodeInfoEncoding& encoding,
1320 uint32_t dex_register_map_offset_in_code_info,
Roland Levillaina552e1c2015-03-26 15:01:03 +00001321 uint16_t number_of_dex_registers) const {
1322 // Offset where the actual mapping data starts within art::DexRegisterMap.
1323 size_t location_mapping_data_offset_in_dex_register_map =
1324 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1325 // Create a temporary art::DexRegisterMap to be able to call
1326 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1327 DexRegisterMap dex_register_map_without_locations(
1328 MemoryRegion(region_.Subregion(dex_register_map_offset_in_code_info,
1329 location_mapping_data_offset_in_dex_register_map)));
1330 size_t number_of_live_dex_registers =
1331 dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
1332 size_t location_mapping_data_size_in_bits =
David Srbecky09ed0982016-02-12 21:58:43 +00001333 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries(encoding))
Roland Levillaina552e1c2015-03-26 15:01:03 +00001334 * number_of_live_dex_registers;
1335 size_t location_mapping_data_size_in_bytes =
1336 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1337 size_t dex_register_map_size =
1338 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1339 return dex_register_map_size;
1340 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001341
Roland Levillaina552e1c2015-03-26 15:01:03 +00001342 // Compute the size of a Dex register location catalog starting at offset `origin`
1343 // in `region_` and containing `number_of_dex_locations` entries.
1344 size_t ComputeDexRegisterLocationCatalogSize(uint32_t origin,
1345 uint32_t number_of_dex_locations) const {
1346 // TODO: Ideally, we would like to use art::DexRegisterLocationCatalog::Size or
1347 // art::DexRegisterLocationCatalog::FindLocationOffset, but the
1348 // DexRegisterLocationCatalog is not yet built. Try to factor common code.
1349 size_t offset = origin + DexRegisterLocationCatalog::kFixedSize;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001350
Roland Levillaina552e1c2015-03-26 15:01:03 +00001351 // Skip the first `number_of_dex_locations - 1` entries.
1352 for (uint16_t i = 0; i < number_of_dex_locations; ++i) {
1353 // Read the first next byte and inspect its first 3 bits to decide
1354 // whether it is a short or a large location.
1355 DexRegisterLocationCatalog::ShortLocation first_byte =
1356 region_.LoadUnaligned<DexRegisterLocationCatalog::ShortLocation>(offset);
1357 DexRegisterLocation::Kind kind =
1358 DexRegisterLocationCatalog::ExtractKindFromShortLocation(first_byte);
1359 if (DexRegisterLocation::IsShortLocationKind(kind)) {
1360 // Short location. Skip the current byte.
1361 offset += DexRegisterLocationCatalog::SingleShortEntrySize();
1362 } else {
1363 // Large location. Skip the 5 next bytes.
1364 offset += DexRegisterLocationCatalog::SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001365 }
1366 }
1367 size_t size = offset - origin;
1368 return size;
1369 }
1370
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001371 MemoryRegion region_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001372 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001373};
1374
Roland Levillain1c1da432015-07-16 11:54:44 +01001375#undef ELEMENT_BYTE_OFFSET_AFTER
1376#undef ELEMENT_BIT_OFFSET_AFTER
1377
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001378} // namespace art
1379
1380#endif // ART_RUNTIME_STACK_MAP_H_