blob: f7a64026b7a61a5f3ea6a59d9c9d4d4961422993 [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
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080020#include "arch/code_offset.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010021#include "base/bit_vector.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/bit_utils.h"
Mathieu Chartier12f1b992017-01-19 18:00:45 -080023#include "bit_memory_region.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010024#include "dex_file.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010025#include "memory_region.h"
David Srbecky09ed0982016-02-12 21:58:43 +000026#include "leb128.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010027
28namespace art {
29
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010030class VariableIndentationOutputStream;
31
Roland Levillaina2d8ec62015-03-12 15:25:29 +000032// Size of a frame slot, in bytes. This constant is a signed value,
33// to please the compiler in arithmetic operations involving int32_t
34// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000035static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000036
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000037// Size of Dex virtual registers.
Roland Levillaina552e1c2015-03-26 15:01:03 +000038static constexpr size_t kVRegSize = 4;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000039
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000040class ArtMethod;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000041class CodeInfo;
David Brazdilf677ebf2015-05-29 16:29:43 +010042class StackMapEncoding;
David Srbecky09ed0982016-02-12 21:58:43 +000043struct CodeInfoEncoding;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000044
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010045/**
46 * Classes in the following file are wrapper on stack map information backed
47 * by a MemoryRegion. As such they read and write to the region, they don't have
48 * their own fields.
49 */
50
Roland Levillaina2d8ec62015-03-12 15:25:29 +000051// Dex register location container used by DexRegisterMap and StackMapStream.
52class DexRegisterLocation {
53 public:
54 /*
55 * The location kind used to populate the Dex register information in a
56 * StackMapStream can either be:
David Brazdild9cb68e2015-08-25 13:52:43 +010057 * - kStack: vreg stored on the stack, value holds the stack offset;
58 * - kInRegister: vreg stored in low 32 bits of a core physical register,
59 * value holds the register number;
60 * - kInRegisterHigh: vreg stored in high 32 bits of a core physical register,
61 * value holds the register number;
62 * - kInFpuRegister: vreg stored in low 32 bits of an FPU register,
63 * value holds the register number;
64 * - kInFpuRegisterHigh: vreg stored in high 32 bits of an FPU register,
65 * value holds the register number;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000066 * - kConstant: value holds the constant;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000067 *
68 * In addition, DexRegisterMap also uses these values:
69 * - kInStackLargeOffset: value holds a "large" stack offset (greater than
Roland Levillaina552e1c2015-03-26 15:01:03 +000070 * or equal to 128 bytes);
71 * - kConstantLargeValue: value holds a "large" constant (lower than 0, or
David Brazdild9cb68e2015-08-25 13:52:43 +010072 * or greater than or equal to 32);
73 * - kNone: the register has no location, meaning it has not been set.
Roland Levillaina2d8ec62015-03-12 15:25:29 +000074 */
75 enum class Kind : uint8_t {
76 // Short location kinds, for entries fitting on one byte (3 bits
77 // for the kind, 5 bits for the value) in a DexRegisterMap.
David Brazdild9cb68e2015-08-25 13:52:43 +010078 kInStack = 0, // 0b000
79 kInRegister = 1, // 0b001
80 kInRegisterHigh = 2, // 0b010
Roland Levillaina2d8ec62015-03-12 15:25:29 +000081 kInFpuRegister = 3, // 0b011
David Brazdild9cb68e2015-08-25 13:52:43 +010082 kInFpuRegisterHigh = 4, // 0b100
83 kConstant = 5, // 0b101
Roland Levillaina2d8ec62015-03-12 15:25:29 +000084
85 // Large location kinds, requiring a 5-byte encoding (1 byte for the
86 // kind, 4 bytes for the value).
87
88 // Stack location at a large offset, meaning that the offset value
89 // divided by the stack frame slot size (4 bytes) cannot fit on a
90 // 5-bit unsigned integer (i.e., this offset value is greater than
91 // or equal to 2^5 * 4 = 128 bytes).
David Brazdild9cb68e2015-08-25 13:52:43 +010092 kInStackLargeOffset = 6, // 0b110
Roland Levillaina2d8ec62015-03-12 15:25:29 +000093
94 // Large constant, that cannot fit on a 5-bit signed integer (i.e.,
Roland Levillaina552e1c2015-03-26 15:01:03 +000095 // lower than 0, or greater than or equal to 2^5 = 32).
David Brazdild9cb68e2015-08-25 13:52:43 +010096 kConstantLargeValue = 7, // 0b111
97
98 // Entries with no location are not stored and do not need own marker.
99 kNone = static_cast<uint8_t>(-1),
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000100
101 kLastLocationKind = kConstantLargeValue
102 };
103
104 static_assert(
105 sizeof(Kind) == 1u,
106 "art::DexRegisterLocation::Kind has a size different from one byte.");
107
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000108 static bool IsShortLocationKind(Kind kind) {
109 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000110 case Kind::kInStack:
111 case Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100112 case Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000113 case Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100114 case Kind::kInFpuRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000115 case Kind::kConstant:
116 return true;
117
118 case Kind::kInStackLargeOffset:
119 case Kind::kConstantLargeValue:
120 return false;
121
David Brazdild9cb68e2015-08-25 13:52:43 +0100122 case Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000123 LOG(FATAL) << "Unexpected location kind";
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000124 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100125 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000126 }
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) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000133 case Kind::kInStack:
134 case Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100135 case Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000136 case Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100137 case Kind::kInFpuRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000138 case Kind::kConstant:
139 return kind;
140
141 case Kind::kInStackLargeOffset:
142 return Kind::kInStack;
143
144 case Kind::kConstantLargeValue:
145 return Kind::kConstant;
146
David Brazdild9cb68e2015-08-25 13:52:43 +0100147 case Kind::kNone:
148 return kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000149 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100150 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000151 }
152
Roland Levillaina552e1c2015-03-26 15:01:03 +0000153 // Required by art::StackMapStream::LocationCatalogEntriesIndices.
154 DexRegisterLocation() : kind_(Kind::kNone), value_(0) {}
155
156 DexRegisterLocation(Kind kind, int32_t value) : kind_(kind), value_(value) {}
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000157
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000158 static DexRegisterLocation None() {
159 return DexRegisterLocation(Kind::kNone, 0);
160 }
161
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000162 // Get the "surface" kind of the location, i.e., the one that doesn't
163 // include any value with a "large" qualifier.
164 Kind GetKind() const {
165 return ConvertToSurfaceKind(kind_);
166 }
167
168 // Get the value of the location.
169 int32_t GetValue() const { return value_; }
170
171 // Get the actual kind of the location.
172 Kind GetInternalKind() const { return kind_; }
173
Calin Juravle6ae70962015-03-18 16:31:28 +0000174 bool operator==(DexRegisterLocation other) const {
175 return kind_ == other.kind_ && value_ == other.value_;
176 }
177
178 bool operator!=(DexRegisterLocation other) const {
179 return !(*this == other);
180 }
181
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000182 private:
183 Kind kind_;
184 int32_t value_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000185
186 friend class DexRegisterLocationHashFn;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000187};
188
David Srbecky7dc11782016-02-25 13:23:56 +0000189std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation::Kind& kind);
190
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100191/**
Roland Levillaina552e1c2015-03-26 15:01:03 +0000192 * Store information on unique Dex register locations used in a method.
193 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100194 *
195 * [DexRegisterLocation+].
196 *
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000197 * DexRegisterLocations are either 1- or 5-byte wide (see art::DexRegisterLocation::Kind).
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100198 */
Roland Levillaina552e1c2015-03-26 15:01:03 +0000199class DexRegisterLocationCatalog {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100200 public:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000201 explicit DexRegisterLocationCatalog(MemoryRegion region) : region_(region) {}
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100202
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000203 // Short (compressed) location, fitting on one byte.
204 typedef uint8_t ShortLocation;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100205
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000206 void SetRegisterInfo(size_t offset, const DexRegisterLocation& dex_register_location) {
207 DexRegisterLocation::Kind kind = ComputeCompressedKind(dex_register_location);
208 int32_t value = dex_register_location.GetValue();
209 if (DexRegisterLocation::IsShortLocationKind(kind)) {
210 // Short location. Compress the kind and the value as a single byte.
211 if (kind == DexRegisterLocation::Kind::kInStack) {
212 // Instead of storing stack offsets expressed in bytes for
213 // short stack locations, store slot offsets. A stack offset
214 // is a multiple of 4 (kFrameSlotSize). This means that by
215 // dividing it by 4, we can fit values from the [0, 128)
216 // interval in a short stack location, and not just values
217 // from the [0, 32) interval.
218 DCHECK_EQ(value % kFrameSlotSize, 0);
219 value /= kFrameSlotSize;
220 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000221 DCHECK(IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000222 region_.StoreUnaligned<ShortLocation>(offset, MakeShortLocation(kind, value));
223 } else {
224 // Large location. Write the location on one byte and the value
225 // on 4 bytes.
Roland Levillaina552e1c2015-03-26 15:01:03 +0000226 DCHECK(!IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000227 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
228 // Also divide large stack offsets by 4 for the sake of consistency.
229 DCHECK_EQ(value % kFrameSlotSize, 0);
230 value /= kFrameSlotSize;
231 }
232 // Data can be unaligned as the written Dex register locations can
233 // either be 1-byte or 5-byte wide. Use
234 // art::MemoryRegion::StoreUnaligned instead of
235 // art::MemoryRegion::Store to prevent unligned word accesses on ARM.
236 region_.StoreUnaligned<DexRegisterLocation::Kind>(offset, kind);
237 region_.StoreUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind), value);
Roland Levillain442b46a2015-02-18 16:54:21 +0000238 }
239 }
240
Roland Levillaina552e1c2015-03-26 15:01:03 +0000241 // Find the offset of the location catalog entry number `location_catalog_entry_index`.
242 size_t FindLocationOffset(size_t location_catalog_entry_index) const {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000243 size_t offset = kFixedSize;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000244 // Skip the first `location_catalog_entry_index - 1` entries.
245 for (uint16_t i = 0; i < location_catalog_entry_index; ++i) {
246 // Read the first next byte and inspect its first 3 bits to decide
247 // whether it is a short or a large location.
248 DexRegisterLocation::Kind kind = ExtractKindAtOffset(offset);
249 if (DexRegisterLocation::IsShortLocationKind(kind)) {
250 // Short location. Skip the current byte.
251 offset += SingleShortEntrySize();
252 } else {
253 // Large location. Skip the 5 next bytes.
254 offset += SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000255 }
256 }
257 return offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100258 }
259
Roland Levillaina552e1c2015-03-26 15:01:03 +0000260 // Get the internal kind of entry at `location_catalog_entry_index`.
261 DexRegisterLocation::Kind GetLocationInternalKind(size_t location_catalog_entry_index) const {
262 if (location_catalog_entry_index == kNoLocationEntryIndex) {
263 return DexRegisterLocation::Kind::kNone;
264 }
265 return ExtractKindAtOffset(FindLocationOffset(location_catalog_entry_index));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100266 }
267
Roland Levillaina552e1c2015-03-26 15:01:03 +0000268 // Get the (surface) kind and value of entry at `location_catalog_entry_index`.
269 DexRegisterLocation GetDexRegisterLocation(size_t location_catalog_entry_index) const {
270 if (location_catalog_entry_index == kNoLocationEntryIndex) {
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000271 return DexRegisterLocation::None();
272 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000273 size_t offset = FindLocationOffset(location_catalog_entry_index);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000274 // Read the first byte and inspect its first 3 bits to get the location.
275 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
276 DexRegisterLocation::Kind kind = ExtractKindFromShortLocation(first_byte);
277 if (DexRegisterLocation::IsShortLocationKind(kind)) {
278 // Short location. Extract the value from the remaining 5 bits.
279 int32_t value = ExtractValueFromShortLocation(first_byte);
280 if (kind == DexRegisterLocation::Kind::kInStack) {
281 // Convert the stack slot (short) offset to a byte offset value.
282 value *= kFrameSlotSize;
283 }
284 return DexRegisterLocation(kind, value);
285 } else {
286 // Large location. Read the four next bytes to get the value.
287 int32_t value = region_.LoadUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind));
288 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
289 // Convert the stack slot (large) offset to a byte offset value.
290 value *= kFrameSlotSize;
291 }
292 return DexRegisterLocation(kind, value);
293 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100294 }
295
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000296 // Compute the compressed kind of `location`.
297 static DexRegisterLocation::Kind ComputeCompressedKind(const DexRegisterLocation& location) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100298 DexRegisterLocation::Kind kind = location.GetInternalKind();
299 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000300 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000301 return IsShortStackOffsetValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000302 ? DexRegisterLocation::Kind::kInStack
303 : DexRegisterLocation::Kind::kInStackLargeOffset;
304
David Brazdild9cb68e2015-08-25 13:52:43 +0100305 case DexRegisterLocation::Kind::kInRegister:
306 case DexRegisterLocation::Kind::kInRegisterHigh:
307 DCHECK_GE(location.GetValue(), 0);
308 DCHECK_LT(location.GetValue(), 1 << kValueBits);
309 return kind;
310
311 case DexRegisterLocation::Kind::kInFpuRegister:
312 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
313 DCHECK_GE(location.GetValue(), 0);
314 DCHECK_LT(location.GetValue(), 1 << kValueBits);
315 return kind;
316
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000317 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000318 return IsShortConstantValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000319 ? DexRegisterLocation::Kind::kConstant
320 : DexRegisterLocation::Kind::kConstantLargeValue;
321
David Brazdild9cb68e2015-08-25 13:52:43 +0100322 case DexRegisterLocation::Kind::kConstantLargeValue:
323 case DexRegisterLocation::Kind::kInStackLargeOffset:
324 case DexRegisterLocation::Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000325 LOG(FATAL) << "Unexpected location kind " << kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000326 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100327 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000328 }
329
330 // Can `location` be turned into a short location?
331 static bool CanBeEncodedAsShortLocation(const DexRegisterLocation& location) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100332 DexRegisterLocation::Kind kind = location.GetInternalKind();
333 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000334 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000335 return IsShortStackOffsetValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000336
David Brazdild9cb68e2015-08-25 13:52:43 +0100337 case DexRegisterLocation::Kind::kInRegister:
338 case DexRegisterLocation::Kind::kInRegisterHigh:
339 case DexRegisterLocation::Kind::kInFpuRegister:
340 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
341 return true;
342
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000343 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000344 return IsShortConstantValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000345
David Brazdild9cb68e2015-08-25 13:52:43 +0100346 case DexRegisterLocation::Kind::kConstantLargeValue:
347 case DexRegisterLocation::Kind::kInStackLargeOffset:
348 case DexRegisterLocation::Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000349 LOG(FATAL) << "Unexpected location kind " << kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000350 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100351 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000352 }
353
354 static size_t EntrySize(const DexRegisterLocation& location) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000355 return CanBeEncodedAsShortLocation(location) ? SingleShortEntrySize() : SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000356 }
357
358 static size_t SingleShortEntrySize() {
359 return sizeof(ShortLocation);
360 }
361
362 static size_t SingleLargeEntrySize() {
363 return sizeof(DexRegisterLocation::Kind) + sizeof(int32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100364 }
365
Roland Levillain12baf472015-03-05 12:41:42 +0000366 size_t Size() const {
367 return region_.size();
368 }
369
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100370 void Dump(VariableIndentationOutputStream* vios, const CodeInfo& code_info);
Roland Levillain0396ed72015-05-27 15:12:19 +0100371
Roland Levillaina552e1c2015-03-26 15:01:03 +0000372 // Special (invalid) Dex register location catalog entry index meaning
373 // that there is no location for a given Dex register (i.e., it is
374 // mapped to a DexRegisterLocation::Kind::kNone location).
375 static constexpr size_t kNoLocationEntryIndex = -1;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100376
Roland Levillain12baf472015-03-05 12:41:42 +0000377 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000378 static constexpr int kFixedSize = 0;
379
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000380 // Width of the kind "field" in a short location, in bits.
381 static constexpr size_t kKindBits = 3;
382 // Width of the value "field" in a short location, in bits.
383 static constexpr size_t kValueBits = 5;
384
385 static constexpr uint8_t kKindMask = (1 << kKindBits) - 1;
386 static constexpr int32_t kValueMask = (1 << kValueBits) - 1;
387 static constexpr size_t kKindOffset = 0;
388 static constexpr size_t kValueOffset = kKindBits;
389
Roland Levillaina552e1c2015-03-26 15:01:03 +0000390 static bool IsShortStackOffsetValue(int32_t value) {
391 DCHECK_EQ(value % kFrameSlotSize, 0);
392 return IsShortValue(value / kFrameSlotSize);
393 }
394
395 static bool IsShortConstantValue(int32_t value) {
396 return IsShortValue(value);
397 }
398
399 static bool IsShortValue(int32_t value) {
400 return IsUint<kValueBits>(value);
401 }
402
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000403 static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000404 uint8_t kind_integer_value = static_cast<uint8_t>(kind);
405 DCHECK(IsUint<kKindBits>(kind_integer_value)) << kind_integer_value;
406 DCHECK(IsShortValue(value)) << value;
407 return (kind_integer_value & kKindMask) << kKindOffset
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000408 | (value & kValueMask) << kValueOffset;
409 }
410
411 static DexRegisterLocation::Kind ExtractKindFromShortLocation(ShortLocation location) {
412 uint8_t kind = (location >> kKindOffset) & kKindMask;
413 DCHECK_LE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kLastLocationKind));
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000414 // We do not encode kNone locations in the stack map.
415 DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000416 return static_cast<DexRegisterLocation::Kind>(kind);
417 }
418
419 static int32_t ExtractValueFromShortLocation(ShortLocation location) {
420 return (location >> kValueOffset) & kValueMask;
421 }
422
423 // Extract a location kind from the byte at position `offset`.
424 DexRegisterLocation::Kind ExtractKindAtOffset(size_t offset) const {
425 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
426 return ExtractKindFromShortLocation(first_byte);
427 }
428
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100429 MemoryRegion region_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000430
431 friend class CodeInfo;
432 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100433};
434
Roland Levillaina552e1c2015-03-26 15:01:03 +0000435/* Information on Dex register locations for a specific PC, mapping a
436 * stack map's Dex register to a location entry in a DexRegisterLocationCatalog.
437 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100438 *
439 * [live_bit_mask, entries*]
440 *
Roland Levillaina552e1c2015-03-26 15:01:03 +0000441 * where entries are concatenated unsigned integer values encoded on a number
442 * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending
443 * on the number of entries in the Dex register location catalog
444 * (see DexRegisterMap::SingleEntrySizeInBits). The map is 1-byte aligned.
445 */
446class DexRegisterMap {
447 public:
448 explicit DexRegisterMap(MemoryRegion region) : region_(region) {}
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000449 DexRegisterMap() {}
450
451 bool IsValid() const { return region_.pointer() != nullptr; }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000452
453 // Get the surface kind of Dex register `dex_register_number`.
454 DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number,
455 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100456 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000457 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000458 return DexRegisterLocation::ConvertToSurfaceKind(
David Brazdilf677ebf2015-05-29 16:29:43 +0100459 GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info, enc));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000460 }
461
462 // Get the internal kind of Dex register `dex_register_number`.
463 DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number,
464 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100465 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000466 const CodeInfoEncoding& enc) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000467
468 // Get the Dex register location `dex_register_number`.
469 DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number,
470 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100471 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000472 const CodeInfoEncoding& enc) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000473
474 int32_t GetStackOffsetInBytes(uint16_t dex_register_number,
475 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100476 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000477 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000478 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100479 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000480 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
481 // GetDexRegisterLocation returns the offset in bytes.
482 return location.GetValue();
483 }
484
485 int32_t GetConstant(uint16_t dex_register_number,
486 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100487 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000488 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000489 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100490 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
David Srbecky7dc11782016-02-25 13:23:56 +0000491 DCHECK_EQ(location.GetKind(), DexRegisterLocation::Kind::kConstant);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000492 return location.GetValue();
493 }
494
495 int32_t GetMachineRegister(uint16_t dex_register_number,
496 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100497 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000498 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000499 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100500 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
David Brazdild9cb68e2015-08-25 13:52:43 +0100501 DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister ||
502 location.GetInternalKind() == DexRegisterLocation::Kind::kInRegisterHigh ||
503 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister ||
504 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh)
David Srbecky7dc11782016-02-25 13:23:56 +0000505 << location.GetInternalKind();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000506 return location.GetValue();
507 }
508
509 // Get the index of the entry in the Dex register location catalog
510 // corresponding to `dex_register_number`.
511 size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number,
512 uint16_t number_of_dex_registers,
513 size_t number_of_location_catalog_entries) const {
514 if (!IsDexRegisterLive(dex_register_number)) {
515 return DexRegisterLocationCatalog::kNoLocationEntryIndex;
516 }
517
518 if (number_of_location_catalog_entries == 1) {
519 // We do not allocate space for location maps in the case of a
520 // single-entry location catalog, as it is useless. The only valid
521 // entry index is 0;
522 return 0;
523 }
524
525 // The bit offset of the beginning of the map locations.
526 size_t map_locations_offset_in_bits =
527 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
528 size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number);
529 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
530 // The bit size of an entry.
531 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
532 // The bit offset where `index_in_dex_register_map` is located.
533 size_t entry_offset_in_bits =
534 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
535 size_t location_catalog_entry_index =
536 region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits);
537 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
538 return location_catalog_entry_index;
539 }
540
541 // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`.
542 void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map,
543 size_t location_catalog_entry_index,
544 uint16_t number_of_dex_registers,
545 size_t number_of_location_catalog_entries) {
546 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
547 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
548
549 if (number_of_location_catalog_entries == 1) {
550 // We do not allocate space for location maps in the case of a
551 // single-entry location catalog, as it is useless.
552 return;
553 }
554
555 // The bit offset of the beginning of the map locations.
556 size_t map_locations_offset_in_bits =
557 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
558 // The bit size of an entry.
559 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
560 // The bit offset where `index_in_dex_register_map` is located.
561 size_t entry_offset_in_bits =
562 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
563 region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits);
564 }
565
566 void SetLiveBitMask(uint16_t number_of_dex_registers,
567 const BitVector& live_dex_registers_mask) {
568 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
569 for (uint16_t i = 0; i < number_of_dex_registers; ++i) {
570 region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i));
571 }
572 }
573
574 bool IsDexRegisterLive(uint16_t dex_register_number) const {
575 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
576 return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number);
577 }
578
579 size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const {
580 size_t number_of_live_dex_registers = 0;
581 for (size_t i = 0; i < number_of_dex_registers; ++i) {
582 if (IsDexRegisterLive(i)) {
583 ++number_of_live_dex_registers;
584 }
585 }
586 return number_of_live_dex_registers;
587 }
588
589 static size_t GetLiveBitMaskOffset() {
590 return kFixedSize;
591 }
592
593 // Compute the size of the live register bit mask (in bytes), for a
594 // method having `number_of_dex_registers` Dex registers.
595 static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) {
596 return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte;
597 }
598
599 static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) {
600 return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers);
601 }
602
603 size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers,
604 size_t number_of_location_catalog_entries) const {
605 size_t location_mapping_data_size_in_bits =
606 GetNumberOfLiveDexRegisters(number_of_dex_registers)
607 * SingleEntrySizeInBits(number_of_location_catalog_entries);
608 return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
609 }
610
611 // Return the size of a map entry in bits. Note that if
612 // `number_of_location_catalog_entries` equals 1, this function returns 0,
613 // which is fine, as there is no need to allocate a map for a
614 // single-entry location catalog; the only valid location catalog entry index
615 // for a live register in this case is 0 and there is no need to
616 // store it.
617 static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) {
618 // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2.
619 return number_of_location_catalog_entries == 0
620 ? 0u
621 : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries));
622 }
623
624 // Return the size of the DexRegisterMap object, in bytes.
625 size_t Size() const {
626 return region_.size();
627 }
628
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100629 void Dump(VariableIndentationOutputStream* vios,
630 const CodeInfo& code_info, uint16_t number_of_dex_registers) const;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100631
Roland Levillaina552e1c2015-03-26 15:01:03 +0000632 private:
633 // Return the index in the Dex register map corresponding to the Dex
634 // register number `dex_register_number`.
635 size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const {
636 if (!IsDexRegisterLive(dex_register_number)) {
637 return kInvalidIndexInDexRegisterMap;
638 }
639 return GetNumberOfLiveDexRegisters(dex_register_number);
640 }
641
642 // Special (invalid) Dex register map entry index meaning that there
643 // is no index in the map for a given Dex register (i.e., it must
644 // have been mapped to a DexRegisterLocation::Kind::kNone location).
645 static constexpr size_t kInvalidIndexInDexRegisterMap = -1;
646
647 static constexpr int kFixedSize = 0;
648
649 MemoryRegion region_;
650
651 friend class CodeInfo;
652 friend class StackMapStream;
653};
654
David Srbecky09ed0982016-02-12 21:58:43 +0000655// Represents bit range of bit-packed integer field.
656// We reuse the idea from ULEB128p1 to support encoding of -1 (aka 0xFFFFFFFF).
657// If min_value is set to -1, we implicitly subtract one from any loaded value,
658// and add one to any stored value. This is generalized to any negative values.
659// In other words, min_value acts as a base and the stored value is added to it.
660struct FieldEncoding {
661 FieldEncoding(size_t start_offset, size_t end_offset, int32_t min_value = 0)
662 : start_offset_(start_offset), end_offset_(end_offset), min_value_(min_value) {
663 DCHECK_LE(start_offset_, end_offset_);
664 DCHECK_LE(BitSize(), 32u);
665 }
666
667 ALWAYS_INLINE size_t BitSize() const { return end_offset_ - start_offset_; }
668
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800669 template <typename Region>
670 ALWAYS_INLINE int32_t Load(const Region& region) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000671 DCHECK_LE(end_offset_, region.size_in_bits());
Mathieu Chartier3ceedc02017-01-25 11:11:02 -0800672 return static_cast<int32_t>(region.LoadBits(start_offset_, BitSize())) + min_value_;
David Srbecky09ed0982016-02-12 21:58:43 +0000673 }
674
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800675 template <typename Region>
676 ALWAYS_INLINE void Store(Region region, int32_t value) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000677 region.StoreBits(start_offset_, value - min_value_, BitSize());
678 DCHECK_EQ(Load(region), value);
679 }
680
681 private:
682 size_t start_offset_;
683 size_t end_offset_;
684 int32_t min_value_;
685};
686
David Brazdilf677ebf2015-05-29 16:29:43 +0100687class StackMapEncoding {
688 public:
689 StackMapEncoding() {}
690
David Srbecky09ed0982016-02-12 21:58:43 +0000691 // Set stack map bit layout based on given sizes.
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800692 // Returns the size of stack map in bits.
David Srbecky09ed0982016-02-12 21:58:43 +0000693 size_t SetFromSizes(size_t native_pc_max,
694 size_t dex_pc_max,
695 size_t dex_register_map_size,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800696 size_t number_of_inline_info,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800697 size_t number_of_register_masks,
David Srbecky45aa5982016-03-18 02:15:09 +0000698 size_t number_of_stack_masks) {
699 total_bit_size_ = 0;
700 DCHECK_EQ(kNativePcBitOffset, total_bit_size_);
701 total_bit_size_ += MinimumBitsToStore(native_pc_max);
David Brazdilf677ebf2015-05-29 16:29:43 +0100702
David Srbecky45aa5982016-03-18 02:15:09 +0000703 dex_pc_bit_offset_ = total_bit_size_;
704 total_bit_size_ += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max);
David Srbecky09ed0982016-02-12 21:58:43 +0000705
706 // We also need +1 for kNoDexRegisterMap, but since the size is strictly
707 // greater than any offset we might try to encode, we already implicitly have it.
David Srbecky45aa5982016-03-18 02:15:09 +0000708 dex_register_map_bit_offset_ = total_bit_size_;
709 total_bit_size_ += MinimumBitsToStore(dex_register_map_size);
David Srbecky09ed0982016-02-12 21:58:43 +0000710
711 // We also need +1 for kNoInlineInfo, but since the inline_info_size is strictly
712 // greater than the offset we might try to encode, we already implicitly have it.
713 // If inline_info_size is zero, we can encode only kNoInlineInfo (in zero bits).
David Srbecky45aa5982016-03-18 02:15:09 +0000714 inline_info_bit_offset_ = total_bit_size_;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800715 total_bit_size_ += MinimumBitsToStore(number_of_inline_info);
David Srbecky09ed0982016-02-12 21:58:43 +0000716
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800717 register_mask_index_bit_offset_ = total_bit_size_;
718 total_bit_size_ += MinimumBitsToStore(number_of_register_masks);
David Srbecky09ed0982016-02-12 21:58:43 +0000719
David Srbecky45aa5982016-03-18 02:15:09 +0000720 stack_mask_index_bit_offset_ = total_bit_size_;
721 total_bit_size_ += MinimumBitsToStore(number_of_stack_masks);
David Srbecky09ed0982016-02-12 21:58:43 +0000722
David Srbecky45aa5982016-03-18 02:15:09 +0000723 return total_bit_size_;
David Brazdilf677ebf2015-05-29 16:29:43 +0100724 }
725
David Srbecky09ed0982016-02-12 21:58:43 +0000726 ALWAYS_INLINE FieldEncoding GetNativePcEncoding() const {
727 return FieldEncoding(kNativePcBitOffset, dex_pc_bit_offset_);
728 }
729 ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const {
730 return FieldEncoding(dex_pc_bit_offset_, dex_register_map_bit_offset_, -1 /* min_value */);
731 }
732 ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const {
733 return FieldEncoding(dex_register_map_bit_offset_, inline_info_bit_offset_, -1 /* min_value */);
734 }
735 ALWAYS_INLINE FieldEncoding GetInlineInfoEncoding() const {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800736 return FieldEncoding(inline_info_bit_offset_,
737 register_mask_index_bit_offset_,
738 -1 /* min_value */);
David Srbecky09ed0982016-02-12 21:58:43 +0000739 }
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800740 ALWAYS_INLINE FieldEncoding GetRegisterMaskIndexEncoding() const {
741 return FieldEncoding(register_mask_index_bit_offset_, stack_mask_index_bit_offset_);
David Srbecky09ed0982016-02-12 21:58:43 +0000742 }
David Srbecky45aa5982016-03-18 02:15:09 +0000743 ALWAYS_INLINE FieldEncoding GetStackMaskIndexEncoding() const {
744 return FieldEncoding(stack_mask_index_bit_offset_, total_bit_size_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100745 }
David Srbecky45aa5982016-03-18 02:15:09 +0000746 ALWAYS_INLINE size_t BitSize() const {
747 return total_bit_size_;
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800748 }
David Brazdilf677ebf2015-05-29 16:29:43 +0100749
Mathieu Chartierc420a802017-02-14 15:16:19 -0800750 // Encode the encoding into the vector.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800751 template<typename Vector>
752 void Encode(Vector* dest) const {
753 static_assert(alignof(StackMapEncoding) == 1, "Should not require alignment");
754 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
755 dest->insert(dest->end(), ptr, ptr + sizeof(*this));
756 }
757
Mathieu Chartierc420a802017-02-14 15:16:19 -0800758 // Decode the encoding from a pointer, updates the pointer.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800759 void Decode(const uint8_t** ptr) {
760 *this = *reinterpret_cast<const StackMapEncoding*>(*ptr);
761 *ptr += sizeof(*this);
762 }
763
David Srbecky09ed0982016-02-12 21:58:43 +0000764 void Dump(VariableIndentationOutputStream* vios) const;
David Brazdilf677ebf2015-05-29 16:29:43 +0100765
766 private:
David Srbecky09ed0982016-02-12 21:58:43 +0000767 static constexpr size_t kNativePcBitOffset = 0;
768 uint8_t dex_pc_bit_offset_;
769 uint8_t dex_register_map_bit_offset_;
770 uint8_t inline_info_bit_offset_;
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800771 uint8_t register_mask_index_bit_offset_;
David Srbecky45aa5982016-03-18 02:15:09 +0000772 uint8_t stack_mask_index_bit_offset_;
773 uint8_t total_bit_size_;
David Brazdilf677ebf2015-05-29 16:29:43 +0100774};
775
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100776/**
777 * A Stack Map holds compilation information for a specific PC necessary for:
778 * - Mapping it to a dex PC,
779 * - Knowing which stack entries are objects,
780 * - Knowing which registers hold objects,
781 * - Knowing the inlining information,
782 * - Knowing the values of dex registers.
783 *
784 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100785 *
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800786 * [native_pc_offset, dex_pc, dex_register_map_offset, inlining_info_index, register_mask_index,
David Srbecky45aa5982016-03-18 02:15:09 +0000787 * stack_mask_index].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100788 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100789class StackMap {
790 public:
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100791 StackMap() {}
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800792 explicit StackMap(BitMemoryRegion region) : region_(region) {}
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100793
David Srbecky09ed0982016-02-12 21:58:43 +0000794 ALWAYS_INLINE bool IsValid() const { return region_.pointer() != nullptr; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100795
David Srbecky09ed0982016-02-12 21:58:43 +0000796 ALWAYS_INLINE uint32_t GetDexPc(const StackMapEncoding& encoding) const {
797 return encoding.GetDexPcEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100798 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100799
David Srbecky09ed0982016-02-12 21:58:43 +0000800 ALWAYS_INLINE void SetDexPc(const StackMapEncoding& encoding, uint32_t dex_pc) {
801 encoding.GetDexPcEncoding().Store(region_, dex_pc);
David Brazdilf677ebf2015-05-29 16:29:43 +0100802 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100803
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800804 ALWAYS_INLINE uint32_t GetNativePcOffset(const StackMapEncoding& encoding,
805 InstructionSet instruction_set) const {
806 CodeOffset offset(
807 CodeOffset::FromCompressedOffset(encoding.GetNativePcEncoding().Load(region_)));
808 return offset.Uint32Value(instruction_set);
David Brazdilf677ebf2015-05-29 16:29:43 +0100809 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100810
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800811 ALWAYS_INLINE void SetNativePcCodeOffset(const StackMapEncoding& encoding,
812 CodeOffset native_pc_offset) {
813 encoding.GetNativePcEncoding().Store(region_, native_pc_offset.CompressedValue());
David Brazdilf677ebf2015-05-29 16:29:43 +0100814 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100815
David Srbecky09ed0982016-02-12 21:58:43 +0000816 ALWAYS_INLINE uint32_t GetDexRegisterMapOffset(const StackMapEncoding& encoding) const {
817 return encoding.GetDexRegisterMapEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100818 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100819
David Srbecky09ed0982016-02-12 21:58:43 +0000820 ALWAYS_INLINE void SetDexRegisterMapOffset(const StackMapEncoding& encoding, uint32_t offset) {
821 encoding.GetDexRegisterMapEncoding().Store(region_, offset);
David Brazdilf677ebf2015-05-29 16:29:43 +0100822 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100823
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800824 ALWAYS_INLINE uint32_t GetInlineInfoIndex(const StackMapEncoding& encoding) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000825 return encoding.GetInlineInfoEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100826 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100827
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800828 ALWAYS_INLINE void SetInlineInfoIndex(const StackMapEncoding& encoding, uint32_t index) {
829 encoding.GetInlineInfoEncoding().Store(region_, index);
David Brazdilf677ebf2015-05-29 16:29:43 +0100830 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100831
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800832 ALWAYS_INLINE uint32_t GetRegisterMaskIndex(const StackMapEncoding& encoding) const {
833 return encoding.GetRegisterMaskIndexEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100834 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100835
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800836 ALWAYS_INLINE void SetRegisterMaskIndex(const StackMapEncoding& encoding, uint32_t mask) {
837 encoding.GetRegisterMaskIndexEncoding().Store(region_, mask);
David Brazdilf677ebf2015-05-29 16:29:43 +0100838 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100839
David Srbecky45aa5982016-03-18 02:15:09 +0000840 ALWAYS_INLINE uint32_t GetStackMaskIndex(const StackMapEncoding& encoding) const {
841 return encoding.GetStackMaskIndexEncoding().Load(region_);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100842 }
843
David Srbecky45aa5982016-03-18 02:15:09 +0000844 ALWAYS_INLINE void SetStackMaskIndex(const StackMapEncoding& encoding, uint32_t mask) {
845 encoding.GetStackMaskIndexEncoding().Store(region_, mask);
David Srbecky09ed0982016-02-12 21:58:43 +0000846 }
847
848 ALWAYS_INLINE bool HasDexRegisterMap(const StackMapEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +0100849 return GetDexRegisterMapOffset(encoding) != kNoDexRegisterMap;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100850 }
851
David Srbecky09ed0982016-02-12 21:58:43 +0000852 ALWAYS_INLINE bool HasInlineInfo(const StackMapEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800853 return GetInlineInfoIndex(encoding) != kNoInlineInfo;
Roland Levillain442b46a2015-02-18 16:54:21 +0000854 }
855
David Srbecky09ed0982016-02-12 21:58:43 +0000856 ALWAYS_INLINE bool Equals(const StackMap& other) const {
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800857 return region_.pointer() == other.region_.pointer() &&
858 region_.size() == other.region_.size() &&
859 region_.BitOffset() == other.region_.BitOffset();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100860 }
861
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100862 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100863 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000864 const CodeInfoEncoding& encoding,
Roland Levillainf2650d12015-05-28 14:53:28 +0100865 uint32_t code_offset,
866 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800867 InstructionSet instruction_set,
Roland Levillainf2650d12015-05-28 14:53:28 +0100868 const std::string& header_suffix = "") const;
869
Roland Levillain442b46a2015-02-18 16:54:21 +0000870 // Special (invalid) offset for the DexRegisterMapOffset field meaning
871 // that there is no Dex register map for this stack map.
872 static constexpr uint32_t kNoDexRegisterMap = -1;
873
874 // Special (invalid) offset for the InlineDescriptorOffset field meaning
875 // that there is no inline info for this stack map.
876 static constexpr uint32_t kNoInlineInfo = -1;
877
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100878 private:
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100879 static constexpr int kFixedSize = 0;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100880
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800881 BitMemoryRegion region_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100882
Nicolas Geoffray39468442014-09-02 15:17:15 +0100883 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100884};
885
David Srbecky61b28a12016-02-25 21:55:03 +0000886class InlineInfoEncoding {
887 public:
888 void SetFromSizes(size_t method_index_max,
889 size_t dex_pc_max,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000890 size_t extra_data_max,
David Srbecky61b28a12016-02-25 21:55:03 +0000891 size_t dex_register_map_size) {
892 total_bit_size_ = kMethodIndexBitOffset;
893 total_bit_size_ += MinimumBitsToStore(method_index_max);
894
895 dex_pc_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100896 // Note: We're not encoding the dex pc if there is none. That's the case
897 // for an intrinsified native method, such as String.charAt().
898 if (dex_pc_max != DexFile::kDexNoIndex) {
899 total_bit_size_ += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max);
900 }
David Srbecky61b28a12016-02-25 21:55:03 +0000901
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000902 extra_data_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
903 total_bit_size_ += MinimumBitsToStore(extra_data_max);
David Srbecky61b28a12016-02-25 21:55:03 +0000904
905 // We also need +1 for kNoDexRegisterMap, but since the size is strictly
906 // greater than any offset we might try to encode, we already implicitly have it.
907 dex_register_map_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
908 total_bit_size_ += MinimumBitsToStore(dex_register_map_size);
909 }
910
911 ALWAYS_INLINE FieldEncoding GetMethodIndexEncoding() const {
912 return FieldEncoding(kMethodIndexBitOffset, dex_pc_bit_offset_);
913 }
914 ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000915 return FieldEncoding(dex_pc_bit_offset_, extra_data_bit_offset_, -1 /* min_value */);
David Srbecky61b28a12016-02-25 21:55:03 +0000916 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000917 ALWAYS_INLINE FieldEncoding GetExtraDataEncoding() const {
918 return FieldEncoding(extra_data_bit_offset_, dex_register_map_bit_offset_);
David Srbecky61b28a12016-02-25 21:55:03 +0000919 }
920 ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const {
921 return FieldEncoding(dex_register_map_bit_offset_, total_bit_size_, -1 /* min_value */);
922 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800923 ALWAYS_INLINE size_t BitSize() const {
924 return total_bit_size_;
David Srbecky61b28a12016-02-25 21:55:03 +0000925 }
926
927 void Dump(VariableIndentationOutputStream* vios) const;
928
Mathieu Chartierc420a802017-02-14 15:16:19 -0800929 // Encode the encoding into the vector.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800930 template<typename Vector>
931 void Encode(Vector* dest) const {
932 static_assert(alignof(InlineInfoEncoding) == 1, "Should not require alignment");
933 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
934 dest->insert(dest->end(), ptr, ptr + sizeof(*this));
935 }
936
Mathieu Chartierc420a802017-02-14 15:16:19 -0800937 // Decode the encoding from a pointer, updates the pointer.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800938 void Decode(const uint8_t** ptr) {
939 *this = *reinterpret_cast<const InlineInfoEncoding*>(*ptr);
940 *ptr += sizeof(*this);
941 }
942
David Srbecky61b28a12016-02-25 21:55:03 +0000943 private:
944 static constexpr uint8_t kIsLastBitOffset = 0;
945 static constexpr uint8_t kMethodIndexBitOffset = 1;
946 uint8_t dex_pc_bit_offset_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000947 uint8_t extra_data_bit_offset_;
David Srbecky61b28a12016-02-25 21:55:03 +0000948 uint8_t dex_register_map_bit_offset_;
949 uint8_t total_bit_size_;
950};
951
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100952/**
953 * Inline information for a specific PC. The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100954 *
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000955 * [is_last,
956 * method_index (or ArtMethod high bits),
957 * dex_pc,
958 * extra_data (ArtMethod low bits or 1),
959 * dex_register_map_offset]+.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100960 */
961class InlineInfo {
962 public:
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800963 explicit InlineInfo(BitMemoryRegion region) : region_(region) {}
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100964
David Srbecky61b28a12016-02-25 21:55:03 +0000965 ALWAYS_INLINE uint32_t GetDepth(const InlineInfoEncoding& encoding) const {
966 size_t depth = 0;
967 while (!GetRegionAtDepth(encoding, depth++).LoadBit(0)) { } // Check is_last bit.
968 return depth;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100969 }
970
David Srbecky61b28a12016-02-25 21:55:03 +0000971 ALWAYS_INLINE void SetDepth(const InlineInfoEncoding& encoding, uint32_t depth) {
972 DCHECK_GT(depth, 0u);
973 for (size_t d = 0; d < depth; ++d) {
974 GetRegionAtDepth(encoding, d).StoreBit(0, d == depth - 1); // Set is_last bit.
975 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100976 }
977
David Srbecky61b28a12016-02-25 21:55:03 +0000978 ALWAYS_INLINE uint32_t GetMethodIndexAtDepth(const InlineInfoEncoding& encoding,
979 uint32_t depth) const {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000980 DCHECK(!EncodesArtMethodAtDepth(encoding, depth));
David Srbecky61b28a12016-02-25 21:55:03 +0000981 return encoding.GetMethodIndexEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100982 }
983
David Srbecky61b28a12016-02-25 21:55:03 +0000984 ALWAYS_INLINE void SetMethodIndexAtDepth(const InlineInfoEncoding& encoding,
985 uint32_t depth,
986 uint32_t index) {
987 encoding.GetMethodIndexEncoding().Store(GetRegionAtDepth(encoding, depth), index);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100988 }
989
David Srbecky61b28a12016-02-25 21:55:03 +0000990 ALWAYS_INLINE uint32_t GetDexPcAtDepth(const InlineInfoEncoding& encoding,
991 uint32_t depth) const {
992 return encoding.GetDexPcEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100993 }
994
David Srbecky61b28a12016-02-25 21:55:03 +0000995 ALWAYS_INLINE void SetDexPcAtDepth(const InlineInfoEncoding& encoding,
996 uint32_t depth,
997 uint32_t dex_pc) {
998 encoding.GetDexPcEncoding().Store(GetRegionAtDepth(encoding, depth), dex_pc);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100999 }
1000
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001001 ALWAYS_INLINE bool EncodesArtMethodAtDepth(const InlineInfoEncoding& encoding,
1002 uint32_t depth) const {
1003 return (encoding.GetExtraDataEncoding().Load(GetRegionAtDepth(encoding, depth)) & 1) == 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001004 }
1005
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001006 ALWAYS_INLINE void SetExtraDataAtDepth(const InlineInfoEncoding& encoding,
1007 uint32_t depth,
1008 uint32_t extra_data) {
1009 encoding.GetExtraDataEncoding().Store(GetRegionAtDepth(encoding, depth), extra_data);
1010 }
1011
1012 ALWAYS_INLINE ArtMethod* GetArtMethodAtDepth(const InlineInfoEncoding& encoding,
1013 uint32_t depth) const {
1014 uint32_t low_bits = encoding.GetExtraDataEncoding().Load(GetRegionAtDepth(encoding, depth));
1015 uint32_t high_bits = encoding.GetMethodIndexEncoding().Load(GetRegionAtDepth(encoding, depth));
1016 if (high_bits == 0) {
1017 return reinterpret_cast<ArtMethod*>(low_bits);
1018 } else {
1019 uint64_t address = high_bits;
1020 address = address << 32;
1021 return reinterpret_cast<ArtMethod*>(address | low_bits);
1022 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001023 }
1024
David Srbecky61b28a12016-02-25 21:55:03 +00001025 ALWAYS_INLINE uint32_t GetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding,
1026 uint32_t depth) const {
1027 return encoding.GetDexRegisterMapEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001028 }
1029
David Srbecky61b28a12016-02-25 21:55:03 +00001030 ALWAYS_INLINE void SetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding,
1031 uint32_t depth,
1032 uint32_t offset) {
1033 encoding.GetDexRegisterMapEncoding().Store(GetRegionAtDepth(encoding, depth), offset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001034 }
1035
David Srbecky61b28a12016-02-25 21:55:03 +00001036 ALWAYS_INLINE bool HasDexRegisterMapAtDepth(const InlineInfoEncoding& encoding,
1037 uint32_t depth) const {
1038 return GetDexRegisterMapOffsetAtDepth(encoding, depth) != StackMap::kNoDexRegisterMap;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001039 }
1040
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001041 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +00001042 const CodeInfo& info,
1043 uint16_t* number_of_dex_registers) const;
Roland Levillain1c1da432015-07-16 11:54:44 +01001044
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001045 private:
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001046 ALWAYS_INLINE BitMemoryRegion GetRegionAtDepth(const InlineInfoEncoding& encoding,
1047 uint32_t depth) const {
1048 size_t entry_size = encoding.BitSize();
David Srbecky61b28a12016-02-25 21:55:03 +00001049 DCHECK_GT(entry_size, 0u);
1050 return region_.Subregion(depth * entry_size, entry_size);
1051 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001052
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001053 BitMemoryRegion region_;
1054};
1055
1056// Bit sized region encoding, may be more than 255 bits.
1057class BitRegionEncoding {
1058 public:
1059 uint32_t num_bits = 0;
1060
1061 ALWAYS_INLINE size_t BitSize() const {
1062 return num_bits;
1063 }
1064
1065 template<typename Vector>
1066 void Encode(Vector* dest) const {
1067 EncodeUnsignedLeb128(dest, num_bits); // Use leb in case num_bits is greater than 255.
1068 }
1069
1070 void Decode(const uint8_t** ptr) {
1071 num_bits = DecodeUnsignedLeb128(ptr);
1072 }
1073};
1074
1075// A table of bit sized encodings.
1076template <typename Encoding>
1077struct BitEncodingTable {
1078 static constexpr size_t kInvalidOffset = static_cast<size_t>(-1);
1079 // How the encoding is laid out (serialized).
1080 Encoding encoding;
1081
1082 // Number of entries in the table (serialized).
1083 size_t num_entries;
1084
1085 // Bit offset for the base of the table (computed).
1086 size_t bit_offset = kInvalidOffset;
1087
1088 template<typename Vector>
1089 void Encode(Vector* dest) const {
1090 EncodeUnsignedLeb128(dest, num_entries);
1091 encoding.Encode(dest);
1092 }
1093
1094 ALWAYS_INLINE void Decode(const uint8_t** ptr) {
1095 num_entries = DecodeUnsignedLeb128(ptr);
1096 encoding.Decode(ptr);
1097 }
1098
1099 // Set the bit offset in the table and adds the space used by the table to offset.
1100 void UpdateBitOffset(size_t* offset) {
1101 DCHECK(offset != nullptr);
1102 bit_offset = *offset;
1103 *offset += encoding.BitSize() * num_entries;
1104 }
1105
1106 // Return the bit region for the map at index i.
1107 ALWAYS_INLINE BitMemoryRegion BitRegion(MemoryRegion region, size_t index) const {
1108 DCHECK_NE(bit_offset, kInvalidOffset) << "Invalid table offset";
1109 DCHECK_LT(index, num_entries);
1110 const size_t map_size = encoding.BitSize();
1111 return BitMemoryRegion(region, bit_offset + index * map_size, map_size);
1112 }
1113};
1114
1115// A byte sized table of possible variable sized encodings.
1116struct ByteSizedTable {
1117 static constexpr size_t kInvalidOffset = static_cast<size_t>(-1);
1118
1119 // Number of entries in the table (serialized).
1120 size_t num_entries = 0;
1121
1122 // Number of bytes of the table (serialized).
1123 size_t num_bytes;
1124
1125 // Bit offset for the base of the table (computed).
1126 size_t byte_offset = kInvalidOffset;
1127
1128 template<typename Vector>
1129 void Encode(Vector* dest) const {
1130 EncodeUnsignedLeb128(dest, num_entries);
1131 EncodeUnsignedLeb128(dest, num_bytes);
1132 }
1133
1134 ALWAYS_INLINE void Decode(const uint8_t** ptr) {
1135 num_entries = DecodeUnsignedLeb128(ptr);
1136 num_bytes = DecodeUnsignedLeb128(ptr);
1137 }
1138
1139 // Set the bit offset of the table. Adds the total bit size of the table to offset.
1140 void UpdateBitOffset(size_t* offset) {
1141 DCHECK(offset != nullptr);
1142 DCHECK_ALIGNED(*offset, kBitsPerByte);
1143 byte_offset = *offset / kBitsPerByte;
1144 *offset += num_bytes * kBitsPerByte;
1145 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001146};
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001147
David Srbecky09ed0982016-02-12 21:58:43 +00001148// Most of the fields are encoded as ULEB128 to save space.
1149struct CodeInfoEncoding {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001150 static constexpr uint32_t kInvalidSize = static_cast<size_t>(-1);
1151 // Byte sized tables go first to avoid unnecessary alignment bits.
1152 ByteSizedTable dex_register_map;
1153 ByteSizedTable location_catalog;
1154 BitEncodingTable<StackMapEncoding> stack_map;
1155 BitEncodingTable<BitRegionEncoding> register_mask;
1156 BitEncodingTable<BitRegionEncoding> stack_mask;
1157 BitEncodingTable<InlineInfoEncoding> inline_info;
David Srbecky09ed0982016-02-12 21:58:43 +00001158
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001159 CodeInfoEncoding() {}
David Srbecky09ed0982016-02-12 21:58:43 +00001160
1161 explicit CodeInfoEncoding(const void* data) {
1162 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001163 dex_register_map.Decode(&ptr);
1164 location_catalog.Decode(&ptr);
1165 stack_map.Decode(&ptr);
1166 register_mask.Decode(&ptr);
1167 stack_mask.Decode(&ptr);
1168 if (stack_map.encoding.GetInlineInfoEncoding().BitSize() > 0) {
1169 inline_info.Decode(&ptr);
David Srbecky61b28a12016-02-25 21:55:03 +00001170 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001171 inline_info = BitEncodingTable<InlineInfoEncoding>();
David Srbecky61b28a12016-02-25 21:55:03 +00001172 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001173 cache_header_size =
1174 dchecked_integral_cast<uint32_t>(ptr - reinterpret_cast<const uint8_t*>(data));
1175 ComputeTableOffsets();
David Srbecky09ed0982016-02-12 21:58:43 +00001176 }
1177
Mathieu Chartierc420a802017-02-14 15:16:19 -08001178 // Compress is not const since it calculates cache_header_size. This is used by PrepareForFillIn.
David Srbecky09ed0982016-02-12 21:58:43 +00001179 template<typename Vector>
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001180 void Compress(Vector* dest) {
1181 dex_register_map.Encode(dest);
1182 location_catalog.Encode(dest);
1183 stack_map.Encode(dest);
1184 register_mask.Encode(dest);
1185 stack_mask.Encode(dest);
1186 if (stack_map.encoding.GetInlineInfoEncoding().BitSize() > 0) {
1187 inline_info.Encode(dest);
David Srbecky61b28a12016-02-25 21:55:03 +00001188 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001189 cache_header_size = dest->size();
David Srbecky09ed0982016-02-12 21:58:43 +00001190 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001191
1192 ALWAYS_INLINE void ComputeTableOffsets() {
1193 // Skip the header.
1194 size_t bit_offset = HeaderSize() * kBitsPerByte;
1195 // The byte tables must be aligned so they must go first.
1196 dex_register_map.UpdateBitOffset(&bit_offset);
1197 location_catalog.UpdateBitOffset(&bit_offset);
1198 // Other tables don't require alignment.
1199 stack_map.UpdateBitOffset(&bit_offset);
1200 register_mask.UpdateBitOffset(&bit_offset);
1201 stack_mask.UpdateBitOffset(&bit_offset);
1202 inline_info.UpdateBitOffset(&bit_offset);
1203 cache_non_header_size = RoundUp(bit_offset, kBitsPerByte) / kBitsPerByte - HeaderSize();
1204 }
1205
1206 ALWAYS_INLINE size_t HeaderSize() const {
1207 DCHECK_NE(cache_header_size, kInvalidSize) << "Uninitialized";
1208 return cache_header_size;
1209 }
1210
1211 ALWAYS_INLINE size_t NonHeaderSize() const {
1212 DCHECK_NE(cache_non_header_size, kInvalidSize) << "Uninitialized";
1213 return cache_non_header_size;
1214 }
1215
1216 private:
1217 // Computed fields (not serialized).
Mathieu Chartierc420a802017-02-14 15:16:19 -08001218 // Header size in bytes, cached to avoid needing to re-decoding the encoding in HeaderSize.
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001219 uint32_t cache_header_size = kInvalidSize;
Mathieu Chartierc420a802017-02-14 15:16:19 -08001220 // Non header size in bytes, cached to avoid needing to re-decoding the encoding in NonHeaderSize.
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001221 uint32_t cache_non_header_size = kInvalidSize;
David Srbecky09ed0982016-02-12 21:58:43 +00001222};
1223
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001224/**
1225 * Wrapper around all compiler information collected for a method.
1226 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +01001227 *
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001228 * [CodeInfoEncoding, DexRegisterMap+, DexLocationCatalog+, StackMap+, RegisterMask+, StackMask+,
Mathieu Chartierc420a802017-02-14 15:16:19 -08001229 * InlineInfo*]
1230 *
1231 * where CodeInfoEncoding is of the form:
1232 *
1233 * [ByteSizedTable(dex_register_map), ByteSizedTable(location_catalog),
1234 * BitEncodingTable<StackMapEncoding>, BitEncodingTable<BitRegionEncoding>,
1235 * BitEncodingTable<BitRegionEncoding>, BitEncodingTable<InlineInfoEncoding>]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001236 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001237class CodeInfo {
1238 public:
David Srbecky09ed0982016-02-12 21:58:43 +00001239 explicit CodeInfo(MemoryRegion region) : region_(region) {
1240 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001241
Nicolas Geoffray39468442014-09-02 15:17:15 +01001242 explicit CodeInfo(const void* data) {
David Srbecky09ed0982016-02-12 21:58:43 +00001243 CodeInfoEncoding encoding = CodeInfoEncoding(data);
1244 region_ = MemoryRegion(const_cast<void*>(data),
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001245 encoding.HeaderSize() + encoding.NonHeaderSize());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001246 }
1247
David Srbecky09ed0982016-02-12 21:58:43 +00001248 CodeInfoEncoding ExtractEncoding() const {
David Srbecky45aa5982016-03-18 02:15:09 +00001249 CodeInfoEncoding encoding(region_.begin());
Mathieu Chartier01c78142017-01-05 10:17:55 -08001250 AssertValidStackMap(encoding);
1251 return encoding;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001252 }
1253
David Srbecky09ed0982016-02-12 21:58:43 +00001254 bool HasInlineInfo(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001255 return encoding.stack_map.encoding.GetInlineInfoEncoding().BitSize() > 0;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001256 }
1257
David Srbecky09ed0982016-02-12 21:58:43 +00001258 DexRegisterLocationCatalog GetDexRegisterLocationCatalog(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001259 return DexRegisterLocationCatalog(region_.Subregion(encoding.location_catalog.byte_offset,
1260 encoding.location_catalog.num_bytes));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001261 }
1262
Mathieu Chartier12f1b992017-01-19 18:00:45 -08001263 ALWAYS_INLINE size_t GetNumberOfStackMaskBits(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001264 return encoding.stack_mask.encoding.BitSize();
Mathieu Chartier12f1b992017-01-19 18:00:45 -08001265 }
1266
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001267 ALWAYS_INLINE StackMap GetStackMapAt(size_t index, const CodeInfoEncoding& encoding) const {
1268 return StackMap(encoding.stack_map.BitRegion(region_, index));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001269 }
1270
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001271 BitMemoryRegion GetStackMask(size_t index, const CodeInfoEncoding& encoding) const {
1272 return encoding.stack_mask.BitRegion(region_, index);
David Srbecky45aa5982016-03-18 02:15:09 +00001273 }
1274
1275 BitMemoryRegion GetStackMaskOf(const CodeInfoEncoding& encoding,
1276 const StackMap& stack_map) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001277 return GetStackMask(stack_map.GetStackMaskIndex(encoding.stack_map.encoding), encoding);
David Srbecky45aa5982016-03-18 02:15:09 +00001278 }
1279
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001280 BitMemoryRegion GetRegisterMask(size_t index, const CodeInfoEncoding& encoding) const {
1281 return encoding.register_mask.BitRegion(region_, index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001282 }
1283
1284 uint32_t GetRegisterMaskOf(const CodeInfoEncoding& encoding, const StackMap& stack_map) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001285 size_t index = stack_map.GetRegisterMaskIndex(encoding.stack_map.encoding);
1286 return GetRegisterMask(index, encoding).LoadBits(0u, encoding.register_mask.encoding.BitSize());
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001287 }
1288
David Srbecky09ed0982016-02-12 21:58:43 +00001289 uint32_t GetNumberOfLocationCatalogEntries(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001290 return encoding.location_catalog.num_entries;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001291 }
1292
David Srbecky09ed0982016-02-12 21:58:43 +00001293 uint32_t GetDexRegisterLocationCatalogSize(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001294 return encoding.location_catalog.num_bytes;
Roland Levillaina552e1c2015-03-26 15:01:03 +00001295 }
1296
David Srbecky09ed0982016-02-12 21:58:43 +00001297 uint32_t GetNumberOfStackMaps(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001298 return encoding.stack_map.num_entries;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001299 }
1300
David Srbecky45aa5982016-03-18 02:15:09 +00001301 // Get the size of all the stack maps of this CodeInfo object, in bits. Not byte aligned.
1302 ALWAYS_INLINE size_t GetStackMapsSizeInBits(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001303 return encoding.stack_map.encoding.BitSize() * GetNumberOfStackMaps(encoding);
Nicolas Geoffray6530baf2015-05-26 15:22:58 +01001304 }
1305
David Brazdilf677ebf2015-05-29 16:29:43 +01001306 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map,
David Srbecky09ed0982016-02-12 21:58:43 +00001307 const CodeInfoEncoding& encoding,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001308 size_t number_of_dex_registers) const {
1309 if (!stack_map.HasDexRegisterMap(encoding.stack_map.encoding)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001310 return DexRegisterMap();
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001311 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001312 const uint32_t offset = encoding.dex_register_map.byte_offset +
1313 stack_map.GetDexRegisterMapOffset(encoding.stack_map.encoding);
1314 size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers);
1315 return DexRegisterMap(region_.Subregion(offset, size));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001316 }
1317
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001318 size_t GetDexRegisterMapsSize(const CodeInfoEncoding& encoding,
1319 uint32_t number_of_dex_registers) const {
1320 size_t total = 0;
1321 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
1322 StackMap stack_map = GetStackMapAt(i, encoding);
1323 DexRegisterMap map(GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers));
1324 total += map.Size();
1325 }
1326 return total;
1327 }
1328
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001329 // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`.
1330 DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
1331 InlineInfo inline_info,
David Srbecky09ed0982016-02-12 21:58:43 +00001332 const CodeInfoEncoding& encoding,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001333 uint32_t number_of_dex_registers) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001334 if (!inline_info.HasDexRegisterMapAtDepth(encoding.inline_info.encoding, depth)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001335 return DexRegisterMap();
1336 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001337 uint32_t offset = encoding.dex_register_map.byte_offset +
1338 inline_info.GetDexRegisterMapOffsetAtDepth(encoding.inline_info.encoding, depth);
David Srbecky09ed0982016-02-12 21:58:43 +00001339 size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001340 return DexRegisterMap(region_.Subregion(offset, size));
1341 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001342 }
1343
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001344 InlineInfo GetInlineInfo(size_t index, const CodeInfoEncoding& encoding) const {
Mathieu Chartierc420a802017-02-14 15:16:19 -08001345 // Since we do not know the depth, we just return the whole remaining map. The caller may
1346 // access the inline info for arbitrary depths. To return the precise inline info we would need
1347 // to count the depth before returning.
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001348 // TODO: Clean this up.
1349 const size_t bit_offset = encoding.inline_info.bit_offset +
1350 index * encoding.inline_info.encoding.BitSize();
1351 return InlineInfo(BitMemoryRegion(region_, bit_offset, region_.size_in_bits() - bit_offset));
1352 }
1353
David Srbecky09ed0982016-02-12 21:58:43 +00001354 InlineInfo GetInlineInfoOf(StackMap stack_map, const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001355 DCHECK(stack_map.HasInlineInfo(encoding.stack_map.encoding));
1356 uint32_t index = stack_map.GetInlineInfoIndex(encoding.stack_map.encoding);
1357 return GetInlineInfo(index, encoding);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001358 }
1359
David Srbecky09ed0982016-02-12 21:58:43 +00001360 StackMap GetStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1361 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +01001362 StackMap stack_map = GetStackMapAt(i, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001363 if (stack_map.GetDexPc(encoding.stack_map.encoding) == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001364 return stack_map;
1365 }
1366 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001367 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001368 }
1369
David Brazdil77a48ae2015-09-15 12:34:04 +00001370 // Searches the stack map list backwards because catch stack maps are stored
1371 // at the end.
David Srbecky09ed0982016-02-12 21:58:43 +00001372 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1373 for (size_t i = GetNumberOfStackMaps(encoding); i > 0; --i) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001374 StackMap stack_map = GetStackMapAt(i - 1, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001375 if (stack_map.GetDexPc(encoding.stack_map.encoding) == dex_pc) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001376 return stack_map;
1377 }
1378 }
1379 return StackMap();
1380 }
1381
David Srbecky09ed0982016-02-12 21:58:43 +00001382 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1383 size_t e = GetNumberOfStackMaps(encoding);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001384 if (e == 0) {
1385 // There cannot be OSR stack map if there is no stack map.
1386 return StackMap();
1387 }
1388 // Walk over all stack maps. If two consecutive stack maps are identical, then we
1389 // have found a stack map suitable for OSR.
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001390 const StackMapEncoding& stack_map_encoding = encoding.stack_map.encoding;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001391 for (size_t i = 0; i < e - 1; ++i) {
1392 StackMap stack_map = GetStackMapAt(i, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001393 if (stack_map.GetDexPc(stack_map_encoding) == dex_pc) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001394 StackMap other = GetStackMapAt(i + 1, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001395 if (other.GetDexPc(stack_map_encoding) == dex_pc &&
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001396 other.GetNativePcOffset(stack_map_encoding, kRuntimeISA) ==
1397 stack_map.GetNativePcOffset(stack_map_encoding, kRuntimeISA)) {
David Srbecky09ed0982016-02-12 21:58:43 +00001398 DCHECK_EQ(other.GetDexRegisterMapOffset(stack_map_encoding),
1399 stack_map.GetDexRegisterMapOffset(stack_map_encoding));
1400 DCHECK(!stack_map.HasInlineInfo(stack_map_encoding));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001401 if (i < e - 2) {
1402 // Make sure there are not three identical stack maps following each other.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001403 DCHECK_NE(
1404 stack_map.GetNativePcOffset(stack_map_encoding, kRuntimeISA),
1405 GetStackMapAt(i + 2, encoding).GetNativePcOffset(stack_map_encoding, kRuntimeISA));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001406 }
1407 return stack_map;
1408 }
1409 }
1410 }
1411 return StackMap();
1412 }
1413
David Brazdilf677ebf2015-05-29 16:29:43 +01001414 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset,
David Srbecky09ed0982016-02-12 21:58:43 +00001415 const CodeInfoEncoding& encoding) const {
David Brazdil77a48ae2015-09-15 12:34:04 +00001416 // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack
1417 // maps are not. If we knew that the method does not have try/catch,
1418 // we could do binary search.
David Srbecky09ed0982016-02-12 21:58:43 +00001419 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +01001420 StackMap stack_map = GetStackMapAt(i, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001421 if (stack_map.GetNativePcOffset(encoding.stack_map.encoding, kRuntimeISA) ==
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001422 native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001423 return stack_map;
1424 }
1425 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001426 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001427 }
1428
Roland Levillainf2650d12015-05-28 14:53:28 +01001429 // Dump this CodeInfo object on `os`. `code_offset` is the (absolute)
1430 // native PC of the compiled method and `number_of_dex_registers` the
1431 // number of Dex virtual registers used in this method. If
1432 // `dump_stack_maps` is true, also dump the stack maps and the
1433 // associated Dex register maps.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001434 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001435 uint32_t code_offset,
1436 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001437 bool dump_stack_maps,
1438 InstructionSet instruction_set) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001439
Mathieu Chartier01c78142017-01-05 10:17:55 -08001440 // Check that the code info has valid stack map and abort if it does not.
1441 void AssertValidStackMap(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001442 if (region_.size() != 0 && region_.size_in_bits() < GetStackMapsSizeInBits(encoding)) {
Mathieu Chartier01c78142017-01-05 10:17:55 -08001443 LOG(FATAL) << region_.size() << "\n"
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001444 << encoding.HeaderSize() << "\n"
1445 << encoding.NonHeaderSize() << "\n"
1446 << encoding.location_catalog.num_entries << "\n"
1447 << encoding.stack_map.num_entries << "\n"
1448 << encoding.stack_map.encoding.BitSize();
Mathieu Chartier01c78142017-01-05 10:17:55 -08001449 }
1450 }
1451
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001452 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +00001453 // Compute the size of the Dex register map associated to the stack map at
1454 // `dex_register_map_offset_in_code_info`.
David Srbecky09ed0982016-02-12 21:58:43 +00001455 size_t ComputeDexRegisterMapSizeOf(const CodeInfoEncoding& encoding,
1456 uint32_t dex_register_map_offset_in_code_info,
Roland Levillaina552e1c2015-03-26 15:01:03 +00001457 uint16_t number_of_dex_registers) const {
1458 // Offset where the actual mapping data starts within art::DexRegisterMap.
1459 size_t location_mapping_data_offset_in_dex_register_map =
1460 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1461 // Create a temporary art::DexRegisterMap to be able to call
1462 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1463 DexRegisterMap dex_register_map_without_locations(
1464 MemoryRegion(region_.Subregion(dex_register_map_offset_in_code_info,
1465 location_mapping_data_offset_in_dex_register_map)));
1466 size_t number_of_live_dex_registers =
1467 dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
1468 size_t location_mapping_data_size_in_bits =
David Srbecky09ed0982016-02-12 21:58:43 +00001469 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries(encoding))
Roland Levillaina552e1c2015-03-26 15:01:03 +00001470 * number_of_live_dex_registers;
1471 size_t location_mapping_data_size_in_bytes =
1472 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1473 size_t dex_register_map_size =
1474 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1475 return dex_register_map_size;
1476 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001477
Roland Levillaina552e1c2015-03-26 15:01:03 +00001478 // Compute the size of a Dex register location catalog starting at offset `origin`
1479 // in `region_` and containing `number_of_dex_locations` entries.
1480 size_t ComputeDexRegisterLocationCatalogSize(uint32_t origin,
1481 uint32_t number_of_dex_locations) const {
1482 // TODO: Ideally, we would like to use art::DexRegisterLocationCatalog::Size or
1483 // art::DexRegisterLocationCatalog::FindLocationOffset, but the
1484 // DexRegisterLocationCatalog is not yet built. Try to factor common code.
1485 size_t offset = origin + DexRegisterLocationCatalog::kFixedSize;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001486
Roland Levillaina552e1c2015-03-26 15:01:03 +00001487 // Skip the first `number_of_dex_locations - 1` entries.
1488 for (uint16_t i = 0; i < number_of_dex_locations; ++i) {
1489 // Read the first next byte and inspect its first 3 bits to decide
1490 // whether it is a short or a large location.
1491 DexRegisterLocationCatalog::ShortLocation first_byte =
1492 region_.LoadUnaligned<DexRegisterLocationCatalog::ShortLocation>(offset);
1493 DexRegisterLocation::Kind kind =
1494 DexRegisterLocationCatalog::ExtractKindFromShortLocation(first_byte);
1495 if (DexRegisterLocation::IsShortLocationKind(kind)) {
1496 // Short location. Skip the current byte.
1497 offset += DexRegisterLocationCatalog::SingleShortEntrySize();
1498 } else {
1499 // Large location. Skip the 5 next bytes.
1500 offset += DexRegisterLocationCatalog::SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001501 }
1502 }
1503 size_t size = offset - origin;
1504 return size;
1505 }
1506
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001507 MemoryRegion region_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001508 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001509};
1510
Roland Levillain1c1da432015-07-16 11:54:44 +01001511#undef ELEMENT_BYTE_OFFSET_AFTER
1512#undef ELEMENT_BIT_OFFSET_AFTER
1513
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001514} // namespace art
1515
1516#endif // ART_RUNTIME_STACK_MAP_H_