blob: 16ae772caf0d240309f6abfdd9ebc9177cacb719 [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) {}
644
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000645 uint32_t GetDexPc(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100646
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000647 void SetDexPc(const CodeInfo& info, uint32_t dex_pc);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100648
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000649 uint32_t GetNativePcOffset(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100650
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000651 void SetNativePcOffset(const CodeInfo& info, uint32_t native_pc_offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100652
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000653 uint32_t GetDexRegisterMapOffset(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100654
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000655 void SetDexRegisterMapOffset(const CodeInfo& info, uint32_t offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100656
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000657 uint32_t GetInlineDescriptorOffset(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100658
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000659 void SetInlineDescriptorOffset(const CodeInfo& info, uint32_t offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100660
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000661 uint32_t GetRegisterMask(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100662
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000663 void SetRegisterMask(const CodeInfo& info, uint32_t mask);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100664
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000665 MemoryRegion GetStackMask(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100666
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000667 void SetStackMask(const CodeInfo& info, const BitVector& sp_map) {
668 MemoryRegion region = GetStackMask(info);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100669 for (size_t i = 0; i < region.size_in_bits(); i++) {
670 region.StoreBit(i, sp_map.IsBitSet(i));
671 }
672 }
673
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000674 bool HasDexRegisterMap(const CodeInfo& info) const {
675 return GetDexRegisterMapOffset(info) != kNoDexRegisterMap;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100676 }
677
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000678 bool HasInlineInfo(const CodeInfo& info) const {
679 return GetInlineDescriptorOffset(info) != kNoInlineInfo;
Roland Levillain442b46a2015-02-18 16:54:21 +0000680 }
681
682 bool Equals(const StackMap& other) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100683 return region_.pointer() == other.region_.pointer()
684 && region_.size() == other.region_.size();
685 }
686
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000687 static size_t ComputeStackMapSize(size_t stack_mask_size,
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000688 size_t inline_info_size,
689 size_t dex_register_map_size,
690 size_t dex_pc_max,
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100691 size_t native_pc_max,
692 size_t register_mask_max);
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000693
Roland Levillain442b46a2015-02-18 16:54:21 +0000694 // Special (invalid) offset for the DexRegisterMapOffset field meaning
695 // that there is no Dex register map for this stack map.
696 static constexpr uint32_t kNoDexRegisterMap = -1;
697
698 // Special (invalid) offset for the InlineDescriptorOffset field meaning
699 // that there is no inline info for this stack map.
700 static constexpr uint32_t kNoInlineInfo = -1;
701
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100702 private:
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100703 static size_t ComputeStackMapSizeInternal(size_t stack_mask_size,
704 size_t number_of_bytes_for_inline_info,
705 size_t number_of_bytes_for_dex_map,
706 size_t number_of_bytes_for_dex_pc,
707 size_t number_of_bytes_for_native_pc,
708 size_t number_of_bytes_for_register_mask);
709
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000710 // TODO: Instead of plain types such as "uint32_t", introduce
711 // typedefs (and document the memory layout of StackMap).
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000712 static constexpr int kRegisterMaskOffset = 0;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100713 static constexpr int kFixedSize = 0;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100714
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100715 MemoryRegion region_;
716
Nicolas Geoffray39468442014-09-02 15:17:15 +0100717 friend class CodeInfo;
718 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100719};
720
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100721/**
722 * Inline information for a specific PC. The information is of the form:
723 * [inlining_depth, [dex_pc, method_index, dex_register_map_offset]+]
724 */
725class InlineInfo {
726 public:
727 explicit InlineInfo(MemoryRegion region) : region_(region) {}
728
729 uint8_t GetDepth() const {
730 return region_.LoadUnaligned<uint8_t>(kDepthOffset);
731 }
732
733 void SetDepth(uint8_t depth) {
734 region_.StoreUnaligned<uint8_t>(kDepthOffset, depth);
735 }
736
737 uint32_t GetMethodIndexAtDepth(uint8_t depth) const {
738 return region_.LoadUnaligned<uint32_t>(kFixedSize + depth * SingleEntrySize());
739 }
740
741 void SetMethodIndexAtDepth(uint8_t depth, uint32_t index) {
742 region_.StoreUnaligned<uint32_t>(kFixedSize + depth * SingleEntrySize(), index);
743 }
744
745 uint32_t GetDexPcAtDepth(uint8_t depth) const {
746 return region_.LoadUnaligned<uint32_t>(
747 kFixedSize + depth * SingleEntrySize() + sizeof(uint32_t));
748 }
749
750 void SetDexPcAtDepth(uint8_t depth, uint32_t dex_pc) {
751 region_.StoreUnaligned<uint32_t>(
752 kFixedSize + depth * SingleEntrySize() + sizeof(uint32_t), dex_pc);
753 }
754
755 uint32_t GetDexRegisterMapOffsetAtDepth(uint8_t depth) const {
756 return region_.LoadUnaligned<uint32_t>(
757 kFixedSize + depth * SingleEntrySize() + sizeof(uint32_t) + sizeof(uint32_t));
758 }
759
760 void SetDexRegisterMapOffsetAtDepth(uint8_t depth, uint32_t offset) {
761 region_.StoreUnaligned<uint32_t>(
762 kFixedSize + depth * SingleEntrySize() + sizeof(uint32_t) + sizeof(uint32_t), offset);
763 }
764
765 bool HasDexRegisterMapAtDepth(uint8_t depth) const {
766 return GetDexRegisterMapOffsetAtDepth(depth) != StackMap::kNoDexRegisterMap;
767 }
768
769 static size_t SingleEntrySize() {
770 return sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint32_t);
771 }
772
773 void Dump(std::ostream& os, const CodeInfo& info, uint16_t* number_of_dex_registers) const;
774
775 private:
776 // TODO: Instead of plain types such as "uint8_t", introduce
777 // typedefs (and document the memory layout of InlineInfo).
778 static constexpr int kDepthOffset = 0;
779 static constexpr int kFixedSize = kDepthOffset + sizeof(uint8_t);
780
781 MemoryRegion region_;
782
783 friend class CodeInfo;
784 friend class StackMap;
785 friend class StackMapStream;
786};
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100787
788/**
789 * Wrapper around all compiler information collected for a method.
790 * The information is of the form:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000791 * [overall_size, number_of_location_catalog_entries, number_of_stack_maps, stack_mask_size,
792 * DexRegisterLocationCatalog+, StackMap+, DexRegisterMap+, InlineInfo*].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100793 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100794class CodeInfo {
795 public:
796 explicit CodeInfo(MemoryRegion region) : region_(region) {}
797
Nicolas Geoffray39468442014-09-02 15:17:15 +0100798 explicit CodeInfo(const void* data) {
799 uint32_t size = reinterpret_cast<const uint32_t*>(data)[0];
800 region_ = MemoryRegion(const_cast<void*>(data), size);
801 }
802
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100803 static size_t EncodingSizeInBytes(size_t max_element) {
804 DCHECK(IsUint<32>(max_element));
805 return (max_element == 0) ? 0
806 : IsUint<8>(max_element) ? 1
807 : IsUint<16>(max_element) ? 2
808 : IsUint<24>(max_element) ? 3
809 : 4;
810 }
811
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000812 void SetEncoding(size_t inline_info_size,
813 size_t dex_register_map_size,
814 size_t dex_pc_max,
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100815 size_t native_pc_max,
816 size_t register_mask_max) {
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000817 if (inline_info_size != 0) {
818 region_.StoreBit(kHasInlineInfoBitOffset, 1);
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100819 // + 1 to also encode kNoInlineInfo: if an inline info offset
820 // is at 0xFF, we want to overflow to a larger encoding, because it will
821 // conflict with kNoInlineInfo.
822 // The offset is relative to the dex register map. TODO: Change this.
823 SetEncodingAt(kInlineInfoBitOffset,
824 EncodingSizeInBytes(dex_register_map_size + inline_info_size + 1));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000825 } else {
826 region_.StoreBit(kHasInlineInfoBitOffset, 0);
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100827 SetEncodingAt(kInlineInfoBitOffset, 0);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000828 }
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100829 // + 1 to also encode kNoDexRegisterMap: if a dex register map offset
830 // is at 0xFF, we want to overflow to a larger encoding, because it will
831 // conflict with kNoDexRegisterMap.
832 SetEncodingAt(kDexRegisterMapBitOffset, EncodingSizeInBytes(dex_register_map_size + 1));
833 SetEncodingAt(kDexPcBitOffset, EncodingSizeInBytes(dex_pc_max));
834 SetEncodingAt(kNativePcBitOffset, EncodingSizeInBytes(native_pc_max));
835 SetEncodingAt(kRegisterMaskBitOffset, EncodingSizeInBytes(register_mask_max));
836 }
837
838 void SetEncodingAt(size_t bit_offset, size_t number_of_bytes) {
839 // We encode the number of bytes needed for writing a value on 3 bits,
840 // for values that we know are maximum 32bits.
841 region_.StoreBit(bit_offset, (number_of_bytes & 1));
842 region_.StoreBit(bit_offset + 1, (number_of_bytes & 2));
843 region_.StoreBit(bit_offset + 2, (number_of_bytes & 4));
844 }
845
846 size_t GetNumberOfBytesForEncoding(size_t bit_offset) const {
847 return region_.LoadBit(bit_offset)
848 + (region_.LoadBit(bit_offset + 1) << 1)
849 + (region_.LoadBit(bit_offset + 2) << 2);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000850 }
851
852 bool HasInlineInfo() const {
853 return region_.LoadBit(kHasInlineInfoBitOffset);
854 }
855
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100856 size_t NumberOfBytesForInlineInfo() const {
857 return GetNumberOfBytesForEncoding(kInlineInfoBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000858 }
859
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100860 size_t NumberOfBytesForDexRegisterMap() const {
861 return GetNumberOfBytesForEncoding(kDexRegisterMapBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000862 }
863
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100864 size_t NumberOfBytesForRegisterMask() const {
865 return GetNumberOfBytesForEncoding(kRegisterMaskBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000866 }
867
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100868 size_t NumberOfBytesForNativePc() const {
869 return GetNumberOfBytesForEncoding(kNativePcBitOffset);
870 }
871
872 size_t NumberOfBytesForDexPc() const {
873 return GetNumberOfBytesForEncoding(kDexPcBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000874 }
875
876 size_t ComputeStackMapRegisterMaskOffset() const {
877 return StackMap::kRegisterMaskOffset;
878 }
879
880 size_t ComputeStackMapStackMaskOffset() const {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100881 return ComputeStackMapRegisterMaskOffset()
882 + (NumberOfBytesForRegisterMask() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000883 }
884
885 size_t ComputeStackMapDexPcOffset() const {
886 return ComputeStackMapStackMaskOffset() + GetStackMaskSize();
887 }
888
889 size_t ComputeStackMapNativePcOffset() const {
890 return ComputeStackMapDexPcOffset()
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100891 + (NumberOfBytesForDexPc() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000892 }
893
894 size_t ComputeStackMapDexRegisterMapOffset() const {
895 return ComputeStackMapNativePcOffset()
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100896 + (NumberOfBytesForNativePc() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000897 }
898
899 size_t ComputeStackMapInlineInfoOffset() const {
900 CHECK(HasInlineInfo());
901 return ComputeStackMapDexRegisterMapOffset()
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100902 + (NumberOfBytesForDexRegisterMap() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000903 }
904
Roland Levillaina552e1c2015-03-26 15:01:03 +0000905 uint32_t GetDexRegisterLocationCatalogOffset() const {
906 return kFixedSize;
907 }
908
909 DexRegisterLocationCatalog GetDexRegisterLocationCatalog() const {
910 return DexRegisterLocationCatalog(region_.Subregion(
911 GetDexRegisterLocationCatalogOffset(),
912 GetDexRegisterLocationCatalogSize()));
913 }
914
Nicolas Geoffray39468442014-09-02 15:17:15 +0100915 StackMap GetStackMapAt(size_t i) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100916 size_t size = StackMapSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100917 return StackMap(GetStackMaps().Subregion(i * size, size));
918 }
919
920 uint32_t GetOverallSize() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000921 return region_.LoadUnaligned<uint32_t>(kOverallSizeOffset);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100922 }
923
924 void SetOverallSize(uint32_t size) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000925 region_.StoreUnaligned<uint32_t>(kOverallSizeOffset, size);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100926 }
927
Roland Levillaina552e1c2015-03-26 15:01:03 +0000928 uint32_t GetNumberOfDexRegisterLocationCatalogEntries() const {
929 return region_.LoadUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset);
930 }
931
932 void SetNumberOfDexRegisterLocationCatalogEntries(uint32_t num_entries) {
933 region_.StoreUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset, num_entries);
934 }
935
936 uint32_t GetDexRegisterLocationCatalogSize() const {
937 return ComputeDexRegisterLocationCatalogSize(GetDexRegisterLocationCatalogOffset(),
938 GetNumberOfDexRegisterLocationCatalogEntries());
939 }
940
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100941 uint32_t GetStackMaskSize() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000942 return region_.LoadUnaligned<uint32_t>(kStackMaskSizeOffset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100943 }
944
945 void SetStackMaskSize(uint32_t size) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000946 region_.StoreUnaligned<uint32_t>(kStackMaskSizeOffset, size);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100947 }
948
949 size_t GetNumberOfStackMaps() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000950 return region_.LoadUnaligned<uint32_t>(kNumberOfStackMapsOffset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100951 }
952
953 void SetNumberOfStackMaps(uint32_t number_of_stack_maps) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000954 region_.StoreUnaligned<uint32_t>(kNumberOfStackMapsOffset, number_of_stack_maps);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100955 }
956
Roland Levillain29ba1b02015-03-13 11:45:07 +0000957 // Get the size of one stack map of this CodeInfo object, in bytes.
958 // All stack maps of a CodeInfo have the same size.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100959 size_t StackMapSize() const {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100960 return StackMap::ComputeStackMapSizeInternal(GetStackMaskSize(),
961 NumberOfBytesForInlineInfo(),
962 NumberOfBytesForDexRegisterMap(),
963 NumberOfBytesForDexPc(),
964 NumberOfBytesForNativePc(),
965 NumberOfBytesForRegisterMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100966 }
967
Roland Levillain29ba1b02015-03-13 11:45:07 +0000968 // Get the size all the stack maps of this CodeInfo object, in bytes.
Roland Levillaina552e1c2015-03-26 15:01:03 +0000969 size_t GetStackMapsSize() const {
Roland Levillain29ba1b02015-03-13 11:45:07 +0000970 return StackMapSize() * GetNumberOfStackMaps();
971 }
972
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000973 size_t GetDexRegisterMapsOffset() const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000974 return GetStackMapsOffset() + GetStackMapsSize();
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000975 }
976
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000977 uint32_t GetStackMapsOffset() const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000978 return GetDexRegisterLocationCatalogOffset() + GetDexRegisterLocationCatalogSize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000979 }
980
Roland Levillain442b46a2015-02-18 16:54:21 +0000981 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map, uint32_t number_of_dex_registers) const {
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000982 DCHECK(stack_map.HasDexRegisterMap(*this));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000983 uint32_t offset = GetDexRegisterMapsOffset() + stack_map.GetDexRegisterMapOffset(*this);
984 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000985 return DexRegisterMap(region_.Subregion(offset, size));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100986 }
987
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100988 // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`.
989 DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
990 InlineInfo inline_info,
991 uint32_t number_of_dex_registers) const {
992 DCHECK(inline_info.HasDexRegisterMapAtDepth(depth));
993 uint32_t offset =
994 GetDexRegisterMapsOffset() + inline_info.GetDexRegisterMapOffsetAtDepth(depth);
995 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
996 return DexRegisterMap(region_.Subregion(offset, size));
997 }
998
Roland Levillain442b46a2015-02-18 16:54:21 +0000999 InlineInfo GetInlineInfoOf(StackMap stack_map) const {
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001000 DCHECK(stack_map.HasInlineInfo(*this));
1001 uint32_t offset = stack_map.GetInlineDescriptorOffset(*this) + GetDexRegisterMapsOffset();
1002 uint8_t depth = region_.LoadUnaligned<uint8_t>(offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001003 return InlineInfo(region_.Subregion(offset,
1004 InlineInfo::kFixedSize + depth * InlineInfo::SingleEntrySize()));
1005 }
1006
Roland Levillain442b46a2015-02-18 16:54:21 +00001007 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001008 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001009 StackMap stack_map = GetStackMapAt(i);
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001010 if (stack_map.GetDexPc(*this) == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001011 return stack_map;
1012 }
1013 }
1014 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001015 UNREACHABLE();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001016 }
1017
Roland Levillain442b46a2015-02-18 16:54:21 +00001018 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001019 // TODO: stack maps are sorted by native pc, we can do a binary search.
1020 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001021 StackMap stack_map = GetStackMapAt(i);
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001022 if (stack_map.GetNativePcOffset(*this) == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001023 return stack_map;
1024 }
1025 }
1026 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001027 UNREACHABLE();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001028 }
1029
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001030 void Dump(std::ostream& os, uint16_t number_of_dex_registers) const;
1031 void DumpStackMapHeader(std::ostream& os, size_t stack_map_num) const;
1032
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001033 private:
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001034 // TODO: Instead of plain types such as "uint32_t", introduce
1035 // typedefs (and document the memory layout of CodeInfo).
Nicolas Geoffray39468442014-09-02 15:17:15 +01001036 static constexpr int kOverallSizeOffset = 0;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001037 static constexpr int kEncodingInfoOffset = kOverallSizeOffset + sizeof(uint32_t);
Roland Levillaina552e1c2015-03-26 15:01:03 +00001038 static constexpr int kNumberOfDexRegisterLocationCatalogEntriesOffset =
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001039 kEncodingInfoOffset + sizeof(uint16_t);
Roland Levillaina552e1c2015-03-26 15:01:03 +00001040 static constexpr int kNumberOfStackMapsOffset =
1041 kNumberOfDexRegisterLocationCatalogEntriesOffset + sizeof(uint32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001042 static constexpr int kStackMaskSizeOffset = kNumberOfStackMapsOffset + sizeof(uint32_t);
1043 static constexpr int kFixedSize = kStackMaskSizeOffset + sizeof(uint32_t);
1044
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001045 static constexpr int kHasInlineInfoBitOffset = (kEncodingInfoOffset * kBitsPerByte);
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001046 static constexpr int kInlineInfoBitOffset = kHasInlineInfoBitOffset + 1;
1047 static constexpr int kDexRegisterMapBitOffset = kInlineInfoBitOffset + 3;
1048 static constexpr int kDexPcBitOffset = kDexRegisterMapBitOffset + 3;
1049 static constexpr int kNativePcBitOffset = kDexPcBitOffset + 3;
1050 static constexpr int kRegisterMaskBitOffset = kNativePcBitOffset + 3;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001051
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001052 MemoryRegion GetStackMaps() const {
1053 return region_.size() == 0
1054 ? MemoryRegion()
Roland Levillaina552e1c2015-03-26 15:01:03 +00001055 : region_.Subregion(GetStackMapsOffset(), GetStackMapsSize());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001056 }
1057
Roland Levillaina552e1c2015-03-26 15:01:03 +00001058 // Compute the size of the Dex register map associated to the stack map at
1059 // `dex_register_map_offset_in_code_info`.
1060 size_t ComputeDexRegisterMapSizeOf(uint32_t dex_register_map_offset_in_code_info,
1061 uint16_t number_of_dex_registers) const {
1062 // Offset where the actual mapping data starts within art::DexRegisterMap.
1063 size_t location_mapping_data_offset_in_dex_register_map =
1064 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1065 // Create a temporary art::DexRegisterMap to be able to call
1066 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1067 DexRegisterMap dex_register_map_without_locations(
1068 MemoryRegion(region_.Subregion(dex_register_map_offset_in_code_info,
1069 location_mapping_data_offset_in_dex_register_map)));
1070 size_t number_of_live_dex_registers =
1071 dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
1072 size_t location_mapping_data_size_in_bits =
1073 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfDexRegisterLocationCatalogEntries())
1074 * number_of_live_dex_registers;
1075 size_t location_mapping_data_size_in_bytes =
1076 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1077 size_t dex_register_map_size =
1078 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1079 return dex_register_map_size;
1080 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001081
Roland Levillaina552e1c2015-03-26 15:01:03 +00001082 // Compute the size of a Dex register location catalog starting at offset `origin`
1083 // in `region_` and containing `number_of_dex_locations` entries.
1084 size_t ComputeDexRegisterLocationCatalogSize(uint32_t origin,
1085 uint32_t number_of_dex_locations) const {
1086 // TODO: Ideally, we would like to use art::DexRegisterLocationCatalog::Size or
1087 // art::DexRegisterLocationCatalog::FindLocationOffset, but the
1088 // DexRegisterLocationCatalog is not yet built. Try to factor common code.
1089 size_t offset = origin + DexRegisterLocationCatalog::kFixedSize;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001090
Roland Levillaina552e1c2015-03-26 15:01:03 +00001091 // Skip the first `number_of_dex_locations - 1` entries.
1092 for (uint16_t i = 0; i < number_of_dex_locations; ++i) {
1093 // Read the first next byte and inspect its first 3 bits to decide
1094 // whether it is a short or a large location.
1095 DexRegisterLocationCatalog::ShortLocation first_byte =
1096 region_.LoadUnaligned<DexRegisterLocationCatalog::ShortLocation>(offset);
1097 DexRegisterLocation::Kind kind =
1098 DexRegisterLocationCatalog::ExtractKindFromShortLocation(first_byte);
1099 if (DexRegisterLocation::IsShortLocationKind(kind)) {
1100 // Short location. Skip the current byte.
1101 offset += DexRegisterLocationCatalog::SingleShortEntrySize();
1102 } else {
1103 // Large location. Skip the 5 next bytes.
1104 offset += DexRegisterLocationCatalog::SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001105 }
1106 }
1107 size_t size = offset - origin;
1108 return size;
1109 }
1110
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001111 MemoryRegion region_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001112 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001113};
1114
1115} // namespace art
1116
1117#endif // ART_RUNTIME_STACK_MAP_H_