blob: b425a465b45d8b335dbe845545a0b4d249ab2829 [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"
21#include "memory_region.h"
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +000022#include "utils.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010023
24namespace art {
25
Roland Levillaina2d8ec62015-03-12 15:25:29 +000026// Size of a frame slot, in bytes. This constant is a signed value,
27// to please the compiler in arithmetic operations involving int32_t
28// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000029static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000030
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000031// Size of Dex virtual registers.
Roland Levillaina552e1c2015-03-26 15:01:03 +000032static constexpr size_t kVRegSize = 4;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000033
Nicolas Geoffray004c2302015-03-20 10:06:38 +000034class CodeInfo;
35
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010036/**
37 * Classes in the following file are wrapper on stack map information backed
38 * by a MemoryRegion. As such they read and write to the region, they don't have
39 * their own fields.
40 */
41
Roland Levillaina2d8ec62015-03-12 15:25:29 +000042// Dex register location container used by DexRegisterMap and StackMapStream.
43class DexRegisterLocation {
44 public:
45 /*
46 * The location kind used to populate the Dex register information in a
47 * StackMapStream can either be:
48 * - kNone: the register has no location yet, meaning it has not been set;
49 * - kConstant: value holds the constant;
50 * - kStack: value holds the stack offset;
51 * - kRegister: value holds the physical register number;
52 * - kFpuRegister: value holds the physical register number.
53 *
54 * In addition, DexRegisterMap also uses these values:
55 * - kInStackLargeOffset: value holds a "large" stack offset (greater than
Roland Levillaina552e1c2015-03-26 15:01:03 +000056 * or equal to 128 bytes);
57 * - kConstantLargeValue: value holds a "large" constant (lower than 0, or
58 * or greater than or equal to 32).
Roland Levillaina2d8ec62015-03-12 15:25:29 +000059 */
60 enum class Kind : uint8_t {
61 // Short location kinds, for entries fitting on one byte (3 bits
62 // for the kind, 5 bits for the value) in a DexRegisterMap.
63 kNone = 0, // 0b000
64 kInStack = 1, // 0b001
65 kInRegister = 2, // 0b010
66 kInFpuRegister = 3, // 0b011
67 kConstant = 4, // 0b100
68
69 // Large location kinds, requiring a 5-byte encoding (1 byte for the
70 // kind, 4 bytes for the value).
71
72 // Stack location at a large offset, meaning that the offset value
73 // divided by the stack frame slot size (4 bytes) cannot fit on a
74 // 5-bit unsigned integer (i.e., this offset value is greater than
75 // or equal to 2^5 * 4 = 128 bytes).
76 kInStackLargeOffset = 5, // 0b101
77
78 // Large constant, that cannot fit on a 5-bit signed integer (i.e.,
Roland Levillaina552e1c2015-03-26 15:01:03 +000079 // lower than 0, or greater than or equal to 2^5 = 32).
Roland Levillaina2d8ec62015-03-12 15:25:29 +000080 kConstantLargeValue = 6, // 0b110
81
82 kLastLocationKind = kConstantLargeValue
83 };
84
85 static_assert(
86 sizeof(Kind) == 1u,
87 "art::DexRegisterLocation::Kind has a size different from one byte.");
88
89 static const char* PrettyDescriptor(Kind kind) {
90 switch (kind) {
91 case Kind::kNone:
92 return "none";
93 case Kind::kInStack:
94 return "in stack";
95 case Kind::kInRegister:
96 return "in register";
97 case Kind::kInFpuRegister:
98 return "in fpu register";
99 case Kind::kConstant:
100 return "as constant";
101 case Kind::kInStackLargeOffset:
102 return "in stack (large offset)";
103 case Kind::kConstantLargeValue:
104 return "as constant (large value)";
105 default:
106 UNREACHABLE();
107 }
108 }
109
110 static bool IsShortLocationKind(Kind kind) {
111 switch (kind) {
112 case Kind::kNone:
113 case Kind::kInStack:
114 case Kind::kInRegister:
115 case Kind::kInFpuRegister:
116 case Kind::kConstant:
117 return true;
118
119 case Kind::kInStackLargeOffset:
120 case Kind::kConstantLargeValue:
121 return false;
122
123 default:
124 UNREACHABLE();
125 }
126 }
127
128 // Convert `kind` to a "surface" kind, i.e. one that doesn't include
129 // any value with a "large" qualifier.
130 // TODO: Introduce another enum type for the surface kind?
131 static Kind ConvertToSurfaceKind(Kind kind) {
132 switch (kind) {
133 case Kind::kNone:
134 case Kind::kInStack:
135 case Kind::kInRegister:
136 case Kind::kInFpuRegister:
137 case Kind::kConstant:
138 return kind;
139
140 case Kind::kInStackLargeOffset:
141 return Kind::kInStack;
142
143 case Kind::kConstantLargeValue:
144 return Kind::kConstant;
145
146 default:
147 UNREACHABLE();
148 }
149 }
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
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:
190 * [DexRegisterLocation+].
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000191 * DexRegisterLocations are either 1- or 5-byte wide (see art::DexRegisterLocation::Kind).
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100192 */
Roland Levillaina552e1c2015-03-26 15:01:03 +0000193class DexRegisterLocationCatalog {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100194 public:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000195 explicit DexRegisterLocationCatalog(MemoryRegion region) : region_(region) {}
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100196
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000197 // Short (compressed) location, fitting on one byte.
198 typedef uint8_t ShortLocation;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100199
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000200 void SetRegisterInfo(size_t offset, const DexRegisterLocation& dex_register_location) {
201 DexRegisterLocation::Kind kind = ComputeCompressedKind(dex_register_location);
202 int32_t value = dex_register_location.GetValue();
203 if (DexRegisterLocation::IsShortLocationKind(kind)) {
204 // Short location. Compress the kind and the value as a single byte.
205 if (kind == DexRegisterLocation::Kind::kInStack) {
206 // Instead of storing stack offsets expressed in bytes for
207 // short stack locations, store slot offsets. A stack offset
208 // is a multiple of 4 (kFrameSlotSize). This means that by
209 // dividing it by 4, we can fit values from the [0, 128)
210 // interval in a short stack location, and not just values
211 // from the [0, 32) interval.
212 DCHECK_EQ(value % kFrameSlotSize, 0);
213 value /= kFrameSlotSize;
214 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000215 DCHECK(IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000216 region_.StoreUnaligned<ShortLocation>(offset, MakeShortLocation(kind, value));
217 } else {
218 // Large location. Write the location on one byte and the value
219 // on 4 bytes.
Roland Levillaina552e1c2015-03-26 15:01:03 +0000220 DCHECK(!IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000221 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
222 // Also divide large stack offsets by 4 for the sake of consistency.
223 DCHECK_EQ(value % kFrameSlotSize, 0);
224 value /= kFrameSlotSize;
225 }
226 // Data can be unaligned as the written Dex register locations can
227 // either be 1-byte or 5-byte wide. Use
228 // art::MemoryRegion::StoreUnaligned instead of
229 // art::MemoryRegion::Store to prevent unligned word accesses on ARM.
230 region_.StoreUnaligned<DexRegisterLocation::Kind>(offset, kind);
231 region_.StoreUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind), value);
Roland Levillain442b46a2015-02-18 16:54:21 +0000232 }
233 }
234
Roland Levillaina552e1c2015-03-26 15:01:03 +0000235 // Find the offset of the location catalog entry number `location_catalog_entry_index`.
236 size_t FindLocationOffset(size_t location_catalog_entry_index) const {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000237 size_t offset = kFixedSize;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000238 // Skip the first `location_catalog_entry_index - 1` entries.
239 for (uint16_t i = 0; i < location_catalog_entry_index; ++i) {
240 // Read the first next byte and inspect its first 3 bits to decide
241 // whether it is a short or a large location.
242 DexRegisterLocation::Kind kind = ExtractKindAtOffset(offset);
243 if (DexRegisterLocation::IsShortLocationKind(kind)) {
244 // Short location. Skip the current byte.
245 offset += SingleShortEntrySize();
246 } else {
247 // Large location. Skip the 5 next bytes.
248 offset += SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000249 }
250 }
251 return offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100252 }
253
Roland Levillaina552e1c2015-03-26 15:01:03 +0000254 // Get the internal kind of entry at `location_catalog_entry_index`.
255 DexRegisterLocation::Kind GetLocationInternalKind(size_t location_catalog_entry_index) const {
256 if (location_catalog_entry_index == kNoLocationEntryIndex) {
257 return DexRegisterLocation::Kind::kNone;
258 }
259 return ExtractKindAtOffset(FindLocationOffset(location_catalog_entry_index));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100260 }
261
Roland Levillaina552e1c2015-03-26 15:01:03 +0000262 // Get the (surface) kind and value of entry at `location_catalog_entry_index`.
263 DexRegisterLocation GetDexRegisterLocation(size_t location_catalog_entry_index) const {
264 if (location_catalog_entry_index == kNoLocationEntryIndex) {
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000265 return DexRegisterLocation::None();
266 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000267 size_t offset = FindLocationOffset(location_catalog_entry_index);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000268 // Read the first byte and inspect its first 3 bits to get the location.
269 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
270 DexRegisterLocation::Kind kind = ExtractKindFromShortLocation(first_byte);
271 if (DexRegisterLocation::IsShortLocationKind(kind)) {
272 // Short location. Extract the value from the remaining 5 bits.
273 int32_t value = ExtractValueFromShortLocation(first_byte);
274 if (kind == DexRegisterLocation::Kind::kInStack) {
275 // Convert the stack slot (short) offset to a byte offset value.
276 value *= kFrameSlotSize;
277 }
278 return DexRegisterLocation(kind, value);
279 } else {
280 // Large location. Read the four next bytes to get the value.
281 int32_t value = region_.LoadUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind));
282 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
283 // Convert the stack slot (large) offset to a byte offset value.
284 value *= kFrameSlotSize;
285 }
286 return DexRegisterLocation(kind, value);
287 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100288 }
289
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000290 // Compute the compressed kind of `location`.
291 static DexRegisterLocation::Kind ComputeCompressedKind(const DexRegisterLocation& location) {
292 switch (location.GetInternalKind()) {
293 case DexRegisterLocation::Kind::kNone:
294 DCHECK_EQ(location.GetValue(), 0);
295 return DexRegisterLocation::Kind::kNone;
296
297 case DexRegisterLocation::Kind::kInRegister:
298 DCHECK_GE(location.GetValue(), 0);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000299 DCHECK_LT(location.GetValue(), 1 << kValueBits);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000300 return DexRegisterLocation::Kind::kInRegister;
301
302 case DexRegisterLocation::Kind::kInFpuRegister:
303 DCHECK_GE(location.GetValue(), 0);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000304 DCHECK_LT(location.GetValue(), 1 << kValueBits);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000305 return DexRegisterLocation::Kind::kInFpuRegister;
306
307 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000308 return IsShortStackOffsetValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000309 ? DexRegisterLocation::Kind::kInStack
310 : DexRegisterLocation::Kind::kInStackLargeOffset;
311
312 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000313 return IsShortConstantValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000314 ? DexRegisterLocation::Kind::kConstant
315 : DexRegisterLocation::Kind::kConstantLargeValue;
316
317 default:
318 LOG(FATAL) << "Unexpected location kind"
319 << DexRegisterLocation::PrettyDescriptor(location.GetInternalKind());
320 UNREACHABLE();
321 }
322 }
323
324 // Can `location` be turned into a short location?
325 static bool CanBeEncodedAsShortLocation(const DexRegisterLocation& location) {
326 switch (location.GetInternalKind()) {
327 case DexRegisterLocation::Kind::kNone:
328 case DexRegisterLocation::Kind::kInRegister:
329 case DexRegisterLocation::Kind::kInFpuRegister:
330 return true;
331
332 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000333 return IsShortStackOffsetValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000334
335 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000336 return IsShortConstantValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000337
338 default:
339 UNREACHABLE();
340 }
341 }
342
343 static size_t EntrySize(const DexRegisterLocation& location) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000344 return CanBeEncodedAsShortLocation(location) ? SingleShortEntrySize() : SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000345 }
346
347 static size_t SingleShortEntrySize() {
348 return sizeof(ShortLocation);
349 }
350
351 static size_t SingleLargeEntrySize() {
352 return sizeof(DexRegisterLocation::Kind) + sizeof(int32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100353 }
354
Roland Levillain12baf472015-03-05 12:41:42 +0000355 size_t Size() const {
356 return region_.size();
357 }
358
Roland Levillaina552e1c2015-03-26 15:01:03 +0000359 // Special (invalid) Dex register location catalog entry index meaning
360 // that there is no location for a given Dex register (i.e., it is
361 // mapped to a DexRegisterLocation::Kind::kNone location).
362 static constexpr size_t kNoLocationEntryIndex = -1;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100363
Roland Levillain12baf472015-03-05 12:41:42 +0000364 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000365 static constexpr int kFixedSize = 0;
366
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000367 // Width of the kind "field" in a short location, in bits.
368 static constexpr size_t kKindBits = 3;
369 // Width of the value "field" in a short location, in bits.
370 static constexpr size_t kValueBits = 5;
371
372 static constexpr uint8_t kKindMask = (1 << kKindBits) - 1;
373 static constexpr int32_t kValueMask = (1 << kValueBits) - 1;
374 static constexpr size_t kKindOffset = 0;
375 static constexpr size_t kValueOffset = kKindBits;
376
Roland Levillaina552e1c2015-03-26 15:01:03 +0000377 static bool IsShortStackOffsetValue(int32_t value) {
378 DCHECK_EQ(value % kFrameSlotSize, 0);
379 return IsShortValue(value / kFrameSlotSize);
380 }
381
382 static bool IsShortConstantValue(int32_t value) {
383 return IsShortValue(value);
384 }
385
386 static bool IsShortValue(int32_t value) {
387 return IsUint<kValueBits>(value);
388 }
389
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000390 static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000391 uint8_t kind_integer_value = static_cast<uint8_t>(kind);
392 DCHECK(IsUint<kKindBits>(kind_integer_value)) << kind_integer_value;
393 DCHECK(IsShortValue(value)) << value;
394 return (kind_integer_value & kKindMask) << kKindOffset
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000395 | (value & kValueMask) << kValueOffset;
396 }
397
398 static DexRegisterLocation::Kind ExtractKindFromShortLocation(ShortLocation location) {
399 uint8_t kind = (location >> kKindOffset) & kKindMask;
400 DCHECK_LE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kLastLocationKind));
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000401 // We do not encode kNone locations in the stack map.
402 DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000403 return static_cast<DexRegisterLocation::Kind>(kind);
404 }
405
406 static int32_t ExtractValueFromShortLocation(ShortLocation location) {
407 return (location >> kValueOffset) & kValueMask;
408 }
409
410 // Extract a location kind from the byte at position `offset`.
411 DexRegisterLocation::Kind ExtractKindAtOffset(size_t offset) const {
412 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
413 return ExtractKindFromShortLocation(first_byte);
414 }
415
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100416 MemoryRegion region_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000417
418 friend class CodeInfo;
419 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100420};
421
Roland Levillaina552e1c2015-03-26 15:01:03 +0000422/* Information on Dex register locations for a specific PC, mapping a
423 * stack map's Dex register to a location entry in a DexRegisterLocationCatalog.
424 * The information is of the form:
425 * [live_bit_mask, entries*]
426 * where entries are concatenated unsigned integer values encoded on a number
427 * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending
428 * on the number of entries in the Dex register location catalog
429 * (see DexRegisterMap::SingleEntrySizeInBits). The map is 1-byte aligned.
430 */
431class DexRegisterMap {
432 public:
433 explicit DexRegisterMap(MemoryRegion region) : region_(region) {}
434
435 // Get the surface kind of Dex register `dex_register_number`.
436 DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number,
437 uint16_t number_of_dex_registers,
438 const CodeInfo& code_info) const {
439 return DexRegisterLocation::ConvertToSurfaceKind(
440 GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info));
441 }
442
443 // Get the internal kind of Dex register `dex_register_number`.
444 DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number,
445 uint16_t number_of_dex_registers,
446 const CodeInfo& code_info) const;
447
448 // Get the Dex register location `dex_register_number`.
449 DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number,
450 uint16_t number_of_dex_registers,
451 const CodeInfo& code_info) const;
452
453 int32_t GetStackOffsetInBytes(uint16_t dex_register_number,
454 uint16_t number_of_dex_registers,
455 const CodeInfo& code_info) const {
456 DexRegisterLocation location =
457 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
458 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
459 // GetDexRegisterLocation returns the offset in bytes.
460 return location.GetValue();
461 }
462
463 int32_t GetConstant(uint16_t dex_register_number,
464 uint16_t number_of_dex_registers,
465 const CodeInfo& code_info) const {
466 DexRegisterLocation location =
467 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100468 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kConstant)
469 << DexRegisterLocation::PrettyDescriptor(location.GetKind());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000470 return location.GetValue();
471 }
472
473 int32_t GetMachineRegister(uint16_t dex_register_number,
474 uint16_t number_of_dex_registers,
475 const CodeInfo& code_info) const {
476 DexRegisterLocation location =
477 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
478 DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister
479 || location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister)
480 << DexRegisterLocation::PrettyDescriptor(location.GetInternalKind());
481 return location.GetValue();
482 }
483
484 // Get the index of the entry in the Dex register location catalog
485 // corresponding to `dex_register_number`.
486 size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number,
487 uint16_t number_of_dex_registers,
488 size_t number_of_location_catalog_entries) const {
489 if (!IsDexRegisterLive(dex_register_number)) {
490 return DexRegisterLocationCatalog::kNoLocationEntryIndex;
491 }
492
493 if (number_of_location_catalog_entries == 1) {
494 // We do not allocate space for location maps in the case of a
495 // single-entry location catalog, as it is useless. The only valid
496 // entry index is 0;
497 return 0;
498 }
499
500 // The bit offset of the beginning of the map locations.
501 size_t map_locations_offset_in_bits =
502 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
503 size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number);
504 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
505 // The bit size of an entry.
506 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
507 // The bit offset where `index_in_dex_register_map` is located.
508 size_t entry_offset_in_bits =
509 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
510 size_t location_catalog_entry_index =
511 region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits);
512 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
513 return location_catalog_entry_index;
514 }
515
516 // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`.
517 void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map,
518 size_t location_catalog_entry_index,
519 uint16_t number_of_dex_registers,
520 size_t number_of_location_catalog_entries) {
521 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
522 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
523
524 if (number_of_location_catalog_entries == 1) {
525 // We do not allocate space for location maps in the case of a
526 // single-entry location catalog, as it is useless.
527 return;
528 }
529
530 // The bit offset of the beginning of the map locations.
531 size_t map_locations_offset_in_bits =
532 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
533 // The bit size of an entry.
534 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
535 // The bit offset where `index_in_dex_register_map` is located.
536 size_t entry_offset_in_bits =
537 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
538 region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits);
539 }
540
541 void SetLiveBitMask(uint16_t number_of_dex_registers,
542 const BitVector& live_dex_registers_mask) {
543 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
544 for (uint16_t i = 0; i < number_of_dex_registers; ++i) {
545 region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i));
546 }
547 }
548
549 bool IsDexRegisterLive(uint16_t dex_register_number) const {
550 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
551 return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number);
552 }
553
554 size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const {
555 size_t number_of_live_dex_registers = 0;
556 for (size_t i = 0; i < number_of_dex_registers; ++i) {
557 if (IsDexRegisterLive(i)) {
558 ++number_of_live_dex_registers;
559 }
560 }
561 return number_of_live_dex_registers;
562 }
563
564 static size_t GetLiveBitMaskOffset() {
565 return kFixedSize;
566 }
567
568 // Compute the size of the live register bit mask (in bytes), for a
569 // method having `number_of_dex_registers` Dex registers.
570 static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) {
571 return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte;
572 }
573
574 static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) {
575 return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers);
576 }
577
578 size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers,
579 size_t number_of_location_catalog_entries) const {
580 size_t location_mapping_data_size_in_bits =
581 GetNumberOfLiveDexRegisters(number_of_dex_registers)
582 * SingleEntrySizeInBits(number_of_location_catalog_entries);
583 return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
584 }
585
586 // Return the size of a map entry in bits. Note that if
587 // `number_of_location_catalog_entries` equals 1, this function returns 0,
588 // which is fine, as there is no need to allocate a map for a
589 // single-entry location catalog; the only valid location catalog entry index
590 // for a live register in this case is 0 and there is no need to
591 // store it.
592 static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) {
593 // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2.
594 return number_of_location_catalog_entries == 0
595 ? 0u
596 : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries));
597 }
598
599 // Return the size of the DexRegisterMap object, in bytes.
600 size_t Size() const {
601 return region_.size();
602 }
603
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100604 void Dump(std::ostream& o, const CodeInfo& code_info, uint16_t number_of_dex_registers) const;
605
Roland Levillaina552e1c2015-03-26 15:01:03 +0000606 private:
607 // Return the index in the Dex register map corresponding to the Dex
608 // register number `dex_register_number`.
609 size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const {
610 if (!IsDexRegisterLive(dex_register_number)) {
611 return kInvalidIndexInDexRegisterMap;
612 }
613 return GetNumberOfLiveDexRegisters(dex_register_number);
614 }
615
616 // Special (invalid) Dex register map entry index meaning that there
617 // is no index in the map for a given Dex register (i.e., it must
618 // have been mapped to a DexRegisterLocation::Kind::kNone location).
619 static constexpr size_t kInvalidIndexInDexRegisterMap = -1;
620
621 static constexpr int kFixedSize = 0;
622
623 MemoryRegion region_;
624
625 friend class CodeInfo;
626 friend class StackMapStream;
627};
628
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100629/**
630 * A Stack Map holds compilation information for a specific PC necessary for:
631 * - Mapping it to a dex PC,
632 * - Knowing which stack entries are objects,
633 * - Knowing which registers hold objects,
634 * - Knowing the inlining information,
635 * - Knowing the values of dex registers.
636 *
637 * The information is of the form:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000638 * [dex_pc, native_pc_offset, dex_register_map_offset, inlining_info_offset, register_mask,
639 * stack_mask].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100640 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100641class StackMap {
642 public:
643 explicit StackMap(MemoryRegion region) : region_(region) {}
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100644 StackMap() {}
645
646 bool IsValid() const { return region_.pointer() != nullptr; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100647
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000648 uint32_t GetDexPc(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100649
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000650 void SetDexPc(const CodeInfo& info, uint32_t dex_pc);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100651
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000652 uint32_t GetNativePcOffset(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100653
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000654 void SetNativePcOffset(const CodeInfo& info, uint32_t native_pc_offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100655
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000656 uint32_t GetDexRegisterMapOffset(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100657
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000658 void SetDexRegisterMapOffset(const CodeInfo& info, uint32_t offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100659
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000660 uint32_t GetInlineDescriptorOffset(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100661
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000662 void SetInlineDescriptorOffset(const CodeInfo& info, uint32_t offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100663
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000664 uint32_t GetRegisterMask(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100665
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000666 void SetRegisterMask(const CodeInfo& info, uint32_t mask);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100667
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000668 MemoryRegion GetStackMask(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100669
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000670 void SetStackMask(const CodeInfo& info, const BitVector& sp_map) {
671 MemoryRegion region = GetStackMask(info);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100672 for (size_t i = 0; i < region.size_in_bits(); i++) {
673 region.StoreBit(i, sp_map.IsBitSet(i));
674 }
675 }
676
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000677 bool HasDexRegisterMap(const CodeInfo& info) const {
678 return GetDexRegisterMapOffset(info) != kNoDexRegisterMap;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100679 }
680
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000681 bool HasInlineInfo(const CodeInfo& info) const {
682 return GetInlineDescriptorOffset(info) != kNoInlineInfo;
Roland Levillain442b46a2015-02-18 16:54:21 +0000683 }
684
685 bool Equals(const StackMap& other) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100686 return region_.pointer() == other.region_.pointer()
687 && region_.size() == other.region_.size();
688 }
689
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000690 static size_t ComputeStackMapSize(size_t stack_mask_size,
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000691 size_t inline_info_size,
692 size_t dex_register_map_size,
693 size_t dex_pc_max,
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100694 size_t native_pc_max,
695 size_t register_mask_max);
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000696
Roland Levillain442b46a2015-02-18 16:54:21 +0000697 // Special (invalid) offset for the DexRegisterMapOffset field meaning
698 // that there is no Dex register map for this stack map.
699 static constexpr uint32_t kNoDexRegisterMap = -1;
700
701 // Special (invalid) offset for the InlineDescriptorOffset field meaning
702 // that there is no inline info for this stack map.
703 static constexpr uint32_t kNoInlineInfo = -1;
704
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100705 private:
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100706 static size_t ComputeStackMapSizeInternal(size_t stack_mask_size,
707 size_t number_of_bytes_for_inline_info,
708 size_t number_of_bytes_for_dex_map,
709 size_t number_of_bytes_for_dex_pc,
710 size_t number_of_bytes_for_native_pc,
711 size_t number_of_bytes_for_register_mask);
712
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000713 // TODO: Instead of plain types such as "uint32_t", introduce
714 // typedefs (and document the memory layout of StackMap).
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000715 static constexpr int kRegisterMaskOffset = 0;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100716 static constexpr int kFixedSize = 0;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100717
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100718 MemoryRegion region_;
719
Nicolas Geoffray39468442014-09-02 15:17:15 +0100720 friend class CodeInfo;
721 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100722};
723
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100724/**
725 * Inline information for a specific PC. The information is of the form:
726 * [inlining_depth, [dex_pc, method_index, dex_register_map_offset]+]
727 */
728class InlineInfo {
729 public:
730 explicit InlineInfo(MemoryRegion region) : region_(region) {}
731
732 uint8_t GetDepth() const {
733 return region_.LoadUnaligned<uint8_t>(kDepthOffset);
734 }
735
736 void SetDepth(uint8_t depth) {
737 region_.StoreUnaligned<uint8_t>(kDepthOffset, depth);
738 }
739
740 uint32_t GetMethodIndexAtDepth(uint8_t depth) const {
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100741 return region_.LoadUnaligned<uint32_t>(
742 kFixedSize + depth * SingleEntrySize() + kMethodIndexOffset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100743 }
744
745 void SetMethodIndexAtDepth(uint8_t depth, uint32_t index) {
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100746 region_.StoreUnaligned<uint32_t>(
747 kFixedSize + depth * SingleEntrySize() + kMethodIndexOffset, index);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100748 }
749
750 uint32_t GetDexPcAtDepth(uint8_t depth) const {
751 return region_.LoadUnaligned<uint32_t>(
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100752 kFixedSize + depth * SingleEntrySize() + kDexPcOffset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100753 }
754
755 void SetDexPcAtDepth(uint8_t depth, uint32_t dex_pc) {
756 region_.StoreUnaligned<uint32_t>(
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100757 kFixedSize + depth * SingleEntrySize() + kDexPcOffset, dex_pc);
758 }
759
760 uint8_t GetInvokeTypeAtDepth(uint8_t depth) const {
761 return region_.LoadUnaligned<uint8_t>(
762 kFixedSize + depth * SingleEntrySize() + kInvokeTypeOffset);
763 }
764
765 void SetInvokeTypeAtDepth(uint8_t depth, uint8_t invoke_type) {
766 region_.StoreUnaligned<uint8_t>(
767 kFixedSize + depth * SingleEntrySize() + kInvokeTypeOffset, invoke_type);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100768 }
769
770 uint32_t GetDexRegisterMapOffsetAtDepth(uint8_t depth) const {
771 return region_.LoadUnaligned<uint32_t>(
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100772 kFixedSize + depth * SingleEntrySize() + kDexRegisterMapOffset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100773 }
774
775 void SetDexRegisterMapOffsetAtDepth(uint8_t depth, uint32_t offset) {
776 region_.StoreUnaligned<uint32_t>(
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100777 kFixedSize + depth * SingleEntrySize() + kDexRegisterMapOffset, offset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100778 }
779
780 bool HasDexRegisterMapAtDepth(uint8_t depth) const {
781 return GetDexRegisterMapOffsetAtDepth(depth) != StackMap::kNoDexRegisterMap;
782 }
783
784 static size_t SingleEntrySize() {
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100785 return kFixedEntrySize;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100786 }
787
788 void Dump(std::ostream& os, const CodeInfo& info, uint16_t* number_of_dex_registers) const;
789
790 private:
791 // TODO: Instead of plain types such as "uint8_t", introduce
792 // typedefs (and document the memory layout of InlineInfo).
793 static constexpr int kDepthOffset = 0;
794 static constexpr int kFixedSize = kDepthOffset + sizeof(uint8_t);
795
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100796 static constexpr int kMethodIndexOffset = 0;
797 static constexpr int kDexPcOffset = kMethodIndexOffset + sizeof(uint32_t);
798 static constexpr int kInvokeTypeOffset = kDexPcOffset + sizeof(uint32_t);
799 static constexpr int kDexRegisterMapOffset = kInvokeTypeOffset + sizeof(uint8_t);
800 static constexpr int kFixedEntrySize = kDexRegisterMapOffset + sizeof(uint32_t);
801
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100802 MemoryRegion region_;
803
804 friend class CodeInfo;
805 friend class StackMap;
806 friend class StackMapStream;
807};
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100808
809/**
810 * Wrapper around all compiler information collected for a method.
811 * The information is of the form:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000812 * [overall_size, number_of_location_catalog_entries, number_of_stack_maps, stack_mask_size,
813 * DexRegisterLocationCatalog+, StackMap+, DexRegisterMap+, InlineInfo*].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100814 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100815class CodeInfo {
816 public:
817 explicit CodeInfo(MemoryRegion region) : region_(region) {}
818
Nicolas Geoffray39468442014-09-02 15:17:15 +0100819 explicit CodeInfo(const void* data) {
820 uint32_t size = reinterpret_cast<const uint32_t*>(data)[0];
821 region_ = MemoryRegion(const_cast<void*>(data), size);
822 }
823
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100824 static size_t EncodingSizeInBytes(size_t max_element) {
825 DCHECK(IsUint<32>(max_element));
826 return (max_element == 0) ? 0
827 : IsUint<8>(max_element) ? 1
828 : IsUint<16>(max_element) ? 2
829 : IsUint<24>(max_element) ? 3
830 : 4;
831 }
832
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000833 void SetEncoding(size_t inline_info_size,
834 size_t dex_register_map_size,
835 size_t dex_pc_max,
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100836 size_t native_pc_max,
837 size_t register_mask_max) {
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000838 if (inline_info_size != 0) {
839 region_.StoreBit(kHasInlineInfoBitOffset, 1);
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100840 // + 1 to also encode kNoInlineInfo: if an inline info offset
841 // is at 0xFF, we want to overflow to a larger encoding, because it will
842 // conflict with kNoInlineInfo.
843 // The offset is relative to the dex register map. TODO: Change this.
844 SetEncodingAt(kInlineInfoBitOffset,
845 EncodingSizeInBytes(dex_register_map_size + inline_info_size + 1));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000846 } else {
847 region_.StoreBit(kHasInlineInfoBitOffset, 0);
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100848 SetEncodingAt(kInlineInfoBitOffset, 0);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000849 }
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100850 // + 1 to also encode kNoDexRegisterMap: if a dex register map offset
851 // is at 0xFF, we want to overflow to a larger encoding, because it will
852 // conflict with kNoDexRegisterMap.
853 SetEncodingAt(kDexRegisterMapBitOffset, EncodingSizeInBytes(dex_register_map_size + 1));
854 SetEncodingAt(kDexPcBitOffset, EncodingSizeInBytes(dex_pc_max));
855 SetEncodingAt(kNativePcBitOffset, EncodingSizeInBytes(native_pc_max));
856 SetEncodingAt(kRegisterMaskBitOffset, EncodingSizeInBytes(register_mask_max));
857 }
858
859 void SetEncodingAt(size_t bit_offset, size_t number_of_bytes) {
860 // We encode the number of bytes needed for writing a value on 3 bits,
861 // for values that we know are maximum 32bits.
862 region_.StoreBit(bit_offset, (number_of_bytes & 1));
863 region_.StoreBit(bit_offset + 1, (number_of_bytes & 2));
864 region_.StoreBit(bit_offset + 2, (number_of_bytes & 4));
865 }
866
867 size_t GetNumberOfBytesForEncoding(size_t bit_offset) const {
868 return region_.LoadBit(bit_offset)
869 + (region_.LoadBit(bit_offset + 1) << 1)
870 + (region_.LoadBit(bit_offset + 2) << 2);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000871 }
872
873 bool HasInlineInfo() const {
874 return region_.LoadBit(kHasInlineInfoBitOffset);
875 }
876
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100877 size_t NumberOfBytesForInlineInfo() const {
878 return GetNumberOfBytesForEncoding(kInlineInfoBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000879 }
880
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100881 size_t NumberOfBytesForDexRegisterMap() const {
882 return GetNumberOfBytesForEncoding(kDexRegisterMapBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000883 }
884
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100885 size_t NumberOfBytesForRegisterMask() const {
886 return GetNumberOfBytesForEncoding(kRegisterMaskBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000887 }
888
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100889 size_t NumberOfBytesForNativePc() const {
890 return GetNumberOfBytesForEncoding(kNativePcBitOffset);
891 }
892
893 size_t NumberOfBytesForDexPc() const {
894 return GetNumberOfBytesForEncoding(kDexPcBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000895 }
896
897 size_t ComputeStackMapRegisterMaskOffset() const {
898 return StackMap::kRegisterMaskOffset;
899 }
900
901 size_t ComputeStackMapStackMaskOffset() const {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100902 return ComputeStackMapRegisterMaskOffset()
903 + (NumberOfBytesForRegisterMask() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000904 }
905
906 size_t ComputeStackMapDexPcOffset() const {
907 return ComputeStackMapStackMaskOffset() + GetStackMaskSize();
908 }
909
910 size_t ComputeStackMapNativePcOffset() const {
911 return ComputeStackMapDexPcOffset()
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100912 + (NumberOfBytesForDexPc() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000913 }
914
915 size_t ComputeStackMapDexRegisterMapOffset() const {
916 return ComputeStackMapNativePcOffset()
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100917 + (NumberOfBytesForNativePc() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000918 }
919
920 size_t ComputeStackMapInlineInfoOffset() const {
921 CHECK(HasInlineInfo());
922 return ComputeStackMapDexRegisterMapOffset()
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100923 + (NumberOfBytesForDexRegisterMap() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000924 }
925
Roland Levillaina552e1c2015-03-26 15:01:03 +0000926 DexRegisterLocationCatalog GetDexRegisterLocationCatalog() const {
927 return DexRegisterLocationCatalog(region_.Subregion(
928 GetDexRegisterLocationCatalogOffset(),
929 GetDexRegisterLocationCatalogSize()));
930 }
931
Nicolas Geoffray39468442014-09-02 15:17:15 +0100932 StackMap GetStackMapAt(size_t i) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100933 size_t size = StackMapSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100934 return StackMap(GetStackMaps().Subregion(i * size, size));
935 }
936
937 uint32_t GetOverallSize() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000938 return region_.LoadUnaligned<uint32_t>(kOverallSizeOffset);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100939 }
940
941 void SetOverallSize(uint32_t size) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000942 region_.StoreUnaligned<uint32_t>(kOverallSizeOffset, size);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100943 }
944
Roland Levillaina552e1c2015-03-26 15:01:03 +0000945 uint32_t GetNumberOfDexRegisterLocationCatalogEntries() const {
946 return region_.LoadUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset);
947 }
948
949 void SetNumberOfDexRegisterLocationCatalogEntries(uint32_t num_entries) {
950 region_.StoreUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset, num_entries);
951 }
952
953 uint32_t GetDexRegisterLocationCatalogSize() const {
954 return ComputeDexRegisterLocationCatalogSize(GetDexRegisterLocationCatalogOffset(),
955 GetNumberOfDexRegisterLocationCatalogEntries());
956 }
957
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100958 uint32_t GetStackMaskSize() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000959 return region_.LoadUnaligned<uint32_t>(kStackMaskSizeOffset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100960 }
961
962 void SetStackMaskSize(uint32_t size) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000963 region_.StoreUnaligned<uint32_t>(kStackMaskSizeOffset, size);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100964 }
965
966 size_t GetNumberOfStackMaps() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000967 return region_.LoadUnaligned<uint32_t>(kNumberOfStackMapsOffset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100968 }
969
970 void SetNumberOfStackMaps(uint32_t number_of_stack_maps) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000971 region_.StoreUnaligned<uint32_t>(kNumberOfStackMapsOffset, number_of_stack_maps);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100972 }
973
Roland Levillain29ba1b02015-03-13 11:45:07 +0000974 // Get the size of one stack map of this CodeInfo object, in bytes.
975 // All stack maps of a CodeInfo have the same size.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100976 size_t StackMapSize() const {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100977 return StackMap::ComputeStackMapSizeInternal(GetStackMaskSize(),
978 NumberOfBytesForInlineInfo(),
979 NumberOfBytesForDexRegisterMap(),
980 NumberOfBytesForDexPc(),
981 NumberOfBytesForNativePc(),
982 NumberOfBytesForRegisterMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100983 }
984
Roland Levillain29ba1b02015-03-13 11:45:07 +0000985 // Get the size all the stack maps of this CodeInfo object, in bytes.
Roland Levillaina552e1c2015-03-26 15:01:03 +0000986 size_t GetStackMapsSize() const {
Roland Levillain29ba1b02015-03-13 11:45:07 +0000987 return StackMapSize() * GetNumberOfStackMaps();
988 }
989
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100990 uint32_t GetDexRegisterLocationCatalogOffset() const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000991 return GetStackMapsOffset() + GetStackMapsSize();
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000992 }
993
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100994 size_t GetDexRegisterMapsOffset() const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000995 return GetDexRegisterLocationCatalogOffset() + GetDexRegisterLocationCatalogSize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000996 }
997
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100998 uint32_t GetStackMapsOffset() const {
999 return kFixedSize;
1000 }
1001
Roland Levillain442b46a2015-02-18 16:54:21 +00001002 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map, uint32_t number_of_dex_registers) const {
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001003 DCHECK(stack_map.HasDexRegisterMap(*this));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001004 uint32_t offset = GetDexRegisterMapsOffset() + stack_map.GetDexRegisterMapOffset(*this);
1005 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001006 return DexRegisterMap(region_.Subregion(offset, size));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001007 }
1008
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001009 // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`.
1010 DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
1011 InlineInfo inline_info,
1012 uint32_t number_of_dex_registers) const {
1013 DCHECK(inline_info.HasDexRegisterMapAtDepth(depth));
1014 uint32_t offset =
1015 GetDexRegisterMapsOffset() + inline_info.GetDexRegisterMapOffsetAtDepth(depth);
1016 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
1017 return DexRegisterMap(region_.Subregion(offset, size));
1018 }
1019
Roland Levillain442b46a2015-02-18 16:54:21 +00001020 InlineInfo GetInlineInfoOf(StackMap stack_map) const {
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001021 DCHECK(stack_map.HasInlineInfo(*this));
1022 uint32_t offset = stack_map.GetInlineDescriptorOffset(*this) + GetDexRegisterMapsOffset();
1023 uint8_t depth = region_.LoadUnaligned<uint8_t>(offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001024 return InlineInfo(region_.Subregion(offset,
1025 InlineInfo::kFixedSize + depth * InlineInfo::SingleEntrySize()));
1026 }
1027
Roland Levillain442b46a2015-02-18 16:54:21 +00001028 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001029 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001030 StackMap stack_map = GetStackMapAt(i);
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001031 if (stack_map.GetDexPc(*this) == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001032 return stack_map;
1033 }
1034 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001035 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001036 }
1037
Roland Levillain442b46a2015-02-18 16:54:21 +00001038 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001039 // TODO: stack maps are sorted by native pc, we can do a binary search.
1040 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001041 StackMap stack_map = GetStackMapAt(i);
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001042 if (stack_map.GetNativePcOffset(*this) == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001043 return stack_map;
1044 }
1045 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001046 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001047 }
1048
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001049 void Dump(std::ostream& os, uint16_t number_of_dex_registers) const;
1050 void DumpStackMapHeader(std::ostream& os, size_t stack_map_num) const;
1051
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001052 private:
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001053 // TODO: Instead of plain types such as "uint32_t", introduce
1054 // typedefs (and document the memory layout of CodeInfo).
Nicolas Geoffray39468442014-09-02 15:17:15 +01001055 static constexpr int kOverallSizeOffset = 0;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001056 static constexpr int kEncodingInfoOffset = kOverallSizeOffset + sizeof(uint32_t);
Roland Levillaina552e1c2015-03-26 15:01:03 +00001057 static constexpr int kNumberOfDexRegisterLocationCatalogEntriesOffset =
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001058 kEncodingInfoOffset + sizeof(uint16_t);
Roland Levillaina552e1c2015-03-26 15:01:03 +00001059 static constexpr int kNumberOfStackMapsOffset =
1060 kNumberOfDexRegisterLocationCatalogEntriesOffset + sizeof(uint32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001061 static constexpr int kStackMaskSizeOffset = kNumberOfStackMapsOffset + sizeof(uint32_t);
1062 static constexpr int kFixedSize = kStackMaskSizeOffset + sizeof(uint32_t);
1063
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001064 static constexpr int kHasInlineInfoBitOffset = (kEncodingInfoOffset * kBitsPerByte);
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001065 static constexpr int kInlineInfoBitOffset = kHasInlineInfoBitOffset + 1;
1066 static constexpr int kDexRegisterMapBitOffset = kInlineInfoBitOffset + 3;
1067 static constexpr int kDexPcBitOffset = kDexRegisterMapBitOffset + 3;
1068 static constexpr int kNativePcBitOffset = kDexPcBitOffset + 3;
1069 static constexpr int kRegisterMaskBitOffset = kNativePcBitOffset + 3;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001070
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001071 MemoryRegion GetStackMaps() const {
1072 return region_.size() == 0
1073 ? MemoryRegion()
Roland Levillaina552e1c2015-03-26 15:01:03 +00001074 : region_.Subregion(GetStackMapsOffset(), GetStackMapsSize());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001075 }
1076
Roland Levillaina552e1c2015-03-26 15:01:03 +00001077 // Compute the size of the Dex register map associated to the stack map at
1078 // `dex_register_map_offset_in_code_info`.
1079 size_t ComputeDexRegisterMapSizeOf(uint32_t dex_register_map_offset_in_code_info,
1080 uint16_t number_of_dex_registers) const {
1081 // Offset where the actual mapping data starts within art::DexRegisterMap.
1082 size_t location_mapping_data_offset_in_dex_register_map =
1083 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1084 // Create a temporary art::DexRegisterMap to be able to call
1085 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1086 DexRegisterMap dex_register_map_without_locations(
1087 MemoryRegion(region_.Subregion(dex_register_map_offset_in_code_info,
1088 location_mapping_data_offset_in_dex_register_map)));
1089 size_t number_of_live_dex_registers =
1090 dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
1091 size_t location_mapping_data_size_in_bits =
1092 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfDexRegisterLocationCatalogEntries())
1093 * number_of_live_dex_registers;
1094 size_t location_mapping_data_size_in_bytes =
1095 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1096 size_t dex_register_map_size =
1097 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1098 return dex_register_map_size;
1099 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001100
Roland Levillaina552e1c2015-03-26 15:01:03 +00001101 // Compute the size of a Dex register location catalog starting at offset `origin`
1102 // in `region_` and containing `number_of_dex_locations` entries.
1103 size_t ComputeDexRegisterLocationCatalogSize(uint32_t origin,
1104 uint32_t number_of_dex_locations) const {
1105 // TODO: Ideally, we would like to use art::DexRegisterLocationCatalog::Size or
1106 // art::DexRegisterLocationCatalog::FindLocationOffset, but the
1107 // DexRegisterLocationCatalog is not yet built. Try to factor common code.
1108 size_t offset = origin + DexRegisterLocationCatalog::kFixedSize;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001109
Roland Levillaina552e1c2015-03-26 15:01:03 +00001110 // Skip the first `number_of_dex_locations - 1` entries.
1111 for (uint16_t i = 0; i < number_of_dex_locations; ++i) {
1112 // Read the first next byte and inspect its first 3 bits to decide
1113 // whether it is a short or a large location.
1114 DexRegisterLocationCatalog::ShortLocation first_byte =
1115 region_.LoadUnaligned<DexRegisterLocationCatalog::ShortLocation>(offset);
1116 DexRegisterLocation::Kind kind =
1117 DexRegisterLocationCatalog::ExtractKindFromShortLocation(first_byte);
1118 if (DexRegisterLocation::IsShortLocationKind(kind)) {
1119 // Short location. Skip the current byte.
1120 offset += DexRegisterLocationCatalog::SingleShortEntrySize();
1121 } else {
1122 // Large location. Skip the 5 next bytes.
1123 offset += DexRegisterLocationCatalog::SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001124 }
1125 }
1126 size_t size = offset - origin;
1127 return size;
1128 }
1129
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001130 MemoryRegion region_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001131 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001132};
1133
1134} // namespace art
1135
1136#endif // ART_RUNTIME_STACK_MAP_H_