blob: eefdaa7391e75ade69b853337cc58204bfd5b2e9 [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);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100674 for (size_t i = 0; i < region.size_in_bits(); i++) {
675 region.StoreBit(i, sp_map.IsBitSet(i));
676 }
677 }
678
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000679 bool HasDexRegisterMap(const CodeInfo& info) const {
680 return GetDexRegisterMapOffset(info) != kNoDexRegisterMap;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100681 }
682
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000683 bool HasInlineInfo(const CodeInfo& info) const {
684 return GetInlineDescriptorOffset(info) != kNoInlineInfo;
Roland Levillain442b46a2015-02-18 16:54:21 +0000685 }
686
687 bool Equals(const StackMap& other) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100688 return region_.pointer() == other.region_.pointer()
689 && region_.size() == other.region_.size();
690 }
691
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000692 static size_t ComputeStackMapSize(size_t stack_mask_size,
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000693 size_t inline_info_size,
694 size_t dex_register_map_size,
695 size_t dex_pc_max,
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100696 size_t native_pc_max,
697 size_t register_mask_max);
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000698
Roland Levillain442b46a2015-02-18 16:54:21 +0000699 // Special (invalid) offset for the DexRegisterMapOffset field meaning
700 // that there is no Dex register map for this stack map.
701 static constexpr uint32_t kNoDexRegisterMap = -1;
702
703 // Special (invalid) offset for the InlineDescriptorOffset field meaning
704 // that there is no inline info for this stack map.
705 static constexpr uint32_t kNoInlineInfo = -1;
706
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100707 private:
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100708 static size_t ComputeStackMapSizeInternal(size_t stack_mask_size,
709 size_t number_of_bytes_for_inline_info,
710 size_t number_of_bytes_for_dex_map,
711 size_t number_of_bytes_for_dex_pc,
712 size_t number_of_bytes_for_native_pc,
713 size_t number_of_bytes_for_register_mask);
714
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000715 // TODO: Instead of plain types such as "uint32_t", introduce
716 // typedefs (and document the memory layout of StackMap).
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000717 static constexpr int kRegisterMaskOffset = 0;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100718 static constexpr int kFixedSize = 0;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100719
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100720 MemoryRegion region_;
721
Nicolas Geoffray39468442014-09-02 15:17:15 +0100722 friend class CodeInfo;
723 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100724};
725
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100726/**
727 * Inline information for a specific PC. The information is of the form:
728 * [inlining_depth, [dex_pc, method_index, dex_register_map_offset]+]
729 */
730class InlineInfo {
731 public:
732 explicit InlineInfo(MemoryRegion region) : region_(region) {}
733
734 uint8_t GetDepth() const {
735 return region_.LoadUnaligned<uint8_t>(kDepthOffset);
736 }
737
738 void SetDepth(uint8_t depth) {
739 region_.StoreUnaligned<uint8_t>(kDepthOffset, depth);
740 }
741
742 uint32_t GetMethodIndexAtDepth(uint8_t depth) const {
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100743 return region_.LoadUnaligned<uint32_t>(
744 kFixedSize + depth * SingleEntrySize() + kMethodIndexOffset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100745 }
746
747 void SetMethodIndexAtDepth(uint8_t depth, uint32_t index) {
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100748 region_.StoreUnaligned<uint32_t>(
749 kFixedSize + depth * SingleEntrySize() + kMethodIndexOffset, index);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100750 }
751
752 uint32_t GetDexPcAtDepth(uint8_t depth) const {
753 return region_.LoadUnaligned<uint32_t>(
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100754 kFixedSize + depth * SingleEntrySize() + kDexPcOffset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100755 }
756
757 void SetDexPcAtDepth(uint8_t depth, uint32_t dex_pc) {
758 region_.StoreUnaligned<uint32_t>(
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100759 kFixedSize + depth * SingleEntrySize() + kDexPcOffset, dex_pc);
760 }
761
762 uint8_t GetInvokeTypeAtDepth(uint8_t depth) const {
763 return region_.LoadUnaligned<uint8_t>(
764 kFixedSize + depth * SingleEntrySize() + kInvokeTypeOffset);
765 }
766
767 void SetInvokeTypeAtDepth(uint8_t depth, uint8_t invoke_type) {
768 region_.StoreUnaligned<uint8_t>(
769 kFixedSize + depth * SingleEntrySize() + kInvokeTypeOffset, invoke_type);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100770 }
771
772 uint32_t GetDexRegisterMapOffsetAtDepth(uint8_t depth) const {
773 return region_.LoadUnaligned<uint32_t>(
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100774 kFixedSize + depth * SingleEntrySize() + kDexRegisterMapOffset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100775 }
776
777 void SetDexRegisterMapOffsetAtDepth(uint8_t depth, uint32_t offset) {
778 region_.StoreUnaligned<uint32_t>(
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100779 kFixedSize + depth * SingleEntrySize() + kDexRegisterMapOffset, offset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100780 }
781
782 bool HasDexRegisterMapAtDepth(uint8_t depth) const {
783 return GetDexRegisterMapOffsetAtDepth(depth) != StackMap::kNoDexRegisterMap;
784 }
785
786 static size_t SingleEntrySize() {
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100787 return kFixedEntrySize;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100788 }
789
790 void Dump(std::ostream& os, const CodeInfo& info, uint16_t* number_of_dex_registers) const;
791
792 private:
793 // TODO: Instead of plain types such as "uint8_t", introduce
794 // typedefs (and document the memory layout of InlineInfo).
795 static constexpr int kDepthOffset = 0;
796 static constexpr int kFixedSize = kDepthOffset + sizeof(uint8_t);
797
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100798 static constexpr int kMethodIndexOffset = 0;
799 static constexpr int kDexPcOffset = kMethodIndexOffset + sizeof(uint32_t);
800 static constexpr int kInvokeTypeOffset = kDexPcOffset + sizeof(uint32_t);
801 static constexpr int kDexRegisterMapOffset = kInvokeTypeOffset + sizeof(uint8_t);
802 static constexpr int kFixedEntrySize = kDexRegisterMapOffset + sizeof(uint32_t);
803
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100804 MemoryRegion region_;
805
806 friend class CodeInfo;
807 friend class StackMap;
808 friend class StackMapStream;
809};
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100810
811/**
812 * Wrapper around all compiler information collected for a method.
813 * The information is of the form:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000814 * [overall_size, number_of_location_catalog_entries, number_of_stack_maps, stack_mask_size,
815 * DexRegisterLocationCatalog+, StackMap+, DexRegisterMap+, InlineInfo*].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100816 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100817class CodeInfo {
818 public:
819 explicit CodeInfo(MemoryRegion region) : region_(region) {}
820
Nicolas Geoffray39468442014-09-02 15:17:15 +0100821 explicit CodeInfo(const void* data) {
822 uint32_t size = reinterpret_cast<const uint32_t*>(data)[0];
823 region_ = MemoryRegion(const_cast<void*>(data), size);
824 }
825
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100826 static size_t EncodingSizeInBytes(size_t max_element) {
827 DCHECK(IsUint<32>(max_element));
828 return (max_element == 0) ? 0
829 : IsUint<8>(max_element) ? 1
830 : IsUint<16>(max_element) ? 2
831 : IsUint<24>(max_element) ? 3
832 : 4;
833 }
834
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000835 void SetEncoding(size_t inline_info_size,
836 size_t dex_register_map_size,
837 size_t dex_pc_max,
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100838 size_t native_pc_max,
839 size_t register_mask_max) {
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000840 if (inline_info_size != 0) {
841 region_.StoreBit(kHasInlineInfoBitOffset, 1);
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100842 // + 1 to also encode kNoInlineInfo: if an inline info offset
843 // is at 0xFF, we want to overflow to a larger encoding, because it will
844 // conflict with kNoInlineInfo.
845 // The offset is relative to the dex register map. TODO: Change this.
846 SetEncodingAt(kInlineInfoBitOffset,
847 EncodingSizeInBytes(dex_register_map_size + inline_info_size + 1));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000848 } else {
849 region_.StoreBit(kHasInlineInfoBitOffset, 0);
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100850 SetEncodingAt(kInlineInfoBitOffset, 0);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000851 }
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100852 // + 1 to also encode kNoDexRegisterMap: if a dex register map offset
853 // is at 0xFF, we want to overflow to a larger encoding, because it will
854 // conflict with kNoDexRegisterMap.
855 SetEncodingAt(kDexRegisterMapBitOffset, EncodingSizeInBytes(dex_register_map_size + 1));
856 SetEncodingAt(kDexPcBitOffset, EncodingSizeInBytes(dex_pc_max));
857 SetEncodingAt(kNativePcBitOffset, EncodingSizeInBytes(native_pc_max));
858 SetEncodingAt(kRegisterMaskBitOffset, EncodingSizeInBytes(register_mask_max));
859 }
860
861 void SetEncodingAt(size_t bit_offset, size_t number_of_bytes) {
862 // We encode the number of bytes needed for writing a value on 3 bits,
863 // for values that we know are maximum 32bits.
864 region_.StoreBit(bit_offset, (number_of_bytes & 1));
865 region_.StoreBit(bit_offset + 1, (number_of_bytes & 2));
866 region_.StoreBit(bit_offset + 2, (number_of_bytes & 4));
867 }
868
869 size_t GetNumberOfBytesForEncoding(size_t bit_offset) const {
870 return region_.LoadBit(bit_offset)
871 + (region_.LoadBit(bit_offset + 1) << 1)
872 + (region_.LoadBit(bit_offset + 2) << 2);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000873 }
874
875 bool HasInlineInfo() const {
876 return region_.LoadBit(kHasInlineInfoBitOffset);
877 }
878
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100879 size_t NumberOfBytesForInlineInfo() const {
880 return GetNumberOfBytesForEncoding(kInlineInfoBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000881 }
882
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100883 size_t NumberOfBytesForDexRegisterMap() const {
884 return GetNumberOfBytesForEncoding(kDexRegisterMapBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000885 }
886
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100887 size_t NumberOfBytesForRegisterMask() const {
888 return GetNumberOfBytesForEncoding(kRegisterMaskBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000889 }
890
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100891 size_t NumberOfBytesForNativePc() const {
892 return GetNumberOfBytesForEncoding(kNativePcBitOffset);
893 }
894
895 size_t NumberOfBytesForDexPc() const {
896 return GetNumberOfBytesForEncoding(kDexPcBitOffset);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000897 }
898
899 size_t ComputeStackMapRegisterMaskOffset() const {
900 return StackMap::kRegisterMaskOffset;
901 }
902
903 size_t ComputeStackMapStackMaskOffset() const {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100904 return ComputeStackMapRegisterMaskOffset()
905 + (NumberOfBytesForRegisterMask() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000906 }
907
908 size_t ComputeStackMapDexPcOffset() const {
909 return ComputeStackMapStackMaskOffset() + GetStackMaskSize();
910 }
911
912 size_t ComputeStackMapNativePcOffset() const {
913 return ComputeStackMapDexPcOffset()
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100914 + (NumberOfBytesForDexPc() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000915 }
916
917 size_t ComputeStackMapDexRegisterMapOffset() const {
918 return ComputeStackMapNativePcOffset()
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100919 + (NumberOfBytesForNativePc() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000920 }
921
922 size_t ComputeStackMapInlineInfoOffset() const {
923 CHECK(HasInlineInfo());
924 return ComputeStackMapDexRegisterMapOffset()
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100925 + (NumberOfBytesForDexRegisterMap() * sizeof(uint8_t));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000926 }
927
Roland Levillaina552e1c2015-03-26 15:01:03 +0000928 DexRegisterLocationCatalog GetDexRegisterLocationCatalog() const {
929 return DexRegisterLocationCatalog(region_.Subregion(
930 GetDexRegisterLocationCatalogOffset(),
931 GetDexRegisterLocationCatalogSize()));
932 }
933
Nicolas Geoffray39468442014-09-02 15:17:15 +0100934 StackMap GetStackMapAt(size_t i) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100935 size_t size = StackMapSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100936 return StackMap(GetStackMaps().Subregion(i * size, size));
937 }
938
939 uint32_t GetOverallSize() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000940 return region_.LoadUnaligned<uint32_t>(kOverallSizeOffset);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100941 }
942
943 void SetOverallSize(uint32_t size) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000944 region_.StoreUnaligned<uint32_t>(kOverallSizeOffset, size);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100945 }
946
Roland Levillaina552e1c2015-03-26 15:01:03 +0000947 uint32_t GetNumberOfDexRegisterLocationCatalogEntries() const {
948 return region_.LoadUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset);
949 }
950
951 void SetNumberOfDexRegisterLocationCatalogEntries(uint32_t num_entries) {
952 region_.StoreUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset, num_entries);
953 }
954
955 uint32_t GetDexRegisterLocationCatalogSize() const {
956 return ComputeDexRegisterLocationCatalogSize(GetDexRegisterLocationCatalogOffset(),
957 GetNumberOfDexRegisterLocationCatalogEntries());
958 }
959
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100960 uint32_t GetStackMaskSize() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000961 return region_.LoadUnaligned<uint32_t>(kStackMaskSizeOffset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100962 }
963
964 void SetStackMaskSize(uint32_t size) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000965 region_.StoreUnaligned<uint32_t>(kStackMaskSizeOffset, size);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100966 }
967
968 size_t GetNumberOfStackMaps() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000969 return region_.LoadUnaligned<uint32_t>(kNumberOfStackMapsOffset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100970 }
971
972 void SetNumberOfStackMaps(uint32_t number_of_stack_maps) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000973 region_.StoreUnaligned<uint32_t>(kNumberOfStackMapsOffset, number_of_stack_maps);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100974 }
975
Roland Levillain29ba1b02015-03-13 11:45:07 +0000976 // Get the size of one stack map of this CodeInfo object, in bytes.
977 // All stack maps of a CodeInfo have the same size.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100978 size_t StackMapSize() const {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100979 return StackMap::ComputeStackMapSizeInternal(GetStackMaskSize(),
980 NumberOfBytesForInlineInfo(),
981 NumberOfBytesForDexRegisterMap(),
982 NumberOfBytesForDexPc(),
983 NumberOfBytesForNativePc(),
984 NumberOfBytesForRegisterMask());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100985 }
986
Roland Levillain29ba1b02015-03-13 11:45:07 +0000987 // Get the size all the stack maps of this CodeInfo object, in bytes.
Roland Levillaina552e1c2015-03-26 15:01:03 +0000988 size_t GetStackMapsSize() const {
Roland Levillain29ba1b02015-03-13 11:45:07 +0000989 return StackMapSize() * GetNumberOfStackMaps();
990 }
991
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100992 uint32_t GetDexRegisterLocationCatalogOffset() const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000993 return GetStackMapsOffset() + GetStackMapsSize();
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000994 }
995
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100996 size_t GetDexRegisterMapsOffset() const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000997 return GetDexRegisterLocationCatalogOffset() + GetDexRegisterLocationCatalogSize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000998 }
999
Nicolas Geoffray6530baf2015-05-26 15:22:58 +01001000 uint32_t GetStackMapsOffset() const {
1001 return kFixedSize;
1002 }
1003
Roland Levillain442b46a2015-02-18 16:54:21 +00001004 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map, uint32_t number_of_dex_registers) const {
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001005 DCHECK(stack_map.HasDexRegisterMap(*this));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001006 uint32_t offset = GetDexRegisterMapsOffset() + stack_map.GetDexRegisterMapOffset(*this);
1007 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001008 return DexRegisterMap(region_.Subregion(offset, size));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001009 }
1010
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001011 // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`.
1012 DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
1013 InlineInfo inline_info,
1014 uint32_t number_of_dex_registers) const {
1015 DCHECK(inline_info.HasDexRegisterMapAtDepth(depth));
1016 uint32_t offset =
1017 GetDexRegisterMapsOffset() + inline_info.GetDexRegisterMapOffsetAtDepth(depth);
1018 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
1019 return DexRegisterMap(region_.Subregion(offset, size));
1020 }
1021
Roland Levillain442b46a2015-02-18 16:54:21 +00001022 InlineInfo GetInlineInfoOf(StackMap stack_map) const {
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001023 DCHECK(stack_map.HasInlineInfo(*this));
1024 uint32_t offset = stack_map.GetInlineDescriptorOffset(*this) + GetDexRegisterMapsOffset();
1025 uint8_t depth = region_.LoadUnaligned<uint8_t>(offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001026 return InlineInfo(region_.Subregion(offset,
1027 InlineInfo::kFixedSize + depth * InlineInfo::SingleEntrySize()));
1028 }
1029
Roland Levillain442b46a2015-02-18 16:54:21 +00001030 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001031 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001032 StackMap stack_map = GetStackMapAt(i);
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001033 if (stack_map.GetDexPc(*this) == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001034 return stack_map;
1035 }
1036 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001037 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001038 }
1039
Roland Levillain442b46a2015-02-18 16:54:21 +00001040 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001041 // TODO: stack maps are sorted by native pc, we can do a binary search.
1042 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001043 StackMap stack_map = GetStackMapAt(i);
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001044 if (stack_map.GetNativePcOffset(*this) == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001045 return stack_map;
1046 }
1047 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001048 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001049 }
1050
Roland Levillain0396ed72015-05-27 15:12:19 +01001051 // Dump this CodeInfo object on `os`. If `dump_stack_maps` is true,
1052 // also dump the stack maps and the associated Dex register maps.
1053 void Dump(std::ostream& os, uint16_t number_of_dex_registers, bool dump_stack_maps) const;
1054
1055 // Dump stack map number `stack_map_num` as well as associated data on `os`,
1056 // such as Dex register locations.
1057 void DumpStackMap(std::ostream& os, size_t stack_map_num, uint16_t number_of_dex_registers) const;
1058 // Dump the header of stack map number `stack_map_num` on `os`, without
1059 // associated data.
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001060 void DumpStackMapHeader(std::ostream& os, size_t stack_map_num) const;
1061
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001062 private:
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001063 // TODO: Instead of plain types such as "uint32_t", introduce
1064 // typedefs (and document the memory layout of CodeInfo).
Nicolas Geoffray39468442014-09-02 15:17:15 +01001065 static constexpr int kOverallSizeOffset = 0;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001066 static constexpr int kEncodingInfoOffset = kOverallSizeOffset + sizeof(uint32_t);
Roland Levillaina552e1c2015-03-26 15:01:03 +00001067 static constexpr int kNumberOfDexRegisterLocationCatalogEntriesOffset =
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001068 kEncodingInfoOffset + sizeof(uint16_t);
Roland Levillaina552e1c2015-03-26 15:01:03 +00001069 static constexpr int kNumberOfStackMapsOffset =
1070 kNumberOfDexRegisterLocationCatalogEntriesOffset + sizeof(uint32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001071 static constexpr int kStackMaskSizeOffset = kNumberOfStackMapsOffset + sizeof(uint32_t);
1072 static constexpr int kFixedSize = kStackMaskSizeOffset + sizeof(uint32_t);
1073
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001074 static constexpr int kHasInlineInfoBitOffset = (kEncodingInfoOffset * kBitsPerByte);
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001075 static constexpr int kInlineInfoBitOffset = kHasInlineInfoBitOffset + 1;
1076 static constexpr int kDexRegisterMapBitOffset = kInlineInfoBitOffset + 3;
1077 static constexpr int kDexPcBitOffset = kDexRegisterMapBitOffset + 3;
1078 static constexpr int kNativePcBitOffset = kDexPcBitOffset + 3;
1079 static constexpr int kRegisterMaskBitOffset = kNativePcBitOffset + 3;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001080
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001081 MemoryRegion GetStackMaps() const {
1082 return region_.size() == 0
1083 ? MemoryRegion()
Roland Levillaina552e1c2015-03-26 15:01:03 +00001084 : region_.Subregion(GetStackMapsOffset(), GetStackMapsSize());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001085 }
1086
Roland Levillaina552e1c2015-03-26 15:01:03 +00001087 // Compute the size of the Dex register map associated to the stack map at
1088 // `dex_register_map_offset_in_code_info`.
1089 size_t ComputeDexRegisterMapSizeOf(uint32_t dex_register_map_offset_in_code_info,
1090 uint16_t number_of_dex_registers) const {
1091 // Offset where the actual mapping data starts within art::DexRegisterMap.
1092 size_t location_mapping_data_offset_in_dex_register_map =
1093 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1094 // Create a temporary art::DexRegisterMap to be able to call
1095 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1096 DexRegisterMap dex_register_map_without_locations(
1097 MemoryRegion(region_.Subregion(dex_register_map_offset_in_code_info,
1098 location_mapping_data_offset_in_dex_register_map)));
1099 size_t number_of_live_dex_registers =
1100 dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
1101 size_t location_mapping_data_size_in_bits =
1102 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfDexRegisterLocationCatalogEntries())
1103 * number_of_live_dex_registers;
1104 size_t location_mapping_data_size_in_bytes =
1105 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1106 size_t dex_register_map_size =
1107 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1108 return dex_register_map_size;
1109 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001110
Roland Levillaina552e1c2015-03-26 15:01:03 +00001111 // Compute the size of a Dex register location catalog starting at offset `origin`
1112 // in `region_` and containing `number_of_dex_locations` entries.
1113 size_t ComputeDexRegisterLocationCatalogSize(uint32_t origin,
1114 uint32_t number_of_dex_locations) const {
1115 // TODO: Ideally, we would like to use art::DexRegisterLocationCatalog::Size or
1116 // art::DexRegisterLocationCatalog::FindLocationOffset, but the
1117 // DexRegisterLocationCatalog is not yet built. Try to factor common code.
1118 size_t offset = origin + DexRegisterLocationCatalog::kFixedSize;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001119
Roland Levillaina552e1c2015-03-26 15:01:03 +00001120 // Skip the first `number_of_dex_locations - 1` entries.
1121 for (uint16_t i = 0; i < number_of_dex_locations; ++i) {
1122 // Read the first next byte and inspect its first 3 bits to decide
1123 // whether it is a short or a large location.
1124 DexRegisterLocationCatalog::ShortLocation first_byte =
1125 region_.LoadUnaligned<DexRegisterLocationCatalog::ShortLocation>(offset);
1126 DexRegisterLocation::Kind kind =
1127 DexRegisterLocationCatalog::ExtractKindFromShortLocation(first_byte);
1128 if (DexRegisterLocation::IsShortLocationKind(kind)) {
1129 // Short location. Skip the current byte.
1130 offset += DexRegisterLocationCatalog::SingleShortEntrySize();
1131 } else {
1132 // Large location. Skip the 5 next bytes.
1133 offset += DexRegisterLocationCatalog::SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001134 }
1135 }
1136 size_t size = offset - origin;
1137 return size;
1138 }
1139
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001140 MemoryRegion region_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001141 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001142};
1143
1144} // namespace art
1145
1146#endif // ART_RUNTIME_STACK_MAP_H_