blob: 13886f2109e66db758aef35cc17e01b1570a9819 [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
20#include "base/bit_vector.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010021#include "base/bit_utils.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010022#include "dex_file.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010023#include "memory_region.h"
David Srbecky09ed0982016-02-12 21:58:43 +000024#include "leb128.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010025
26namespace art {
27
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010028class VariableIndentationOutputStream;
29
Roland Levillaina2d8ec62015-03-12 15:25:29 +000030// Size of a frame slot, in bytes. This constant is a signed value,
31// to please the compiler in arithmetic operations involving int32_t
32// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000033static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000034
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000035// Size of Dex virtual registers.
Roland Levillaina552e1c2015-03-26 15:01:03 +000036static constexpr size_t kVRegSize = 4;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000037
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000038class ArtMethod;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000039class CodeInfo;
David Brazdilf677ebf2015-05-29 16:29:43 +010040class StackMapEncoding;
David Srbecky09ed0982016-02-12 21:58:43 +000041struct CodeInfoEncoding;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000042
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010043/**
44 * Classes in the following file are wrapper on stack map information backed
45 * by a MemoryRegion. As such they read and write to the region, they don't have
46 * their own fields.
47 */
48
Roland Levillaina2d8ec62015-03-12 15:25:29 +000049// Dex register location container used by DexRegisterMap and StackMapStream.
50class DexRegisterLocation {
51 public:
52 /*
53 * The location kind used to populate the Dex register information in a
54 * StackMapStream can either be:
David Brazdild9cb68e2015-08-25 13:52:43 +010055 * - kStack: vreg stored on the stack, value holds the stack offset;
56 * - kInRegister: vreg stored in low 32 bits of a core physical register,
57 * value holds the register number;
58 * - kInRegisterHigh: vreg stored in high 32 bits of a core physical register,
59 * value holds the register number;
60 * - kInFpuRegister: vreg stored in low 32 bits of an FPU register,
61 * value holds the register number;
62 * - kInFpuRegisterHigh: vreg stored in high 32 bits of an FPU register,
63 * value holds the register number;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000064 * - kConstant: value holds the constant;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000065 *
66 * In addition, DexRegisterMap also uses these values:
67 * - kInStackLargeOffset: value holds a "large" stack offset (greater than
Roland Levillaina552e1c2015-03-26 15:01:03 +000068 * or equal to 128 bytes);
69 * - kConstantLargeValue: value holds a "large" constant (lower than 0, or
David Brazdild9cb68e2015-08-25 13:52:43 +010070 * or greater than or equal to 32);
71 * - kNone: the register has no location, meaning it has not been set.
Roland Levillaina2d8ec62015-03-12 15:25:29 +000072 */
73 enum class Kind : uint8_t {
74 // Short location kinds, for entries fitting on one byte (3 bits
75 // for the kind, 5 bits for the value) in a DexRegisterMap.
David Brazdild9cb68e2015-08-25 13:52:43 +010076 kInStack = 0, // 0b000
77 kInRegister = 1, // 0b001
78 kInRegisterHigh = 2, // 0b010
Roland Levillaina2d8ec62015-03-12 15:25:29 +000079 kInFpuRegister = 3, // 0b011
David Brazdild9cb68e2015-08-25 13:52:43 +010080 kInFpuRegisterHigh = 4, // 0b100
81 kConstant = 5, // 0b101
Roland Levillaina2d8ec62015-03-12 15:25:29 +000082
83 // Large location kinds, requiring a 5-byte encoding (1 byte for the
84 // kind, 4 bytes for the value).
85
86 // Stack location at a large offset, meaning that the offset value
87 // divided by the stack frame slot size (4 bytes) cannot fit on a
88 // 5-bit unsigned integer (i.e., this offset value is greater than
89 // or equal to 2^5 * 4 = 128 bytes).
David Brazdild9cb68e2015-08-25 13:52:43 +010090 kInStackLargeOffset = 6, // 0b110
Roland Levillaina2d8ec62015-03-12 15:25:29 +000091
92 // Large constant, that cannot fit on a 5-bit signed integer (i.e.,
Roland Levillaina552e1c2015-03-26 15:01:03 +000093 // lower than 0, or greater than or equal to 2^5 = 32).
David Brazdild9cb68e2015-08-25 13:52:43 +010094 kConstantLargeValue = 7, // 0b111
95
96 // Entries with no location are not stored and do not need own marker.
97 kNone = static_cast<uint8_t>(-1),
Roland Levillaina2d8ec62015-03-12 15:25:29 +000098
99 kLastLocationKind = kConstantLargeValue
100 };
101
102 static_assert(
103 sizeof(Kind) == 1u,
104 "art::DexRegisterLocation::Kind has a size different from one byte.");
105
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000106 static bool IsShortLocationKind(Kind kind) {
107 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000108 case Kind::kInStack:
109 case Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100110 case Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000111 case Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100112 case Kind::kInFpuRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000113 case Kind::kConstant:
114 return true;
115
116 case Kind::kInStackLargeOffset:
117 case Kind::kConstantLargeValue:
118 return false;
119
David Brazdild9cb68e2015-08-25 13:52:43 +0100120 case Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000121 LOG(FATAL) << "Unexpected location kind";
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000122 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100123 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000124 }
125
126 // Convert `kind` to a "surface" kind, i.e. one that doesn't include
127 // any value with a "large" qualifier.
128 // TODO: Introduce another enum type for the surface kind?
129 static Kind ConvertToSurfaceKind(Kind kind) {
130 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000131 case Kind::kInStack:
132 case Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100133 case Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000134 case Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100135 case Kind::kInFpuRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000136 case Kind::kConstant:
137 return kind;
138
139 case Kind::kInStackLargeOffset:
140 return Kind::kInStack;
141
142 case Kind::kConstantLargeValue:
143 return Kind::kConstant;
144
David Brazdild9cb68e2015-08-25 13:52:43 +0100145 case Kind::kNone:
146 return kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000147 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100148 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000149 }
150
Roland Levillaina552e1c2015-03-26 15:01:03 +0000151 // Required by art::StackMapStream::LocationCatalogEntriesIndices.
152 DexRegisterLocation() : kind_(Kind::kNone), value_(0) {}
153
154 DexRegisterLocation(Kind kind, int32_t value) : kind_(kind), value_(value) {}
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000155
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000156 static DexRegisterLocation None() {
157 return DexRegisterLocation(Kind::kNone, 0);
158 }
159
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000160 // Get the "surface" kind of the location, i.e., the one that doesn't
161 // include any value with a "large" qualifier.
162 Kind GetKind() const {
163 return ConvertToSurfaceKind(kind_);
164 }
165
166 // Get the value of the location.
167 int32_t GetValue() const { return value_; }
168
169 // Get the actual kind of the location.
170 Kind GetInternalKind() const { return kind_; }
171
Calin Juravle6ae70962015-03-18 16:31:28 +0000172 bool operator==(DexRegisterLocation other) const {
173 return kind_ == other.kind_ && value_ == other.value_;
174 }
175
176 bool operator!=(DexRegisterLocation other) const {
177 return !(*this == other);
178 }
179
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000180 private:
181 Kind kind_;
182 int32_t value_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000183
184 friend class DexRegisterLocationHashFn;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000185};
186
David Srbecky7dc11782016-02-25 13:23:56 +0000187std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation::Kind& kind);
188
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100189/**
Roland Levillaina552e1c2015-03-26 15:01:03 +0000190 * Store information on unique Dex register locations used in a method.
191 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100192 *
193 * [DexRegisterLocation+].
194 *
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000195 * DexRegisterLocations are either 1- or 5-byte wide (see art::DexRegisterLocation::Kind).
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100196 */
Roland Levillaina552e1c2015-03-26 15:01:03 +0000197class DexRegisterLocationCatalog {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100198 public:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000199 explicit DexRegisterLocationCatalog(MemoryRegion region) : region_(region) {}
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100200
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000201 // Short (compressed) location, fitting on one byte.
202 typedef uint8_t ShortLocation;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100203
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000204 void SetRegisterInfo(size_t offset, const DexRegisterLocation& dex_register_location) {
205 DexRegisterLocation::Kind kind = ComputeCompressedKind(dex_register_location);
206 int32_t value = dex_register_location.GetValue();
207 if (DexRegisterLocation::IsShortLocationKind(kind)) {
208 // Short location. Compress the kind and the value as a single byte.
209 if (kind == DexRegisterLocation::Kind::kInStack) {
210 // Instead of storing stack offsets expressed in bytes for
211 // short stack locations, store slot offsets. A stack offset
212 // is a multiple of 4 (kFrameSlotSize). This means that by
213 // dividing it by 4, we can fit values from the [0, 128)
214 // interval in a short stack location, and not just values
215 // from the [0, 32) interval.
216 DCHECK_EQ(value % kFrameSlotSize, 0);
217 value /= kFrameSlotSize;
218 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000219 DCHECK(IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000220 region_.StoreUnaligned<ShortLocation>(offset, MakeShortLocation(kind, value));
221 } else {
222 // Large location. Write the location on one byte and the value
223 // on 4 bytes.
Roland Levillaina552e1c2015-03-26 15:01:03 +0000224 DCHECK(!IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000225 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
226 // Also divide large stack offsets by 4 for the sake of consistency.
227 DCHECK_EQ(value % kFrameSlotSize, 0);
228 value /= kFrameSlotSize;
229 }
230 // Data can be unaligned as the written Dex register locations can
231 // either be 1-byte or 5-byte wide. Use
232 // art::MemoryRegion::StoreUnaligned instead of
233 // art::MemoryRegion::Store to prevent unligned word accesses on ARM.
234 region_.StoreUnaligned<DexRegisterLocation::Kind>(offset, kind);
235 region_.StoreUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind), value);
Roland Levillain442b46a2015-02-18 16:54:21 +0000236 }
237 }
238
Roland Levillaina552e1c2015-03-26 15:01:03 +0000239 // Find the offset of the location catalog entry number `location_catalog_entry_index`.
240 size_t FindLocationOffset(size_t location_catalog_entry_index) const {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000241 size_t offset = kFixedSize;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000242 // Skip the first `location_catalog_entry_index - 1` entries.
243 for (uint16_t i = 0; i < location_catalog_entry_index; ++i) {
244 // Read the first next byte and inspect its first 3 bits to decide
245 // whether it is a short or a large location.
246 DexRegisterLocation::Kind kind = ExtractKindAtOffset(offset);
247 if (DexRegisterLocation::IsShortLocationKind(kind)) {
248 // Short location. Skip the current byte.
249 offset += SingleShortEntrySize();
250 } else {
251 // Large location. Skip the 5 next bytes.
252 offset += SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000253 }
254 }
255 return offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100256 }
257
Roland Levillaina552e1c2015-03-26 15:01:03 +0000258 // Get the internal kind of entry at `location_catalog_entry_index`.
259 DexRegisterLocation::Kind GetLocationInternalKind(size_t location_catalog_entry_index) const {
260 if (location_catalog_entry_index == kNoLocationEntryIndex) {
261 return DexRegisterLocation::Kind::kNone;
262 }
263 return ExtractKindAtOffset(FindLocationOffset(location_catalog_entry_index));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100264 }
265
Roland Levillaina552e1c2015-03-26 15:01:03 +0000266 // Get the (surface) kind and value of entry at `location_catalog_entry_index`.
267 DexRegisterLocation GetDexRegisterLocation(size_t location_catalog_entry_index) const {
268 if (location_catalog_entry_index == kNoLocationEntryIndex) {
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000269 return DexRegisterLocation::None();
270 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000271 size_t offset = FindLocationOffset(location_catalog_entry_index);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000272 // Read the first byte and inspect its first 3 bits to get the location.
273 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
274 DexRegisterLocation::Kind kind = ExtractKindFromShortLocation(first_byte);
275 if (DexRegisterLocation::IsShortLocationKind(kind)) {
276 // Short location. Extract the value from the remaining 5 bits.
277 int32_t value = ExtractValueFromShortLocation(first_byte);
278 if (kind == DexRegisterLocation::Kind::kInStack) {
279 // Convert the stack slot (short) offset to a byte offset value.
280 value *= kFrameSlotSize;
281 }
282 return DexRegisterLocation(kind, value);
283 } else {
284 // Large location. Read the four next bytes to get the value.
285 int32_t value = region_.LoadUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind));
286 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
287 // Convert the stack slot (large) offset to a byte offset value.
288 value *= kFrameSlotSize;
289 }
290 return DexRegisterLocation(kind, value);
291 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100292 }
293
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000294 // Compute the compressed kind of `location`.
295 static DexRegisterLocation::Kind ComputeCompressedKind(const DexRegisterLocation& location) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100296 DexRegisterLocation::Kind kind = location.GetInternalKind();
297 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000298 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000299 return IsShortStackOffsetValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000300 ? DexRegisterLocation::Kind::kInStack
301 : DexRegisterLocation::Kind::kInStackLargeOffset;
302
David Brazdild9cb68e2015-08-25 13:52:43 +0100303 case DexRegisterLocation::Kind::kInRegister:
304 case DexRegisterLocation::Kind::kInRegisterHigh:
305 DCHECK_GE(location.GetValue(), 0);
306 DCHECK_LT(location.GetValue(), 1 << kValueBits);
307 return kind;
308
309 case DexRegisterLocation::Kind::kInFpuRegister:
310 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
311 DCHECK_GE(location.GetValue(), 0);
312 DCHECK_LT(location.GetValue(), 1 << kValueBits);
313 return kind;
314
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000315 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000316 return IsShortConstantValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000317 ? DexRegisterLocation::Kind::kConstant
318 : DexRegisterLocation::Kind::kConstantLargeValue;
319
David Brazdild9cb68e2015-08-25 13:52:43 +0100320 case DexRegisterLocation::Kind::kConstantLargeValue:
321 case DexRegisterLocation::Kind::kInStackLargeOffset:
322 case DexRegisterLocation::Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000323 LOG(FATAL) << "Unexpected location kind " << kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000324 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100325 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000326 }
327
328 // Can `location` be turned into a short location?
329 static bool CanBeEncodedAsShortLocation(const DexRegisterLocation& location) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100330 DexRegisterLocation::Kind kind = location.GetInternalKind();
331 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000332 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000333 return IsShortStackOffsetValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000334
David Brazdild9cb68e2015-08-25 13:52:43 +0100335 case DexRegisterLocation::Kind::kInRegister:
336 case DexRegisterLocation::Kind::kInRegisterHigh:
337 case DexRegisterLocation::Kind::kInFpuRegister:
338 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
339 return true;
340
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000341 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000342 return IsShortConstantValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000343
David Brazdild9cb68e2015-08-25 13:52:43 +0100344 case DexRegisterLocation::Kind::kConstantLargeValue:
345 case DexRegisterLocation::Kind::kInStackLargeOffset:
346 case DexRegisterLocation::Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000347 LOG(FATAL) << "Unexpected location kind " << kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000348 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100349 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000350 }
351
352 static size_t EntrySize(const DexRegisterLocation& location) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000353 return CanBeEncodedAsShortLocation(location) ? SingleShortEntrySize() : SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000354 }
355
356 static size_t SingleShortEntrySize() {
357 return sizeof(ShortLocation);
358 }
359
360 static size_t SingleLargeEntrySize() {
361 return sizeof(DexRegisterLocation::Kind) + sizeof(int32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100362 }
363
Roland Levillain12baf472015-03-05 12:41:42 +0000364 size_t Size() const {
365 return region_.size();
366 }
367
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100368 void Dump(VariableIndentationOutputStream* vios, const CodeInfo& code_info);
Roland Levillain0396ed72015-05-27 15:12:19 +0100369
Roland Levillaina552e1c2015-03-26 15:01:03 +0000370 // Special (invalid) Dex register location catalog entry index meaning
371 // that there is no location for a given Dex register (i.e., it is
372 // mapped to a DexRegisterLocation::Kind::kNone location).
373 static constexpr size_t kNoLocationEntryIndex = -1;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100374
Roland Levillain12baf472015-03-05 12:41:42 +0000375 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000376 static constexpr int kFixedSize = 0;
377
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000378 // Width of the kind "field" in a short location, in bits.
379 static constexpr size_t kKindBits = 3;
380 // Width of the value "field" in a short location, in bits.
381 static constexpr size_t kValueBits = 5;
382
383 static constexpr uint8_t kKindMask = (1 << kKindBits) - 1;
384 static constexpr int32_t kValueMask = (1 << kValueBits) - 1;
385 static constexpr size_t kKindOffset = 0;
386 static constexpr size_t kValueOffset = kKindBits;
387
Roland Levillaina552e1c2015-03-26 15:01:03 +0000388 static bool IsShortStackOffsetValue(int32_t value) {
389 DCHECK_EQ(value % kFrameSlotSize, 0);
390 return IsShortValue(value / kFrameSlotSize);
391 }
392
393 static bool IsShortConstantValue(int32_t value) {
394 return IsShortValue(value);
395 }
396
397 static bool IsShortValue(int32_t value) {
398 return IsUint<kValueBits>(value);
399 }
400
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000401 static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000402 uint8_t kind_integer_value = static_cast<uint8_t>(kind);
403 DCHECK(IsUint<kKindBits>(kind_integer_value)) << kind_integer_value;
404 DCHECK(IsShortValue(value)) << value;
405 return (kind_integer_value & kKindMask) << kKindOffset
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000406 | (value & kValueMask) << kValueOffset;
407 }
408
409 static DexRegisterLocation::Kind ExtractKindFromShortLocation(ShortLocation location) {
410 uint8_t kind = (location >> kKindOffset) & kKindMask;
411 DCHECK_LE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kLastLocationKind));
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000412 // We do not encode kNone locations in the stack map.
413 DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000414 return static_cast<DexRegisterLocation::Kind>(kind);
415 }
416
417 static int32_t ExtractValueFromShortLocation(ShortLocation location) {
418 return (location >> kValueOffset) & kValueMask;
419 }
420
421 // Extract a location kind from the byte at position `offset`.
422 DexRegisterLocation::Kind ExtractKindAtOffset(size_t offset) const {
423 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
424 return ExtractKindFromShortLocation(first_byte);
425 }
426
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100427 MemoryRegion region_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000428
429 friend class CodeInfo;
430 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100431};
432
Roland Levillaina552e1c2015-03-26 15:01:03 +0000433/* Information on Dex register locations for a specific PC, mapping a
434 * stack map's Dex register to a location entry in a DexRegisterLocationCatalog.
435 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100436 *
437 * [live_bit_mask, entries*]
438 *
Roland Levillaina552e1c2015-03-26 15:01:03 +0000439 * where entries are concatenated unsigned integer values encoded on a number
440 * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending
441 * on the number of entries in the Dex register location catalog
442 * (see DexRegisterMap::SingleEntrySizeInBits). The map is 1-byte aligned.
443 */
444class DexRegisterMap {
445 public:
446 explicit DexRegisterMap(MemoryRegion region) : region_(region) {}
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000447 DexRegisterMap() {}
448
449 bool IsValid() const { return region_.pointer() != nullptr; }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000450
451 // Get the surface kind of Dex register `dex_register_number`.
452 DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number,
453 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100454 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000455 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000456 return DexRegisterLocation::ConvertToSurfaceKind(
David Brazdilf677ebf2015-05-29 16:29:43 +0100457 GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info, enc));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000458 }
459
460 // Get the internal kind of Dex register `dex_register_number`.
461 DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number,
462 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100463 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000464 const CodeInfoEncoding& enc) 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 Brazdilf677ebf2015-05-29 16:29:43 +0100469 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000470 const CodeInfoEncoding& enc) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000471
472 int32_t GetStackOffsetInBytes(uint16_t dex_register_number,
473 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100474 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000475 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000476 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100477 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000478 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
479 // GetDexRegisterLocation returns the offset in bytes.
480 return location.GetValue();
481 }
482
483 int32_t GetConstant(uint16_t dex_register_number,
484 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100485 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000486 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000487 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100488 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
David Srbecky7dc11782016-02-25 13:23:56 +0000489 DCHECK_EQ(location.GetKind(), DexRegisterLocation::Kind::kConstant);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000490 return location.GetValue();
491 }
492
493 int32_t GetMachineRegister(uint16_t dex_register_number,
494 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100495 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000496 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000497 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100498 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
David Brazdild9cb68e2015-08-25 13:52:43 +0100499 DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister ||
500 location.GetInternalKind() == DexRegisterLocation::Kind::kInRegisterHigh ||
501 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister ||
502 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh)
David Srbecky7dc11782016-02-25 13:23:56 +0000503 << location.GetInternalKind();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000504 return location.GetValue();
505 }
506
507 // Get the index of the entry in the Dex register location catalog
508 // corresponding to `dex_register_number`.
509 size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number,
510 uint16_t number_of_dex_registers,
511 size_t number_of_location_catalog_entries) const {
512 if (!IsDexRegisterLive(dex_register_number)) {
513 return DexRegisterLocationCatalog::kNoLocationEntryIndex;
514 }
515
516 if (number_of_location_catalog_entries == 1) {
517 // We do not allocate space for location maps in the case of a
518 // single-entry location catalog, as it is useless. The only valid
519 // entry index is 0;
520 return 0;
521 }
522
523 // The bit offset of the beginning of the map locations.
524 size_t map_locations_offset_in_bits =
525 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
526 size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number);
527 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
528 // The bit size of an entry.
529 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
530 // The bit offset where `index_in_dex_register_map` is located.
531 size_t entry_offset_in_bits =
532 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
533 size_t location_catalog_entry_index =
534 region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits);
535 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
536 return location_catalog_entry_index;
537 }
538
539 // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`.
540 void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map,
541 size_t location_catalog_entry_index,
542 uint16_t number_of_dex_registers,
543 size_t number_of_location_catalog_entries) {
544 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
545 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
546
547 if (number_of_location_catalog_entries == 1) {
548 // We do not allocate space for location maps in the case of a
549 // single-entry location catalog, as it is useless.
550 return;
551 }
552
553 // The bit offset of the beginning of the map locations.
554 size_t map_locations_offset_in_bits =
555 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
556 // The bit size of an entry.
557 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
558 // The bit offset where `index_in_dex_register_map` is located.
559 size_t entry_offset_in_bits =
560 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
561 region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits);
562 }
563
564 void SetLiveBitMask(uint16_t number_of_dex_registers,
565 const BitVector& live_dex_registers_mask) {
566 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
567 for (uint16_t i = 0; i < number_of_dex_registers; ++i) {
568 region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i));
569 }
570 }
571
572 bool IsDexRegisterLive(uint16_t dex_register_number) const {
573 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
574 return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number);
575 }
576
577 size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const {
578 size_t number_of_live_dex_registers = 0;
579 for (size_t i = 0; i < number_of_dex_registers; ++i) {
580 if (IsDexRegisterLive(i)) {
581 ++number_of_live_dex_registers;
582 }
583 }
584 return number_of_live_dex_registers;
585 }
586
587 static size_t GetLiveBitMaskOffset() {
588 return kFixedSize;
589 }
590
591 // Compute the size of the live register bit mask (in bytes), for a
592 // method having `number_of_dex_registers` Dex registers.
593 static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) {
594 return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte;
595 }
596
597 static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) {
598 return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers);
599 }
600
601 size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers,
602 size_t number_of_location_catalog_entries) const {
603 size_t location_mapping_data_size_in_bits =
604 GetNumberOfLiveDexRegisters(number_of_dex_registers)
605 * SingleEntrySizeInBits(number_of_location_catalog_entries);
606 return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
607 }
608
609 // Return the size of a map entry in bits. Note that if
610 // `number_of_location_catalog_entries` equals 1, this function returns 0,
611 // which is fine, as there is no need to allocate a map for a
612 // single-entry location catalog; the only valid location catalog entry index
613 // for a live register in this case is 0 and there is no need to
614 // store it.
615 static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) {
616 // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2.
617 return number_of_location_catalog_entries == 0
618 ? 0u
619 : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries));
620 }
621
622 // Return the size of the DexRegisterMap object, in bytes.
623 size_t Size() const {
624 return region_.size();
625 }
626
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100627 void Dump(VariableIndentationOutputStream* vios,
628 const CodeInfo& code_info, uint16_t number_of_dex_registers) const;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100629
Roland Levillaina552e1c2015-03-26 15:01:03 +0000630 private:
631 // Return the index in the Dex register map corresponding to the Dex
632 // register number `dex_register_number`.
633 size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const {
634 if (!IsDexRegisterLive(dex_register_number)) {
635 return kInvalidIndexInDexRegisterMap;
636 }
637 return GetNumberOfLiveDexRegisters(dex_register_number);
638 }
639
640 // Special (invalid) Dex register map entry index meaning that there
641 // is no index in the map for a given Dex register (i.e., it must
642 // have been mapped to a DexRegisterLocation::Kind::kNone location).
643 static constexpr size_t kInvalidIndexInDexRegisterMap = -1;
644
645 static constexpr int kFixedSize = 0;
646
647 MemoryRegion region_;
648
649 friend class CodeInfo;
650 friend class StackMapStream;
651};
652
David Srbecky09ed0982016-02-12 21:58:43 +0000653// Represents bit range of bit-packed integer field.
654// We reuse the idea from ULEB128p1 to support encoding of -1 (aka 0xFFFFFFFF).
655// If min_value is set to -1, we implicitly subtract one from any loaded value,
656// and add one to any stored value. This is generalized to any negative values.
657// In other words, min_value acts as a base and the stored value is added to it.
658struct FieldEncoding {
659 FieldEncoding(size_t start_offset, size_t end_offset, int32_t min_value = 0)
660 : start_offset_(start_offset), end_offset_(end_offset), min_value_(min_value) {
661 DCHECK_LE(start_offset_, end_offset_);
662 DCHECK_LE(BitSize(), 32u);
663 }
664
665 ALWAYS_INLINE size_t BitSize() const { return end_offset_ - start_offset_; }
666
667 ALWAYS_INLINE int32_t Load(const MemoryRegion& region) const {
668 DCHECK_LE(end_offset_, region.size_in_bits());
669 const size_t bit_count = BitSize();
670 if (bit_count == 0) {
671 // Do not touch any memory if the range is empty.
672 return min_value_;
673 }
674 uint8_t* address = region.start() + start_offset_ / kBitsPerByte;
675 const uint32_t shift = start_offset_ & (kBitsPerByte - 1);
676 // Load the value (reading only the strictly needed bytes).
677 const uint32_t load_bit_count = shift + bit_count;
678 uint32_t value = *address++ >> shift;
679 if (load_bit_count > 8) {
680 value |= static_cast<uint32_t>(*address++) << (8 - shift);
681 if (load_bit_count > 16) {
682 value |= static_cast<uint32_t>(*address++) << (16 - shift);
683 if (load_bit_count > 24) {
684 value |= static_cast<uint32_t>(*address++) << (24 - shift);
685 if (load_bit_count > 32) {
686 value |= static_cast<uint32_t>(*address++) << (32 - shift);
687 }
688 }
689 }
690 }
691 // Clear unwanted most significant bits.
692 uint32_t clear_bit_count = 32 - bit_count;
693 value = (value << clear_bit_count) >> clear_bit_count;
694 return value + min_value_;
695 }
696
697 ALWAYS_INLINE void Store(MemoryRegion region, int32_t value) const {
698 region.StoreBits(start_offset_, value - min_value_, BitSize());
699 DCHECK_EQ(Load(region), value);
700 }
701
702 private:
703 size_t start_offset_;
704 size_t end_offset_;
705 int32_t min_value_;
706};
707
David Brazdilf677ebf2015-05-29 16:29:43 +0100708class StackMapEncoding {
709 public:
710 StackMapEncoding() {}
711
David Srbecky09ed0982016-02-12 21:58:43 +0000712 // Set stack map bit layout based on given sizes.
713 // Returns the size of stack map in bytes.
714 size_t SetFromSizes(size_t native_pc_max,
715 size_t dex_pc_max,
716 size_t dex_register_map_size,
717 size_t inline_info_size,
718 size_t register_mask_max,
719 size_t stack_mask_bit_size) {
720 size_t bit_offset = 0;
721 DCHECK_EQ(kNativePcBitOffset, bit_offset);
722 bit_offset += MinimumBitsToStore(native_pc_max);
David Brazdilf677ebf2015-05-29 16:29:43 +0100723
David Srbecky09ed0982016-02-12 21:58:43 +0000724 dex_pc_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
725 bit_offset += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max);
726
727 // We also need +1 for kNoDexRegisterMap, but since the size is strictly
728 // greater than any offset we might try to encode, we already implicitly have it.
729 dex_register_map_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
730 bit_offset += MinimumBitsToStore(dex_register_map_size);
731
732 // We also need +1 for kNoInlineInfo, but since the inline_info_size is strictly
733 // greater than the offset we might try to encode, we already implicitly have it.
734 // If inline_info_size is zero, we can encode only kNoInlineInfo (in zero bits).
735 inline_info_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
736 if (inline_info_size != 0) {
737 bit_offset += MinimumBitsToStore(dex_register_map_size + inline_info_size);
738 }
739
740 register_mask_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
741 bit_offset += MinimumBitsToStore(register_mask_max);
742
743 stack_mask_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
744 bit_offset += stack_mask_bit_size;
745
746 return RoundUp(bit_offset, kBitsPerByte) / kBitsPerByte;
David Brazdilf677ebf2015-05-29 16:29:43 +0100747 }
748
David Srbecky09ed0982016-02-12 21:58:43 +0000749 ALWAYS_INLINE FieldEncoding GetNativePcEncoding() const {
750 return FieldEncoding(kNativePcBitOffset, dex_pc_bit_offset_);
751 }
752 ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const {
753 return FieldEncoding(dex_pc_bit_offset_, dex_register_map_bit_offset_, -1 /* min_value */);
754 }
755 ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const {
756 return FieldEncoding(dex_register_map_bit_offset_, inline_info_bit_offset_, -1 /* min_value */);
757 }
758 ALWAYS_INLINE FieldEncoding GetInlineInfoEncoding() const {
759 return FieldEncoding(inline_info_bit_offset_, register_mask_bit_offset_, -1 /* min_value */);
760 }
761 ALWAYS_INLINE FieldEncoding GetRegisterMaskEncoding() const {
762 return FieldEncoding(register_mask_bit_offset_, stack_mask_bit_offset_);
763 }
764 ALWAYS_INLINE size_t GetStackMaskBitOffset() const {
765 // The end offset is not encoded. It is implicitly the end of stack map entry.
766 return stack_mask_bit_offset_;
David Brazdilf677ebf2015-05-29 16:29:43 +0100767 }
768
David Srbecky09ed0982016-02-12 21:58:43 +0000769 void Dump(VariableIndentationOutputStream* vios) const;
David Brazdilf677ebf2015-05-29 16:29:43 +0100770
771 private:
David Srbecky09ed0982016-02-12 21:58:43 +0000772 static constexpr size_t kNativePcBitOffset = 0;
773 uint8_t dex_pc_bit_offset_;
774 uint8_t dex_register_map_bit_offset_;
775 uint8_t inline_info_bit_offset_;
776 uint8_t register_mask_bit_offset_;
777 uint8_t stack_mask_bit_offset_;
David Brazdilf677ebf2015-05-29 16:29:43 +0100778};
779
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100780/**
781 * A Stack Map holds compilation information for a specific PC necessary for:
782 * - Mapping it to a dex PC,
783 * - Knowing which stack entries are objects,
784 * - Knowing which registers hold objects,
785 * - Knowing the inlining information,
786 * - Knowing the values of dex registers.
787 *
788 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100789 *
David Srbecky09ed0982016-02-12 21:58:43 +0000790 * [native_pc_offset, dex_pc, dex_register_map_offset, inlining_info_offset, register_mask,
Roland Levillain1c1da432015-07-16 11:54:44 +0100791 * stack_mask].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100792 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100793class StackMap {
794 public:
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100795 StackMap() {}
David Brazdilf677ebf2015-05-29 16:29:43 +0100796 explicit StackMap(MemoryRegion region) : region_(region) {}
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100797
David Srbecky09ed0982016-02-12 21:58:43 +0000798 ALWAYS_INLINE bool IsValid() const { return region_.pointer() != nullptr; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100799
David Srbecky09ed0982016-02-12 21:58:43 +0000800 ALWAYS_INLINE uint32_t GetDexPc(const StackMapEncoding& encoding) const {
801 return encoding.GetDexPcEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100802 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100803
David Srbecky09ed0982016-02-12 21:58:43 +0000804 ALWAYS_INLINE void SetDexPc(const StackMapEncoding& encoding, uint32_t dex_pc) {
805 encoding.GetDexPcEncoding().Store(region_, dex_pc);
David Brazdilf677ebf2015-05-29 16:29:43 +0100806 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100807
David Srbecky09ed0982016-02-12 21:58:43 +0000808 ALWAYS_INLINE uint32_t GetNativePcOffset(const StackMapEncoding& encoding) const {
809 return encoding.GetNativePcEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100810 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100811
David Srbecky09ed0982016-02-12 21:58:43 +0000812 ALWAYS_INLINE void SetNativePcOffset(const StackMapEncoding& encoding, uint32_t native_pc_offset) {
813 encoding.GetNativePcEncoding().Store(region_, native_pc_offset);
David Brazdilf677ebf2015-05-29 16:29:43 +0100814 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100815
David Srbecky09ed0982016-02-12 21:58:43 +0000816 ALWAYS_INLINE uint32_t GetDexRegisterMapOffset(const StackMapEncoding& encoding) const {
817 return encoding.GetDexRegisterMapEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100818 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100819
David Srbecky09ed0982016-02-12 21:58:43 +0000820 ALWAYS_INLINE void SetDexRegisterMapOffset(const StackMapEncoding& encoding, uint32_t offset) {
821 encoding.GetDexRegisterMapEncoding().Store(region_, offset);
David Brazdilf677ebf2015-05-29 16:29:43 +0100822 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100823
David Srbecky09ed0982016-02-12 21:58:43 +0000824 ALWAYS_INLINE uint32_t GetInlineDescriptorOffset(const StackMapEncoding& encoding) const {
825 return encoding.GetInlineInfoEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100826 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100827
David Srbecky09ed0982016-02-12 21:58:43 +0000828 ALWAYS_INLINE void SetInlineDescriptorOffset(const StackMapEncoding& encoding, uint32_t offset) {
829 encoding.GetInlineInfoEncoding().Store(region_, offset);
David Brazdilf677ebf2015-05-29 16:29:43 +0100830 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100831
David Srbecky09ed0982016-02-12 21:58:43 +0000832 ALWAYS_INLINE uint32_t GetRegisterMask(const StackMapEncoding& encoding) const {
833 return encoding.GetRegisterMaskEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100834 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100835
David Srbecky09ed0982016-02-12 21:58:43 +0000836 ALWAYS_INLINE void SetRegisterMask(const StackMapEncoding& encoding, uint32_t mask) {
837 encoding.GetRegisterMaskEncoding().Store(region_, mask);
David Brazdilf677ebf2015-05-29 16:29:43 +0100838 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100839
David Srbecky09ed0982016-02-12 21:58:43 +0000840 ALWAYS_INLINE size_t GetNumberOfStackMaskBits(const StackMapEncoding& encoding) const {
841 return region_.size_in_bits() - encoding.GetStackMaskBitOffset();
David Brazdilf677ebf2015-05-29 16:29:43 +0100842 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100843
David Srbecky09ed0982016-02-12 21:58:43 +0000844 ALWAYS_INLINE bool GetStackMaskBit(const StackMapEncoding& encoding, size_t index) const {
845 return region_.LoadBit(encoding.GetStackMaskBitOffset() + index);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100846 }
847
David Srbecky09ed0982016-02-12 21:58:43 +0000848 ALWAYS_INLINE void SetStackMaskBit(const StackMapEncoding& encoding, size_t index, bool value) {
849 region_.StoreBit(encoding.GetStackMaskBitOffset() + index, value);
850 }
851
852 ALWAYS_INLINE bool HasDexRegisterMap(const StackMapEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +0100853 return GetDexRegisterMapOffset(encoding) != kNoDexRegisterMap;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100854 }
855
David Srbecky09ed0982016-02-12 21:58:43 +0000856 ALWAYS_INLINE bool HasInlineInfo(const StackMapEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +0100857 return GetInlineDescriptorOffset(encoding) != kNoInlineInfo;
Roland Levillain442b46a2015-02-18 16:54:21 +0000858 }
859
David Srbecky09ed0982016-02-12 21:58:43 +0000860 ALWAYS_INLINE bool Equals(const StackMap& other) const {
861 return region_.pointer() == other.region_.pointer() && region_.size() == other.region_.size();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100862 }
863
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100864 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100865 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000866 const CodeInfoEncoding& encoding,
Roland Levillainf2650d12015-05-28 14:53:28 +0100867 uint32_t code_offset,
868 uint16_t number_of_dex_registers,
869 const std::string& header_suffix = "") const;
870
Roland Levillain442b46a2015-02-18 16:54:21 +0000871 // Special (invalid) offset for the DexRegisterMapOffset field meaning
872 // that there is no Dex register map for this stack map.
873 static constexpr uint32_t kNoDexRegisterMap = -1;
874
875 // Special (invalid) offset for the InlineDescriptorOffset field meaning
876 // that there is no inline info for this stack map.
877 static constexpr uint32_t kNoInlineInfo = -1;
878
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100879 private:
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100880 static constexpr int kFixedSize = 0;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100881
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100882 MemoryRegion region_;
883
Nicolas Geoffray39468442014-09-02 15:17:15 +0100884 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100885};
886
David Srbecky61b28a12016-02-25 21:55:03 +0000887class InlineInfoEncoding {
888 public:
889 void SetFromSizes(size_t method_index_max,
890 size_t dex_pc_max,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000891 size_t extra_data_max,
David Srbecky61b28a12016-02-25 21:55:03 +0000892 size_t dex_register_map_size) {
893 total_bit_size_ = kMethodIndexBitOffset;
894 total_bit_size_ += MinimumBitsToStore(method_index_max);
895
896 dex_pc_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100897 // Note: We're not encoding the dex pc if there is none. That's the case
898 // for an intrinsified native method, such as String.charAt().
899 if (dex_pc_max != DexFile::kDexNoIndex) {
900 total_bit_size_ += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max);
901 }
David Srbecky61b28a12016-02-25 21:55:03 +0000902
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000903 extra_data_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
904 total_bit_size_ += MinimumBitsToStore(extra_data_max);
David Srbecky61b28a12016-02-25 21:55:03 +0000905
906 // We also need +1 for kNoDexRegisterMap, but since the size is strictly
907 // greater than any offset we might try to encode, we already implicitly have it.
908 dex_register_map_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
909 total_bit_size_ += MinimumBitsToStore(dex_register_map_size);
910 }
911
912 ALWAYS_INLINE FieldEncoding GetMethodIndexEncoding() const {
913 return FieldEncoding(kMethodIndexBitOffset, dex_pc_bit_offset_);
914 }
915 ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000916 return FieldEncoding(dex_pc_bit_offset_, extra_data_bit_offset_, -1 /* min_value */);
David Srbecky61b28a12016-02-25 21:55:03 +0000917 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000918 ALWAYS_INLINE FieldEncoding GetExtraDataEncoding() const {
919 return FieldEncoding(extra_data_bit_offset_, dex_register_map_bit_offset_);
David Srbecky61b28a12016-02-25 21:55:03 +0000920 }
921 ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const {
922 return FieldEncoding(dex_register_map_bit_offset_, total_bit_size_, -1 /* min_value */);
923 }
924 ALWAYS_INLINE size_t GetEntrySize() const {
925 return RoundUp(total_bit_size_, kBitsPerByte) / kBitsPerByte;
926 }
927
928 void Dump(VariableIndentationOutputStream* vios) const;
929
930 private:
931 static constexpr uint8_t kIsLastBitOffset = 0;
932 static constexpr uint8_t kMethodIndexBitOffset = 1;
933 uint8_t dex_pc_bit_offset_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000934 uint8_t extra_data_bit_offset_;
David Srbecky61b28a12016-02-25 21:55:03 +0000935 uint8_t dex_register_map_bit_offset_;
936 uint8_t total_bit_size_;
937};
938
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100939/**
940 * Inline information for a specific PC. The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100941 *
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000942 * [is_last,
943 * method_index (or ArtMethod high bits),
944 * dex_pc,
945 * extra_data (ArtMethod low bits or 1),
946 * dex_register_map_offset]+.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100947 */
948class InlineInfo {
949 public:
David Srbecky61b28a12016-02-25 21:55:03 +0000950 explicit InlineInfo(MemoryRegion region) : region_(region) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100951 }
952
David Srbecky61b28a12016-02-25 21:55:03 +0000953 ALWAYS_INLINE uint32_t GetDepth(const InlineInfoEncoding& encoding) const {
954 size_t depth = 0;
955 while (!GetRegionAtDepth(encoding, depth++).LoadBit(0)) { } // Check is_last bit.
956 return depth;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100957 }
958
David Srbecky61b28a12016-02-25 21:55:03 +0000959 ALWAYS_INLINE void SetDepth(const InlineInfoEncoding& encoding, uint32_t depth) {
960 DCHECK_GT(depth, 0u);
961 for (size_t d = 0; d < depth; ++d) {
962 GetRegionAtDepth(encoding, d).StoreBit(0, d == depth - 1); // Set is_last bit.
963 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100964 }
965
David Srbecky61b28a12016-02-25 21:55:03 +0000966 ALWAYS_INLINE uint32_t GetMethodIndexAtDepth(const InlineInfoEncoding& encoding,
967 uint32_t depth) const {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000968 DCHECK(!EncodesArtMethodAtDepth(encoding, depth));
David Srbecky61b28a12016-02-25 21:55:03 +0000969 return encoding.GetMethodIndexEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100970 }
971
David Srbecky61b28a12016-02-25 21:55:03 +0000972 ALWAYS_INLINE void SetMethodIndexAtDepth(const InlineInfoEncoding& encoding,
973 uint32_t depth,
974 uint32_t index) {
975 encoding.GetMethodIndexEncoding().Store(GetRegionAtDepth(encoding, depth), index);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100976 }
977
David Srbecky61b28a12016-02-25 21:55:03 +0000978 ALWAYS_INLINE uint32_t GetDexPcAtDepth(const InlineInfoEncoding& encoding,
979 uint32_t depth) const {
980 return encoding.GetDexPcEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100981 }
982
David Srbecky61b28a12016-02-25 21:55:03 +0000983 ALWAYS_INLINE void SetDexPcAtDepth(const InlineInfoEncoding& encoding,
984 uint32_t depth,
985 uint32_t dex_pc) {
986 encoding.GetDexPcEncoding().Store(GetRegionAtDepth(encoding, depth), dex_pc);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100987 }
988
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000989 ALWAYS_INLINE bool EncodesArtMethodAtDepth(const InlineInfoEncoding& encoding,
990 uint32_t depth) const {
991 return (encoding.GetExtraDataEncoding().Load(GetRegionAtDepth(encoding, depth)) & 1) == 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100992 }
993
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000994 ALWAYS_INLINE void SetExtraDataAtDepth(const InlineInfoEncoding& encoding,
995 uint32_t depth,
996 uint32_t extra_data) {
997 encoding.GetExtraDataEncoding().Store(GetRegionAtDepth(encoding, depth), extra_data);
998 }
999
1000 ALWAYS_INLINE ArtMethod* GetArtMethodAtDepth(const InlineInfoEncoding& encoding,
1001 uint32_t depth) const {
1002 uint32_t low_bits = encoding.GetExtraDataEncoding().Load(GetRegionAtDepth(encoding, depth));
1003 uint32_t high_bits = encoding.GetMethodIndexEncoding().Load(GetRegionAtDepth(encoding, depth));
1004 if (high_bits == 0) {
1005 return reinterpret_cast<ArtMethod*>(low_bits);
1006 } else {
1007 uint64_t address = high_bits;
1008 address = address << 32;
1009 return reinterpret_cast<ArtMethod*>(address | low_bits);
1010 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001011 }
1012
David Srbecky61b28a12016-02-25 21:55:03 +00001013 ALWAYS_INLINE uint32_t GetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding,
1014 uint32_t depth) const {
1015 return encoding.GetDexRegisterMapEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001016 }
1017
David Srbecky61b28a12016-02-25 21:55:03 +00001018 ALWAYS_INLINE void SetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding,
1019 uint32_t depth,
1020 uint32_t offset) {
1021 encoding.GetDexRegisterMapEncoding().Store(GetRegionAtDepth(encoding, depth), offset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001022 }
1023
David Srbecky61b28a12016-02-25 21:55:03 +00001024 ALWAYS_INLINE bool HasDexRegisterMapAtDepth(const InlineInfoEncoding& encoding,
1025 uint32_t depth) const {
1026 return GetDexRegisterMapOffsetAtDepth(encoding, depth) != StackMap::kNoDexRegisterMap;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001027 }
1028
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001029 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +00001030 const CodeInfo& info,
1031 uint16_t* number_of_dex_registers) const;
Roland Levillain1c1da432015-07-16 11:54:44 +01001032
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001033 private:
David Srbecky61b28a12016-02-25 21:55:03 +00001034 ALWAYS_INLINE MemoryRegion GetRegionAtDepth(const InlineInfoEncoding& encoding,
1035 uint32_t depth) const {
1036 size_t entry_size = encoding.GetEntrySize();
1037 DCHECK_GT(entry_size, 0u);
1038 return region_.Subregion(depth * entry_size, entry_size);
1039 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001040
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001041 MemoryRegion region_;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001042};
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001043
David Srbecky09ed0982016-02-12 21:58:43 +00001044// Most of the fields are encoded as ULEB128 to save space.
1045struct CodeInfoEncoding {
1046 uint32_t non_header_size;
1047 uint32_t number_of_stack_maps;
1048 uint32_t stack_map_size_in_bytes;
1049 uint32_t number_of_location_catalog_entries;
1050 StackMapEncoding stack_map_encoding;
David Srbecky61b28a12016-02-25 21:55:03 +00001051 InlineInfoEncoding inline_info_encoding;
David Srbecky09ed0982016-02-12 21:58:43 +00001052 uint8_t header_size;
1053
1054 CodeInfoEncoding() { }
1055
1056 explicit CodeInfoEncoding(const void* data) {
1057 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
1058 non_header_size = DecodeUnsignedLeb128(&ptr);
1059 number_of_stack_maps = DecodeUnsignedLeb128(&ptr);
1060 stack_map_size_in_bytes = DecodeUnsignedLeb128(&ptr);
1061 number_of_location_catalog_entries = DecodeUnsignedLeb128(&ptr);
David Srbecky61b28a12016-02-25 21:55:03 +00001062 static_assert(alignof(StackMapEncoding) == 1,
1063 "StackMapEncoding should not require alignment");
David Srbecky09ed0982016-02-12 21:58:43 +00001064 stack_map_encoding = *reinterpret_cast<const StackMapEncoding*>(ptr);
1065 ptr += sizeof(StackMapEncoding);
David Srbecky61b28a12016-02-25 21:55:03 +00001066 if (stack_map_encoding.GetInlineInfoEncoding().BitSize() > 0) {
1067 static_assert(alignof(InlineInfoEncoding) == 1,
1068 "InlineInfoEncoding should not require alignment");
1069 inline_info_encoding = *reinterpret_cast<const InlineInfoEncoding*>(ptr);
1070 ptr += sizeof(InlineInfoEncoding);
1071 } else {
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001072 inline_info_encoding = InlineInfoEncoding{}; // NOLINT.
David Srbecky61b28a12016-02-25 21:55:03 +00001073 }
David Srbecky09ed0982016-02-12 21:58:43 +00001074 header_size = dchecked_integral_cast<uint8_t>(ptr - reinterpret_cast<const uint8_t*>(data));
1075 }
1076
1077 template<typename Vector>
1078 void Compress(Vector* dest) const {
1079 EncodeUnsignedLeb128(dest, non_header_size);
1080 EncodeUnsignedLeb128(dest, number_of_stack_maps);
1081 EncodeUnsignedLeb128(dest, stack_map_size_in_bytes);
1082 EncodeUnsignedLeb128(dest, number_of_location_catalog_entries);
David Srbecky61b28a12016-02-25 21:55:03 +00001083 const uint8_t* stack_map_ptr = reinterpret_cast<const uint8_t*>(&stack_map_encoding);
1084 dest->insert(dest->end(), stack_map_ptr, stack_map_ptr + sizeof(StackMapEncoding));
1085 if (stack_map_encoding.GetInlineInfoEncoding().BitSize() > 0) {
1086 const uint8_t* inline_info_ptr = reinterpret_cast<const uint8_t*>(&inline_info_encoding);
1087 dest->insert(dest->end(), inline_info_ptr, inline_info_ptr + sizeof(InlineInfoEncoding));
1088 }
David Srbecky09ed0982016-02-12 21:58:43 +00001089 }
1090};
1091
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001092/**
1093 * Wrapper around all compiler information collected for a method.
1094 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +01001095 *
David Srbecky09ed0982016-02-12 21:58:43 +00001096 * [CodeInfoEncoding, StackMap+, DexRegisterLocationCatalog+, DexRegisterMap+, InlineInfo*]
Roland Levillain1c1da432015-07-16 11:54:44 +01001097 *
David Srbecky09ed0982016-02-12 21:58:43 +00001098 * where CodeInfoEncoding is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +01001099 *
David Srbecky09ed0982016-02-12 21:58:43 +00001100 * [non_header_size, number_of_stack_maps, stack_map_size_in_bytes,
1101 * number_of_location_catalog_entries, StackMapEncoding]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001102 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001103class CodeInfo {
1104 public:
David Srbecky09ed0982016-02-12 21:58:43 +00001105 explicit CodeInfo(MemoryRegion region) : region_(region) {
1106 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001107
Nicolas Geoffray39468442014-09-02 15:17:15 +01001108 explicit CodeInfo(const void* data) {
David Srbecky09ed0982016-02-12 21:58:43 +00001109 CodeInfoEncoding encoding = CodeInfoEncoding(data);
1110 region_ = MemoryRegion(const_cast<void*>(data),
1111 encoding.header_size + encoding.non_header_size);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001112 }
1113
David Srbecky09ed0982016-02-12 21:58:43 +00001114 CodeInfoEncoding ExtractEncoding() const {
Mathieu Chartier01c78142017-01-05 10:17:55 -08001115 CodeInfoEncoding encoding(region_.start());
1116 AssertValidStackMap(encoding);
1117 return encoding;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001118 }
1119
David Srbecky09ed0982016-02-12 21:58:43 +00001120 bool HasInlineInfo(const CodeInfoEncoding& encoding) const {
1121 return encoding.stack_map_encoding.GetInlineInfoEncoding().BitSize() > 0;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001122 }
1123
David Srbecky09ed0982016-02-12 21:58:43 +00001124 DexRegisterLocationCatalog GetDexRegisterLocationCatalog(const CodeInfoEncoding& encoding) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +00001125 return DexRegisterLocationCatalog(region_.Subregion(
David Brazdilf677ebf2015-05-29 16:29:43 +01001126 GetDexRegisterLocationCatalogOffset(encoding),
1127 GetDexRegisterLocationCatalogSize(encoding)));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001128 }
1129
Mingyao Yangccfa8852017-01-18 14:51:59 -08001130 ALWAYS_INLINE StackMap GetStackMapAt(size_t i, const CodeInfoEncoding& encoding) const {
David Srbecky09ed0982016-02-12 21:58:43 +00001131 size_t stack_map_size = encoding.stack_map_size_in_bytes;
David Brazdilf677ebf2015-05-29 16:29:43 +01001132 return StackMap(GetStackMaps(encoding).Subregion(i * stack_map_size, stack_map_size));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001133 }
1134
David Srbecky09ed0982016-02-12 21:58:43 +00001135 uint32_t GetNumberOfLocationCatalogEntries(const CodeInfoEncoding& encoding) const {
1136 return encoding.number_of_location_catalog_entries;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001137 }
1138
David Srbecky09ed0982016-02-12 21:58:43 +00001139 uint32_t GetDexRegisterLocationCatalogSize(const CodeInfoEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +01001140 return ComputeDexRegisterLocationCatalogSize(GetDexRegisterLocationCatalogOffset(encoding),
David Srbecky09ed0982016-02-12 21:58:43 +00001141 GetNumberOfLocationCatalogEntries(encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001142 }
1143
David Srbecky09ed0982016-02-12 21:58:43 +00001144 uint32_t GetNumberOfStackMaps(const CodeInfoEncoding& encoding) const {
1145 return encoding.number_of_stack_maps;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001146 }
1147
David Brazdil77a48ae2015-09-15 12:34:04 +00001148 // Get the size of all the stack maps of this CodeInfo object, in bytes.
David Srbecky09ed0982016-02-12 21:58:43 +00001149 size_t GetStackMapsSize(const CodeInfoEncoding& encoding) const {
1150 return encoding.stack_map_size_in_bytes * GetNumberOfStackMaps(encoding);
Roland Levillain29ba1b02015-03-13 11:45:07 +00001151 }
1152
David Srbecky09ed0982016-02-12 21:58:43 +00001153 uint32_t GetDexRegisterLocationCatalogOffset(const CodeInfoEncoding& encoding) const {
1154 return GetStackMapsOffset(encoding) + GetStackMapsSize(encoding);
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001155 }
1156
David Srbecky09ed0982016-02-12 21:58:43 +00001157 size_t GetDexRegisterMapsOffset(const CodeInfoEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +01001158 return GetDexRegisterLocationCatalogOffset(encoding)
1159 + GetDexRegisterLocationCatalogSize(encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001160 }
1161
David Srbecky09ed0982016-02-12 21:58:43 +00001162 uint32_t GetStackMapsOffset(const CodeInfoEncoding& encoding) const {
1163 return encoding.header_size;
Nicolas Geoffray6530baf2015-05-26 15:22:58 +01001164 }
1165
David Brazdilf677ebf2015-05-29 16:29:43 +01001166 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map,
David Srbecky09ed0982016-02-12 21:58:43 +00001167 const CodeInfoEncoding& encoding,
David Brazdilf677ebf2015-05-29 16:29:43 +01001168 uint32_t number_of_dex_registers) const {
David Srbecky09ed0982016-02-12 21:58:43 +00001169 if (!stack_map.HasDexRegisterMap(encoding.stack_map_encoding)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001170 return DexRegisterMap();
1171 } else {
1172 uint32_t offset = GetDexRegisterMapsOffset(encoding)
David Srbecky09ed0982016-02-12 21:58:43 +00001173 + stack_map.GetDexRegisterMapOffset(encoding.stack_map_encoding);
1174 size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001175 return DexRegisterMap(region_.Subregion(offset, size));
1176 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001177 }
1178
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001179 // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`.
1180 DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
1181 InlineInfo inline_info,
David Srbecky09ed0982016-02-12 21:58:43 +00001182 const CodeInfoEncoding& encoding,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001183 uint32_t number_of_dex_registers) const {
David Srbecky61b28a12016-02-25 21:55:03 +00001184 if (!inline_info.HasDexRegisterMapAtDepth(encoding.inline_info_encoding, depth)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001185 return DexRegisterMap();
1186 } else {
David Srbecky61b28a12016-02-25 21:55:03 +00001187 uint32_t offset = GetDexRegisterMapsOffset(encoding) +
1188 inline_info.GetDexRegisterMapOffsetAtDepth(encoding.inline_info_encoding, depth);
David Srbecky09ed0982016-02-12 21:58:43 +00001189 size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001190 return DexRegisterMap(region_.Subregion(offset, size));
1191 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001192 }
1193
David Srbecky09ed0982016-02-12 21:58:43 +00001194 InlineInfo GetInlineInfoOf(StackMap stack_map, const CodeInfoEncoding& encoding) const {
1195 DCHECK(stack_map.HasInlineInfo(encoding.stack_map_encoding));
1196 uint32_t offset = stack_map.GetInlineDescriptorOffset(encoding.stack_map_encoding)
David Brazdilf677ebf2015-05-29 16:29:43 +01001197 + GetDexRegisterMapsOffset(encoding);
David Srbecky61b28a12016-02-25 21:55:03 +00001198 return InlineInfo(region_.Subregion(offset, region_.size() - offset));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001199 }
1200
David Srbecky09ed0982016-02-12 21:58:43 +00001201 StackMap GetStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1202 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +01001203 StackMap stack_map = GetStackMapAt(i, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001204 if (stack_map.GetDexPc(encoding.stack_map_encoding) == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001205 return stack_map;
1206 }
1207 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001208 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001209 }
1210
David Brazdil77a48ae2015-09-15 12:34:04 +00001211 // Searches the stack map list backwards because catch stack maps are stored
1212 // at the end.
David Srbecky09ed0982016-02-12 21:58:43 +00001213 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1214 for (size_t i = GetNumberOfStackMaps(encoding); i > 0; --i) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001215 StackMap stack_map = GetStackMapAt(i - 1, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001216 if (stack_map.GetDexPc(encoding.stack_map_encoding) == dex_pc) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001217 return stack_map;
1218 }
1219 }
1220 return StackMap();
1221 }
1222
David Srbecky09ed0982016-02-12 21:58:43 +00001223 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1224 size_t e = GetNumberOfStackMaps(encoding);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001225 if (e == 0) {
1226 // There cannot be OSR stack map if there is no stack map.
1227 return StackMap();
1228 }
1229 // Walk over all stack maps. If two consecutive stack maps are identical, then we
1230 // have found a stack map suitable for OSR.
David Srbecky09ed0982016-02-12 21:58:43 +00001231 const StackMapEncoding& stack_map_encoding = encoding.stack_map_encoding;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001232 for (size_t i = 0; i < e - 1; ++i) {
1233 StackMap stack_map = GetStackMapAt(i, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001234 if (stack_map.GetDexPc(stack_map_encoding) == dex_pc) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001235 StackMap other = GetStackMapAt(i + 1, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001236 if (other.GetDexPc(stack_map_encoding) == dex_pc &&
1237 other.GetNativePcOffset(stack_map_encoding) ==
1238 stack_map.GetNativePcOffset(stack_map_encoding)) {
1239 DCHECK_EQ(other.GetDexRegisterMapOffset(stack_map_encoding),
1240 stack_map.GetDexRegisterMapOffset(stack_map_encoding));
1241 DCHECK(!stack_map.HasInlineInfo(stack_map_encoding));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001242 if (i < e - 2) {
1243 // Make sure there are not three identical stack maps following each other.
David Srbecky09ed0982016-02-12 21:58:43 +00001244 DCHECK_NE(stack_map.GetNativePcOffset(stack_map_encoding),
1245 GetStackMapAt(i + 2, encoding).GetNativePcOffset(stack_map_encoding));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001246 }
1247 return stack_map;
1248 }
1249 }
1250 }
1251 return StackMap();
1252 }
1253
David Brazdilf677ebf2015-05-29 16:29:43 +01001254 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset,
David Srbecky09ed0982016-02-12 21:58:43 +00001255 const CodeInfoEncoding& encoding) const {
David Brazdil77a48ae2015-09-15 12:34:04 +00001256 // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack
1257 // maps are not. If we knew that the method does not have try/catch,
1258 // we could do binary search.
David Srbecky09ed0982016-02-12 21:58:43 +00001259 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +01001260 StackMap stack_map = GetStackMapAt(i, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001261 if (stack_map.GetNativePcOffset(encoding.stack_map_encoding) == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001262 return stack_map;
1263 }
1264 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001265 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001266 }
1267
Roland Levillainf2650d12015-05-28 14:53:28 +01001268 // Dump this CodeInfo object on `os`. `code_offset` is the (absolute)
1269 // native PC of the compiled method and `number_of_dex_registers` the
1270 // number of Dex virtual registers used in this method. If
1271 // `dump_stack_maps` is true, also dump the stack maps and the
1272 // associated Dex register maps.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001273 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001274 uint32_t code_offset,
1275 uint16_t number_of_dex_registers,
1276 bool dump_stack_maps) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001277
Mathieu Chartier01c78142017-01-05 10:17:55 -08001278 // Check that the code info has valid stack map and abort if it does not.
1279 void AssertValidStackMap(const CodeInfoEncoding& encoding) const {
1280 if (region_.size() != 0 && region_.size() < GetStackMapsSize(encoding)) {
1281 LOG(FATAL) << region_.size() << "\n"
1282 << encoding.header_size << "\n"
1283 << encoding.non_header_size << "\n"
1284 << encoding.number_of_location_catalog_entries << "\n"
1285 << encoding.number_of_stack_maps << "\n"
1286 << encoding.stack_map_size_in_bytes;
1287 }
1288 }
1289
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001290 private:
Mingyao Yangccfa8852017-01-18 14:51:59 -08001291 ALWAYS_INLINE MemoryRegion GetStackMaps(const CodeInfoEncoding& encoding) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001292 return region_.size() == 0
1293 ? MemoryRegion()
David Srbecky09ed0982016-02-12 21:58:43 +00001294 : region_.Subregion(GetStackMapsOffset(encoding), GetStackMapsSize(encoding));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001295 }
1296
Roland Levillaina552e1c2015-03-26 15:01:03 +00001297 // Compute the size of the Dex register map associated to the stack map at
1298 // `dex_register_map_offset_in_code_info`.
David Srbecky09ed0982016-02-12 21:58:43 +00001299 size_t ComputeDexRegisterMapSizeOf(const CodeInfoEncoding& encoding,
1300 uint32_t dex_register_map_offset_in_code_info,
Roland Levillaina552e1c2015-03-26 15:01:03 +00001301 uint16_t number_of_dex_registers) const {
1302 // Offset where the actual mapping data starts within art::DexRegisterMap.
1303 size_t location_mapping_data_offset_in_dex_register_map =
1304 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1305 // Create a temporary art::DexRegisterMap to be able to call
1306 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1307 DexRegisterMap dex_register_map_without_locations(
1308 MemoryRegion(region_.Subregion(dex_register_map_offset_in_code_info,
1309 location_mapping_data_offset_in_dex_register_map)));
1310 size_t number_of_live_dex_registers =
1311 dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
1312 size_t location_mapping_data_size_in_bits =
David Srbecky09ed0982016-02-12 21:58:43 +00001313 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries(encoding))
Roland Levillaina552e1c2015-03-26 15:01:03 +00001314 * number_of_live_dex_registers;
1315 size_t location_mapping_data_size_in_bytes =
1316 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1317 size_t dex_register_map_size =
1318 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1319 return dex_register_map_size;
1320 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001321
Roland Levillaina552e1c2015-03-26 15:01:03 +00001322 // Compute the size of a Dex register location catalog starting at offset `origin`
1323 // in `region_` and containing `number_of_dex_locations` entries.
1324 size_t ComputeDexRegisterLocationCatalogSize(uint32_t origin,
1325 uint32_t number_of_dex_locations) const {
1326 // TODO: Ideally, we would like to use art::DexRegisterLocationCatalog::Size or
1327 // art::DexRegisterLocationCatalog::FindLocationOffset, but the
1328 // DexRegisterLocationCatalog is not yet built. Try to factor common code.
1329 size_t offset = origin + DexRegisterLocationCatalog::kFixedSize;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001330
Roland Levillaina552e1c2015-03-26 15:01:03 +00001331 // Skip the first `number_of_dex_locations - 1` entries.
1332 for (uint16_t i = 0; i < number_of_dex_locations; ++i) {
1333 // Read the first next byte and inspect its first 3 bits to decide
1334 // whether it is a short or a large location.
1335 DexRegisterLocationCatalog::ShortLocation first_byte =
1336 region_.LoadUnaligned<DexRegisterLocationCatalog::ShortLocation>(offset);
1337 DexRegisterLocation::Kind kind =
1338 DexRegisterLocationCatalog::ExtractKindFromShortLocation(first_byte);
1339 if (DexRegisterLocation::IsShortLocationKind(kind)) {
1340 // Short location. Skip the current byte.
1341 offset += DexRegisterLocationCatalog::SingleShortEntrySize();
1342 } else {
1343 // Large location. Skip the 5 next bytes.
1344 offset += DexRegisterLocationCatalog::SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001345 }
1346 }
1347 size_t size = offset - origin;
1348 return size;
1349 }
1350
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001351 MemoryRegion region_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001352 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001353};
1354
Roland Levillain1c1da432015-07-16 11:54:44 +01001355#undef ELEMENT_BYTE_OFFSET_AFTER
1356#undef ELEMENT_BIT_OFFSET_AFTER
1357
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001358} // namespace art
1359
1360#endif // ART_RUNTIME_STACK_MAP_H_