blob: ab7f926d39eac01a7be81e2c5f65492736aa3da6 [file] [log] [blame]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_STACK_MAP_H_
18#define ART_RUNTIME_STACK_MAP_H_
19
20#include "base/bit_vector.h"
21#include "memory_region.h"
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +000022#include "utils.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010023
24namespace art {
25
Roland Levillaina2d8ec62015-03-12 15:25:29 +000026// Size of a frame slot, in bytes. This constant is a signed value,
27// to please the compiler in arithmetic operations involving int32_t
28// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000029static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000030
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000031// Size of Dex virtual registers.
Roland Levillaina552e1c2015-03-26 15:01:03 +000032static constexpr size_t kVRegSize = 4;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000033
Nicolas Geoffray004c2302015-03-20 10:06:38 +000034class CodeInfo;
35
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010036/**
37 * Classes in the following file are wrapper on stack map information backed
38 * by a MemoryRegion. As such they read and write to the region, they don't have
39 * their own fields.
40 */
41
42/**
43 * Inline information for a specific PC. The information is of the form:
44 * [inlining_depth, [method_dex reference]+]
45 */
46class InlineInfo {
47 public:
48 explicit InlineInfo(MemoryRegion region) : region_(region) {}
49
50 uint8_t GetDepth() const {
Roland Levillainede7bf82015-03-13 12:23:04 +000051 return region_.LoadUnaligned<uint8_t>(kDepthOffset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010052 }
53
54 void SetDepth(uint8_t depth) {
Roland Levillainede7bf82015-03-13 12:23:04 +000055 region_.StoreUnaligned<uint8_t>(kDepthOffset, depth);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010056 }
57
58 uint32_t GetMethodReferenceIndexAtDepth(uint8_t depth) const {
Roland Levillainede7bf82015-03-13 12:23:04 +000059 return region_.LoadUnaligned<uint32_t>(kFixedSize + depth * SingleEntrySize());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010060 }
61
62 void SetMethodReferenceIndexAtDepth(uint8_t depth, uint32_t index) {
Roland Levillainede7bf82015-03-13 12:23:04 +000063 region_.StoreUnaligned<uint32_t>(kFixedSize + depth * SingleEntrySize(), index);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010064 }
65
66 static size_t SingleEntrySize() {
67 return sizeof(uint32_t);
68 }
69
70 private:
Roland Levillaina2d8ec62015-03-12 15:25:29 +000071 // TODO: Instead of plain types such as "uint8_t", introduce
72 // typedefs (and document the memory layout of InlineInfo).
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010073 static constexpr int kDepthOffset = 0;
74 static constexpr int kFixedSize = kDepthOffset + sizeof(uint8_t);
75
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010076 MemoryRegion region_;
77
Nicolas Geoffray39468442014-09-02 15:17:15 +010078 friend class CodeInfo;
79 friend class StackMap;
80 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010081};
82
Roland Levillaina2d8ec62015-03-12 15:25:29 +000083// Dex register location container used by DexRegisterMap and StackMapStream.
84class DexRegisterLocation {
85 public:
86 /*
87 * The location kind used to populate the Dex register information in a
88 * StackMapStream can either be:
89 * - kNone: the register has no location yet, meaning it has not been set;
90 * - kConstant: value holds the constant;
91 * - kStack: value holds the stack offset;
92 * - kRegister: value holds the physical register number;
93 * - kFpuRegister: value holds the physical register number.
94 *
95 * In addition, DexRegisterMap also uses these values:
96 * - kInStackLargeOffset: value holds a "large" stack offset (greater than
Roland Levillaina552e1c2015-03-26 15:01:03 +000097 * or equal to 128 bytes);
98 * - kConstantLargeValue: value holds a "large" constant (lower than 0, or
99 * or greater than or equal to 32).
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000100 */
101 enum class Kind : uint8_t {
102 // Short location kinds, for entries fitting on one byte (3 bits
103 // for the kind, 5 bits for the value) in a DexRegisterMap.
104 kNone = 0, // 0b000
105 kInStack = 1, // 0b001
106 kInRegister = 2, // 0b010
107 kInFpuRegister = 3, // 0b011
108 kConstant = 4, // 0b100
109
110 // Large location kinds, requiring a 5-byte encoding (1 byte for the
111 // kind, 4 bytes for the value).
112
113 // Stack location at a large offset, meaning that the offset value
114 // divided by the stack frame slot size (4 bytes) cannot fit on a
115 // 5-bit unsigned integer (i.e., this offset value is greater than
116 // or equal to 2^5 * 4 = 128 bytes).
117 kInStackLargeOffset = 5, // 0b101
118
119 // Large constant, that cannot fit on a 5-bit signed integer (i.e.,
Roland Levillaina552e1c2015-03-26 15:01:03 +0000120 // lower than 0, or greater than or equal to 2^5 = 32).
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000121 kConstantLargeValue = 6, // 0b110
122
123 kLastLocationKind = kConstantLargeValue
124 };
125
126 static_assert(
127 sizeof(Kind) == 1u,
128 "art::DexRegisterLocation::Kind has a size different from one byte.");
129
130 static const char* PrettyDescriptor(Kind kind) {
131 switch (kind) {
132 case Kind::kNone:
133 return "none";
134 case Kind::kInStack:
135 return "in stack";
136 case Kind::kInRegister:
137 return "in register";
138 case Kind::kInFpuRegister:
139 return "in fpu register";
140 case Kind::kConstant:
141 return "as constant";
142 case Kind::kInStackLargeOffset:
143 return "in stack (large offset)";
144 case Kind::kConstantLargeValue:
145 return "as constant (large value)";
146 default:
147 UNREACHABLE();
148 }
149 }
150
151 static bool IsShortLocationKind(Kind kind) {
152 switch (kind) {
153 case Kind::kNone:
154 case Kind::kInStack:
155 case Kind::kInRegister:
156 case Kind::kInFpuRegister:
157 case Kind::kConstant:
158 return true;
159
160 case Kind::kInStackLargeOffset:
161 case Kind::kConstantLargeValue:
162 return false;
163
164 default:
165 UNREACHABLE();
166 }
167 }
168
169 // Convert `kind` to a "surface" kind, i.e. one that doesn't include
170 // any value with a "large" qualifier.
171 // TODO: Introduce another enum type for the surface kind?
172 static Kind ConvertToSurfaceKind(Kind kind) {
173 switch (kind) {
174 case Kind::kNone:
175 case Kind::kInStack:
176 case Kind::kInRegister:
177 case Kind::kInFpuRegister:
178 case Kind::kConstant:
179 return kind;
180
181 case Kind::kInStackLargeOffset:
182 return Kind::kInStack;
183
184 case Kind::kConstantLargeValue:
185 return Kind::kConstant;
186
187 default:
188 UNREACHABLE();
189 }
190 }
191
Roland Levillaina552e1c2015-03-26 15:01:03 +0000192 // Required by art::StackMapStream::LocationCatalogEntriesIndices.
193 DexRegisterLocation() : kind_(Kind::kNone), value_(0) {}
194
195 DexRegisterLocation(Kind kind, int32_t value) : kind_(kind), value_(value) {}
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000196
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000197 static DexRegisterLocation None() {
198 return DexRegisterLocation(Kind::kNone, 0);
199 }
200
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000201 // Get the "surface" kind of the location, i.e., the one that doesn't
202 // include any value with a "large" qualifier.
203 Kind GetKind() const {
204 return ConvertToSurfaceKind(kind_);
205 }
206
207 // Get the value of the location.
208 int32_t GetValue() const { return value_; }
209
210 // Get the actual kind of the location.
211 Kind GetInternalKind() const { return kind_; }
212
Calin Juravle6ae70962015-03-18 16:31:28 +0000213 bool operator==(DexRegisterLocation other) const {
214 return kind_ == other.kind_ && value_ == other.value_;
215 }
216
217 bool operator!=(DexRegisterLocation other) const {
218 return !(*this == other);
219 }
220
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000221 private:
222 Kind kind_;
223 int32_t value_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000224
225 friend class DexRegisterLocationHashFn;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000226};
227
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100228/**
Roland Levillaina552e1c2015-03-26 15:01:03 +0000229 * Store information on unique Dex register locations used in a method.
230 * The information is of the form:
231 * [DexRegisterLocation+].
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000232 * DexRegisterLocations are either 1- or 5-byte wide (see art::DexRegisterLocation::Kind).
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100233 */
Roland Levillaina552e1c2015-03-26 15:01:03 +0000234class DexRegisterLocationCatalog {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100235 public:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000236 explicit DexRegisterLocationCatalog(MemoryRegion region) : region_(region) {}
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100237
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000238 // Short (compressed) location, fitting on one byte.
239 typedef uint8_t ShortLocation;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100240
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000241 void SetRegisterInfo(size_t offset, const DexRegisterLocation& dex_register_location) {
242 DexRegisterLocation::Kind kind = ComputeCompressedKind(dex_register_location);
243 int32_t value = dex_register_location.GetValue();
244 if (DexRegisterLocation::IsShortLocationKind(kind)) {
245 // Short location. Compress the kind and the value as a single byte.
246 if (kind == DexRegisterLocation::Kind::kInStack) {
247 // Instead of storing stack offsets expressed in bytes for
248 // short stack locations, store slot offsets. A stack offset
249 // is a multiple of 4 (kFrameSlotSize). This means that by
250 // dividing it by 4, we can fit values from the [0, 128)
251 // interval in a short stack location, and not just values
252 // from the [0, 32) interval.
253 DCHECK_EQ(value % kFrameSlotSize, 0);
254 value /= kFrameSlotSize;
255 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000256 DCHECK(IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000257 region_.StoreUnaligned<ShortLocation>(offset, MakeShortLocation(kind, value));
258 } else {
259 // Large location. Write the location on one byte and the value
260 // on 4 bytes.
Roland Levillaina552e1c2015-03-26 15:01:03 +0000261 DCHECK(!IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000262 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
263 // Also divide large stack offsets by 4 for the sake of consistency.
264 DCHECK_EQ(value % kFrameSlotSize, 0);
265 value /= kFrameSlotSize;
266 }
267 // Data can be unaligned as the written Dex register locations can
268 // either be 1-byte or 5-byte wide. Use
269 // art::MemoryRegion::StoreUnaligned instead of
270 // art::MemoryRegion::Store to prevent unligned word accesses on ARM.
271 region_.StoreUnaligned<DexRegisterLocation::Kind>(offset, kind);
272 region_.StoreUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind), value);
Roland Levillain442b46a2015-02-18 16:54:21 +0000273 }
274 }
275
Roland Levillaina552e1c2015-03-26 15:01:03 +0000276 // Find the offset of the location catalog entry number `location_catalog_entry_index`.
277 size_t FindLocationOffset(size_t location_catalog_entry_index) const {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000278 size_t offset = kFixedSize;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000279 // Skip the first `location_catalog_entry_index - 1` entries.
280 for (uint16_t i = 0; i < location_catalog_entry_index; ++i) {
281 // Read the first next byte and inspect its first 3 bits to decide
282 // whether it is a short or a large location.
283 DexRegisterLocation::Kind kind = ExtractKindAtOffset(offset);
284 if (DexRegisterLocation::IsShortLocationKind(kind)) {
285 // Short location. Skip the current byte.
286 offset += SingleShortEntrySize();
287 } else {
288 // Large location. Skip the 5 next bytes.
289 offset += SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000290 }
291 }
292 return offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100293 }
294
Roland Levillaina552e1c2015-03-26 15:01:03 +0000295 // Get the internal kind of entry at `location_catalog_entry_index`.
296 DexRegisterLocation::Kind GetLocationInternalKind(size_t location_catalog_entry_index) const {
297 if (location_catalog_entry_index == kNoLocationEntryIndex) {
298 return DexRegisterLocation::Kind::kNone;
299 }
300 return ExtractKindAtOffset(FindLocationOffset(location_catalog_entry_index));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100301 }
302
Roland Levillaina552e1c2015-03-26 15:01:03 +0000303 // Get the (surface) kind and value of entry at `location_catalog_entry_index`.
304 DexRegisterLocation GetDexRegisterLocation(size_t location_catalog_entry_index) const {
305 if (location_catalog_entry_index == kNoLocationEntryIndex) {
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000306 return DexRegisterLocation::None();
307 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000308 size_t offset = FindLocationOffset(location_catalog_entry_index);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000309 // Read the first byte and inspect its first 3 bits to get the location.
310 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
311 DexRegisterLocation::Kind kind = ExtractKindFromShortLocation(first_byte);
312 if (DexRegisterLocation::IsShortLocationKind(kind)) {
313 // Short location. Extract the value from the remaining 5 bits.
314 int32_t value = ExtractValueFromShortLocation(first_byte);
315 if (kind == DexRegisterLocation::Kind::kInStack) {
316 // Convert the stack slot (short) offset to a byte offset value.
317 value *= kFrameSlotSize;
318 }
319 return DexRegisterLocation(kind, value);
320 } else {
321 // Large location. Read the four next bytes to get the value.
322 int32_t value = region_.LoadUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind));
323 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
324 // Convert the stack slot (large) offset to a byte offset value.
325 value *= kFrameSlotSize;
326 }
327 return DexRegisterLocation(kind, value);
328 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100329 }
330
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000331 // Compute the compressed kind of `location`.
332 static DexRegisterLocation::Kind ComputeCompressedKind(const DexRegisterLocation& location) {
333 switch (location.GetInternalKind()) {
334 case DexRegisterLocation::Kind::kNone:
335 DCHECK_EQ(location.GetValue(), 0);
336 return DexRegisterLocation::Kind::kNone;
337
338 case DexRegisterLocation::Kind::kInRegister:
339 DCHECK_GE(location.GetValue(), 0);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000340 DCHECK_LT(location.GetValue(), 1 << kValueBits);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000341 return DexRegisterLocation::Kind::kInRegister;
342
343 case DexRegisterLocation::Kind::kInFpuRegister:
344 DCHECK_GE(location.GetValue(), 0);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000345 DCHECK_LT(location.GetValue(), 1 << kValueBits);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000346 return DexRegisterLocation::Kind::kInFpuRegister;
347
348 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000349 return IsShortStackOffsetValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000350 ? DexRegisterLocation::Kind::kInStack
351 : DexRegisterLocation::Kind::kInStackLargeOffset;
352
353 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000354 return IsShortConstantValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000355 ? DexRegisterLocation::Kind::kConstant
356 : DexRegisterLocation::Kind::kConstantLargeValue;
357
358 default:
359 LOG(FATAL) << "Unexpected location kind"
360 << DexRegisterLocation::PrettyDescriptor(location.GetInternalKind());
361 UNREACHABLE();
362 }
363 }
364
365 // Can `location` be turned into a short location?
366 static bool CanBeEncodedAsShortLocation(const DexRegisterLocation& location) {
367 switch (location.GetInternalKind()) {
368 case DexRegisterLocation::Kind::kNone:
369 case DexRegisterLocation::Kind::kInRegister:
370 case DexRegisterLocation::Kind::kInFpuRegister:
371 return true;
372
373 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000374 return IsShortStackOffsetValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000375
376 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000377 return IsShortConstantValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000378
379 default:
380 UNREACHABLE();
381 }
382 }
383
384 static size_t EntrySize(const DexRegisterLocation& location) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000385 return CanBeEncodedAsShortLocation(location) ? SingleShortEntrySize() : SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000386 }
387
388 static size_t SingleShortEntrySize() {
389 return sizeof(ShortLocation);
390 }
391
392 static size_t SingleLargeEntrySize() {
393 return sizeof(DexRegisterLocation::Kind) + sizeof(int32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100394 }
395
Roland Levillain12baf472015-03-05 12:41:42 +0000396 size_t Size() const {
397 return region_.size();
398 }
399
Roland Levillaina552e1c2015-03-26 15:01:03 +0000400 // Special (invalid) Dex register location catalog entry index meaning
401 // that there is no location for a given Dex register (i.e., it is
402 // mapped to a DexRegisterLocation::Kind::kNone location).
403 static constexpr size_t kNoLocationEntryIndex = -1;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100404
Roland Levillain12baf472015-03-05 12:41:42 +0000405 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000406 static constexpr int kFixedSize = 0;
407
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000408 // Width of the kind "field" in a short location, in bits.
409 static constexpr size_t kKindBits = 3;
410 // Width of the value "field" in a short location, in bits.
411 static constexpr size_t kValueBits = 5;
412
413 static constexpr uint8_t kKindMask = (1 << kKindBits) - 1;
414 static constexpr int32_t kValueMask = (1 << kValueBits) - 1;
415 static constexpr size_t kKindOffset = 0;
416 static constexpr size_t kValueOffset = kKindBits;
417
Roland Levillaina552e1c2015-03-26 15:01:03 +0000418 static bool IsShortStackOffsetValue(int32_t value) {
419 DCHECK_EQ(value % kFrameSlotSize, 0);
420 return IsShortValue(value / kFrameSlotSize);
421 }
422
423 static bool IsShortConstantValue(int32_t value) {
424 return IsShortValue(value);
425 }
426
427 static bool IsShortValue(int32_t value) {
428 return IsUint<kValueBits>(value);
429 }
430
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000431 static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000432 uint8_t kind_integer_value = static_cast<uint8_t>(kind);
433 DCHECK(IsUint<kKindBits>(kind_integer_value)) << kind_integer_value;
434 DCHECK(IsShortValue(value)) << value;
435 return (kind_integer_value & kKindMask) << kKindOffset
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000436 | (value & kValueMask) << kValueOffset;
437 }
438
439 static DexRegisterLocation::Kind ExtractKindFromShortLocation(ShortLocation location) {
440 uint8_t kind = (location >> kKindOffset) & kKindMask;
441 DCHECK_LE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kLastLocationKind));
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000442 // We do not encode kNone locations in the stack map.
443 DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000444 return static_cast<DexRegisterLocation::Kind>(kind);
445 }
446
447 static int32_t ExtractValueFromShortLocation(ShortLocation location) {
448 return (location >> kValueOffset) & kValueMask;
449 }
450
451 // Extract a location kind from the byte at position `offset`.
452 DexRegisterLocation::Kind ExtractKindAtOffset(size_t offset) const {
453 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
454 return ExtractKindFromShortLocation(first_byte);
455 }
456
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100457 MemoryRegion region_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000458
459 friend class CodeInfo;
460 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100461};
462
Roland Levillaina552e1c2015-03-26 15:01:03 +0000463/* Information on Dex register locations for a specific PC, mapping a
464 * stack map's Dex register to a location entry in a DexRegisterLocationCatalog.
465 * The information is of the form:
466 * [live_bit_mask, entries*]
467 * where entries are concatenated unsigned integer values encoded on a number
468 * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending
469 * on the number of entries in the Dex register location catalog
470 * (see DexRegisterMap::SingleEntrySizeInBits). The map is 1-byte aligned.
471 */
472class DexRegisterMap {
473 public:
474 explicit DexRegisterMap(MemoryRegion region) : region_(region) {}
475
476 // Get the surface kind of Dex register `dex_register_number`.
477 DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number,
478 uint16_t number_of_dex_registers,
479 const CodeInfo& code_info) const {
480 return DexRegisterLocation::ConvertToSurfaceKind(
481 GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info));
482 }
483
484 // Get the internal kind of Dex register `dex_register_number`.
485 DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number,
486 uint16_t number_of_dex_registers,
487 const CodeInfo& code_info) const;
488
489 // Get the Dex register location `dex_register_number`.
490 DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number,
491 uint16_t number_of_dex_registers,
492 const CodeInfo& code_info) const;
493
494 int32_t GetStackOffsetInBytes(uint16_t dex_register_number,
495 uint16_t number_of_dex_registers,
496 const CodeInfo& code_info) const {
497 DexRegisterLocation location =
498 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
499 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
500 // GetDexRegisterLocation returns the offset in bytes.
501 return location.GetValue();
502 }
503
504 int32_t GetConstant(uint16_t dex_register_number,
505 uint16_t number_of_dex_registers,
506 const CodeInfo& code_info) const {
507 DexRegisterLocation location =
508 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
509 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kConstant);
510 return location.GetValue();
511 }
512
513 int32_t GetMachineRegister(uint16_t dex_register_number,
514 uint16_t number_of_dex_registers,
515 const CodeInfo& code_info) const {
516 DexRegisterLocation location =
517 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
518 DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister
519 || location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister)
520 << DexRegisterLocation::PrettyDescriptor(location.GetInternalKind());
521 return location.GetValue();
522 }
523
524 // Get the index of the entry in the Dex register location catalog
525 // corresponding to `dex_register_number`.
526 size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number,
527 uint16_t number_of_dex_registers,
528 size_t number_of_location_catalog_entries) const {
529 if (!IsDexRegisterLive(dex_register_number)) {
530 return DexRegisterLocationCatalog::kNoLocationEntryIndex;
531 }
532
533 if (number_of_location_catalog_entries == 1) {
534 // We do not allocate space for location maps in the case of a
535 // single-entry location catalog, as it is useless. The only valid
536 // entry index is 0;
537 return 0;
538 }
539
540 // The bit offset of the beginning of the map locations.
541 size_t map_locations_offset_in_bits =
542 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
543 size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number);
544 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
545 // The bit size of an entry.
546 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
547 // The bit offset where `index_in_dex_register_map` is located.
548 size_t entry_offset_in_bits =
549 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
550 size_t location_catalog_entry_index =
551 region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits);
552 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
553 return location_catalog_entry_index;
554 }
555
556 // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`.
557 void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map,
558 size_t location_catalog_entry_index,
559 uint16_t number_of_dex_registers,
560 size_t number_of_location_catalog_entries) {
561 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
562 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
563
564 if (number_of_location_catalog_entries == 1) {
565 // We do not allocate space for location maps in the case of a
566 // single-entry location catalog, as it is useless.
567 return;
568 }
569
570 // The bit offset of the beginning of the map locations.
571 size_t map_locations_offset_in_bits =
572 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
573 // The bit size of an entry.
574 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
575 // The bit offset where `index_in_dex_register_map` is located.
576 size_t entry_offset_in_bits =
577 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
578 region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits);
579 }
580
581 void SetLiveBitMask(uint16_t number_of_dex_registers,
582 const BitVector& live_dex_registers_mask) {
583 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
584 for (uint16_t i = 0; i < number_of_dex_registers; ++i) {
585 region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i));
586 }
587 }
588
589 bool IsDexRegisterLive(uint16_t dex_register_number) const {
590 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
591 return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number);
592 }
593
594 size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const {
595 size_t number_of_live_dex_registers = 0;
596 for (size_t i = 0; i < number_of_dex_registers; ++i) {
597 if (IsDexRegisterLive(i)) {
598 ++number_of_live_dex_registers;
599 }
600 }
601 return number_of_live_dex_registers;
602 }
603
604 static size_t GetLiveBitMaskOffset() {
605 return kFixedSize;
606 }
607
608 // Compute the size of the live register bit mask (in bytes), for a
609 // method having `number_of_dex_registers` Dex registers.
610 static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) {
611 return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte;
612 }
613
614 static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) {
615 return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers);
616 }
617
618 size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers,
619 size_t number_of_location_catalog_entries) const {
620 size_t location_mapping_data_size_in_bits =
621 GetNumberOfLiveDexRegisters(number_of_dex_registers)
622 * SingleEntrySizeInBits(number_of_location_catalog_entries);
623 return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
624 }
625
626 // Return the size of a map entry in bits. Note that if
627 // `number_of_location_catalog_entries` equals 1, this function returns 0,
628 // which is fine, as there is no need to allocate a map for a
629 // single-entry location catalog; the only valid location catalog entry index
630 // for a live register in this case is 0 and there is no need to
631 // store it.
632 static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) {
633 // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2.
634 return number_of_location_catalog_entries == 0
635 ? 0u
636 : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries));
637 }
638
639 // Return the size of the DexRegisterMap object, in bytes.
640 size_t Size() const {
641 return region_.size();
642 }
643
644 private:
645 // Return the index in the Dex register map corresponding to the Dex
646 // register number `dex_register_number`.
647 size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const {
648 if (!IsDexRegisterLive(dex_register_number)) {
649 return kInvalidIndexInDexRegisterMap;
650 }
651 return GetNumberOfLiveDexRegisters(dex_register_number);
652 }
653
654 // Special (invalid) Dex register map entry index meaning that there
655 // is no index in the map for a given Dex register (i.e., it must
656 // have been mapped to a DexRegisterLocation::Kind::kNone location).
657 static constexpr size_t kInvalidIndexInDexRegisterMap = -1;
658
659 static constexpr int kFixedSize = 0;
660
661 MemoryRegion region_;
662
663 friend class CodeInfo;
664 friend class StackMapStream;
665};
666
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100667/**
668 * A Stack Map holds compilation information for a specific PC necessary for:
669 * - Mapping it to a dex PC,
670 * - Knowing which stack entries are objects,
671 * - Knowing which registers hold objects,
672 * - Knowing the inlining information,
673 * - Knowing the values of dex registers.
674 *
675 * The information is of the form:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000676 * [dex_pc, native_pc_offset, dex_register_map_offset, inlining_info_offset, register_mask,
677 * stack_mask].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100678 *
679 * Note that register_mask is fixed size, but stack_mask is variable size, depending on the
680 * stack size of a method.
681 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100682class StackMap {
683 public:
684 explicit StackMap(MemoryRegion region) : region_(region) {}
685
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000686 uint32_t GetDexPc(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100687
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000688 void SetDexPc(const CodeInfo& info, uint32_t dex_pc);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100689
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000690 uint32_t GetNativePcOffset(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100691
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000692 void SetNativePcOffset(const CodeInfo& info, uint32_t native_pc_offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100693
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000694 uint32_t GetDexRegisterMapOffset(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100695
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000696 void SetDexRegisterMapOffset(const CodeInfo& info, uint32_t offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100697
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000698 uint32_t GetInlineDescriptorOffset(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100699
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000700 void SetInlineDescriptorOffset(const CodeInfo& info, uint32_t offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100701
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000702 uint32_t GetRegisterMask(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100703
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000704 void SetRegisterMask(const CodeInfo& info, uint32_t mask);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100705
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000706 MemoryRegion GetStackMask(const CodeInfo& info) const;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100707
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000708 void SetStackMask(const CodeInfo& info, const BitVector& sp_map) {
709 MemoryRegion region = GetStackMask(info);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100710 for (size_t i = 0; i < region.size_in_bits(); i++) {
711 region.StoreBit(i, sp_map.IsBitSet(i));
712 }
713 }
714
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000715 bool HasDexRegisterMap(const CodeInfo& info) const {
716 return GetDexRegisterMapOffset(info) != kNoDexRegisterMap;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100717 }
718
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000719 bool HasInlineInfo(const CodeInfo& info) const {
720 return GetInlineDescriptorOffset(info) != kNoInlineInfo;
Roland Levillain442b46a2015-02-18 16:54:21 +0000721 }
722
723 bool Equals(const StackMap& other) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100724 return region_.pointer() == other.region_.pointer()
725 && region_.size() == other.region_.size();
726 }
727
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000728 static size_t ComputeStackMapSize(size_t stack_mask_size,
729 bool has_inline_info,
730 bool is_small_inline_info,
731 bool is_small_dex_map,
732 bool is_small_dex_pc,
733 bool is_small_native_pc);
734
735 static size_t ComputeStackMapSize(size_t stack_mask_size,
736 size_t inline_info_size,
737 size_t dex_register_map_size,
738 size_t dex_pc_max,
739 size_t native_pc_max);
740
741 // TODO: Revisit this abstraction if we allow 3 bytes encoding.
742 typedef uint8_t kSmallEncoding;
743 typedef uint32_t kLargeEncoding;
744 static constexpr size_t kBytesForSmallEncoding = sizeof(kSmallEncoding);
745 static constexpr size_t kBitsForSmallEncoding = kBitsPerByte * kBytesForSmallEncoding;
746 static constexpr size_t kBytesForLargeEncoding = sizeof(kLargeEncoding);
747 static constexpr size_t kBitsForLargeEncoding = kBitsPerByte * kBytesForLargeEncoding;
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000748
Roland Levillain442b46a2015-02-18 16:54:21 +0000749 // Special (invalid) offset for the DexRegisterMapOffset field meaning
750 // that there is no Dex register map for this stack map.
751 static constexpr uint32_t kNoDexRegisterMap = -1;
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000752 static constexpr uint32_t kNoDexRegisterMapSmallEncoding =
753 std::numeric_limits<kSmallEncoding>::max();
Roland Levillain442b46a2015-02-18 16:54:21 +0000754
755 // Special (invalid) offset for the InlineDescriptorOffset field meaning
756 // that there is no inline info for this stack map.
757 static constexpr uint32_t kNoInlineInfo = -1;
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000758 static constexpr uint32_t kNoInlineInfoSmallEncoding =
759 std::numeric_limits<kSmallEncoding>::max();
760
761 // Returns the number of bytes needed for an entry in the StackMap.
762 static size_t NumberOfBytesForEntry(bool small_encoding) {
763 return small_encoding ? kBytesForSmallEncoding : kBytesForLargeEncoding;
764 }
Roland Levillain442b46a2015-02-18 16:54:21 +0000765
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100766 private:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000767 // TODO: Instead of plain types such as "uint32_t", introduce
768 // typedefs (and document the memory layout of StackMap).
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000769 static constexpr int kRegisterMaskOffset = 0;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100770 static constexpr int kFixedSize = kRegisterMaskOffset + sizeof(uint32_t);
771 static constexpr int kStackMaskOffset = kFixedSize;
772
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100773 MemoryRegion region_;
774
Nicolas Geoffray39468442014-09-02 15:17:15 +0100775 friend class CodeInfo;
776 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100777};
778
779
780/**
781 * Wrapper around all compiler information collected for a method.
782 * The information is of the form:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000783 * [overall_size, number_of_location_catalog_entries, number_of_stack_maps, stack_mask_size,
784 * DexRegisterLocationCatalog+, StackMap+, DexRegisterMap+, InlineInfo*].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100785 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100786class CodeInfo {
787 public:
788 explicit CodeInfo(MemoryRegion region) : region_(region) {}
789
Nicolas Geoffray39468442014-09-02 15:17:15 +0100790 explicit CodeInfo(const void* data) {
791 uint32_t size = reinterpret_cast<const uint32_t*>(data)[0];
792 region_ = MemoryRegion(const_cast<void*>(data), size);
793 }
794
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000795 void SetEncoding(size_t inline_info_size,
796 size_t dex_register_map_size,
797 size_t dex_pc_max,
798 size_t native_pc_max) {
799 if (inline_info_size != 0) {
800 region_.StoreBit(kHasInlineInfoBitOffset, 1);
801 region_.StoreBit(kHasSmallInlineInfoBitOffset, IsUint<StackMap::kBitsForSmallEncoding>(
802 // + 1 to also encode kNoInlineInfo: if an inline info offset
803 // is at 0xFF, we want to overflow to a larger encoding, because it will
804 // conflict with kNoInlineInfo.
805 // The offset is relative to the dex register map. TODO: Change this.
806 inline_info_size + dex_register_map_size + 1));
807 } else {
808 region_.StoreBit(kHasInlineInfoBitOffset, 0);
809 region_.StoreBit(kHasSmallInlineInfoBitOffset, 0);
810 }
811 region_.StoreBit(kHasSmallDexRegisterMapBitOffset,
812 // + 1 to also encode kNoDexRegisterMap: if a dex register map offset
813 // is at 0xFF, we want to overflow to a larger encoding, because it will
814 // conflict with kNoDexRegisterMap.
815 IsUint<StackMap::kBitsForSmallEncoding>(dex_register_map_size + 1));
816 region_.StoreBit(kHasSmallDexPcBitOffset, IsUint<StackMap::kBitsForSmallEncoding>(dex_pc_max));
817 region_.StoreBit(kHasSmallNativePcBitOffset,
818 IsUint<StackMap::kBitsForSmallEncoding>(native_pc_max));
819 }
820
821 bool HasInlineInfo() const {
822 return region_.LoadBit(kHasInlineInfoBitOffset);
823 }
824
825 bool HasSmallInlineInfo() const {
826 return region_.LoadBit(kHasSmallInlineInfoBitOffset);
827 }
828
829 bool HasSmallDexRegisterMap() const {
830 return region_.LoadBit(kHasSmallDexRegisterMapBitOffset);
831 }
832
833 bool HasSmallNativePc() const {
834 return region_.LoadBit(kHasSmallNativePcBitOffset);
835 }
836
837 bool HasSmallDexPc() const {
838 return region_.LoadBit(kHasSmallDexPcBitOffset);
839 }
840
841 size_t ComputeStackMapRegisterMaskOffset() const {
842 return StackMap::kRegisterMaskOffset;
843 }
844
845 size_t ComputeStackMapStackMaskOffset() const {
846 return StackMap::kStackMaskOffset;
847 }
848
849 size_t ComputeStackMapDexPcOffset() const {
850 return ComputeStackMapStackMaskOffset() + GetStackMaskSize();
851 }
852
853 size_t ComputeStackMapNativePcOffset() const {
854 return ComputeStackMapDexPcOffset()
855 + (HasSmallDexPc() ? sizeof(uint8_t) : sizeof(uint32_t));
856 }
857
858 size_t ComputeStackMapDexRegisterMapOffset() const {
859 return ComputeStackMapNativePcOffset()
860 + (HasSmallNativePc() ? sizeof(uint8_t) : sizeof(uint32_t));
861 }
862
863 size_t ComputeStackMapInlineInfoOffset() const {
864 CHECK(HasInlineInfo());
865 return ComputeStackMapDexRegisterMapOffset()
866 + (HasSmallDexRegisterMap() ? sizeof(uint8_t) : sizeof(uint32_t));
867 }
868
Roland Levillaina552e1c2015-03-26 15:01:03 +0000869 uint32_t GetDexRegisterLocationCatalogOffset() const {
870 return kFixedSize;
871 }
872
873 DexRegisterLocationCatalog GetDexRegisterLocationCatalog() const {
874 return DexRegisterLocationCatalog(region_.Subregion(
875 GetDexRegisterLocationCatalogOffset(),
876 GetDexRegisterLocationCatalogSize()));
877 }
878
Nicolas Geoffray39468442014-09-02 15:17:15 +0100879 StackMap GetStackMapAt(size_t i) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100880 size_t size = StackMapSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100881 return StackMap(GetStackMaps().Subregion(i * size, size));
882 }
883
884 uint32_t GetOverallSize() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000885 return region_.LoadUnaligned<uint32_t>(kOverallSizeOffset);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100886 }
887
888 void SetOverallSize(uint32_t size) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000889 region_.StoreUnaligned<uint32_t>(kOverallSizeOffset, size);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100890 }
891
Roland Levillaina552e1c2015-03-26 15:01:03 +0000892 uint32_t GetNumberOfDexRegisterLocationCatalogEntries() const {
893 return region_.LoadUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset);
894 }
895
896 void SetNumberOfDexRegisterLocationCatalogEntries(uint32_t num_entries) {
897 region_.StoreUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset, num_entries);
898 }
899
900 uint32_t GetDexRegisterLocationCatalogSize() const {
901 return ComputeDexRegisterLocationCatalogSize(GetDexRegisterLocationCatalogOffset(),
902 GetNumberOfDexRegisterLocationCatalogEntries());
903 }
904
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100905 uint32_t GetStackMaskSize() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000906 return region_.LoadUnaligned<uint32_t>(kStackMaskSizeOffset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100907 }
908
909 void SetStackMaskSize(uint32_t size) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000910 region_.StoreUnaligned<uint32_t>(kStackMaskSizeOffset, size);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100911 }
912
913 size_t GetNumberOfStackMaps() const {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000914 return region_.LoadUnaligned<uint32_t>(kNumberOfStackMapsOffset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100915 }
916
917 void SetNumberOfStackMaps(uint32_t number_of_stack_maps) {
Nicolas Geoffrayaec8f932015-03-18 10:42:22 +0000918 region_.StoreUnaligned<uint32_t>(kNumberOfStackMapsOffset, number_of_stack_maps);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100919 }
920
Roland Levillain29ba1b02015-03-13 11:45:07 +0000921 // Get the size of one stack map of this CodeInfo object, in bytes.
922 // All stack maps of a CodeInfo have the same size.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100923 size_t StackMapSize() const {
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000924 return StackMap::ComputeStackMapSize(GetStackMaskSize(),
925 HasInlineInfo(),
926 HasSmallInlineInfo(),
927 HasSmallDexRegisterMap(),
928 HasSmallDexPc(),
929 HasSmallNativePc());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100930 }
931
Roland Levillain29ba1b02015-03-13 11:45:07 +0000932 // Get the size all the stack maps of this CodeInfo object, in bytes.
Roland Levillaina552e1c2015-03-26 15:01:03 +0000933 size_t GetStackMapsSize() const {
Roland Levillain29ba1b02015-03-13 11:45:07 +0000934 return StackMapSize() * GetNumberOfStackMaps();
935 }
936
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000937 size_t GetDexRegisterMapsOffset() const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000938 return GetStackMapsOffset() + GetStackMapsSize();
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000939 }
940
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000941 uint32_t GetStackMapsOffset() const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000942 return GetDexRegisterLocationCatalogOffset() + GetDexRegisterLocationCatalogSize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000943 }
944
Roland Levillain442b46a2015-02-18 16:54:21 +0000945 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map, uint32_t number_of_dex_registers) const {
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000946 DCHECK(stack_map.HasDexRegisterMap(*this));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000947 uint32_t offset = GetDexRegisterMapsOffset() + stack_map.GetDexRegisterMapOffset(*this);
948 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000949 return DexRegisterMap(region_.Subregion(offset, size));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100950 }
951
Roland Levillain442b46a2015-02-18 16:54:21 +0000952 InlineInfo GetInlineInfoOf(StackMap stack_map) const {
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000953 DCHECK(stack_map.HasInlineInfo(*this));
954 uint32_t offset = stack_map.GetInlineDescriptorOffset(*this) + GetDexRegisterMapsOffset();
955 uint8_t depth = region_.LoadUnaligned<uint8_t>(offset);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100956 return InlineInfo(region_.Subregion(offset,
957 InlineInfo::kFixedSize + depth * InlineInfo::SingleEntrySize()));
958 }
959
Roland Levillain442b46a2015-02-18 16:54:21 +0000960 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100961 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100962 StackMap stack_map = GetStackMapAt(i);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000963 if (stack_map.GetDexPc(*this) == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100964 return stack_map;
965 }
966 }
967 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700968 UNREACHABLE();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100969 }
970
Roland Levillain442b46a2015-02-18 16:54:21 +0000971 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100972 // TODO: stack maps are sorted by native pc, we can do a binary search.
973 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100974 StackMap stack_map = GetStackMapAt(i);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000975 if (stack_map.GetNativePcOffset(*this) == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100976 return stack_map;
977 }
978 }
979 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700980 UNREACHABLE();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100981 }
982
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000983 void Dump(std::ostream& os, uint16_t number_of_dex_registers) const;
984 void DumpStackMapHeader(std::ostream& os, size_t stack_map_num) const;
985
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100986 private:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000987 // TODO: Instead of plain types such as "uint32_t", introduce
988 // typedefs (and document the memory layout of CodeInfo).
Nicolas Geoffray39468442014-09-02 15:17:15 +0100989 static constexpr int kOverallSizeOffset = 0;
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000990 static constexpr int kEncodingInfoOffset = kOverallSizeOffset + sizeof(uint32_t);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000991 static constexpr int kNumberOfDexRegisterLocationCatalogEntriesOffset =
992 kEncodingInfoOffset + sizeof(uint8_t);
993 static constexpr int kNumberOfStackMapsOffset =
994 kNumberOfDexRegisterLocationCatalogEntriesOffset + sizeof(uint32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100995 static constexpr int kStackMaskSizeOffset = kNumberOfStackMapsOffset + sizeof(uint32_t);
996 static constexpr int kFixedSize = kStackMaskSizeOffset + sizeof(uint32_t);
997
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000998 static constexpr int kHasInlineInfoBitOffset = (kEncodingInfoOffset * kBitsPerByte);
999 static constexpr int kHasSmallInlineInfoBitOffset = kHasInlineInfoBitOffset + 1;
1000 static constexpr int kHasSmallDexRegisterMapBitOffset = kHasSmallInlineInfoBitOffset + 1;
1001 static constexpr int kHasSmallDexPcBitOffset = kHasSmallDexRegisterMapBitOffset + 1;
1002 static constexpr int kHasSmallNativePcBitOffset = kHasSmallDexPcBitOffset + 1;
1003
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001004 MemoryRegion GetStackMaps() const {
1005 return region_.size() == 0
1006 ? MemoryRegion()
Roland Levillaina552e1c2015-03-26 15:01:03 +00001007 : region_.Subregion(GetStackMapsOffset(), GetStackMapsSize());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001008 }
1009
Roland Levillaina552e1c2015-03-26 15:01:03 +00001010 // Compute the size of the Dex register map associated to the stack map at
1011 // `dex_register_map_offset_in_code_info`.
1012 size_t ComputeDexRegisterMapSizeOf(uint32_t dex_register_map_offset_in_code_info,
1013 uint16_t number_of_dex_registers) const {
1014 // Offset where the actual mapping data starts within art::DexRegisterMap.
1015 size_t location_mapping_data_offset_in_dex_register_map =
1016 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1017 // Create a temporary art::DexRegisterMap to be able to call
1018 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1019 DexRegisterMap dex_register_map_without_locations(
1020 MemoryRegion(region_.Subregion(dex_register_map_offset_in_code_info,
1021 location_mapping_data_offset_in_dex_register_map)));
1022 size_t number_of_live_dex_registers =
1023 dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
1024 size_t location_mapping_data_size_in_bits =
1025 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfDexRegisterLocationCatalogEntries())
1026 * number_of_live_dex_registers;
1027 size_t location_mapping_data_size_in_bytes =
1028 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1029 size_t dex_register_map_size =
1030 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1031 return dex_register_map_size;
1032 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001033
Roland Levillaina552e1c2015-03-26 15:01:03 +00001034 // Compute the size of a Dex register location catalog starting at offset `origin`
1035 // in `region_` and containing `number_of_dex_locations` entries.
1036 size_t ComputeDexRegisterLocationCatalogSize(uint32_t origin,
1037 uint32_t number_of_dex_locations) const {
1038 // TODO: Ideally, we would like to use art::DexRegisterLocationCatalog::Size or
1039 // art::DexRegisterLocationCatalog::FindLocationOffset, but the
1040 // DexRegisterLocationCatalog is not yet built. Try to factor common code.
1041 size_t offset = origin + DexRegisterLocationCatalog::kFixedSize;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001042
Roland Levillaina552e1c2015-03-26 15:01:03 +00001043 // Skip the first `number_of_dex_locations - 1` entries.
1044 for (uint16_t i = 0; i < number_of_dex_locations; ++i) {
1045 // Read the first next byte and inspect its first 3 bits to decide
1046 // whether it is a short or a large location.
1047 DexRegisterLocationCatalog::ShortLocation first_byte =
1048 region_.LoadUnaligned<DexRegisterLocationCatalog::ShortLocation>(offset);
1049 DexRegisterLocation::Kind kind =
1050 DexRegisterLocationCatalog::ExtractKindFromShortLocation(first_byte);
1051 if (DexRegisterLocation::IsShortLocationKind(kind)) {
1052 // Short location. Skip the current byte.
1053 offset += DexRegisterLocationCatalog::SingleShortEntrySize();
1054 } else {
1055 // Large location. Skip the 5 next bytes.
1056 offset += DexRegisterLocationCatalog::SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001057 }
1058 }
1059 size_t size = offset - origin;
1060 return size;
1061 }
1062
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001063 MemoryRegion region_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001064 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001065};
1066
1067} // namespace art
1068
1069#endif // ART_RUNTIME_STACK_MAP_H_