blob: 7c50f97d39f9a9a7366e434e014a82191f0212a7 [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"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010022#include "memory_region.h"
David Srbecky09ed0982016-02-12 21:58:43 +000023#include "leb128.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010024
25namespace art {
26
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010027class VariableIndentationOutputStream;
28
Roland Levillaina2d8ec62015-03-12 15:25:29 +000029// Size of a frame slot, in bytes. This constant is a signed value,
30// to please the compiler in arithmetic operations involving int32_t
31// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000032static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000033
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000034// Size of Dex virtual registers.
Roland Levillaina552e1c2015-03-26 15:01:03 +000035static constexpr size_t kVRegSize = 4;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000036
Nicolas Geoffray004c2302015-03-20 10:06:38 +000037class CodeInfo;
David Brazdilf677ebf2015-05-29 16:29:43 +010038class StackMapEncoding;
David Srbecky09ed0982016-02-12 21:58:43 +000039struct CodeInfoEncoding;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000040
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010041/**
42 * Classes in the following file are wrapper on stack map information backed
43 * by a MemoryRegion. As such they read and write to the region, they don't have
44 * their own fields.
45 */
46
Roland Levillaina2d8ec62015-03-12 15:25:29 +000047// Dex register location container used by DexRegisterMap and StackMapStream.
48class DexRegisterLocation {
49 public:
50 /*
51 * The location kind used to populate the Dex register information in a
52 * StackMapStream can either be:
David Brazdild9cb68e2015-08-25 13:52:43 +010053 * - kStack: vreg stored on the stack, value holds the stack offset;
54 * - kInRegister: vreg stored in low 32 bits of a core physical register,
55 * value holds the register number;
56 * - kInRegisterHigh: vreg stored in high 32 bits of a core physical register,
57 * value holds the register number;
58 * - kInFpuRegister: vreg stored in low 32 bits of an FPU register,
59 * value holds the register number;
60 * - kInFpuRegisterHigh: vreg stored in high 32 bits of an FPU register,
61 * value holds the register number;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000062 * - kConstant: value holds the constant;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000063 *
64 * In addition, DexRegisterMap also uses these values:
65 * - kInStackLargeOffset: value holds a "large" stack offset (greater than
Roland Levillaina552e1c2015-03-26 15:01:03 +000066 * or equal to 128 bytes);
67 * - kConstantLargeValue: value holds a "large" constant (lower than 0, or
David Brazdild9cb68e2015-08-25 13:52:43 +010068 * or greater than or equal to 32);
69 * - kNone: the register has no location, meaning it has not been set.
Roland Levillaina2d8ec62015-03-12 15:25:29 +000070 */
71 enum class Kind : uint8_t {
72 // Short location kinds, for entries fitting on one byte (3 bits
73 // for the kind, 5 bits for the value) in a DexRegisterMap.
David Brazdild9cb68e2015-08-25 13:52:43 +010074 kInStack = 0, // 0b000
75 kInRegister = 1, // 0b001
76 kInRegisterHigh = 2, // 0b010
Roland Levillaina2d8ec62015-03-12 15:25:29 +000077 kInFpuRegister = 3, // 0b011
David Brazdild9cb68e2015-08-25 13:52:43 +010078 kInFpuRegisterHigh = 4, // 0b100
79 kConstant = 5, // 0b101
Roland Levillaina2d8ec62015-03-12 15:25:29 +000080
81 // Large location kinds, requiring a 5-byte encoding (1 byte for the
82 // kind, 4 bytes for the value).
83
84 // Stack location at a large offset, meaning that the offset value
85 // divided by the stack frame slot size (4 bytes) cannot fit on a
86 // 5-bit unsigned integer (i.e., this offset value is greater than
87 // or equal to 2^5 * 4 = 128 bytes).
David Brazdild9cb68e2015-08-25 13:52:43 +010088 kInStackLargeOffset = 6, // 0b110
Roland Levillaina2d8ec62015-03-12 15:25:29 +000089
90 // Large constant, that cannot fit on a 5-bit signed integer (i.e.,
Roland Levillaina552e1c2015-03-26 15:01:03 +000091 // lower than 0, or greater than or equal to 2^5 = 32).
David Brazdild9cb68e2015-08-25 13:52:43 +010092 kConstantLargeValue = 7, // 0b111
93
94 // Entries with no location are not stored and do not need own marker.
95 kNone = static_cast<uint8_t>(-1),
Roland Levillaina2d8ec62015-03-12 15:25:29 +000096
97 kLastLocationKind = kConstantLargeValue
98 };
99
100 static_assert(
101 sizeof(Kind) == 1u,
102 "art::DexRegisterLocation::Kind has a size different from one byte.");
103
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000104 static bool IsShortLocationKind(Kind kind) {
105 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000106 case Kind::kInStack:
107 case Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100108 case Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000109 case Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100110 case Kind::kInFpuRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000111 case Kind::kConstant:
112 return true;
113
114 case Kind::kInStackLargeOffset:
115 case Kind::kConstantLargeValue:
116 return false;
117
David Brazdild9cb68e2015-08-25 13:52:43 +0100118 case Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000119 LOG(FATAL) << "Unexpected location kind";
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000120 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100121 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000122 }
123
124 // Convert `kind` to a "surface" kind, i.e. one that doesn't include
125 // any value with a "large" qualifier.
126 // TODO: Introduce another enum type for the surface kind?
127 static Kind ConvertToSurfaceKind(Kind kind) {
128 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000129 case Kind::kInStack:
130 case Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100131 case Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000132 case Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100133 case Kind::kInFpuRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000134 case Kind::kConstant:
135 return kind;
136
137 case Kind::kInStackLargeOffset:
138 return Kind::kInStack;
139
140 case Kind::kConstantLargeValue:
141 return Kind::kConstant;
142
David Brazdild9cb68e2015-08-25 13:52:43 +0100143 case Kind::kNone:
144 return kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000145 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100146 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000147 }
148
Roland Levillaina552e1c2015-03-26 15:01:03 +0000149 // Required by art::StackMapStream::LocationCatalogEntriesIndices.
150 DexRegisterLocation() : kind_(Kind::kNone), value_(0) {}
151
152 DexRegisterLocation(Kind kind, int32_t value) : kind_(kind), value_(value) {}
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000153
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000154 static DexRegisterLocation None() {
155 return DexRegisterLocation(Kind::kNone, 0);
156 }
157
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000158 // Get the "surface" kind of the location, i.e., the one that doesn't
159 // include any value with a "large" qualifier.
160 Kind GetKind() const {
161 return ConvertToSurfaceKind(kind_);
162 }
163
164 // Get the value of the location.
165 int32_t GetValue() const { return value_; }
166
167 // Get the actual kind of the location.
168 Kind GetInternalKind() const { return kind_; }
169
Calin Juravle6ae70962015-03-18 16:31:28 +0000170 bool operator==(DexRegisterLocation other) const {
171 return kind_ == other.kind_ && value_ == other.value_;
172 }
173
174 bool operator!=(DexRegisterLocation other) const {
175 return !(*this == other);
176 }
177
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000178 private:
179 Kind kind_;
180 int32_t value_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000181
182 friend class DexRegisterLocationHashFn;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000183};
184
David Srbecky7dc11782016-02-25 13:23:56 +0000185std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation::Kind& kind);
186
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100187/**
Roland Levillaina552e1c2015-03-26 15:01:03 +0000188 * Store information on unique Dex register locations used in a method.
189 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100190 *
191 * [DexRegisterLocation+].
192 *
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000193 * DexRegisterLocations are either 1- or 5-byte wide (see art::DexRegisterLocation::Kind).
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100194 */
Roland Levillaina552e1c2015-03-26 15:01:03 +0000195class DexRegisterLocationCatalog {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100196 public:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000197 explicit DexRegisterLocationCatalog(MemoryRegion region) : region_(region) {}
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100198
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000199 // Short (compressed) location, fitting on one byte.
200 typedef uint8_t ShortLocation;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100201
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000202 void SetRegisterInfo(size_t offset, const DexRegisterLocation& dex_register_location) {
203 DexRegisterLocation::Kind kind = ComputeCompressedKind(dex_register_location);
204 int32_t value = dex_register_location.GetValue();
205 if (DexRegisterLocation::IsShortLocationKind(kind)) {
206 // Short location. Compress the kind and the value as a single byte.
207 if (kind == DexRegisterLocation::Kind::kInStack) {
208 // Instead of storing stack offsets expressed in bytes for
209 // short stack locations, store slot offsets. A stack offset
210 // is a multiple of 4 (kFrameSlotSize). This means that by
211 // dividing it by 4, we can fit values from the [0, 128)
212 // interval in a short stack location, and not just values
213 // from the [0, 32) interval.
214 DCHECK_EQ(value % kFrameSlotSize, 0);
215 value /= kFrameSlotSize;
216 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000217 DCHECK(IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000218 region_.StoreUnaligned<ShortLocation>(offset, MakeShortLocation(kind, value));
219 } else {
220 // Large location. Write the location on one byte and the value
221 // on 4 bytes.
Roland Levillaina552e1c2015-03-26 15:01:03 +0000222 DCHECK(!IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000223 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
224 // Also divide large stack offsets by 4 for the sake of consistency.
225 DCHECK_EQ(value % kFrameSlotSize, 0);
226 value /= kFrameSlotSize;
227 }
228 // Data can be unaligned as the written Dex register locations can
229 // either be 1-byte or 5-byte wide. Use
230 // art::MemoryRegion::StoreUnaligned instead of
231 // art::MemoryRegion::Store to prevent unligned word accesses on ARM.
232 region_.StoreUnaligned<DexRegisterLocation::Kind>(offset, kind);
233 region_.StoreUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind), value);
Roland Levillain442b46a2015-02-18 16:54:21 +0000234 }
235 }
236
Roland Levillaina552e1c2015-03-26 15:01:03 +0000237 // Find the offset of the location catalog entry number `location_catalog_entry_index`.
238 size_t FindLocationOffset(size_t location_catalog_entry_index) const {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000239 size_t offset = kFixedSize;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000240 // Skip the first `location_catalog_entry_index - 1` entries.
241 for (uint16_t i = 0; i < location_catalog_entry_index; ++i) {
242 // Read the first next byte and inspect its first 3 bits to decide
243 // whether it is a short or a large location.
244 DexRegisterLocation::Kind kind = ExtractKindAtOffset(offset);
245 if (DexRegisterLocation::IsShortLocationKind(kind)) {
246 // Short location. Skip the current byte.
247 offset += SingleShortEntrySize();
248 } else {
249 // Large location. Skip the 5 next bytes.
250 offset += SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000251 }
252 }
253 return offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100254 }
255
Roland Levillaina552e1c2015-03-26 15:01:03 +0000256 // Get the internal kind of entry at `location_catalog_entry_index`.
257 DexRegisterLocation::Kind GetLocationInternalKind(size_t location_catalog_entry_index) const {
258 if (location_catalog_entry_index == kNoLocationEntryIndex) {
259 return DexRegisterLocation::Kind::kNone;
260 }
261 return ExtractKindAtOffset(FindLocationOffset(location_catalog_entry_index));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100262 }
263
Roland Levillaina552e1c2015-03-26 15:01:03 +0000264 // Get the (surface) kind and value of entry at `location_catalog_entry_index`.
265 DexRegisterLocation GetDexRegisterLocation(size_t location_catalog_entry_index) const {
266 if (location_catalog_entry_index == kNoLocationEntryIndex) {
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000267 return DexRegisterLocation::None();
268 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000269 size_t offset = FindLocationOffset(location_catalog_entry_index);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000270 // Read the first byte and inspect its first 3 bits to get the location.
271 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
272 DexRegisterLocation::Kind kind = ExtractKindFromShortLocation(first_byte);
273 if (DexRegisterLocation::IsShortLocationKind(kind)) {
274 // Short location. Extract the value from the remaining 5 bits.
275 int32_t value = ExtractValueFromShortLocation(first_byte);
276 if (kind == DexRegisterLocation::Kind::kInStack) {
277 // Convert the stack slot (short) offset to a byte offset value.
278 value *= kFrameSlotSize;
279 }
280 return DexRegisterLocation(kind, value);
281 } else {
282 // Large location. Read the four next bytes to get the value.
283 int32_t value = region_.LoadUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind));
284 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
285 // Convert the stack slot (large) offset to a byte offset value.
286 value *= kFrameSlotSize;
287 }
288 return DexRegisterLocation(kind, value);
289 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100290 }
291
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000292 // Compute the compressed kind of `location`.
293 static DexRegisterLocation::Kind ComputeCompressedKind(const DexRegisterLocation& location) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100294 DexRegisterLocation::Kind kind = location.GetInternalKind();
295 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000296 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000297 return IsShortStackOffsetValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000298 ? DexRegisterLocation::Kind::kInStack
299 : DexRegisterLocation::Kind::kInStackLargeOffset;
300
David Brazdild9cb68e2015-08-25 13:52:43 +0100301 case DexRegisterLocation::Kind::kInRegister:
302 case DexRegisterLocation::Kind::kInRegisterHigh:
303 DCHECK_GE(location.GetValue(), 0);
304 DCHECK_LT(location.GetValue(), 1 << kValueBits);
305 return kind;
306
307 case DexRegisterLocation::Kind::kInFpuRegister:
308 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
309 DCHECK_GE(location.GetValue(), 0);
310 DCHECK_LT(location.GetValue(), 1 << kValueBits);
311 return kind;
312
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000313 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000314 return IsShortConstantValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000315 ? DexRegisterLocation::Kind::kConstant
316 : DexRegisterLocation::Kind::kConstantLargeValue;
317
David Brazdild9cb68e2015-08-25 13:52:43 +0100318 case DexRegisterLocation::Kind::kConstantLargeValue:
319 case DexRegisterLocation::Kind::kInStackLargeOffset:
320 case DexRegisterLocation::Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000321 LOG(FATAL) << "Unexpected location kind " << kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000322 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100323 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000324 }
325
326 // Can `location` be turned into a short location?
327 static bool CanBeEncodedAsShortLocation(const DexRegisterLocation& location) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100328 DexRegisterLocation::Kind kind = location.GetInternalKind();
329 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000330 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000331 return IsShortStackOffsetValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000332
David Brazdild9cb68e2015-08-25 13:52:43 +0100333 case DexRegisterLocation::Kind::kInRegister:
334 case DexRegisterLocation::Kind::kInRegisterHigh:
335 case DexRegisterLocation::Kind::kInFpuRegister:
336 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
337 return true;
338
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000339 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000340 return IsShortConstantValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000341
David Brazdild9cb68e2015-08-25 13:52:43 +0100342 case DexRegisterLocation::Kind::kConstantLargeValue:
343 case DexRegisterLocation::Kind::kInStackLargeOffset:
344 case DexRegisterLocation::Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000345 LOG(FATAL) << "Unexpected location kind " << kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000346 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100347 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000348 }
349
350 static size_t EntrySize(const DexRegisterLocation& location) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000351 return CanBeEncodedAsShortLocation(location) ? SingleShortEntrySize() : SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000352 }
353
354 static size_t SingleShortEntrySize() {
355 return sizeof(ShortLocation);
356 }
357
358 static size_t SingleLargeEntrySize() {
359 return sizeof(DexRegisterLocation::Kind) + sizeof(int32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100360 }
361
Roland Levillain12baf472015-03-05 12:41:42 +0000362 size_t Size() const {
363 return region_.size();
364 }
365
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100366 void Dump(VariableIndentationOutputStream* vios, const CodeInfo& code_info);
Roland Levillain0396ed72015-05-27 15:12:19 +0100367
Roland Levillaina552e1c2015-03-26 15:01:03 +0000368 // Special (invalid) Dex register location catalog entry index meaning
369 // that there is no location for a given Dex register (i.e., it is
370 // mapped to a DexRegisterLocation::Kind::kNone location).
371 static constexpr size_t kNoLocationEntryIndex = -1;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100372
Roland Levillain12baf472015-03-05 12:41:42 +0000373 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000374 static constexpr int kFixedSize = 0;
375
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000376 // Width of the kind "field" in a short location, in bits.
377 static constexpr size_t kKindBits = 3;
378 // Width of the value "field" in a short location, in bits.
379 static constexpr size_t kValueBits = 5;
380
381 static constexpr uint8_t kKindMask = (1 << kKindBits) - 1;
382 static constexpr int32_t kValueMask = (1 << kValueBits) - 1;
383 static constexpr size_t kKindOffset = 0;
384 static constexpr size_t kValueOffset = kKindBits;
385
Roland Levillaina552e1c2015-03-26 15:01:03 +0000386 static bool IsShortStackOffsetValue(int32_t value) {
387 DCHECK_EQ(value % kFrameSlotSize, 0);
388 return IsShortValue(value / kFrameSlotSize);
389 }
390
391 static bool IsShortConstantValue(int32_t value) {
392 return IsShortValue(value);
393 }
394
395 static bool IsShortValue(int32_t value) {
396 return IsUint<kValueBits>(value);
397 }
398
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000399 static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000400 uint8_t kind_integer_value = static_cast<uint8_t>(kind);
401 DCHECK(IsUint<kKindBits>(kind_integer_value)) << kind_integer_value;
402 DCHECK(IsShortValue(value)) << value;
403 return (kind_integer_value & kKindMask) << kKindOffset
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000404 | (value & kValueMask) << kValueOffset;
405 }
406
407 static DexRegisterLocation::Kind ExtractKindFromShortLocation(ShortLocation location) {
408 uint8_t kind = (location >> kKindOffset) & kKindMask;
409 DCHECK_LE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kLastLocationKind));
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000410 // We do not encode kNone locations in the stack map.
411 DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000412 return static_cast<DexRegisterLocation::Kind>(kind);
413 }
414
415 static int32_t ExtractValueFromShortLocation(ShortLocation location) {
416 return (location >> kValueOffset) & kValueMask;
417 }
418
419 // Extract a location kind from the byte at position `offset`.
420 DexRegisterLocation::Kind ExtractKindAtOffset(size_t offset) const {
421 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
422 return ExtractKindFromShortLocation(first_byte);
423 }
424
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100425 MemoryRegion region_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000426
427 friend class CodeInfo;
428 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100429};
430
Roland Levillaina552e1c2015-03-26 15:01:03 +0000431/* Information on Dex register locations for a specific PC, mapping a
432 * stack map's Dex register to a location entry in a DexRegisterLocationCatalog.
433 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100434 *
435 * [live_bit_mask, entries*]
436 *
Roland Levillaina552e1c2015-03-26 15:01:03 +0000437 * where entries are concatenated unsigned integer values encoded on a number
438 * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending
439 * on the number of entries in the Dex register location catalog
440 * (see DexRegisterMap::SingleEntrySizeInBits). The map is 1-byte aligned.
441 */
442class DexRegisterMap {
443 public:
444 explicit DexRegisterMap(MemoryRegion region) : region_(region) {}
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000445 DexRegisterMap() {}
446
447 bool IsValid() const { return region_.pointer() != nullptr; }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000448
449 // Get the surface kind of Dex register `dex_register_number`.
450 DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number,
451 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100452 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000453 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000454 return DexRegisterLocation::ConvertToSurfaceKind(
David Brazdilf677ebf2015-05-29 16:29:43 +0100455 GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info, enc));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000456 }
457
458 // Get the internal kind of Dex register `dex_register_number`.
459 DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number,
460 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100461 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000462 const CodeInfoEncoding& enc) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000463
464 // Get the Dex register location `dex_register_number`.
465 DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number,
466 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100467 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000468 const CodeInfoEncoding& enc) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000469
470 int32_t GetStackOffsetInBytes(uint16_t dex_register_number,
471 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100472 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000473 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000474 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100475 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000476 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
477 // GetDexRegisterLocation returns the offset in bytes.
478 return location.GetValue();
479 }
480
481 int32_t GetConstant(uint16_t dex_register_number,
482 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100483 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000484 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000485 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100486 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
David Srbecky7dc11782016-02-25 13:23:56 +0000487 DCHECK_EQ(location.GetKind(), DexRegisterLocation::Kind::kConstant);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000488 return location.GetValue();
489 }
490
491 int32_t GetMachineRegister(uint16_t dex_register_number,
492 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100493 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000494 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000495 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100496 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
David Brazdild9cb68e2015-08-25 13:52:43 +0100497 DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister ||
498 location.GetInternalKind() == DexRegisterLocation::Kind::kInRegisterHigh ||
499 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister ||
500 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh)
David Srbecky7dc11782016-02-25 13:23:56 +0000501 << location.GetInternalKind();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000502 return location.GetValue();
503 }
504
505 // Get the index of the entry in the Dex register location catalog
506 // corresponding to `dex_register_number`.
507 size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number,
508 uint16_t number_of_dex_registers,
509 size_t number_of_location_catalog_entries) const {
510 if (!IsDexRegisterLive(dex_register_number)) {
511 return DexRegisterLocationCatalog::kNoLocationEntryIndex;
512 }
513
514 if (number_of_location_catalog_entries == 1) {
515 // We do not allocate space for location maps in the case of a
516 // single-entry location catalog, as it is useless. The only valid
517 // entry index is 0;
518 return 0;
519 }
520
521 // The bit offset of the beginning of the map locations.
522 size_t map_locations_offset_in_bits =
523 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
524 size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number);
525 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
526 // The bit size of an entry.
527 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
528 // The bit offset where `index_in_dex_register_map` is located.
529 size_t entry_offset_in_bits =
530 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
531 size_t location_catalog_entry_index =
532 region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits);
533 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
534 return location_catalog_entry_index;
535 }
536
537 // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`.
538 void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map,
539 size_t location_catalog_entry_index,
540 uint16_t number_of_dex_registers,
541 size_t number_of_location_catalog_entries) {
542 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
543 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
544
545 if (number_of_location_catalog_entries == 1) {
546 // We do not allocate space for location maps in the case of a
547 // single-entry location catalog, as it is useless.
548 return;
549 }
550
551 // The bit offset of the beginning of the map locations.
552 size_t map_locations_offset_in_bits =
553 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
554 // The bit size of an entry.
555 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
556 // The bit offset where `index_in_dex_register_map` is located.
557 size_t entry_offset_in_bits =
558 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
559 region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits);
560 }
561
562 void SetLiveBitMask(uint16_t number_of_dex_registers,
563 const BitVector& live_dex_registers_mask) {
564 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
565 for (uint16_t i = 0; i < number_of_dex_registers; ++i) {
566 region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i));
567 }
568 }
569
570 bool IsDexRegisterLive(uint16_t dex_register_number) const {
571 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
572 return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number);
573 }
574
575 size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const {
576 size_t number_of_live_dex_registers = 0;
577 for (size_t i = 0; i < number_of_dex_registers; ++i) {
578 if (IsDexRegisterLive(i)) {
579 ++number_of_live_dex_registers;
580 }
581 }
582 return number_of_live_dex_registers;
583 }
584
585 static size_t GetLiveBitMaskOffset() {
586 return kFixedSize;
587 }
588
589 // Compute the size of the live register bit mask (in bytes), for a
590 // method having `number_of_dex_registers` Dex registers.
591 static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) {
592 return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte;
593 }
594
595 static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) {
596 return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers);
597 }
598
599 size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers,
600 size_t number_of_location_catalog_entries) const {
601 size_t location_mapping_data_size_in_bits =
602 GetNumberOfLiveDexRegisters(number_of_dex_registers)
603 * SingleEntrySizeInBits(number_of_location_catalog_entries);
604 return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
605 }
606
607 // Return the size of a map entry in bits. Note that if
608 // `number_of_location_catalog_entries` equals 1, this function returns 0,
609 // which is fine, as there is no need to allocate a map for a
610 // single-entry location catalog; the only valid location catalog entry index
611 // for a live register in this case is 0 and there is no need to
612 // store it.
613 static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) {
614 // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2.
615 return number_of_location_catalog_entries == 0
616 ? 0u
617 : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries));
618 }
619
620 // Return the size of the DexRegisterMap object, in bytes.
621 size_t Size() const {
622 return region_.size();
623 }
624
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100625 void Dump(VariableIndentationOutputStream* vios,
626 const CodeInfo& code_info, uint16_t number_of_dex_registers) const;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100627
Roland Levillaina552e1c2015-03-26 15:01:03 +0000628 private:
629 // Return the index in the Dex register map corresponding to the Dex
630 // register number `dex_register_number`.
631 size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const {
632 if (!IsDexRegisterLive(dex_register_number)) {
633 return kInvalidIndexInDexRegisterMap;
634 }
635 return GetNumberOfLiveDexRegisters(dex_register_number);
636 }
637
638 // Special (invalid) Dex register map entry index meaning that there
639 // is no index in the map for a given Dex register (i.e., it must
640 // have been mapped to a DexRegisterLocation::Kind::kNone location).
641 static constexpr size_t kInvalidIndexInDexRegisterMap = -1;
642
643 static constexpr int kFixedSize = 0;
644
645 MemoryRegion region_;
646
647 friend class CodeInfo;
648 friend class StackMapStream;
649};
650
David Srbecky09ed0982016-02-12 21:58:43 +0000651// Represents bit range of bit-packed integer field.
652// We reuse the idea from ULEB128p1 to support encoding of -1 (aka 0xFFFFFFFF).
653// If min_value is set to -1, we implicitly subtract one from any loaded value,
654// and add one to any stored value. This is generalized to any negative values.
655// In other words, min_value acts as a base and the stored value is added to it.
656struct FieldEncoding {
657 FieldEncoding(size_t start_offset, size_t end_offset, int32_t min_value = 0)
658 : start_offset_(start_offset), end_offset_(end_offset), min_value_(min_value) {
659 DCHECK_LE(start_offset_, end_offset_);
660 DCHECK_LE(BitSize(), 32u);
661 }
662
663 ALWAYS_INLINE size_t BitSize() const { return end_offset_ - start_offset_; }
664
665 ALWAYS_INLINE int32_t Load(const MemoryRegion& region) const {
666 DCHECK_LE(end_offset_, region.size_in_bits());
667 const size_t bit_count = BitSize();
668 if (bit_count == 0) {
669 // Do not touch any memory if the range is empty.
670 return min_value_;
671 }
672 uint8_t* address = region.start() + start_offset_ / kBitsPerByte;
673 const uint32_t shift = start_offset_ & (kBitsPerByte - 1);
674 // Load the value (reading only the strictly needed bytes).
675 const uint32_t load_bit_count = shift + bit_count;
676 uint32_t value = *address++ >> shift;
677 if (load_bit_count > 8) {
678 value |= static_cast<uint32_t>(*address++) << (8 - shift);
679 if (load_bit_count > 16) {
680 value |= static_cast<uint32_t>(*address++) << (16 - shift);
681 if (load_bit_count > 24) {
682 value |= static_cast<uint32_t>(*address++) << (24 - shift);
683 if (load_bit_count > 32) {
684 value |= static_cast<uint32_t>(*address++) << (32 - shift);
685 }
686 }
687 }
688 }
689 // Clear unwanted most significant bits.
690 uint32_t clear_bit_count = 32 - bit_count;
691 value = (value << clear_bit_count) >> clear_bit_count;
692 return value + min_value_;
693 }
694
695 ALWAYS_INLINE void Store(MemoryRegion region, int32_t value) const {
696 region.StoreBits(start_offset_, value - min_value_, BitSize());
697 DCHECK_EQ(Load(region), value);
698 }
699
700 private:
701 size_t start_offset_;
702 size_t end_offset_;
703 int32_t min_value_;
704};
705
David Brazdilf677ebf2015-05-29 16:29:43 +0100706class StackMapEncoding {
707 public:
708 StackMapEncoding() {}
709
David Srbecky09ed0982016-02-12 21:58:43 +0000710 // Set stack map bit layout based on given sizes.
711 // Returns the size of stack map in bytes.
712 size_t SetFromSizes(size_t native_pc_max,
713 size_t dex_pc_max,
714 size_t dex_register_map_size,
715 size_t inline_info_size,
716 size_t register_mask_max,
717 size_t stack_mask_bit_size) {
718 size_t bit_offset = 0;
719 DCHECK_EQ(kNativePcBitOffset, bit_offset);
720 bit_offset += MinimumBitsToStore(native_pc_max);
David Brazdilf677ebf2015-05-29 16:29:43 +0100721
David Srbecky09ed0982016-02-12 21:58:43 +0000722 dex_pc_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
723 bit_offset += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max);
724
725 // We also need +1 for kNoDexRegisterMap, but since the size is strictly
726 // greater than any offset we might try to encode, we already implicitly have it.
727 dex_register_map_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
728 bit_offset += MinimumBitsToStore(dex_register_map_size);
729
730 // We also need +1 for kNoInlineInfo, but since the inline_info_size is strictly
731 // greater than the offset we might try to encode, we already implicitly have it.
732 // If inline_info_size is zero, we can encode only kNoInlineInfo (in zero bits).
733 inline_info_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
734 if (inline_info_size != 0) {
735 bit_offset += MinimumBitsToStore(dex_register_map_size + inline_info_size);
736 }
737
738 register_mask_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
739 bit_offset += MinimumBitsToStore(register_mask_max);
740
741 stack_mask_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset);
742 bit_offset += stack_mask_bit_size;
743
744 return RoundUp(bit_offset, kBitsPerByte) / kBitsPerByte;
David Brazdilf677ebf2015-05-29 16:29:43 +0100745 }
746
David Srbecky09ed0982016-02-12 21:58:43 +0000747 ALWAYS_INLINE FieldEncoding GetNativePcEncoding() const {
748 return FieldEncoding(kNativePcBitOffset, dex_pc_bit_offset_);
749 }
750 ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const {
751 return FieldEncoding(dex_pc_bit_offset_, dex_register_map_bit_offset_, -1 /* min_value */);
752 }
753 ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const {
754 return FieldEncoding(dex_register_map_bit_offset_, inline_info_bit_offset_, -1 /* min_value */);
755 }
756 ALWAYS_INLINE FieldEncoding GetInlineInfoEncoding() const {
757 return FieldEncoding(inline_info_bit_offset_, register_mask_bit_offset_, -1 /* min_value */);
758 }
759 ALWAYS_INLINE FieldEncoding GetRegisterMaskEncoding() const {
760 return FieldEncoding(register_mask_bit_offset_, stack_mask_bit_offset_);
761 }
762 ALWAYS_INLINE size_t GetStackMaskBitOffset() const {
763 // The end offset is not encoded. It is implicitly the end of stack map entry.
764 return stack_mask_bit_offset_;
David Brazdilf677ebf2015-05-29 16:29:43 +0100765 }
766
David Srbecky09ed0982016-02-12 21:58:43 +0000767 void Dump(VariableIndentationOutputStream* vios) const;
David Brazdilf677ebf2015-05-29 16:29:43 +0100768
769 private:
David Srbecky09ed0982016-02-12 21:58:43 +0000770 static constexpr size_t kNativePcBitOffset = 0;
771 uint8_t dex_pc_bit_offset_;
772 uint8_t dex_register_map_bit_offset_;
773 uint8_t inline_info_bit_offset_;
774 uint8_t register_mask_bit_offset_;
775 uint8_t stack_mask_bit_offset_;
David Brazdilf677ebf2015-05-29 16:29:43 +0100776};
777
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100778/**
779 * A Stack Map holds compilation information for a specific PC necessary for:
780 * - Mapping it to a dex PC,
781 * - Knowing which stack entries are objects,
782 * - Knowing which registers hold objects,
783 * - Knowing the inlining information,
784 * - Knowing the values of dex registers.
785 *
786 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100787 *
David Srbecky09ed0982016-02-12 21:58:43 +0000788 * [native_pc_offset, dex_pc, dex_register_map_offset, inlining_info_offset, register_mask,
Roland Levillain1c1da432015-07-16 11:54:44 +0100789 * stack_mask].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100790 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100791class StackMap {
792 public:
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100793 StackMap() {}
David Brazdilf677ebf2015-05-29 16:29:43 +0100794 explicit StackMap(MemoryRegion region) : region_(region) {}
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100795
David Srbecky09ed0982016-02-12 21:58:43 +0000796 ALWAYS_INLINE bool IsValid() const { return region_.pointer() != nullptr; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100797
David Srbecky09ed0982016-02-12 21:58:43 +0000798 ALWAYS_INLINE uint32_t GetDexPc(const StackMapEncoding& encoding) const {
799 return encoding.GetDexPcEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100800 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100801
David Srbecky09ed0982016-02-12 21:58:43 +0000802 ALWAYS_INLINE void SetDexPc(const StackMapEncoding& encoding, uint32_t dex_pc) {
803 encoding.GetDexPcEncoding().Store(region_, dex_pc);
David Brazdilf677ebf2015-05-29 16:29:43 +0100804 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100805
David Srbecky09ed0982016-02-12 21:58:43 +0000806 ALWAYS_INLINE uint32_t GetNativePcOffset(const StackMapEncoding& encoding) const {
807 return encoding.GetNativePcEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100808 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100809
David Srbecky09ed0982016-02-12 21:58:43 +0000810 ALWAYS_INLINE void SetNativePcOffset(const StackMapEncoding& encoding, uint32_t native_pc_offset) {
811 encoding.GetNativePcEncoding().Store(region_, native_pc_offset);
David Brazdilf677ebf2015-05-29 16:29:43 +0100812 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100813
David Srbecky09ed0982016-02-12 21:58:43 +0000814 ALWAYS_INLINE uint32_t GetDexRegisterMapOffset(const StackMapEncoding& encoding) const {
815 return encoding.GetDexRegisterMapEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100816 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100817
David Srbecky09ed0982016-02-12 21:58:43 +0000818 ALWAYS_INLINE void SetDexRegisterMapOffset(const StackMapEncoding& encoding, uint32_t offset) {
819 encoding.GetDexRegisterMapEncoding().Store(region_, offset);
David Brazdilf677ebf2015-05-29 16:29:43 +0100820 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100821
David Srbecky09ed0982016-02-12 21:58:43 +0000822 ALWAYS_INLINE uint32_t GetInlineDescriptorOffset(const StackMapEncoding& encoding) const {
823 return encoding.GetInlineInfoEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100824 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100825
David Srbecky09ed0982016-02-12 21:58:43 +0000826 ALWAYS_INLINE void SetInlineDescriptorOffset(const StackMapEncoding& encoding, uint32_t offset) {
827 encoding.GetInlineInfoEncoding().Store(region_, offset);
David Brazdilf677ebf2015-05-29 16:29:43 +0100828 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100829
David Srbecky09ed0982016-02-12 21:58:43 +0000830 ALWAYS_INLINE uint32_t GetRegisterMask(const StackMapEncoding& encoding) const {
831 return encoding.GetRegisterMaskEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100832 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100833
David Srbecky09ed0982016-02-12 21:58:43 +0000834 ALWAYS_INLINE void SetRegisterMask(const StackMapEncoding& encoding, uint32_t mask) {
835 encoding.GetRegisterMaskEncoding().Store(region_, mask);
David Brazdilf677ebf2015-05-29 16:29:43 +0100836 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100837
David Srbecky09ed0982016-02-12 21:58:43 +0000838 ALWAYS_INLINE size_t GetNumberOfStackMaskBits(const StackMapEncoding& encoding) const {
839 return region_.size_in_bits() - encoding.GetStackMaskBitOffset();
David Brazdilf677ebf2015-05-29 16:29:43 +0100840 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100841
David Srbecky09ed0982016-02-12 21:58:43 +0000842 ALWAYS_INLINE bool GetStackMaskBit(const StackMapEncoding& encoding, size_t index) const {
843 return region_.LoadBit(encoding.GetStackMaskBitOffset() + index);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100844 }
845
David Srbecky09ed0982016-02-12 21:58:43 +0000846 ALWAYS_INLINE void SetStackMaskBit(const StackMapEncoding& encoding, size_t index, bool value) {
847 region_.StoreBit(encoding.GetStackMaskBitOffset() + index, value);
848 }
849
850 ALWAYS_INLINE bool HasDexRegisterMap(const StackMapEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +0100851 return GetDexRegisterMapOffset(encoding) != kNoDexRegisterMap;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100852 }
853
David Srbecky09ed0982016-02-12 21:58:43 +0000854 ALWAYS_INLINE bool HasInlineInfo(const StackMapEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +0100855 return GetInlineDescriptorOffset(encoding) != kNoInlineInfo;
Roland Levillain442b46a2015-02-18 16:54:21 +0000856 }
857
David Srbecky09ed0982016-02-12 21:58:43 +0000858 ALWAYS_INLINE bool Equals(const StackMap& other) const {
859 return region_.pointer() == other.region_.pointer() && region_.size() == other.region_.size();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100860 }
861
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100862 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100863 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000864 const CodeInfoEncoding& encoding,
Roland Levillainf2650d12015-05-28 14:53:28 +0100865 uint32_t code_offset,
866 uint16_t number_of_dex_registers,
867 const std::string& header_suffix = "") const;
868
Roland Levillain442b46a2015-02-18 16:54:21 +0000869 // Special (invalid) offset for the DexRegisterMapOffset field meaning
870 // that there is no Dex register map for this stack map.
871 static constexpr uint32_t kNoDexRegisterMap = -1;
872
873 // Special (invalid) offset for the InlineDescriptorOffset field meaning
874 // that there is no inline info for this stack map.
875 static constexpr uint32_t kNoInlineInfo = -1;
876
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100877 private:
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100878 static constexpr int kFixedSize = 0;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100879
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100880 MemoryRegion region_;
881
Nicolas Geoffray39468442014-09-02 15:17:15 +0100882 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100883};
884
David Srbecky61b28a12016-02-25 21:55:03 +0000885class InlineInfoEncoding {
886 public:
887 void SetFromSizes(size_t method_index_max,
888 size_t dex_pc_max,
889 size_t invoke_type_max,
890 size_t dex_register_map_size) {
891 total_bit_size_ = kMethodIndexBitOffset;
892 total_bit_size_ += MinimumBitsToStore(method_index_max);
893
894 dex_pc_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
895 total_bit_size_ += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max);
896
897 invoke_type_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
898 total_bit_size_ += MinimumBitsToStore(invoke_type_max);
899
900 // We also need +1 for kNoDexRegisterMap, but since the size is strictly
901 // greater than any offset we might try to encode, we already implicitly have it.
902 dex_register_map_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
903 total_bit_size_ += MinimumBitsToStore(dex_register_map_size);
904 }
905
906 ALWAYS_INLINE FieldEncoding GetMethodIndexEncoding() const {
907 return FieldEncoding(kMethodIndexBitOffset, dex_pc_bit_offset_);
908 }
909 ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const {
910 return FieldEncoding(dex_pc_bit_offset_, invoke_type_bit_offset_, -1 /* min_value */);
911 }
912 ALWAYS_INLINE FieldEncoding GetInvokeTypeEncoding() const {
913 return FieldEncoding(invoke_type_bit_offset_, dex_register_map_bit_offset_);
914 }
915 ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const {
916 return FieldEncoding(dex_register_map_bit_offset_, total_bit_size_, -1 /* min_value */);
917 }
918 ALWAYS_INLINE size_t GetEntrySize() const {
919 return RoundUp(total_bit_size_, kBitsPerByte) / kBitsPerByte;
920 }
921
922 void Dump(VariableIndentationOutputStream* vios) const;
923
924 private:
925 static constexpr uint8_t kIsLastBitOffset = 0;
926 static constexpr uint8_t kMethodIndexBitOffset = 1;
927 uint8_t dex_pc_bit_offset_;
928 uint8_t invoke_type_bit_offset_;
929 uint8_t dex_register_map_bit_offset_;
930 uint8_t total_bit_size_;
931};
932
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100933/**
934 * Inline information for a specific PC. The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100935 *
David Srbecky61b28a12016-02-25 21:55:03 +0000936 * [is_last, method_index, dex_pc, invoke_type, dex_register_map_offset]+.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100937 */
938class InlineInfo {
939 public:
David Srbecky61b28a12016-02-25 21:55:03 +0000940 explicit InlineInfo(MemoryRegion region) : region_(region) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100941 }
942
David Srbecky61b28a12016-02-25 21:55:03 +0000943 ALWAYS_INLINE uint32_t GetDepth(const InlineInfoEncoding& encoding) const {
944 size_t depth = 0;
945 while (!GetRegionAtDepth(encoding, depth++).LoadBit(0)) { } // Check is_last bit.
946 return depth;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100947 }
948
David Srbecky61b28a12016-02-25 21:55:03 +0000949 ALWAYS_INLINE void SetDepth(const InlineInfoEncoding& encoding, uint32_t depth) {
950 DCHECK_GT(depth, 0u);
951 for (size_t d = 0; d < depth; ++d) {
952 GetRegionAtDepth(encoding, d).StoreBit(0, d == depth - 1); // Set is_last bit.
953 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100954 }
955
David Srbecky61b28a12016-02-25 21:55:03 +0000956 ALWAYS_INLINE uint32_t GetMethodIndexAtDepth(const InlineInfoEncoding& encoding,
957 uint32_t depth) const {
958 return encoding.GetMethodIndexEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100959 }
960
David Srbecky61b28a12016-02-25 21:55:03 +0000961 ALWAYS_INLINE void SetMethodIndexAtDepth(const InlineInfoEncoding& encoding,
962 uint32_t depth,
963 uint32_t index) {
964 encoding.GetMethodIndexEncoding().Store(GetRegionAtDepth(encoding, depth), index);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100965 }
966
David Srbecky61b28a12016-02-25 21:55:03 +0000967 ALWAYS_INLINE uint32_t GetDexPcAtDepth(const InlineInfoEncoding& encoding,
968 uint32_t depth) const {
969 return encoding.GetDexPcEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100970 }
971
David Srbecky61b28a12016-02-25 21:55:03 +0000972 ALWAYS_INLINE void SetDexPcAtDepth(const InlineInfoEncoding& encoding,
973 uint32_t depth,
974 uint32_t dex_pc) {
975 encoding.GetDexPcEncoding().Store(GetRegionAtDepth(encoding, depth), dex_pc);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100976 }
977
David Srbecky61b28a12016-02-25 21:55:03 +0000978 ALWAYS_INLINE uint32_t GetInvokeTypeAtDepth(const InlineInfoEncoding& encoding,
979 uint32_t depth) const {
980 return encoding.GetInvokeTypeEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100981 }
982
David Srbecky61b28a12016-02-25 21:55:03 +0000983 ALWAYS_INLINE void SetInvokeTypeAtDepth(const InlineInfoEncoding& encoding,
984 uint32_t depth,
985 uint32_t invoke_type) {
986 encoding.GetInvokeTypeEncoding().Store(GetRegionAtDepth(encoding, depth), invoke_type);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100987 }
988
David Srbecky61b28a12016-02-25 21:55:03 +0000989 ALWAYS_INLINE uint32_t GetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding,
990 uint32_t depth) const {
991 return encoding.GetDexRegisterMapEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100992 }
993
David Srbecky61b28a12016-02-25 21:55:03 +0000994 ALWAYS_INLINE void SetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding,
995 uint32_t depth,
996 uint32_t offset) {
997 encoding.GetDexRegisterMapEncoding().Store(GetRegionAtDepth(encoding, depth), offset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100998 }
999
David Srbecky61b28a12016-02-25 21:55:03 +00001000 ALWAYS_INLINE bool HasDexRegisterMapAtDepth(const InlineInfoEncoding& encoding,
1001 uint32_t depth) const {
1002 return GetDexRegisterMapOffsetAtDepth(encoding, depth) != StackMap::kNoDexRegisterMap;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001003 }
1004
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001005 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +00001006 const CodeInfo& info,
1007 uint16_t* number_of_dex_registers) const;
Roland Levillain1c1da432015-07-16 11:54:44 +01001008
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001009 private:
David Srbecky61b28a12016-02-25 21:55:03 +00001010 ALWAYS_INLINE MemoryRegion GetRegionAtDepth(const InlineInfoEncoding& encoding,
1011 uint32_t depth) const {
1012 size_t entry_size = encoding.GetEntrySize();
1013 DCHECK_GT(entry_size, 0u);
1014 return region_.Subregion(depth * entry_size, entry_size);
1015 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001016
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001017 MemoryRegion region_;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001018};
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001019
David Srbecky09ed0982016-02-12 21:58:43 +00001020// Most of the fields are encoded as ULEB128 to save space.
1021struct CodeInfoEncoding {
1022 uint32_t non_header_size;
1023 uint32_t number_of_stack_maps;
1024 uint32_t stack_map_size_in_bytes;
1025 uint32_t number_of_location_catalog_entries;
1026 StackMapEncoding stack_map_encoding;
David Srbecky61b28a12016-02-25 21:55:03 +00001027 InlineInfoEncoding inline_info_encoding;
David Srbecky09ed0982016-02-12 21:58:43 +00001028 uint8_t header_size;
1029
1030 CodeInfoEncoding() { }
1031
1032 explicit CodeInfoEncoding(const void* data) {
1033 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
1034 non_header_size = DecodeUnsignedLeb128(&ptr);
1035 number_of_stack_maps = DecodeUnsignedLeb128(&ptr);
1036 stack_map_size_in_bytes = DecodeUnsignedLeb128(&ptr);
1037 number_of_location_catalog_entries = DecodeUnsignedLeb128(&ptr);
David Srbecky61b28a12016-02-25 21:55:03 +00001038 static_assert(alignof(StackMapEncoding) == 1,
1039 "StackMapEncoding should not require alignment");
David Srbecky09ed0982016-02-12 21:58:43 +00001040 stack_map_encoding = *reinterpret_cast<const StackMapEncoding*>(ptr);
1041 ptr += sizeof(StackMapEncoding);
David Srbecky61b28a12016-02-25 21:55:03 +00001042 if (stack_map_encoding.GetInlineInfoEncoding().BitSize() > 0) {
1043 static_assert(alignof(InlineInfoEncoding) == 1,
1044 "InlineInfoEncoding should not require alignment");
1045 inline_info_encoding = *reinterpret_cast<const InlineInfoEncoding*>(ptr);
1046 ptr += sizeof(InlineInfoEncoding);
1047 } else {
1048 inline_info_encoding = InlineInfoEncoding{}; // NOLINT.
1049 }
David Srbecky09ed0982016-02-12 21:58:43 +00001050 header_size = dchecked_integral_cast<uint8_t>(ptr - reinterpret_cast<const uint8_t*>(data));
1051 }
1052
1053 template<typename Vector>
1054 void Compress(Vector* dest) const {
1055 EncodeUnsignedLeb128(dest, non_header_size);
1056 EncodeUnsignedLeb128(dest, number_of_stack_maps);
1057 EncodeUnsignedLeb128(dest, stack_map_size_in_bytes);
1058 EncodeUnsignedLeb128(dest, number_of_location_catalog_entries);
David Srbecky61b28a12016-02-25 21:55:03 +00001059 const uint8_t* stack_map_ptr = reinterpret_cast<const uint8_t*>(&stack_map_encoding);
1060 dest->insert(dest->end(), stack_map_ptr, stack_map_ptr + sizeof(StackMapEncoding));
1061 if (stack_map_encoding.GetInlineInfoEncoding().BitSize() > 0) {
1062 const uint8_t* inline_info_ptr = reinterpret_cast<const uint8_t*>(&inline_info_encoding);
1063 dest->insert(dest->end(), inline_info_ptr, inline_info_ptr + sizeof(InlineInfoEncoding));
1064 }
David Srbecky09ed0982016-02-12 21:58:43 +00001065 }
1066};
1067
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001068/**
1069 * Wrapper around all compiler information collected for a method.
1070 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +01001071 *
David Srbecky09ed0982016-02-12 21:58:43 +00001072 * [CodeInfoEncoding, StackMap+, DexRegisterLocationCatalog+, DexRegisterMap+, InlineInfo*]
Roland Levillain1c1da432015-07-16 11:54:44 +01001073 *
David Srbecky09ed0982016-02-12 21:58:43 +00001074 * where CodeInfoEncoding is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +01001075 *
David Srbecky09ed0982016-02-12 21:58:43 +00001076 * [non_header_size, number_of_stack_maps, stack_map_size_in_bytes,
1077 * number_of_location_catalog_entries, StackMapEncoding]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001078 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001079class CodeInfo {
1080 public:
David Srbecky09ed0982016-02-12 21:58:43 +00001081 explicit CodeInfo(MemoryRegion region) : region_(region) {
1082 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001083
Nicolas Geoffray39468442014-09-02 15:17:15 +01001084 explicit CodeInfo(const void* data) {
David Srbecky09ed0982016-02-12 21:58:43 +00001085 CodeInfoEncoding encoding = CodeInfoEncoding(data);
1086 region_ = MemoryRegion(const_cast<void*>(data),
1087 encoding.header_size + encoding.non_header_size);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001088 }
1089
David Srbecky09ed0982016-02-12 21:58:43 +00001090 CodeInfoEncoding ExtractEncoding() const {
1091 return CodeInfoEncoding(region_.start());
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001092 }
1093
David Srbecky09ed0982016-02-12 21:58:43 +00001094 bool HasInlineInfo(const CodeInfoEncoding& encoding) const {
1095 return encoding.stack_map_encoding.GetInlineInfoEncoding().BitSize() > 0;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001096 }
1097
David Srbecky09ed0982016-02-12 21:58:43 +00001098 DexRegisterLocationCatalog GetDexRegisterLocationCatalog(const CodeInfoEncoding& encoding) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +00001099 return DexRegisterLocationCatalog(region_.Subregion(
David Brazdilf677ebf2015-05-29 16:29:43 +01001100 GetDexRegisterLocationCatalogOffset(encoding),
1101 GetDexRegisterLocationCatalogSize(encoding)));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001102 }
1103
David Srbecky09ed0982016-02-12 21:58:43 +00001104 StackMap GetStackMapAt(size_t i, const CodeInfoEncoding& encoding) const {
1105 size_t stack_map_size = encoding.stack_map_size_in_bytes;
David Brazdilf677ebf2015-05-29 16:29:43 +01001106 return StackMap(GetStackMaps(encoding).Subregion(i * stack_map_size, stack_map_size));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001107 }
1108
David Srbecky09ed0982016-02-12 21:58:43 +00001109 uint32_t GetNumberOfLocationCatalogEntries(const CodeInfoEncoding& encoding) const {
1110 return encoding.number_of_location_catalog_entries;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001111 }
1112
David Srbecky09ed0982016-02-12 21:58:43 +00001113 uint32_t GetDexRegisterLocationCatalogSize(const CodeInfoEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +01001114 return ComputeDexRegisterLocationCatalogSize(GetDexRegisterLocationCatalogOffset(encoding),
David Srbecky09ed0982016-02-12 21:58:43 +00001115 GetNumberOfLocationCatalogEntries(encoding));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001116 }
1117
David Srbecky09ed0982016-02-12 21:58:43 +00001118 uint32_t GetNumberOfStackMaps(const CodeInfoEncoding& encoding) const {
1119 return encoding.number_of_stack_maps;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001120 }
1121
David Brazdil77a48ae2015-09-15 12:34:04 +00001122 // Get the size of all the stack maps of this CodeInfo object, in bytes.
David Srbecky09ed0982016-02-12 21:58:43 +00001123 size_t GetStackMapsSize(const CodeInfoEncoding& encoding) const {
1124 return encoding.stack_map_size_in_bytes * GetNumberOfStackMaps(encoding);
Roland Levillain29ba1b02015-03-13 11:45:07 +00001125 }
1126
David Srbecky09ed0982016-02-12 21:58:43 +00001127 uint32_t GetDexRegisterLocationCatalogOffset(const CodeInfoEncoding& encoding) const {
1128 return GetStackMapsOffset(encoding) + GetStackMapsSize(encoding);
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001129 }
1130
David Srbecky09ed0982016-02-12 21:58:43 +00001131 size_t GetDexRegisterMapsOffset(const CodeInfoEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +01001132 return GetDexRegisterLocationCatalogOffset(encoding)
1133 + GetDexRegisterLocationCatalogSize(encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001134 }
1135
David Srbecky09ed0982016-02-12 21:58:43 +00001136 uint32_t GetStackMapsOffset(const CodeInfoEncoding& encoding) const {
1137 return encoding.header_size;
Nicolas Geoffray6530baf2015-05-26 15:22:58 +01001138 }
1139
David Brazdilf677ebf2015-05-29 16:29:43 +01001140 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map,
David Srbecky09ed0982016-02-12 21:58:43 +00001141 const CodeInfoEncoding& encoding,
David Brazdilf677ebf2015-05-29 16:29:43 +01001142 uint32_t number_of_dex_registers) const {
David Srbecky09ed0982016-02-12 21:58:43 +00001143 if (!stack_map.HasDexRegisterMap(encoding.stack_map_encoding)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001144 return DexRegisterMap();
1145 } else {
1146 uint32_t offset = GetDexRegisterMapsOffset(encoding)
David Srbecky09ed0982016-02-12 21:58:43 +00001147 + stack_map.GetDexRegisterMapOffset(encoding.stack_map_encoding);
1148 size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001149 return DexRegisterMap(region_.Subregion(offset, size));
1150 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001151 }
1152
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001153 // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`.
1154 DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
1155 InlineInfo inline_info,
David Srbecky09ed0982016-02-12 21:58:43 +00001156 const CodeInfoEncoding& encoding,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001157 uint32_t number_of_dex_registers) const {
David Srbecky61b28a12016-02-25 21:55:03 +00001158 if (!inline_info.HasDexRegisterMapAtDepth(encoding.inline_info_encoding, depth)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001159 return DexRegisterMap();
1160 } else {
David Srbecky61b28a12016-02-25 21:55:03 +00001161 uint32_t offset = GetDexRegisterMapsOffset(encoding) +
1162 inline_info.GetDexRegisterMapOffsetAtDepth(encoding.inline_info_encoding, depth);
David Srbecky09ed0982016-02-12 21:58:43 +00001163 size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001164 return DexRegisterMap(region_.Subregion(offset, size));
1165 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001166 }
1167
David Srbecky09ed0982016-02-12 21:58:43 +00001168 InlineInfo GetInlineInfoOf(StackMap stack_map, const CodeInfoEncoding& encoding) const {
1169 DCHECK(stack_map.HasInlineInfo(encoding.stack_map_encoding));
1170 uint32_t offset = stack_map.GetInlineDescriptorOffset(encoding.stack_map_encoding)
David Brazdilf677ebf2015-05-29 16:29:43 +01001171 + GetDexRegisterMapsOffset(encoding);
David Srbecky61b28a12016-02-25 21:55:03 +00001172 return InlineInfo(region_.Subregion(offset, region_.size() - offset));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001173 }
1174
David Srbecky09ed0982016-02-12 21:58:43 +00001175 StackMap GetStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1176 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +01001177 StackMap stack_map = GetStackMapAt(i, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001178 if (stack_map.GetDexPc(encoding.stack_map_encoding) == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001179 return stack_map;
1180 }
1181 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001182 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001183 }
1184
David Brazdil77a48ae2015-09-15 12:34:04 +00001185 // Searches the stack map list backwards because catch stack maps are stored
1186 // at the end.
David Srbecky09ed0982016-02-12 21:58:43 +00001187 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1188 for (size_t i = GetNumberOfStackMaps(encoding); i > 0; --i) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001189 StackMap stack_map = GetStackMapAt(i - 1, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001190 if (stack_map.GetDexPc(encoding.stack_map_encoding) == dex_pc) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001191 return stack_map;
1192 }
1193 }
1194 return StackMap();
1195 }
1196
David Srbecky09ed0982016-02-12 21:58:43 +00001197 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1198 size_t e = GetNumberOfStackMaps(encoding);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001199 if (e == 0) {
1200 // There cannot be OSR stack map if there is no stack map.
1201 return StackMap();
1202 }
1203 // Walk over all stack maps. If two consecutive stack maps are identical, then we
1204 // have found a stack map suitable for OSR.
David Srbecky09ed0982016-02-12 21:58:43 +00001205 const StackMapEncoding& stack_map_encoding = encoding.stack_map_encoding;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001206 for (size_t i = 0; i < e - 1; ++i) {
1207 StackMap stack_map = GetStackMapAt(i, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001208 if (stack_map.GetDexPc(stack_map_encoding) == dex_pc) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001209 StackMap other = GetStackMapAt(i + 1, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001210 if (other.GetDexPc(stack_map_encoding) == dex_pc &&
1211 other.GetNativePcOffset(stack_map_encoding) ==
1212 stack_map.GetNativePcOffset(stack_map_encoding)) {
1213 DCHECK_EQ(other.GetDexRegisterMapOffset(stack_map_encoding),
1214 stack_map.GetDexRegisterMapOffset(stack_map_encoding));
1215 DCHECK(!stack_map.HasInlineInfo(stack_map_encoding));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001216 if (i < e - 2) {
1217 // Make sure there are not three identical stack maps following each other.
David Srbecky09ed0982016-02-12 21:58:43 +00001218 DCHECK_NE(stack_map.GetNativePcOffset(stack_map_encoding),
1219 GetStackMapAt(i + 2, encoding).GetNativePcOffset(stack_map_encoding));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001220 }
1221 return stack_map;
1222 }
1223 }
1224 }
1225 return StackMap();
1226 }
1227
David Brazdilf677ebf2015-05-29 16:29:43 +01001228 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset,
David Srbecky09ed0982016-02-12 21:58:43 +00001229 const CodeInfoEncoding& encoding) const {
David Brazdil77a48ae2015-09-15 12:34:04 +00001230 // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack
1231 // maps are not. If we knew that the method does not have try/catch,
1232 // we could do binary search.
David Srbecky09ed0982016-02-12 21:58:43 +00001233 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +01001234 StackMap stack_map = GetStackMapAt(i, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001235 if (stack_map.GetNativePcOffset(encoding.stack_map_encoding) == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001236 return stack_map;
1237 }
1238 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001239 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001240 }
1241
Roland Levillainf2650d12015-05-28 14:53:28 +01001242 // Dump this CodeInfo object on `os`. `code_offset` is the (absolute)
1243 // native PC of the compiled method and `number_of_dex_registers` the
1244 // number of Dex virtual registers used in this method. If
1245 // `dump_stack_maps` is true, also dump the stack maps and the
1246 // associated Dex register maps.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001247 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001248 uint32_t code_offset,
1249 uint16_t number_of_dex_registers,
1250 bool dump_stack_maps) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001251
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001252 private:
David Srbecky09ed0982016-02-12 21:58:43 +00001253 MemoryRegion GetStackMaps(const CodeInfoEncoding& encoding) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001254 return region_.size() == 0
1255 ? MemoryRegion()
David Srbecky09ed0982016-02-12 21:58:43 +00001256 : region_.Subregion(GetStackMapsOffset(encoding), GetStackMapsSize(encoding));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001257 }
1258
Roland Levillaina552e1c2015-03-26 15:01:03 +00001259 // Compute the size of the Dex register map associated to the stack map at
1260 // `dex_register_map_offset_in_code_info`.
David Srbecky09ed0982016-02-12 21:58:43 +00001261 size_t ComputeDexRegisterMapSizeOf(const CodeInfoEncoding& encoding,
1262 uint32_t dex_register_map_offset_in_code_info,
Roland Levillaina552e1c2015-03-26 15:01:03 +00001263 uint16_t number_of_dex_registers) const {
1264 // Offset where the actual mapping data starts within art::DexRegisterMap.
1265 size_t location_mapping_data_offset_in_dex_register_map =
1266 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1267 // Create a temporary art::DexRegisterMap to be able to call
1268 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1269 DexRegisterMap dex_register_map_without_locations(
1270 MemoryRegion(region_.Subregion(dex_register_map_offset_in_code_info,
1271 location_mapping_data_offset_in_dex_register_map)));
1272 size_t number_of_live_dex_registers =
1273 dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
1274 size_t location_mapping_data_size_in_bits =
David Srbecky09ed0982016-02-12 21:58:43 +00001275 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries(encoding))
Roland Levillaina552e1c2015-03-26 15:01:03 +00001276 * number_of_live_dex_registers;
1277 size_t location_mapping_data_size_in_bytes =
1278 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1279 size_t dex_register_map_size =
1280 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1281 return dex_register_map_size;
1282 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001283
Roland Levillaina552e1c2015-03-26 15:01:03 +00001284 // Compute the size of a Dex register location catalog starting at offset `origin`
1285 // in `region_` and containing `number_of_dex_locations` entries.
1286 size_t ComputeDexRegisterLocationCatalogSize(uint32_t origin,
1287 uint32_t number_of_dex_locations) const {
1288 // TODO: Ideally, we would like to use art::DexRegisterLocationCatalog::Size or
1289 // art::DexRegisterLocationCatalog::FindLocationOffset, but the
1290 // DexRegisterLocationCatalog is not yet built. Try to factor common code.
1291 size_t offset = origin + DexRegisterLocationCatalog::kFixedSize;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001292
Roland Levillaina552e1c2015-03-26 15:01:03 +00001293 // Skip the first `number_of_dex_locations - 1` entries.
1294 for (uint16_t i = 0; i < number_of_dex_locations; ++i) {
1295 // Read the first next byte and inspect its first 3 bits to decide
1296 // whether it is a short or a large location.
1297 DexRegisterLocationCatalog::ShortLocation first_byte =
1298 region_.LoadUnaligned<DexRegisterLocationCatalog::ShortLocation>(offset);
1299 DexRegisterLocation::Kind kind =
1300 DexRegisterLocationCatalog::ExtractKindFromShortLocation(first_byte);
1301 if (DexRegisterLocation::IsShortLocationKind(kind)) {
1302 // Short location. Skip the current byte.
1303 offset += DexRegisterLocationCatalog::SingleShortEntrySize();
1304 } else {
1305 // Large location. Skip the 5 next bytes.
1306 offset += DexRegisterLocationCatalog::SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001307 }
1308 }
1309 size_t size = offset - origin;
1310 return size;
1311 }
1312
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001313 MemoryRegion region_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001314 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001315};
1316
Roland Levillain1c1da432015-07-16 11:54:44 +01001317#undef ELEMENT_BYTE_OFFSET_AFTER
1318#undef ELEMENT_BIT_OFFSET_AFTER
1319
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001320} // namespace art
1321
1322#endif // ART_RUNTIME_STACK_MAP_H_