blob: 7f33e6d090700033bb70e348a975bde9b7511651 [file] [log] [blame]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_STACK_MAP_H_
18#define ART_RUNTIME_STACK_MAP_H_
19
20#include "base/bit_vector.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010021#include "base/bit_utils.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010022#include "memory_region.h"
23
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 Levillain0396ed72015-05-27 15:12:19 +0100359 void Dump(std::ostream& os, const CodeInfo& code_info);
360
Roland Levillaina552e1c2015-03-26 15:01:03 +0000361 // Special (invalid) Dex register location catalog entry index meaning
362 // that there is no location for a given Dex register (i.e., it is
363 // mapped to a DexRegisterLocation::Kind::kNone location).
364 static constexpr size_t kNoLocationEntryIndex = -1;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100365
Roland Levillain12baf472015-03-05 12:41:42 +0000366 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000367 static constexpr int kFixedSize = 0;
368
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000369 // Width of the kind "field" in a short location, in bits.
370 static constexpr size_t kKindBits = 3;
371 // Width of the value "field" in a short location, in bits.
372 static constexpr size_t kValueBits = 5;
373
374 static constexpr uint8_t kKindMask = (1 << kKindBits) - 1;
375 static constexpr int32_t kValueMask = (1 << kValueBits) - 1;
376 static constexpr size_t kKindOffset = 0;
377 static constexpr size_t kValueOffset = kKindBits;
378
Roland Levillaina552e1c2015-03-26 15:01:03 +0000379 static bool IsShortStackOffsetValue(int32_t value) {
380 DCHECK_EQ(value % kFrameSlotSize, 0);
381 return IsShortValue(value / kFrameSlotSize);
382 }
383
384 static bool IsShortConstantValue(int32_t value) {
385 return IsShortValue(value);
386 }
387
388 static bool IsShortValue(int32_t value) {
389 return IsUint<kValueBits>(value);
390 }
391
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000392 static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000393 uint8_t kind_integer_value = static_cast<uint8_t>(kind);
394 DCHECK(IsUint<kKindBits>(kind_integer_value)) << kind_integer_value;
395 DCHECK(IsShortValue(value)) << value;
396 return (kind_integer_value & kKindMask) << kKindOffset
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000397 | (value & kValueMask) << kValueOffset;
398 }
399
400 static DexRegisterLocation::Kind ExtractKindFromShortLocation(ShortLocation location) {
401 uint8_t kind = (location >> kKindOffset) & kKindMask;
402 DCHECK_LE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kLastLocationKind));
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000403 // We do not encode kNone locations in the stack map.
404 DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000405 return static_cast<DexRegisterLocation::Kind>(kind);
406 }
407
408 static int32_t ExtractValueFromShortLocation(ShortLocation location) {
409 return (location >> kValueOffset) & kValueMask;
410 }
411
412 // Extract a location kind from the byte at position `offset`.
413 DexRegisterLocation::Kind ExtractKindAtOffset(size_t offset) const {
414 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
415 return ExtractKindFromShortLocation(first_byte);
416 }
417
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100418 MemoryRegion region_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000419
420 friend class CodeInfo;
421 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100422};
423
Roland Levillaina552e1c2015-03-26 15:01:03 +0000424/* Information on Dex register locations for a specific PC, mapping a
425 * stack map's Dex register to a location entry in a DexRegisterLocationCatalog.
426 * The information is of the form:
427 * [live_bit_mask, entries*]
428 * where entries are concatenated unsigned integer values encoded on a number
429 * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending
430 * on the number of entries in the Dex register location catalog
431 * (see DexRegisterMap::SingleEntrySizeInBits). The map is 1-byte aligned.
432 */
433class DexRegisterMap {
434 public:
435 explicit DexRegisterMap(MemoryRegion region) : region_(region) {}
436
437 // Get the surface kind of Dex register `dex_register_number`.
438 DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number,
439 uint16_t number_of_dex_registers,
440 const CodeInfo& code_info) const {
441 return DexRegisterLocation::ConvertToSurfaceKind(
442 GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info));
443 }
444
445 // Get the internal kind of Dex register `dex_register_number`.
446 DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number,
447 uint16_t number_of_dex_registers,
448 const CodeInfo& code_info) const;
449
450 // Get the Dex register location `dex_register_number`.
451 DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number,
452 uint16_t number_of_dex_registers,
453 const CodeInfo& code_info) const;
454
455 int32_t GetStackOffsetInBytes(uint16_t dex_register_number,
456 uint16_t number_of_dex_registers,
457 const CodeInfo& code_info) const {
458 DexRegisterLocation location =
459 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
460 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
461 // GetDexRegisterLocation returns the offset in bytes.
462 return location.GetValue();
463 }
464
465 int32_t GetConstant(uint16_t dex_register_number,
466 uint16_t number_of_dex_registers,
467 const CodeInfo& code_info) const {
468 DexRegisterLocation location =
469 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100470 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kConstant)
471 << DexRegisterLocation::PrettyDescriptor(location.GetKind());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000472 return location.GetValue();
473 }
474
475 int32_t GetMachineRegister(uint16_t dex_register_number,
476 uint16_t number_of_dex_registers,
477 const CodeInfo& code_info) const {
478 DexRegisterLocation location =
479 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
480 DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister
481 || location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister)
482 << DexRegisterLocation::PrettyDescriptor(location.GetInternalKind());
483 return location.GetValue();
484 }
485
486 // Get the index of the entry in the Dex register location catalog
487 // corresponding to `dex_register_number`.
488 size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number,
489 uint16_t number_of_dex_registers,
490 size_t number_of_location_catalog_entries) const {
491 if (!IsDexRegisterLive(dex_register_number)) {
492 return DexRegisterLocationCatalog::kNoLocationEntryIndex;
493 }
494
495 if (number_of_location_catalog_entries == 1) {
496 // We do not allocate space for location maps in the case of a
497 // single-entry location catalog, as it is useless. The only valid
498 // entry index is 0;
499 return 0;
500 }
501
502 // The bit offset of the beginning of the map locations.
503 size_t map_locations_offset_in_bits =
504 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
505 size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number);
506 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
507 // The bit size of an entry.
508 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
509 // The bit offset where `index_in_dex_register_map` is located.
510 size_t entry_offset_in_bits =
511 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
512 size_t location_catalog_entry_index =
513 region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits);
514 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
515 return location_catalog_entry_index;
516 }
517
518 // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`.
519 void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map,
520 size_t location_catalog_entry_index,
521 uint16_t number_of_dex_registers,
522 size_t number_of_location_catalog_entries) {
523 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
524 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
525
526 if (number_of_location_catalog_entries == 1) {
527 // We do not allocate space for location maps in the case of a
528 // single-entry location catalog, as it is useless.
529 return;
530 }
531
532 // The bit offset of the beginning of the map locations.
533 size_t map_locations_offset_in_bits =
534 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
535 // The bit size of an entry.
536 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
537 // The bit offset where `index_in_dex_register_map` is located.
538 size_t entry_offset_in_bits =
539 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
540 region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits);
541 }
542
543 void SetLiveBitMask(uint16_t number_of_dex_registers,
544 const BitVector& live_dex_registers_mask) {
545 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
546 for (uint16_t i = 0; i < number_of_dex_registers; ++i) {
547 region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i));
548 }
549 }
550
551 bool IsDexRegisterLive(uint16_t dex_register_number) const {
552 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
553 return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number);
554 }
555
556 size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const {
557 size_t number_of_live_dex_registers = 0;
558 for (size_t i = 0; i < number_of_dex_registers; ++i) {
559 if (IsDexRegisterLive(i)) {
560 ++number_of_live_dex_registers;
561 }
562 }
563 return number_of_live_dex_registers;
564 }
565
566 static size_t GetLiveBitMaskOffset() {
567 return kFixedSize;
568 }
569
570 // Compute the size of the live register bit mask (in bytes), for a
571 // method having `number_of_dex_registers` Dex registers.
572 static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) {
573 return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte;
574 }
575
576 static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) {
577 return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers);
578 }
579
580 size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers,
581 size_t number_of_location_catalog_entries) const {
582 size_t location_mapping_data_size_in_bits =
583 GetNumberOfLiveDexRegisters(number_of_dex_registers)
584 * SingleEntrySizeInBits(number_of_location_catalog_entries);
585 return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
586 }
587
588 // Return the size of a map entry in bits. Note that if
589 // `number_of_location_catalog_entries` equals 1, this function returns 0,
590 // which is fine, as there is no need to allocate a map for a
591 // single-entry location catalog; the only valid location catalog entry index
592 // for a live register in this case is 0 and there is no need to
593 // store it.
594 static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) {
595 // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2.
596 return number_of_location_catalog_entries == 0
597 ? 0u
598 : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries));
599 }
600
601 // Return the size of the DexRegisterMap object, in bytes.
602 size_t Size() const {
603 return region_.size();
604 }
605
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100606 void Dump(std::ostream& o, const CodeInfo& code_info, uint16_t number_of_dex_registers) const;
607
Roland Levillaina552e1c2015-03-26 15:01:03 +0000608 private:
609 // Return the index in the Dex register map corresponding to the Dex
610 // register number `dex_register_number`.
611 size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const {
612 if (!IsDexRegisterLive(dex_register_number)) {
613 return kInvalidIndexInDexRegisterMap;
614 }
615 return GetNumberOfLiveDexRegisters(dex_register_number);
616 }
617
618 // Special (invalid) Dex register map entry index meaning that there
619 // is no index in the map for a given Dex register (i.e., it must
620 // have been mapped to a DexRegisterLocation::Kind::kNone location).
621 static constexpr size_t kInvalidIndexInDexRegisterMap = -1;
622
623 static constexpr int kFixedSize = 0;
624
625 MemoryRegion region_;
626
627 friend class CodeInfo;
628 friend class StackMapStream;
629};
630
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100631/**
632 * A Stack Map holds compilation information for a specific PC necessary for:
633 * - Mapping it to a dex PC,
634 * - Knowing which stack entries are objects,
635 * - Knowing which registers hold objects,
636 * - Knowing the inlining information,
637 * - Knowing the values of dex registers.
638 *
639 * The information is of the form:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000640 * [dex_pc, native_pc_offset, dex_register_map_offset, inlining_info_offset, register_mask,
641 * stack_mask].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100642 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100643class StackMap {
644 public:
645 explicit StackMap(MemoryRegion region) : region_(region) {}
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100646 StackMap() {}
647
648 bool IsValid() const { return region_.pointer() != nullptr; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100649
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000650 uint32_t GetDexPc(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100651
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000652 void SetDexPc(const CodeInfo& info, uint32_t dex_pc);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100653
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000654 uint32_t GetNativePcOffset(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100655
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000656 void SetNativePcOffset(const CodeInfo& info, uint32_t native_pc_offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100657
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000658 uint32_t GetDexRegisterMapOffset(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100659
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000660 void SetDexRegisterMapOffset(const CodeInfo& info, uint32_t offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100661
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000662 uint32_t GetInlineDescriptorOffset(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100663
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000664 void SetInlineDescriptorOffset(const CodeInfo& info, uint32_t offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100665
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000666 uint32_t GetRegisterMask(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100667
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000668 void SetRegisterMask(const CodeInfo& info, uint32_t mask);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100669
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000670 MemoryRegion GetStackMask(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100671
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000672 void SetStackMask(const CodeInfo& info, const BitVector& sp_map) {
673 MemoryRegion region = GetStackMask(info);
David Brazdilf10a25f2015-06-02 14:29:52 +0100674 sp_map.CopyTo(region.start(), region.size());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100675 }
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 Levillainf2650d12015-05-28 14:53:28 +0100697 void Dump(std::ostream& os,
698 const CodeInfo& code_info,
699 uint32_t code_offset,
700 uint16_t number_of_dex_registers,
701 const std::string& header_suffix = "") const;
702
Roland Levillain442b46a2015-02-18 16:54:21 +0000703 // Special (invalid) offset for the DexRegisterMapOffset field meaning
704 // that there is no Dex register map for this stack map.
705 static constexpr uint32_t kNoDexRegisterMap = -1;
706
707 // Special (invalid) offset for the InlineDescriptorOffset field meaning
708 // that there is no inline info for this stack map.
709 static constexpr uint32_t kNoInlineInfo = -1;
710
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100711 private:
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100712 static size_t ComputeStackMapSizeInternal(size_t stack_mask_size,
713 size_t number_of_bytes_for_inline_info,
714 size_t number_of_bytes_for_dex_map,
715 size_t number_of_bytes_for_dex_pc,
716 size_t number_of_bytes_for_native_pc,
717 size_t number_of_bytes_for_register_mask);
718
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000719 // TODO: Instead of plain types such as "uint32_t", introduce
720 // typedefs (and document the memory layout of StackMap).
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000721 static constexpr int kRegisterMaskOffset = 0;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100722 static constexpr int kFixedSize = 0;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100723
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100724 MemoryRegion region_;
725
Nicolas Geoffray39468442014-09-02 15:17:15 +0100726 friend class CodeInfo;
727 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100728};
729
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100730/**
731 * Inline information for a specific PC. The information is of the form:
732 * [inlining_depth, [dex_pc, method_index, dex_register_map_offset]+]
733 */
734class InlineInfo {
735 public:
736 explicit InlineInfo(MemoryRegion region) : region_(region) {}
737
738 uint8_t GetDepth() const {
739 return region_.LoadUnaligned<uint8_t>(kDepthOffset);
740 }
741
742 void SetDepth(uint8_t depth) {
743 region_.StoreUnaligned<uint8_t>(kDepthOffset, depth);
744 }
745
746 uint32_t GetMethodIndexAtDepth(uint8_t depth) const {
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100747 return region_.LoadUnaligned<uint32_t>(
748 kFixedSize + depth * SingleEntrySize() + kMethodIndexOffset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100749 }
750
751 void SetMethodIndexAtDepth(uint8_t depth, uint32_t index) {
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100752 region_.StoreUnaligned<uint32_t>(
753 kFixedSize + depth * SingleEntrySize() + kMethodIndexOffset, index);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100754 }
755
756 uint32_t GetDexPcAtDepth(uint8_t depth) const {
757 return region_.LoadUnaligned<uint32_t>(
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100758 kFixedSize + depth * SingleEntrySize() + kDexPcOffset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100759 }
760
761 void SetDexPcAtDepth(uint8_t depth, uint32_t dex_pc) {
762 region_.StoreUnaligned<uint32_t>(
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100763 kFixedSize + depth * SingleEntrySize() + kDexPcOffset, dex_pc);
764 }
765
766 uint8_t GetInvokeTypeAtDepth(uint8_t depth) const {
767 return region_.LoadUnaligned<uint8_t>(
768 kFixedSize + depth * SingleEntrySize() + kInvokeTypeOffset);
769 }
770
771 void SetInvokeTypeAtDepth(uint8_t depth, uint8_t invoke_type) {
772 region_.StoreUnaligned<uint8_t>(
773 kFixedSize + depth * SingleEntrySize() + kInvokeTypeOffset, invoke_type);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100774 }
775
776 uint32_t GetDexRegisterMapOffsetAtDepth(uint8_t depth) const {
777 return region_.LoadUnaligned<uint32_t>(
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100778 kFixedSize + depth * SingleEntrySize() + kDexRegisterMapOffset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100779 }
780
781 void SetDexRegisterMapOffsetAtDepth(uint8_t depth, uint32_t offset) {
782 region_.StoreUnaligned<uint32_t>(
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100783 kFixedSize + depth * SingleEntrySize() + kDexRegisterMapOffset, offset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100784 }
785
786 bool HasDexRegisterMapAtDepth(uint8_t depth) const {
787 return GetDexRegisterMapOffsetAtDepth(depth) != StackMap::kNoDexRegisterMap;
788 }
789
790 static size_t SingleEntrySize() {
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100791 return kFixedEntrySize;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100792 }
793
794 void Dump(std::ostream& os, const CodeInfo& info, uint16_t* number_of_dex_registers) const;
795
796 private:
797 // TODO: Instead of plain types such as "uint8_t", introduce
798 // typedefs (and document the memory layout of InlineInfo).
799 static constexpr int kDepthOffset = 0;
800 static constexpr int kFixedSize = kDepthOffset + sizeof(uint8_t);
801
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100802 static constexpr int kMethodIndexOffset = 0;
803 static constexpr int kDexPcOffset = kMethodIndexOffset + sizeof(uint32_t);
804 static constexpr int kInvokeTypeOffset = kDexPcOffset + sizeof(uint32_t);
805 static constexpr int kDexRegisterMapOffset = kInvokeTypeOffset + sizeof(uint8_t);
806 static constexpr int kFixedEntrySize = kDexRegisterMapOffset + sizeof(uint32_t);
807
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100808 MemoryRegion region_;
809
810 friend class CodeInfo;
811 friend class StackMap;
812 friend class StackMapStream;
813};
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100814
815/**
816 * Wrapper around all compiler information collected for a method.
817 * The information is of the form:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000818 * [overall_size, number_of_location_catalog_entries, number_of_stack_maps, stack_mask_size,
819 * DexRegisterLocationCatalog+, StackMap+, DexRegisterMap+, InlineInfo*].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100820 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100821class CodeInfo {
822 public:
823 explicit CodeInfo(MemoryRegion region) : region_(region) {}
824
Nicolas Geoffray39468442014-09-02 15:17:15 +0100825 explicit CodeInfo(const void* data) {
826 uint32_t size = reinterpret_cast<const uint32_t*>(data)[0];
827 region_ = MemoryRegion(const_cast<void*>(data), size);
828 }
829
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100830 static size_t EncodingSizeInBytes(size_t max_element) {
831 DCHECK(IsUint<32>(max_element));
832 return (max_element == 0) ? 0
833 : IsUint<8>(max_element) ? 1
834 : IsUint<16>(max_element) ? 2
835 : IsUint<24>(max_element) ? 3
836 : 4;
837 }
838
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000839 void SetEncoding(size_t inline_info_size,
840 size_t dex_register_map_size,
841 size_t dex_pc_max,
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100842 size_t native_pc_max,
843 size_t register_mask_max) {
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000844 if (inline_info_size != 0) {
845 region_.StoreBit(kHasInlineInfoBitOffset, 1);
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100846 // + 1 to also encode kNoInlineInfo: if an inline info offset
847 // is at 0xFF, we want to overflow to a larger encoding, because it will
848 // conflict with kNoInlineInfo.
849 // The offset is relative to the dex register map. TODO: Change this.
850 SetEncodingAt(kInlineInfoBitOffset,
851 EncodingSizeInBytes(dex_register_map_size + inline_info_size + 1));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000852 } else {
853 region_.StoreBit(kHasInlineInfoBitOffset, 0);
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100854 SetEncodingAt(kInlineInfoBitOffset, 0);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000855 }
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100856 // + 1 to also encode kNoDexRegisterMap: if a dex register map offset
857 // is at 0xFF, we want to overflow to a larger encoding, because it will
858 // conflict with kNoDexRegisterMap.
859 SetEncodingAt(kDexRegisterMapBitOffset, EncodingSizeInBytes(dex_register_map_size + 1));
860 SetEncodingAt(kDexPcBitOffset, EncodingSizeInBytes(dex_pc_max));
861 SetEncodingAt(kNativePcBitOffset, EncodingSizeInBytes(native_pc_max));
862 SetEncodingAt(kRegisterMaskBitOffset, EncodingSizeInBytes(register_mask_max));
863 }
864
865 void SetEncodingAt(size_t bit_offset, size_t number_of_bytes) {
866 // We encode the number of bytes needed for writing a value on 3 bits,
867 // for values that we know are maximum 32bits.
868 region_.StoreBit(bit_offset, (number_of_bytes & 1));
869 region_.StoreBit(bit_offset + 1, (number_of_bytes & 2));
870 region_.StoreBit(bit_offset + 2, (number_of_bytes & 4));
871 }
872
873 size_t GetNumberOfBytesForEncoding(size_t bit_offset) const {
874 return region_.LoadBit(bit_offset)
875 + (region_.LoadBit(bit_offset + 1) << 1)
876 + (region_.LoadBit(bit_offset + 2) << 2);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000877 }
878
879 bool HasInlineInfo() const {
880 return region_.LoadBit(kHasInlineInfoBitOffset);
881 }
882
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100883 size_t NumberOfBytesForInlineInfo() const {
884 return GetNumberOfBytesForEncoding(kInlineInfoBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000885 }
886
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100887 size_t NumberOfBytesForDexRegisterMap() const {
888 return GetNumberOfBytesForEncoding(kDexRegisterMapBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000889 }
890
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100891 size_t NumberOfBytesForRegisterMask() const {
892 return GetNumberOfBytesForEncoding(kRegisterMaskBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000893 }
894
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100895 size_t NumberOfBytesForNativePc() const {
896 return GetNumberOfBytesForEncoding(kNativePcBitOffset);
897 }
898
899 size_t NumberOfBytesForDexPc() const {
900 return GetNumberOfBytesForEncoding(kDexPcBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000901 }
902
903 size_t ComputeStackMapRegisterMaskOffset() const {
904 return StackMap::kRegisterMaskOffset;
905 }
906
907 size_t ComputeStackMapStackMaskOffset() const {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100908 return ComputeStackMapRegisterMaskOffset()
909 + (NumberOfBytesForRegisterMask() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000910 }
911
912 size_t ComputeStackMapDexPcOffset() const {
913 return ComputeStackMapStackMaskOffset() + GetStackMaskSize();
914 }
915
916 size_t ComputeStackMapNativePcOffset() const {
917 return ComputeStackMapDexPcOffset()
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100918 + (NumberOfBytesForDexPc() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000919 }
920
921 size_t ComputeStackMapDexRegisterMapOffset() const {
922 return ComputeStackMapNativePcOffset()
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100923 + (NumberOfBytesForNativePc() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000924 }
925
926 size_t ComputeStackMapInlineInfoOffset() const {
927 CHECK(HasInlineInfo());
928 return ComputeStackMapDexRegisterMapOffset()
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100929 + (NumberOfBytesForDexRegisterMap() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000930 }
931
Roland Levillaina552e1c2015-03-26 15:01:03 +0000932 DexRegisterLocationCatalog GetDexRegisterLocationCatalog() const {
933 return DexRegisterLocationCatalog(region_.Subregion(
934 GetDexRegisterLocationCatalogOffset(),
935 GetDexRegisterLocationCatalogSize()));
936 }
937
Nicolas Geoffray39468442014-09-02 15:17:15 +0100938 StackMap GetStackMapAt(size_t i) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100939 size_t size = StackMapSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100940 return StackMap(GetStackMaps().Subregion(i * size, size));
941 }
942
943 uint32_t GetOverallSize() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000944 return region_.LoadUnaligned<uint32_t>(kOverallSizeOffset);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100945 }
946
947 void SetOverallSize(uint32_t size) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000948 region_.StoreUnaligned<uint32_t>(kOverallSizeOffset, size);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100949 }
950
Roland Levillaina552e1c2015-03-26 15:01:03 +0000951 uint32_t GetNumberOfDexRegisterLocationCatalogEntries() const {
952 return region_.LoadUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset);
953 }
954
955 void SetNumberOfDexRegisterLocationCatalogEntries(uint32_t num_entries) {
956 region_.StoreUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset, num_entries);
957 }
958
959 uint32_t GetDexRegisterLocationCatalogSize() const {
960 return ComputeDexRegisterLocationCatalogSize(GetDexRegisterLocationCatalogOffset(),
961 GetNumberOfDexRegisterLocationCatalogEntries());
962 }
963
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100964 uint32_t GetStackMaskSize() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000965 return region_.LoadUnaligned<uint32_t>(kStackMaskSizeOffset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100966 }
967
968 void SetStackMaskSize(uint32_t size) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000969 region_.StoreUnaligned<uint32_t>(kStackMaskSizeOffset, size);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100970 }
971
972 size_t GetNumberOfStackMaps() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000973 return region_.LoadUnaligned<uint32_t>(kNumberOfStackMapsOffset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100974 }
975
976 void SetNumberOfStackMaps(uint32_t number_of_stack_maps) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000977 region_.StoreUnaligned<uint32_t>(kNumberOfStackMapsOffset, number_of_stack_maps);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100978 }
979
Roland Levillain29ba1b02015-03-13 11:45:07 +0000980 // Get the size of one stack map of this CodeInfo object, in bytes.
981 // All stack maps of a CodeInfo have the same size.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100982 size_t StackMapSize() const {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100983 return StackMap::ComputeStackMapSizeInternal(GetStackMaskSize(),
984 NumberOfBytesForInlineInfo(),
985 NumberOfBytesForDexRegisterMap(),
986 NumberOfBytesForDexPc(),
987 NumberOfBytesForNativePc(),
988 NumberOfBytesForRegisterMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100989 }
990
Roland Levillain29ba1b02015-03-13 11:45:07 +0000991 // Get the size all the stack maps of this CodeInfo object, in bytes.
Roland Levillaina552e1c2015-03-26 15:01:03 +0000992 size_t GetStackMapsSize() const {
Roland Levillain29ba1b02015-03-13 11:45:07 +0000993 return StackMapSize() * GetNumberOfStackMaps();
994 }
995
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100996 uint32_t GetDexRegisterLocationCatalogOffset() const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000997 return GetStackMapsOffset() + GetStackMapsSize();
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000998 }
999
Nicolas Geoffray6530baf2015-05-26 15:22:58 +01001000 size_t GetDexRegisterMapsOffset() const {
Roland Levillaina552e1c2015-03-26 15:01:03 +00001001 return GetDexRegisterLocationCatalogOffset() + GetDexRegisterLocationCatalogSize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001002 }
1003
Nicolas Geoffray6530baf2015-05-26 15:22:58 +01001004 uint32_t GetStackMapsOffset() const {
1005 return kFixedSize;
1006 }
1007
Roland Levillain442b46a2015-02-18 16:54:21 +00001008 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map, uint32_t number_of_dex_registers) const {
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001009 DCHECK(stack_map.HasDexRegisterMap(*this));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001010 uint32_t offset = GetDexRegisterMapsOffset() + stack_map.GetDexRegisterMapOffset(*this);
1011 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001012 return DexRegisterMap(region_.Subregion(offset, size));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001013 }
1014
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001015 // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`.
1016 DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
1017 InlineInfo inline_info,
1018 uint32_t number_of_dex_registers) const {
1019 DCHECK(inline_info.HasDexRegisterMapAtDepth(depth));
1020 uint32_t offset =
1021 GetDexRegisterMapsOffset() + inline_info.GetDexRegisterMapOffsetAtDepth(depth);
1022 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
1023 return DexRegisterMap(region_.Subregion(offset, size));
1024 }
1025
Roland Levillain442b46a2015-02-18 16:54:21 +00001026 InlineInfo GetInlineInfoOf(StackMap stack_map) const {
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001027 DCHECK(stack_map.HasInlineInfo(*this));
1028 uint32_t offset = stack_map.GetInlineDescriptorOffset(*this) + GetDexRegisterMapsOffset();
1029 uint8_t depth = region_.LoadUnaligned<uint8_t>(offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001030 return InlineInfo(region_.Subregion(offset,
1031 InlineInfo::kFixedSize + depth * InlineInfo::SingleEntrySize()));
1032 }
1033
Roland Levillain442b46a2015-02-18 16:54:21 +00001034 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001035 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001036 StackMap stack_map = GetStackMapAt(i);
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001037 if (stack_map.GetDexPc(*this) == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001038 return stack_map;
1039 }
1040 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001041 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001042 }
1043
Roland Levillain442b46a2015-02-18 16:54:21 +00001044 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001045 // TODO: stack maps are sorted by native pc, we can do a binary search.
1046 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001047 StackMap stack_map = GetStackMapAt(i);
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001048 if (stack_map.GetNativePcOffset(*this) == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001049 return stack_map;
1050 }
1051 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001052 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001053 }
1054
Roland Levillainf2650d12015-05-28 14:53:28 +01001055 // Dump this CodeInfo object on `os`. `code_offset` is the (absolute)
1056 // native PC of the compiled method and `number_of_dex_registers` the
1057 // number of Dex virtual registers used in this method. If
1058 // `dump_stack_maps` is true, also dump the stack maps and the
1059 // associated Dex register maps.
1060 void Dump(std::ostream& os,
1061 uint32_t code_offset,
1062 uint16_t number_of_dex_registers,
1063 bool dump_stack_maps) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001064
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001065 private:
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001066 // TODO: Instead of plain types such as "uint32_t", introduce
1067 // typedefs (and document the memory layout of CodeInfo).
Nicolas Geoffray39468442014-09-02 15:17:15 +01001068 static constexpr int kOverallSizeOffset = 0;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001069 static constexpr int kEncodingInfoOffset = kOverallSizeOffset + sizeof(uint32_t);
Roland Levillaina552e1c2015-03-26 15:01:03 +00001070 static constexpr int kNumberOfDexRegisterLocationCatalogEntriesOffset =
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001071 kEncodingInfoOffset + sizeof(uint16_t);
Roland Levillaina552e1c2015-03-26 15:01:03 +00001072 static constexpr int kNumberOfStackMapsOffset =
1073 kNumberOfDexRegisterLocationCatalogEntriesOffset + sizeof(uint32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001074 static constexpr int kStackMaskSizeOffset = kNumberOfStackMapsOffset + sizeof(uint32_t);
1075 static constexpr int kFixedSize = kStackMaskSizeOffset + sizeof(uint32_t);
1076
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001077 static constexpr int kHasInlineInfoBitOffset = (kEncodingInfoOffset * kBitsPerByte);
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001078 static constexpr int kInlineInfoBitOffset = kHasInlineInfoBitOffset + 1;
1079 static constexpr int kDexRegisterMapBitOffset = kInlineInfoBitOffset + 3;
1080 static constexpr int kDexPcBitOffset = kDexRegisterMapBitOffset + 3;
1081 static constexpr int kNativePcBitOffset = kDexPcBitOffset + 3;
1082 static constexpr int kRegisterMaskBitOffset = kNativePcBitOffset + 3;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001083
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001084 MemoryRegion GetStackMaps() const {
1085 return region_.size() == 0
1086 ? MemoryRegion()
Roland Levillaina552e1c2015-03-26 15:01:03 +00001087 : region_.Subregion(GetStackMapsOffset(), GetStackMapsSize());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001088 }
1089
Roland Levillaina552e1c2015-03-26 15:01:03 +00001090 // Compute the size of the Dex register map associated to the stack map at
1091 // `dex_register_map_offset_in_code_info`.
1092 size_t ComputeDexRegisterMapSizeOf(uint32_t dex_register_map_offset_in_code_info,
1093 uint16_t number_of_dex_registers) const {
1094 // Offset where the actual mapping data starts within art::DexRegisterMap.
1095 size_t location_mapping_data_offset_in_dex_register_map =
1096 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1097 // Create a temporary art::DexRegisterMap to be able to call
1098 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1099 DexRegisterMap dex_register_map_without_locations(
1100 MemoryRegion(region_.Subregion(dex_register_map_offset_in_code_info,
1101 location_mapping_data_offset_in_dex_register_map)));
1102 size_t number_of_live_dex_registers =
1103 dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
1104 size_t location_mapping_data_size_in_bits =
1105 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfDexRegisterLocationCatalogEntries())
1106 * number_of_live_dex_registers;
1107 size_t location_mapping_data_size_in_bytes =
1108 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1109 size_t dex_register_map_size =
1110 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1111 return dex_register_map_size;
1112 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001113
Roland Levillaina552e1c2015-03-26 15:01:03 +00001114 // Compute the size of a Dex register location catalog starting at offset `origin`
1115 // in `region_` and containing `number_of_dex_locations` entries.
1116 size_t ComputeDexRegisterLocationCatalogSize(uint32_t origin,
1117 uint32_t number_of_dex_locations) const {
1118 // TODO: Ideally, we would like to use art::DexRegisterLocationCatalog::Size or
1119 // art::DexRegisterLocationCatalog::FindLocationOffset, but the
1120 // DexRegisterLocationCatalog is not yet built. Try to factor common code.
1121 size_t offset = origin + DexRegisterLocationCatalog::kFixedSize;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001122
Roland Levillaina552e1c2015-03-26 15:01:03 +00001123 // Skip the first `number_of_dex_locations - 1` entries.
1124 for (uint16_t i = 0; i < number_of_dex_locations; ++i) {
1125 // Read the first next byte and inspect its first 3 bits to decide
1126 // whether it is a short or a large location.
1127 DexRegisterLocationCatalog::ShortLocation first_byte =
1128 region_.LoadUnaligned<DexRegisterLocationCatalog::ShortLocation>(offset);
1129 DexRegisterLocation::Kind kind =
1130 DexRegisterLocationCatalog::ExtractKindFromShortLocation(first_byte);
1131 if (DexRegisterLocation::IsShortLocationKind(kind)) {
1132 // Short location. Skip the current byte.
1133 offset += DexRegisterLocationCatalog::SingleShortEntrySize();
1134 } else {
1135 // Large location. Skip the 5 next bytes.
1136 offset += DexRegisterLocationCatalog::SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001137 }
1138 }
1139 size_t size = offset - origin;
1140 return size;
1141 }
1142
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001143 MemoryRegion region_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001144 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001145};
1146
1147} // namespace art
1148
1149#endif // ART_RUNTIME_STACK_MAP_H_