blob: 274e2b20b434a0232215403fdff109da7945b245 [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
Andreas Gampe69489fa2017-06-08 18:03:25 -070020#include <limits>
21
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080022#include "arch/code_offset.h"
David Sehr1ce2b3b2018-04-05 11:02:03 -070023#include "base/bit_memory_region.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010024#include "base/bit_utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "base/bit_vector.h"
David Sehr67bf42e2018-02-26 16:43:04 -080026#include "base/leb128.h"
David Sehr1ce2b3b2018-04-05 11:02:03 -070027#include "base/memory_region.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_file_types.h"
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070029#include "method_info.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010030
31namespace art {
32
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010033class VariableIndentationOutputStream;
34
Roland Levillaina2d8ec62015-03-12 15:25:29 +000035// Size of a frame slot, in bytes. This constant is a signed value,
36// to please the compiler in arithmetic operations involving int32_t
37// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000038static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +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
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700370 void Dump(VariableIndentationOutputStream* vios,
371 const CodeInfo& code_info);
Roland Levillain0396ed72015-05-27 15:12:19 +0100372
Roland Levillaina552e1c2015-03-26 15:01:03 +0000373 // Special (invalid) Dex register location catalog entry index meaning
374 // that there is no location for a given Dex register (i.e., it is
375 // mapped to a DexRegisterLocation::Kind::kNone location).
376 static constexpr size_t kNoLocationEntryIndex = -1;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100377
Roland Levillain12baf472015-03-05 12:41:42 +0000378 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000379 static constexpr int kFixedSize = 0;
380
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000381 // Width of the kind "field" in a short location, in bits.
382 static constexpr size_t kKindBits = 3;
383 // Width of the value "field" in a short location, in bits.
384 static constexpr size_t kValueBits = 5;
385
386 static constexpr uint8_t kKindMask = (1 << kKindBits) - 1;
387 static constexpr int32_t kValueMask = (1 << kValueBits) - 1;
388 static constexpr size_t kKindOffset = 0;
389 static constexpr size_t kValueOffset = kKindBits;
390
Roland Levillaina552e1c2015-03-26 15:01:03 +0000391 static bool IsShortStackOffsetValue(int32_t value) {
392 DCHECK_EQ(value % kFrameSlotSize, 0);
393 return IsShortValue(value / kFrameSlotSize);
394 }
395
396 static bool IsShortConstantValue(int32_t value) {
397 return IsShortValue(value);
398 }
399
400 static bool IsShortValue(int32_t value) {
401 return IsUint<kValueBits>(value);
402 }
403
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000404 static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000405 uint8_t kind_integer_value = static_cast<uint8_t>(kind);
406 DCHECK(IsUint<kKindBits>(kind_integer_value)) << kind_integer_value;
407 DCHECK(IsShortValue(value)) << value;
408 return (kind_integer_value & kKindMask) << kKindOffset
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000409 | (value & kValueMask) << kValueOffset;
410 }
411
412 static DexRegisterLocation::Kind ExtractKindFromShortLocation(ShortLocation location) {
413 uint8_t kind = (location >> kKindOffset) & kKindMask;
414 DCHECK_LE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kLastLocationKind));
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000415 // We do not encode kNone locations in the stack map.
416 DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000417 return static_cast<DexRegisterLocation::Kind>(kind);
418 }
419
420 static int32_t ExtractValueFromShortLocation(ShortLocation location) {
421 return (location >> kValueOffset) & kValueMask;
422 }
423
424 // Extract a location kind from the byte at position `offset`.
425 DexRegisterLocation::Kind ExtractKindAtOffset(size_t offset) const {
426 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
427 return ExtractKindFromShortLocation(first_byte);
428 }
429
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100430 MemoryRegion region_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000431
432 friend class CodeInfo;
433 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100434};
435
Roland Levillaina552e1c2015-03-26 15:01:03 +0000436/* Information on Dex register locations for a specific PC, mapping a
437 * stack map's Dex register to a location entry in a DexRegisterLocationCatalog.
438 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100439 *
440 * [live_bit_mask, entries*]
441 *
Roland Levillaina552e1c2015-03-26 15:01:03 +0000442 * where entries are concatenated unsigned integer values encoded on a number
443 * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending
444 * on the number of entries in the Dex register location catalog
445 * (see DexRegisterMap::SingleEntrySizeInBits). The map is 1-byte aligned.
446 */
447class DexRegisterMap {
448 public:
449 explicit DexRegisterMap(MemoryRegion region) : region_(region) {}
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000450 DexRegisterMap() {}
451
452 bool IsValid() const { return region_.pointer() != nullptr; }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000453
454 // Get the surface kind of Dex register `dex_register_number`.
455 DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number,
456 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100457 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000458 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000459 return DexRegisterLocation::ConvertToSurfaceKind(
David Brazdilf677ebf2015-05-29 16:29:43 +0100460 GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info, enc));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000461 }
462
463 // Get the internal kind of Dex register `dex_register_number`.
464 DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number,
465 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100466 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000467 const CodeInfoEncoding& enc) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000468
469 // Get the Dex register location `dex_register_number`.
470 DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number,
471 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100472 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000473 const CodeInfoEncoding& enc) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000474
475 int32_t GetStackOffsetInBytes(uint16_t dex_register_number,
476 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100477 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000478 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000479 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100480 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000481 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
482 // GetDexRegisterLocation returns the offset in bytes.
483 return location.GetValue();
484 }
485
486 int32_t GetConstant(uint16_t dex_register_number,
487 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100488 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000489 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000490 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100491 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
David Srbecky7dc11782016-02-25 13:23:56 +0000492 DCHECK_EQ(location.GetKind(), DexRegisterLocation::Kind::kConstant);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000493 return location.GetValue();
494 }
495
496 int32_t GetMachineRegister(uint16_t dex_register_number,
497 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100498 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000499 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000500 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100501 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
David Brazdild9cb68e2015-08-25 13:52:43 +0100502 DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister ||
503 location.GetInternalKind() == DexRegisterLocation::Kind::kInRegisterHigh ||
504 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister ||
505 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh)
David Srbecky7dc11782016-02-25 13:23:56 +0000506 << location.GetInternalKind();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000507 return location.GetValue();
508 }
509
510 // Get the index of the entry in the Dex register location catalog
511 // corresponding to `dex_register_number`.
512 size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number,
513 uint16_t number_of_dex_registers,
514 size_t number_of_location_catalog_entries) const {
515 if (!IsDexRegisterLive(dex_register_number)) {
516 return DexRegisterLocationCatalog::kNoLocationEntryIndex;
517 }
518
519 if (number_of_location_catalog_entries == 1) {
520 // We do not allocate space for location maps in the case of a
521 // single-entry location catalog, as it is useless. The only valid
522 // entry index is 0;
523 return 0;
524 }
525
526 // The bit offset of the beginning of the map locations.
527 size_t map_locations_offset_in_bits =
528 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
529 size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number);
530 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
531 // The bit size of an entry.
532 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
533 // The bit offset where `index_in_dex_register_map` is located.
534 size_t entry_offset_in_bits =
535 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
536 size_t location_catalog_entry_index =
537 region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits);
538 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
539 return location_catalog_entry_index;
540 }
541
542 // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`.
543 void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map,
544 size_t location_catalog_entry_index,
545 uint16_t number_of_dex_registers,
546 size_t number_of_location_catalog_entries) {
547 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
548 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
549
550 if (number_of_location_catalog_entries == 1) {
551 // We do not allocate space for location maps in the case of a
552 // single-entry location catalog, as it is useless.
553 return;
554 }
555
556 // The bit offset of the beginning of the map locations.
557 size_t map_locations_offset_in_bits =
558 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
559 // The bit size of an entry.
560 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
561 // The bit offset where `index_in_dex_register_map` is located.
562 size_t entry_offset_in_bits =
563 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
564 region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits);
565 }
566
567 void SetLiveBitMask(uint16_t number_of_dex_registers,
568 const BitVector& live_dex_registers_mask) {
569 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
570 for (uint16_t i = 0; i < number_of_dex_registers; ++i) {
571 region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i));
572 }
573 }
574
Mingyao Yang01b47b02017-02-03 12:09:57 -0800575 ALWAYS_INLINE bool IsDexRegisterLive(uint16_t dex_register_number) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000576 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
577 return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number);
578 }
579
580 size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const {
581 size_t number_of_live_dex_registers = 0;
582 for (size_t i = 0; i < number_of_dex_registers; ++i) {
583 if (IsDexRegisterLive(i)) {
584 ++number_of_live_dex_registers;
585 }
586 }
587 return number_of_live_dex_registers;
588 }
589
590 static size_t GetLiveBitMaskOffset() {
591 return kFixedSize;
592 }
593
594 // Compute the size of the live register bit mask (in bytes), for a
595 // method having `number_of_dex_registers` Dex registers.
596 static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) {
597 return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte;
598 }
599
600 static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) {
601 return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers);
602 }
603
604 size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers,
605 size_t number_of_location_catalog_entries) const {
606 size_t location_mapping_data_size_in_bits =
607 GetNumberOfLiveDexRegisters(number_of_dex_registers)
608 * SingleEntrySizeInBits(number_of_location_catalog_entries);
609 return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
610 }
611
612 // Return the size of a map entry in bits. Note that if
613 // `number_of_location_catalog_entries` equals 1, this function returns 0,
614 // which is fine, as there is no need to allocate a map for a
615 // single-entry location catalog; the only valid location catalog entry index
616 // for a live register in this case is 0 and there is no need to
617 // store it.
618 static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) {
619 // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2.
620 return number_of_location_catalog_entries == 0
621 ? 0u
622 : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries));
623 }
624
625 // Return the size of the DexRegisterMap object, in bytes.
626 size_t Size() const {
627 return region_.size();
628 }
629
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100630 void Dump(VariableIndentationOutputStream* vios,
631 const CodeInfo& code_info, uint16_t number_of_dex_registers) const;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100632
Roland Levillaina552e1c2015-03-26 15:01:03 +0000633 private:
634 // Return the index in the Dex register map corresponding to the Dex
635 // register number `dex_register_number`.
636 size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const {
637 if (!IsDexRegisterLive(dex_register_number)) {
638 return kInvalidIndexInDexRegisterMap;
639 }
640 return GetNumberOfLiveDexRegisters(dex_register_number);
641 }
642
643 // Special (invalid) Dex register map entry index meaning that there
644 // is no index in the map for a given Dex register (i.e., it must
645 // have been mapped to a DexRegisterLocation::Kind::kNone location).
646 static constexpr size_t kInvalidIndexInDexRegisterMap = -1;
647
648 static constexpr int kFixedSize = 0;
649
650 MemoryRegion region_;
651
652 friend class CodeInfo;
653 friend class StackMapStream;
654};
655
David Srbecky09ed0982016-02-12 21:58:43 +0000656// Represents bit range of bit-packed integer field.
657// We reuse the idea from ULEB128p1 to support encoding of -1 (aka 0xFFFFFFFF).
658// If min_value is set to -1, we implicitly subtract one from any loaded value,
659// and add one to any stored value. This is generalized to any negative values.
660// In other words, min_value acts as a base and the stored value is added to it.
661struct FieldEncoding {
662 FieldEncoding(size_t start_offset, size_t end_offset, int32_t min_value = 0)
663 : start_offset_(start_offset), end_offset_(end_offset), min_value_(min_value) {
664 DCHECK_LE(start_offset_, end_offset_);
665 DCHECK_LE(BitSize(), 32u);
666 }
667
668 ALWAYS_INLINE size_t BitSize() const { return end_offset_ - start_offset_; }
669
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800670 template <typename Region>
671 ALWAYS_INLINE int32_t Load(const Region& region) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000672 DCHECK_LE(end_offset_, region.size_in_bits());
Mathieu Chartier3ceedc02017-01-25 11:11:02 -0800673 return static_cast<int32_t>(region.LoadBits(start_offset_, BitSize())) + min_value_;
David Srbecky09ed0982016-02-12 21:58:43 +0000674 }
675
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800676 template <typename Region>
677 ALWAYS_INLINE void Store(Region region, int32_t value) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000678 region.StoreBits(start_offset_, value - min_value_, BitSize());
679 DCHECK_EQ(Load(region), value);
680 }
681
682 private:
683 size_t start_offset_;
684 size_t end_offset_;
685 int32_t min_value_;
686};
687
David Brazdilf677ebf2015-05-29 16:29:43 +0100688class StackMapEncoding {
689 public:
Andreas Gamped9911ee2017-03-27 13:27:24 -0700690 StackMapEncoding()
691 : dex_pc_bit_offset_(0),
692 dex_register_map_bit_offset_(0),
693 inline_info_bit_offset_(0),
694 register_mask_index_bit_offset_(0),
695 stack_mask_index_bit_offset_(0),
696 total_bit_size_(0) {}
David Brazdilf677ebf2015-05-29 16:29:43 +0100697
David Srbecky09ed0982016-02-12 21:58:43 +0000698 // Set stack map bit layout based on given sizes.
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800699 // Returns the size of stack map in bits.
David Srbecky09ed0982016-02-12 21:58:43 +0000700 size_t SetFromSizes(size_t native_pc_max,
701 size_t dex_pc_max,
702 size_t dex_register_map_size,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800703 size_t number_of_inline_info,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800704 size_t number_of_register_masks,
David Srbecky45aa5982016-03-18 02:15:09 +0000705 size_t number_of_stack_masks) {
706 total_bit_size_ = 0;
707 DCHECK_EQ(kNativePcBitOffset, total_bit_size_);
708 total_bit_size_ += MinimumBitsToStore(native_pc_max);
David Brazdilf677ebf2015-05-29 16:29:43 +0100709
David Srbecky45aa5982016-03-18 02:15:09 +0000710 dex_pc_bit_offset_ = total_bit_size_;
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000711 // Note: We're not encoding the dex pc if there is none. That's the case
712 // for an intrinsified native method, such as String.charAt().
713 if (dex_pc_max != dex::kDexNoIndex) {
714 total_bit_size_ += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max);
715 }
David Srbecky09ed0982016-02-12 21:58:43 +0000716
717 // We also need +1 for kNoDexRegisterMap, but since the size is strictly
718 // greater than any offset we might try to encode, we already implicitly have it.
David Srbecky45aa5982016-03-18 02:15:09 +0000719 dex_register_map_bit_offset_ = total_bit_size_;
720 total_bit_size_ += MinimumBitsToStore(dex_register_map_size);
David Srbecky09ed0982016-02-12 21:58:43 +0000721
722 // We also need +1 for kNoInlineInfo, but since the inline_info_size is strictly
723 // greater than the offset we might try to encode, we already implicitly have it.
724 // If inline_info_size is zero, we can encode only kNoInlineInfo (in zero bits).
David Srbecky45aa5982016-03-18 02:15:09 +0000725 inline_info_bit_offset_ = total_bit_size_;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800726 total_bit_size_ += MinimumBitsToStore(number_of_inline_info);
David Srbecky09ed0982016-02-12 21:58:43 +0000727
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800728 register_mask_index_bit_offset_ = total_bit_size_;
729 total_bit_size_ += MinimumBitsToStore(number_of_register_masks);
David Srbecky09ed0982016-02-12 21:58:43 +0000730
David Srbecky45aa5982016-03-18 02:15:09 +0000731 stack_mask_index_bit_offset_ = total_bit_size_;
732 total_bit_size_ += MinimumBitsToStore(number_of_stack_masks);
David Srbecky09ed0982016-02-12 21:58:43 +0000733
David Srbecky45aa5982016-03-18 02:15:09 +0000734 return total_bit_size_;
David Brazdilf677ebf2015-05-29 16:29:43 +0100735 }
736
David Srbecky09ed0982016-02-12 21:58:43 +0000737 ALWAYS_INLINE FieldEncoding GetNativePcEncoding() const {
738 return FieldEncoding(kNativePcBitOffset, dex_pc_bit_offset_);
739 }
740 ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const {
741 return FieldEncoding(dex_pc_bit_offset_, dex_register_map_bit_offset_, -1 /* min_value */);
742 }
743 ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const {
744 return FieldEncoding(dex_register_map_bit_offset_, inline_info_bit_offset_, -1 /* min_value */);
745 }
746 ALWAYS_INLINE FieldEncoding GetInlineInfoEncoding() const {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800747 return FieldEncoding(inline_info_bit_offset_,
748 register_mask_index_bit_offset_,
749 -1 /* min_value */);
David Srbecky09ed0982016-02-12 21:58:43 +0000750 }
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800751 ALWAYS_INLINE FieldEncoding GetRegisterMaskIndexEncoding() const {
752 return FieldEncoding(register_mask_index_bit_offset_, stack_mask_index_bit_offset_);
David Srbecky09ed0982016-02-12 21:58:43 +0000753 }
David Srbecky45aa5982016-03-18 02:15:09 +0000754 ALWAYS_INLINE FieldEncoding GetStackMaskIndexEncoding() const {
755 return FieldEncoding(stack_mask_index_bit_offset_, total_bit_size_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100756 }
David Srbecky45aa5982016-03-18 02:15:09 +0000757 ALWAYS_INLINE size_t BitSize() const {
758 return total_bit_size_;
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800759 }
David Brazdilf677ebf2015-05-29 16:29:43 +0100760
Mathieu Chartierc420a802017-02-14 15:16:19 -0800761 // Encode the encoding into the vector.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800762 template<typename Vector>
763 void Encode(Vector* dest) const {
764 static_assert(alignof(StackMapEncoding) == 1, "Should not require alignment");
765 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
766 dest->insert(dest->end(), ptr, ptr + sizeof(*this));
767 }
768
Mathieu Chartierc420a802017-02-14 15:16:19 -0800769 // Decode the encoding from a pointer, updates the pointer.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800770 void Decode(const uint8_t** ptr) {
771 *this = *reinterpret_cast<const StackMapEncoding*>(*ptr);
772 *ptr += sizeof(*this);
773 }
774
David Srbecky09ed0982016-02-12 21:58:43 +0000775 void Dump(VariableIndentationOutputStream* vios) const;
David Brazdilf677ebf2015-05-29 16:29:43 +0100776
777 private:
David Srbecky09ed0982016-02-12 21:58:43 +0000778 static constexpr size_t kNativePcBitOffset = 0;
779 uint8_t dex_pc_bit_offset_;
780 uint8_t dex_register_map_bit_offset_;
781 uint8_t inline_info_bit_offset_;
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800782 uint8_t register_mask_index_bit_offset_;
David Srbecky45aa5982016-03-18 02:15:09 +0000783 uint8_t stack_mask_index_bit_offset_;
784 uint8_t total_bit_size_;
David Brazdilf677ebf2015-05-29 16:29:43 +0100785};
786
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100787/**
788 * A Stack Map holds compilation information for a specific PC necessary for:
789 * - Mapping it to a dex PC,
790 * - Knowing which stack entries are objects,
791 * - Knowing which registers hold objects,
792 * - Knowing the inlining information,
793 * - Knowing the values of dex registers.
794 *
795 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100796 *
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800797 * [native_pc_offset, dex_pc, dex_register_map_offset, inlining_info_index, register_mask_index,
David Srbecky45aa5982016-03-18 02:15:09 +0000798 * stack_mask_index].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100799 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100800class StackMap {
801 public:
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100802 StackMap() {}
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800803 explicit StackMap(BitMemoryRegion region) : region_(region) {}
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100804
David Srbecky09ed0982016-02-12 21:58:43 +0000805 ALWAYS_INLINE bool IsValid() const { return region_.pointer() != nullptr; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100806
David Srbecky09ed0982016-02-12 21:58:43 +0000807 ALWAYS_INLINE uint32_t GetDexPc(const StackMapEncoding& encoding) const {
808 return encoding.GetDexPcEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100809 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100810
David Srbecky09ed0982016-02-12 21:58:43 +0000811 ALWAYS_INLINE void SetDexPc(const StackMapEncoding& encoding, uint32_t dex_pc) {
812 encoding.GetDexPcEncoding().Store(region_, dex_pc);
David Brazdilf677ebf2015-05-29 16:29:43 +0100813 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100814
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800815 ALWAYS_INLINE uint32_t GetNativePcOffset(const StackMapEncoding& encoding,
816 InstructionSet instruction_set) const {
817 CodeOffset offset(
818 CodeOffset::FromCompressedOffset(encoding.GetNativePcEncoding().Load(region_)));
819 return offset.Uint32Value(instruction_set);
David Brazdilf677ebf2015-05-29 16:29:43 +0100820 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100821
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800822 ALWAYS_INLINE void SetNativePcCodeOffset(const StackMapEncoding& encoding,
823 CodeOffset native_pc_offset) {
824 encoding.GetNativePcEncoding().Store(region_, native_pc_offset.CompressedValue());
David Brazdilf677ebf2015-05-29 16:29:43 +0100825 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100826
David Srbecky09ed0982016-02-12 21:58:43 +0000827 ALWAYS_INLINE uint32_t GetDexRegisterMapOffset(const StackMapEncoding& encoding) const {
828 return encoding.GetDexRegisterMapEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100829 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100830
David Srbecky09ed0982016-02-12 21:58:43 +0000831 ALWAYS_INLINE void SetDexRegisterMapOffset(const StackMapEncoding& encoding, uint32_t offset) {
832 encoding.GetDexRegisterMapEncoding().Store(region_, offset);
David Brazdilf677ebf2015-05-29 16:29:43 +0100833 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100834
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800835 ALWAYS_INLINE uint32_t GetInlineInfoIndex(const StackMapEncoding& encoding) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000836 return encoding.GetInlineInfoEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100837 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100838
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800839 ALWAYS_INLINE void SetInlineInfoIndex(const StackMapEncoding& encoding, uint32_t index) {
840 encoding.GetInlineInfoEncoding().Store(region_, index);
David Brazdilf677ebf2015-05-29 16:29:43 +0100841 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100842
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800843 ALWAYS_INLINE uint32_t GetRegisterMaskIndex(const StackMapEncoding& encoding) const {
844 return encoding.GetRegisterMaskIndexEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100845 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100846
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800847 ALWAYS_INLINE void SetRegisterMaskIndex(const StackMapEncoding& encoding, uint32_t mask) {
848 encoding.GetRegisterMaskIndexEncoding().Store(region_, mask);
David Brazdilf677ebf2015-05-29 16:29:43 +0100849 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100850
David Srbecky45aa5982016-03-18 02:15:09 +0000851 ALWAYS_INLINE uint32_t GetStackMaskIndex(const StackMapEncoding& encoding) const {
852 return encoding.GetStackMaskIndexEncoding().Load(region_);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100853 }
854
David Srbecky45aa5982016-03-18 02:15:09 +0000855 ALWAYS_INLINE void SetStackMaskIndex(const StackMapEncoding& encoding, uint32_t mask) {
856 encoding.GetStackMaskIndexEncoding().Store(region_, mask);
David Srbecky09ed0982016-02-12 21:58:43 +0000857 }
858
859 ALWAYS_INLINE bool HasDexRegisterMap(const StackMapEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +0100860 return GetDexRegisterMapOffset(encoding) != kNoDexRegisterMap;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100861 }
862
David Srbecky09ed0982016-02-12 21:58:43 +0000863 ALWAYS_INLINE bool HasInlineInfo(const StackMapEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800864 return GetInlineInfoIndex(encoding) != kNoInlineInfo;
Roland Levillain442b46a2015-02-18 16:54:21 +0000865 }
866
David Srbecky09ed0982016-02-12 21:58:43 +0000867 ALWAYS_INLINE bool Equals(const StackMap& other) const {
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800868 return region_.pointer() == other.region_.pointer() &&
869 region_.size() == other.region_.size() &&
870 region_.BitOffset() == other.region_.BitOffset();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100871 }
872
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100873 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100874 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000875 const CodeInfoEncoding& encoding,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700876 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100877 uint32_t code_offset,
878 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800879 InstructionSet instruction_set,
Roland Levillainf2650d12015-05-28 14:53:28 +0100880 const std::string& header_suffix = "") const;
881
Roland Levillain442b46a2015-02-18 16:54:21 +0000882 // Special (invalid) offset for the DexRegisterMapOffset field meaning
883 // that there is no Dex register map for this stack map.
884 static constexpr uint32_t kNoDexRegisterMap = -1;
885
886 // Special (invalid) offset for the InlineDescriptorOffset field meaning
887 // that there is no inline info for this stack map.
888 static constexpr uint32_t kNoInlineInfo = -1;
889
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100890 private:
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100891 static constexpr int kFixedSize = 0;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100892
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800893 BitMemoryRegion region_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100894
Nicolas Geoffray39468442014-09-02 15:17:15 +0100895 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100896};
897
David Srbecky61b28a12016-02-25 21:55:03 +0000898class InlineInfoEncoding {
899 public:
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700900 void SetFromSizes(size_t method_index_idx_max,
David Srbecky61b28a12016-02-25 21:55:03 +0000901 size_t dex_pc_max,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000902 size_t extra_data_max,
David Srbecky61b28a12016-02-25 21:55:03 +0000903 size_t dex_register_map_size) {
904 total_bit_size_ = kMethodIndexBitOffset;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700905 total_bit_size_ += MinimumBitsToStore(method_index_idx_max);
David Srbecky61b28a12016-02-25 21:55:03 +0000906
907 dex_pc_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100908 // Note: We're not encoding the dex pc if there is none. That's the case
909 // for an intrinsified native method, such as String.charAt().
Andreas Gampe04c6ab92017-06-08 21:49:14 -0700910 if (dex_pc_max != dex::kDexNoIndex) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100911 total_bit_size_ += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max);
912 }
David Srbecky61b28a12016-02-25 21:55:03 +0000913
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000914 extra_data_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
915 total_bit_size_ += MinimumBitsToStore(extra_data_max);
David Srbecky61b28a12016-02-25 21:55:03 +0000916
917 // We also need +1 for kNoDexRegisterMap, but since the size is strictly
918 // greater than any offset we might try to encode, we already implicitly have it.
919 dex_register_map_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
920 total_bit_size_ += MinimumBitsToStore(dex_register_map_size);
921 }
922
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700923 ALWAYS_INLINE FieldEncoding GetMethodIndexIdxEncoding() const {
David Srbecky61b28a12016-02-25 21:55:03 +0000924 return FieldEncoding(kMethodIndexBitOffset, dex_pc_bit_offset_);
925 }
926 ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000927 return FieldEncoding(dex_pc_bit_offset_, extra_data_bit_offset_, -1 /* min_value */);
David Srbecky61b28a12016-02-25 21:55:03 +0000928 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000929 ALWAYS_INLINE FieldEncoding GetExtraDataEncoding() const {
930 return FieldEncoding(extra_data_bit_offset_, dex_register_map_bit_offset_);
David Srbecky61b28a12016-02-25 21:55:03 +0000931 }
932 ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const {
933 return FieldEncoding(dex_register_map_bit_offset_, total_bit_size_, -1 /* min_value */);
934 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800935 ALWAYS_INLINE size_t BitSize() const {
936 return total_bit_size_;
David Srbecky61b28a12016-02-25 21:55:03 +0000937 }
938
939 void Dump(VariableIndentationOutputStream* vios) const;
940
Mathieu Chartierc420a802017-02-14 15:16:19 -0800941 // Encode the encoding into the vector.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800942 template<typename Vector>
943 void Encode(Vector* dest) const {
944 static_assert(alignof(InlineInfoEncoding) == 1, "Should not require alignment");
945 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
946 dest->insert(dest->end(), ptr, ptr + sizeof(*this));
947 }
948
Mathieu Chartierc420a802017-02-14 15:16:19 -0800949 // Decode the encoding from a pointer, updates the pointer.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800950 void Decode(const uint8_t** ptr) {
951 *this = *reinterpret_cast<const InlineInfoEncoding*>(*ptr);
952 *ptr += sizeof(*this);
953 }
954
David Srbecky61b28a12016-02-25 21:55:03 +0000955 private:
956 static constexpr uint8_t kIsLastBitOffset = 0;
957 static constexpr uint8_t kMethodIndexBitOffset = 1;
958 uint8_t dex_pc_bit_offset_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000959 uint8_t extra_data_bit_offset_;
David Srbecky61b28a12016-02-25 21:55:03 +0000960 uint8_t dex_register_map_bit_offset_;
961 uint8_t total_bit_size_;
962};
963
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100964/**
965 * Inline information for a specific PC. The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100966 *
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000967 * [is_last,
968 * method_index (or ArtMethod high bits),
969 * dex_pc,
970 * extra_data (ArtMethod low bits or 1),
971 * dex_register_map_offset]+.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100972 */
973class InlineInfo {
974 public:
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800975 explicit InlineInfo(BitMemoryRegion region) : region_(region) {}
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100976
David Srbecky61b28a12016-02-25 21:55:03 +0000977 ALWAYS_INLINE uint32_t GetDepth(const InlineInfoEncoding& encoding) const {
978 size_t depth = 0;
979 while (!GetRegionAtDepth(encoding, depth++).LoadBit(0)) { } // Check is_last bit.
980 return depth;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100981 }
982
David Srbecky61b28a12016-02-25 21:55:03 +0000983 ALWAYS_INLINE void SetDepth(const InlineInfoEncoding& encoding, uint32_t depth) {
984 DCHECK_GT(depth, 0u);
985 for (size_t d = 0; d < depth; ++d) {
986 GetRegionAtDepth(encoding, d).StoreBit(0, d == depth - 1); // Set is_last bit.
987 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100988 }
989
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700990 ALWAYS_INLINE uint32_t GetMethodIndexIdxAtDepth(const InlineInfoEncoding& encoding,
991 uint32_t depth) const {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000992 DCHECK(!EncodesArtMethodAtDepth(encoding, depth));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700993 return encoding.GetMethodIndexIdxEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100994 }
995
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700996 ALWAYS_INLINE void SetMethodIndexIdxAtDepth(const InlineInfoEncoding& encoding,
997 uint32_t depth,
998 uint32_t index) {
999 encoding.GetMethodIndexIdxEncoding().Store(GetRegionAtDepth(encoding, depth), index);
1000 }
1001
1002
1003 ALWAYS_INLINE uint32_t GetMethodIndexAtDepth(const InlineInfoEncoding& encoding,
1004 const MethodInfo& method_info,
1005 uint32_t depth) const {
1006 return method_info.GetMethodIndex(GetMethodIndexIdxAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001007 }
1008
David Srbecky61b28a12016-02-25 21:55:03 +00001009 ALWAYS_INLINE uint32_t GetDexPcAtDepth(const InlineInfoEncoding& encoding,
1010 uint32_t depth) const {
1011 return encoding.GetDexPcEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001012 }
1013
David Srbecky61b28a12016-02-25 21:55:03 +00001014 ALWAYS_INLINE void SetDexPcAtDepth(const InlineInfoEncoding& encoding,
1015 uint32_t depth,
1016 uint32_t dex_pc) {
1017 encoding.GetDexPcEncoding().Store(GetRegionAtDepth(encoding, depth), dex_pc);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001018 }
1019
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001020 ALWAYS_INLINE bool EncodesArtMethodAtDepth(const InlineInfoEncoding& encoding,
1021 uint32_t depth) const {
1022 return (encoding.GetExtraDataEncoding().Load(GetRegionAtDepth(encoding, depth)) & 1) == 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001023 }
1024
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001025 ALWAYS_INLINE void SetExtraDataAtDepth(const InlineInfoEncoding& encoding,
1026 uint32_t depth,
1027 uint32_t extra_data) {
1028 encoding.GetExtraDataEncoding().Store(GetRegionAtDepth(encoding, depth), extra_data);
1029 }
1030
1031 ALWAYS_INLINE ArtMethod* GetArtMethodAtDepth(const InlineInfoEncoding& encoding,
1032 uint32_t depth) const {
1033 uint32_t low_bits = encoding.GetExtraDataEncoding().Load(GetRegionAtDepth(encoding, depth));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001034 uint32_t high_bits = encoding.GetMethodIndexIdxEncoding().Load(
1035 GetRegionAtDepth(encoding, depth));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001036 if (high_bits == 0) {
1037 return reinterpret_cast<ArtMethod*>(low_bits);
1038 } else {
1039 uint64_t address = high_bits;
1040 address = address << 32;
1041 return reinterpret_cast<ArtMethod*>(address | low_bits);
1042 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001043 }
1044
David Srbecky61b28a12016-02-25 21:55:03 +00001045 ALWAYS_INLINE uint32_t GetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding,
1046 uint32_t depth) const {
1047 return encoding.GetDexRegisterMapEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001048 }
1049
David Srbecky61b28a12016-02-25 21:55:03 +00001050 ALWAYS_INLINE void SetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding,
1051 uint32_t depth,
1052 uint32_t offset) {
1053 encoding.GetDexRegisterMapEncoding().Store(GetRegionAtDepth(encoding, depth), offset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001054 }
1055
David Srbecky61b28a12016-02-25 21:55:03 +00001056 ALWAYS_INLINE bool HasDexRegisterMapAtDepth(const InlineInfoEncoding& encoding,
1057 uint32_t depth) const {
1058 return GetDexRegisterMapOffsetAtDepth(encoding, depth) != StackMap::kNoDexRegisterMap;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001059 }
1060
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001061 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +00001062 const CodeInfo& info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001063 const MethodInfo& method_info,
David Srbecky61b28a12016-02-25 21:55:03 +00001064 uint16_t* number_of_dex_registers) const;
Roland Levillain1c1da432015-07-16 11:54:44 +01001065
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001066 private:
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001067 ALWAYS_INLINE BitMemoryRegion GetRegionAtDepth(const InlineInfoEncoding& encoding,
1068 uint32_t depth) const {
1069 size_t entry_size = encoding.BitSize();
David Srbecky61b28a12016-02-25 21:55:03 +00001070 DCHECK_GT(entry_size, 0u);
1071 return region_.Subregion(depth * entry_size, entry_size);
1072 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001073
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001074 BitMemoryRegion region_;
1075};
1076
1077// Bit sized region encoding, may be more than 255 bits.
1078class BitRegionEncoding {
1079 public:
1080 uint32_t num_bits = 0;
1081
1082 ALWAYS_INLINE size_t BitSize() const {
1083 return num_bits;
1084 }
1085
1086 template<typename Vector>
1087 void Encode(Vector* dest) const {
1088 EncodeUnsignedLeb128(dest, num_bits); // Use leb in case num_bits is greater than 255.
1089 }
1090
1091 void Decode(const uint8_t** ptr) {
1092 num_bits = DecodeUnsignedLeb128(ptr);
1093 }
1094};
1095
1096// A table of bit sized encodings.
1097template <typename Encoding>
1098struct BitEncodingTable {
1099 static constexpr size_t kInvalidOffset = static_cast<size_t>(-1);
1100 // How the encoding is laid out (serialized).
1101 Encoding encoding;
1102
1103 // Number of entries in the table (serialized).
1104 size_t num_entries;
1105
1106 // Bit offset for the base of the table (computed).
1107 size_t bit_offset = kInvalidOffset;
1108
1109 template<typename Vector>
1110 void Encode(Vector* dest) const {
1111 EncodeUnsignedLeb128(dest, num_entries);
1112 encoding.Encode(dest);
1113 }
1114
1115 ALWAYS_INLINE void Decode(const uint8_t** ptr) {
1116 num_entries = DecodeUnsignedLeb128(ptr);
1117 encoding.Decode(ptr);
1118 }
1119
1120 // Set the bit offset in the table and adds the space used by the table to offset.
1121 void UpdateBitOffset(size_t* offset) {
1122 DCHECK(offset != nullptr);
1123 bit_offset = *offset;
1124 *offset += encoding.BitSize() * num_entries;
1125 }
1126
1127 // Return the bit region for the map at index i.
1128 ALWAYS_INLINE BitMemoryRegion BitRegion(MemoryRegion region, size_t index) const {
1129 DCHECK_NE(bit_offset, kInvalidOffset) << "Invalid table offset";
1130 DCHECK_LT(index, num_entries);
1131 const size_t map_size = encoding.BitSize();
1132 return BitMemoryRegion(region, bit_offset + index * map_size, map_size);
1133 }
1134};
1135
1136// A byte sized table of possible variable sized encodings.
1137struct ByteSizedTable {
1138 static constexpr size_t kInvalidOffset = static_cast<size_t>(-1);
1139
1140 // Number of entries in the table (serialized).
1141 size_t num_entries = 0;
1142
1143 // Number of bytes of the table (serialized).
1144 size_t num_bytes;
1145
1146 // Bit offset for the base of the table (computed).
1147 size_t byte_offset = kInvalidOffset;
1148
1149 template<typename Vector>
1150 void Encode(Vector* dest) const {
1151 EncodeUnsignedLeb128(dest, num_entries);
1152 EncodeUnsignedLeb128(dest, num_bytes);
1153 }
1154
1155 ALWAYS_INLINE void Decode(const uint8_t** ptr) {
1156 num_entries = DecodeUnsignedLeb128(ptr);
1157 num_bytes = DecodeUnsignedLeb128(ptr);
1158 }
1159
1160 // Set the bit offset of the table. Adds the total bit size of the table to offset.
1161 void UpdateBitOffset(size_t* offset) {
1162 DCHECK(offset != nullptr);
1163 DCHECK_ALIGNED(*offset, kBitsPerByte);
1164 byte_offset = *offset / kBitsPerByte;
1165 *offset += num_bytes * kBitsPerByte;
1166 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001167};
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001168
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001169// Format is [native pc, invoke type, method index].
1170class InvokeInfoEncoding {
1171 public:
1172 void SetFromSizes(size_t native_pc_max,
1173 size_t invoke_type_max,
1174 size_t method_index_max) {
1175 total_bit_size_ = 0;
1176 DCHECK_EQ(kNativePcBitOffset, total_bit_size_);
1177 total_bit_size_ += MinimumBitsToStore(native_pc_max);
1178 invoke_type_bit_offset_ = total_bit_size_;
1179 total_bit_size_ += MinimumBitsToStore(invoke_type_max);
1180 method_index_bit_offset_ = total_bit_size_;
1181 total_bit_size_ += MinimumBitsToStore(method_index_max);
1182 }
1183
1184 ALWAYS_INLINE FieldEncoding GetNativePcEncoding() const {
1185 return FieldEncoding(kNativePcBitOffset, invoke_type_bit_offset_);
1186 }
1187
1188 ALWAYS_INLINE FieldEncoding GetInvokeTypeEncoding() const {
1189 return FieldEncoding(invoke_type_bit_offset_, method_index_bit_offset_);
1190 }
1191
1192 ALWAYS_INLINE FieldEncoding GetMethodIndexEncoding() const {
1193 return FieldEncoding(method_index_bit_offset_, total_bit_size_);
1194 }
1195
1196 ALWAYS_INLINE size_t BitSize() const {
1197 return total_bit_size_;
1198 }
1199
1200 template<typename Vector>
1201 void Encode(Vector* dest) const {
1202 static_assert(alignof(InvokeInfoEncoding) == 1, "Should not require alignment");
1203 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
1204 dest->insert(dest->end(), ptr, ptr + sizeof(*this));
1205 }
1206
1207 void Decode(const uint8_t** ptr) {
1208 *this = *reinterpret_cast<const InvokeInfoEncoding*>(*ptr);
1209 *ptr += sizeof(*this);
1210 }
1211
1212 private:
1213 static constexpr uint8_t kNativePcBitOffset = 0;
1214 uint8_t invoke_type_bit_offset_;
1215 uint8_t method_index_bit_offset_;
1216 uint8_t total_bit_size_;
1217};
1218
1219class InvokeInfo {
1220 public:
1221 explicit InvokeInfo(BitMemoryRegion region) : region_(region) {}
1222
1223 ALWAYS_INLINE uint32_t GetNativePcOffset(const InvokeInfoEncoding& encoding,
1224 InstructionSet instruction_set) const {
1225 CodeOffset offset(
1226 CodeOffset::FromCompressedOffset(encoding.GetNativePcEncoding().Load(region_)));
1227 return offset.Uint32Value(instruction_set);
1228 }
1229
1230 ALWAYS_INLINE void SetNativePcCodeOffset(const InvokeInfoEncoding& encoding,
1231 CodeOffset native_pc_offset) {
1232 encoding.GetNativePcEncoding().Store(region_, native_pc_offset.CompressedValue());
1233 }
1234
1235 ALWAYS_INLINE uint32_t GetInvokeType(const InvokeInfoEncoding& encoding) const {
1236 return encoding.GetInvokeTypeEncoding().Load(region_);
1237 }
1238
1239 ALWAYS_INLINE void SetInvokeType(const InvokeInfoEncoding& encoding, uint32_t invoke_type) {
1240 encoding.GetInvokeTypeEncoding().Store(region_, invoke_type);
1241 }
1242
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001243 ALWAYS_INLINE uint32_t GetMethodIndexIdx(const InvokeInfoEncoding& encoding) const {
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001244 return encoding.GetMethodIndexEncoding().Load(region_);
1245 }
1246
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001247 ALWAYS_INLINE void SetMethodIndexIdx(const InvokeInfoEncoding& encoding,
1248 uint32_t method_index_idx) {
1249 encoding.GetMethodIndexEncoding().Store(region_, method_index_idx);
1250 }
1251
1252 ALWAYS_INLINE uint32_t GetMethodIndex(const InvokeInfoEncoding& encoding,
1253 MethodInfo method_info) const {
1254 return method_info.GetMethodIndex(GetMethodIndexIdx(encoding));
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001255 }
1256
1257 bool IsValid() const { return region_.pointer() != nullptr; }
1258
1259 private:
1260 BitMemoryRegion region_;
1261};
1262
David Srbecky09ed0982016-02-12 21:58:43 +00001263// Most of the fields are encoded as ULEB128 to save space.
1264struct CodeInfoEncoding {
Andreas Gampe69489fa2017-06-08 18:03:25 -07001265 using SizeType = uint32_t;
1266
1267 static constexpr SizeType kInvalidSize = std::numeric_limits<SizeType>::max();
1268
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001269 // Byte sized tables go first to avoid unnecessary alignment bits.
1270 ByteSizedTable dex_register_map;
1271 ByteSizedTable location_catalog;
1272 BitEncodingTable<StackMapEncoding> stack_map;
1273 BitEncodingTable<BitRegionEncoding> register_mask;
1274 BitEncodingTable<BitRegionEncoding> stack_mask;
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001275 BitEncodingTable<InvokeInfoEncoding> invoke_info;
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001276 BitEncodingTable<InlineInfoEncoding> inline_info;
David Srbecky09ed0982016-02-12 21:58:43 +00001277
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001278 CodeInfoEncoding() {}
David Srbecky09ed0982016-02-12 21:58:43 +00001279
1280 explicit CodeInfoEncoding(const void* data) {
1281 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001282 dex_register_map.Decode(&ptr);
1283 location_catalog.Decode(&ptr);
1284 stack_map.Decode(&ptr);
1285 register_mask.Decode(&ptr);
1286 stack_mask.Decode(&ptr);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001287 invoke_info.Decode(&ptr);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001288 if (stack_map.encoding.GetInlineInfoEncoding().BitSize() > 0) {
1289 inline_info.Decode(&ptr);
David Srbecky61b28a12016-02-25 21:55:03 +00001290 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001291 inline_info = BitEncodingTable<InlineInfoEncoding>();
David Srbecky61b28a12016-02-25 21:55:03 +00001292 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001293 cache_header_size =
Andreas Gampe69489fa2017-06-08 18:03:25 -07001294 dchecked_integral_cast<SizeType>(ptr - reinterpret_cast<const uint8_t*>(data));
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001295 ComputeTableOffsets();
David Srbecky09ed0982016-02-12 21:58:43 +00001296 }
1297
Mathieu Chartierc420a802017-02-14 15:16:19 -08001298 // Compress is not const since it calculates cache_header_size. This is used by PrepareForFillIn.
David Srbecky09ed0982016-02-12 21:58:43 +00001299 template<typename Vector>
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001300 void Compress(Vector* dest) {
1301 dex_register_map.Encode(dest);
1302 location_catalog.Encode(dest);
1303 stack_map.Encode(dest);
1304 register_mask.Encode(dest);
1305 stack_mask.Encode(dest);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001306 invoke_info.Encode(dest);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001307 if (stack_map.encoding.GetInlineInfoEncoding().BitSize() > 0) {
1308 inline_info.Encode(dest);
David Srbecky61b28a12016-02-25 21:55:03 +00001309 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001310 cache_header_size = dest->size();
David Srbecky09ed0982016-02-12 21:58:43 +00001311 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001312
1313 ALWAYS_INLINE void ComputeTableOffsets() {
1314 // Skip the header.
1315 size_t bit_offset = HeaderSize() * kBitsPerByte;
1316 // The byte tables must be aligned so they must go first.
1317 dex_register_map.UpdateBitOffset(&bit_offset);
1318 location_catalog.UpdateBitOffset(&bit_offset);
1319 // Other tables don't require alignment.
1320 stack_map.UpdateBitOffset(&bit_offset);
1321 register_mask.UpdateBitOffset(&bit_offset);
1322 stack_mask.UpdateBitOffset(&bit_offset);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001323 invoke_info.UpdateBitOffset(&bit_offset);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001324 inline_info.UpdateBitOffset(&bit_offset);
1325 cache_non_header_size = RoundUp(bit_offset, kBitsPerByte) / kBitsPerByte - HeaderSize();
1326 }
1327
1328 ALWAYS_INLINE size_t HeaderSize() const {
1329 DCHECK_NE(cache_header_size, kInvalidSize) << "Uninitialized";
1330 return cache_header_size;
1331 }
1332
1333 ALWAYS_INLINE size_t NonHeaderSize() const {
1334 DCHECK_NE(cache_non_header_size, kInvalidSize) << "Uninitialized";
1335 return cache_non_header_size;
1336 }
1337
1338 private:
1339 // Computed fields (not serialized).
Mathieu Chartierc420a802017-02-14 15:16:19 -08001340 // Header size in bytes, cached to avoid needing to re-decoding the encoding in HeaderSize.
Andreas Gampe69489fa2017-06-08 18:03:25 -07001341 SizeType cache_header_size = kInvalidSize;
Mathieu Chartierc420a802017-02-14 15:16:19 -08001342 // Non header size in bytes, cached to avoid needing to re-decoding the encoding in NonHeaderSize.
Andreas Gampe69489fa2017-06-08 18:03:25 -07001343 SizeType cache_non_header_size = kInvalidSize;
David Srbecky09ed0982016-02-12 21:58:43 +00001344};
1345
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001346/**
1347 * Wrapper around all compiler information collected for a method.
1348 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +01001349 *
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001350 * [CodeInfoEncoding, DexRegisterMap+, DexLocationCatalog+, StackMap+, RegisterMask+, StackMask+,
Mathieu Chartierc420a802017-02-14 15:16:19 -08001351 * InlineInfo*]
1352 *
1353 * where CodeInfoEncoding is of the form:
1354 *
1355 * [ByteSizedTable(dex_register_map), ByteSizedTable(location_catalog),
1356 * BitEncodingTable<StackMapEncoding>, BitEncodingTable<BitRegionEncoding>,
1357 * BitEncodingTable<BitRegionEncoding>, BitEncodingTable<InlineInfoEncoding>]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001358 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001359class CodeInfo {
1360 public:
David Srbecky09ed0982016-02-12 21:58:43 +00001361 explicit CodeInfo(MemoryRegion region) : region_(region) {
1362 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001363
Nicolas Geoffray39468442014-09-02 15:17:15 +01001364 explicit CodeInfo(const void* data) {
David Srbecky09ed0982016-02-12 21:58:43 +00001365 CodeInfoEncoding encoding = CodeInfoEncoding(data);
1366 region_ = MemoryRegion(const_cast<void*>(data),
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001367 encoding.HeaderSize() + encoding.NonHeaderSize());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001368 }
1369
David Srbecky09ed0982016-02-12 21:58:43 +00001370 CodeInfoEncoding ExtractEncoding() const {
David Srbecky45aa5982016-03-18 02:15:09 +00001371 CodeInfoEncoding encoding(region_.begin());
Mathieu Chartier01c78142017-01-05 10:17:55 -08001372 AssertValidStackMap(encoding);
1373 return encoding;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001374 }
1375
David Srbecky09ed0982016-02-12 21:58:43 +00001376 bool HasInlineInfo(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001377 return encoding.stack_map.encoding.GetInlineInfoEncoding().BitSize() > 0;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001378 }
1379
David Srbecky09ed0982016-02-12 21:58:43 +00001380 DexRegisterLocationCatalog GetDexRegisterLocationCatalog(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001381 return DexRegisterLocationCatalog(region_.Subregion(encoding.location_catalog.byte_offset,
1382 encoding.location_catalog.num_bytes));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001383 }
1384
Mathieu Chartier12f1b992017-01-19 18:00:45 -08001385 ALWAYS_INLINE size_t GetNumberOfStackMaskBits(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001386 return encoding.stack_mask.encoding.BitSize();
Mathieu Chartier12f1b992017-01-19 18:00:45 -08001387 }
1388
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001389 ALWAYS_INLINE StackMap GetStackMapAt(size_t index, const CodeInfoEncoding& encoding) const {
1390 return StackMap(encoding.stack_map.BitRegion(region_, index));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001391 }
1392
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001393 BitMemoryRegion GetStackMask(size_t index, const CodeInfoEncoding& encoding) const {
1394 return encoding.stack_mask.BitRegion(region_, index);
David Srbecky45aa5982016-03-18 02:15:09 +00001395 }
1396
1397 BitMemoryRegion GetStackMaskOf(const CodeInfoEncoding& encoding,
1398 const StackMap& stack_map) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001399 return GetStackMask(stack_map.GetStackMaskIndex(encoding.stack_map.encoding), encoding);
David Srbecky45aa5982016-03-18 02:15:09 +00001400 }
1401
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001402 BitMemoryRegion GetRegisterMask(size_t index, const CodeInfoEncoding& encoding) const {
1403 return encoding.register_mask.BitRegion(region_, index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001404 }
1405
1406 uint32_t GetRegisterMaskOf(const CodeInfoEncoding& encoding, const StackMap& stack_map) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001407 size_t index = stack_map.GetRegisterMaskIndex(encoding.stack_map.encoding);
1408 return GetRegisterMask(index, encoding).LoadBits(0u, encoding.register_mask.encoding.BitSize());
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001409 }
1410
David Srbecky09ed0982016-02-12 21:58:43 +00001411 uint32_t GetNumberOfLocationCatalogEntries(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001412 return encoding.location_catalog.num_entries;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001413 }
1414
David Srbecky09ed0982016-02-12 21:58:43 +00001415 uint32_t GetDexRegisterLocationCatalogSize(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001416 return encoding.location_catalog.num_bytes;
Roland Levillaina552e1c2015-03-26 15:01:03 +00001417 }
1418
David Srbecky09ed0982016-02-12 21:58:43 +00001419 uint32_t GetNumberOfStackMaps(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001420 return encoding.stack_map.num_entries;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001421 }
1422
David Srbecky45aa5982016-03-18 02:15:09 +00001423 // Get the size of all the stack maps of this CodeInfo object, in bits. Not byte aligned.
1424 ALWAYS_INLINE size_t GetStackMapsSizeInBits(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001425 return encoding.stack_map.encoding.BitSize() * GetNumberOfStackMaps(encoding);
Nicolas Geoffray6530baf2015-05-26 15:22:58 +01001426 }
1427
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001428 InvokeInfo GetInvokeInfo(const CodeInfoEncoding& encoding, size_t index) const {
1429 return InvokeInfo(encoding.invoke_info.BitRegion(region_, index));
1430 }
1431
David Brazdilf677ebf2015-05-29 16:29:43 +01001432 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map,
David Srbecky09ed0982016-02-12 21:58:43 +00001433 const CodeInfoEncoding& encoding,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001434 size_t number_of_dex_registers) const {
1435 if (!stack_map.HasDexRegisterMap(encoding.stack_map.encoding)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001436 return DexRegisterMap();
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001437 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001438 const uint32_t offset = encoding.dex_register_map.byte_offset +
1439 stack_map.GetDexRegisterMapOffset(encoding.stack_map.encoding);
1440 size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers);
1441 return DexRegisterMap(region_.Subregion(offset, size));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001442 }
1443
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001444 size_t GetDexRegisterMapsSize(const CodeInfoEncoding& encoding,
1445 uint32_t number_of_dex_registers) const {
1446 size_t total = 0;
1447 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
1448 StackMap stack_map = GetStackMapAt(i, encoding);
1449 DexRegisterMap map(GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers));
1450 total += map.Size();
1451 }
1452 return total;
1453 }
1454
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001455 // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`.
1456 DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
1457 InlineInfo inline_info,
David Srbecky09ed0982016-02-12 21:58:43 +00001458 const CodeInfoEncoding& encoding,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001459 uint32_t number_of_dex_registers) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001460 if (!inline_info.HasDexRegisterMapAtDepth(encoding.inline_info.encoding, depth)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001461 return DexRegisterMap();
1462 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001463 uint32_t offset = encoding.dex_register_map.byte_offset +
1464 inline_info.GetDexRegisterMapOffsetAtDepth(encoding.inline_info.encoding, depth);
David Srbecky09ed0982016-02-12 21:58:43 +00001465 size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001466 return DexRegisterMap(region_.Subregion(offset, size));
1467 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001468 }
1469
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001470 InlineInfo GetInlineInfo(size_t index, const CodeInfoEncoding& encoding) const {
Mathieu Chartierc420a802017-02-14 15:16:19 -08001471 // Since we do not know the depth, we just return the whole remaining map. The caller may
1472 // access the inline info for arbitrary depths. To return the precise inline info we would need
1473 // to count the depth before returning.
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001474 // TODO: Clean this up.
1475 const size_t bit_offset = encoding.inline_info.bit_offset +
1476 index * encoding.inline_info.encoding.BitSize();
1477 return InlineInfo(BitMemoryRegion(region_, bit_offset, region_.size_in_bits() - bit_offset));
1478 }
1479
David Srbecky09ed0982016-02-12 21:58:43 +00001480 InlineInfo GetInlineInfoOf(StackMap stack_map, const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001481 DCHECK(stack_map.HasInlineInfo(encoding.stack_map.encoding));
1482 uint32_t index = stack_map.GetInlineInfoIndex(encoding.stack_map.encoding);
1483 return GetInlineInfo(index, encoding);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001484 }
1485
David Srbecky09ed0982016-02-12 21:58:43 +00001486 StackMap GetStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1487 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +01001488 StackMap stack_map = GetStackMapAt(i, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001489 if (stack_map.GetDexPc(encoding.stack_map.encoding) == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001490 return stack_map;
1491 }
1492 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001493 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001494 }
1495
David Brazdil77a48ae2015-09-15 12:34:04 +00001496 // Searches the stack map list backwards because catch stack maps are stored
1497 // at the end.
David Srbecky09ed0982016-02-12 21:58:43 +00001498 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1499 for (size_t i = GetNumberOfStackMaps(encoding); i > 0; --i) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001500 StackMap stack_map = GetStackMapAt(i - 1, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001501 if (stack_map.GetDexPc(encoding.stack_map.encoding) == dex_pc) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001502 return stack_map;
1503 }
1504 }
1505 return StackMap();
1506 }
1507
David Srbecky09ed0982016-02-12 21:58:43 +00001508 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1509 size_t e = GetNumberOfStackMaps(encoding);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001510 if (e == 0) {
1511 // There cannot be OSR stack map if there is no stack map.
1512 return StackMap();
1513 }
1514 // Walk over all stack maps. If two consecutive stack maps are identical, then we
1515 // have found a stack map suitable for OSR.
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001516 const StackMapEncoding& stack_map_encoding = encoding.stack_map.encoding;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001517 for (size_t i = 0; i < e - 1; ++i) {
1518 StackMap stack_map = GetStackMapAt(i, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001519 if (stack_map.GetDexPc(stack_map_encoding) == dex_pc) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001520 StackMap other = GetStackMapAt(i + 1, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001521 if (other.GetDexPc(stack_map_encoding) == dex_pc &&
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001522 other.GetNativePcOffset(stack_map_encoding, kRuntimeISA) ==
1523 stack_map.GetNativePcOffset(stack_map_encoding, kRuntimeISA)) {
David Srbecky09ed0982016-02-12 21:58:43 +00001524 DCHECK_EQ(other.GetDexRegisterMapOffset(stack_map_encoding),
1525 stack_map.GetDexRegisterMapOffset(stack_map_encoding));
1526 DCHECK(!stack_map.HasInlineInfo(stack_map_encoding));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001527 if (i < e - 2) {
1528 // Make sure there are not three identical stack maps following each other.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001529 DCHECK_NE(
1530 stack_map.GetNativePcOffset(stack_map_encoding, kRuntimeISA),
1531 GetStackMapAt(i + 2, encoding).GetNativePcOffset(stack_map_encoding, kRuntimeISA));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001532 }
1533 return stack_map;
1534 }
1535 }
1536 }
1537 return StackMap();
1538 }
1539
David Brazdilf677ebf2015-05-29 16:29:43 +01001540 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset,
David Srbecky09ed0982016-02-12 21:58:43 +00001541 const CodeInfoEncoding& encoding) const {
David Brazdil77a48ae2015-09-15 12:34:04 +00001542 // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack
1543 // maps are not. If we knew that the method does not have try/catch,
1544 // we could do binary search.
David Srbecky09ed0982016-02-12 21:58:43 +00001545 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +01001546 StackMap stack_map = GetStackMapAt(i, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001547 if (stack_map.GetNativePcOffset(encoding.stack_map.encoding, kRuntimeISA) ==
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001548 native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001549 return stack_map;
1550 }
1551 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001552 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001553 }
1554
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001555 InvokeInfo GetInvokeInfoForNativePcOffset(uint32_t native_pc_offset,
1556 const CodeInfoEncoding& encoding) {
1557 for (size_t index = 0; index < encoding.invoke_info.num_entries; index++) {
1558 InvokeInfo item = GetInvokeInfo(encoding, index);
1559 if (item.GetNativePcOffset(encoding.invoke_info.encoding, kRuntimeISA) == native_pc_offset) {
1560 return item;
1561 }
1562 }
1563 return InvokeInfo(BitMemoryRegion());
1564 }
1565
Roland Levillainf2650d12015-05-28 14:53:28 +01001566 // Dump this CodeInfo object on `os`. `code_offset` is the (absolute)
1567 // native PC of the compiled method and `number_of_dex_registers` the
1568 // number of Dex virtual registers used in this method. If
1569 // `dump_stack_maps` is true, also dump the stack maps and the
1570 // associated Dex register maps.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001571 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001572 uint32_t code_offset,
1573 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001574 bool dump_stack_maps,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001575 InstructionSet instruction_set,
1576 const MethodInfo& method_info) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001577
Mathieu Chartier01c78142017-01-05 10:17:55 -08001578 // Check that the code info has valid stack map and abort if it does not.
1579 void AssertValidStackMap(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001580 if (region_.size() != 0 && region_.size_in_bits() < GetStackMapsSizeInBits(encoding)) {
Mathieu Chartier01c78142017-01-05 10:17:55 -08001581 LOG(FATAL) << region_.size() << "\n"
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001582 << encoding.HeaderSize() << "\n"
1583 << encoding.NonHeaderSize() << "\n"
1584 << encoding.location_catalog.num_entries << "\n"
1585 << encoding.stack_map.num_entries << "\n"
1586 << encoding.stack_map.encoding.BitSize();
Mathieu Chartier01c78142017-01-05 10:17:55 -08001587 }
1588 }
1589
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001590 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +00001591 // Compute the size of the Dex register map associated to the stack map at
1592 // `dex_register_map_offset_in_code_info`.
David Srbecky09ed0982016-02-12 21:58:43 +00001593 size_t ComputeDexRegisterMapSizeOf(const CodeInfoEncoding& encoding,
1594 uint32_t dex_register_map_offset_in_code_info,
Roland Levillaina552e1c2015-03-26 15:01:03 +00001595 uint16_t number_of_dex_registers) const {
1596 // Offset where the actual mapping data starts within art::DexRegisterMap.
1597 size_t location_mapping_data_offset_in_dex_register_map =
1598 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1599 // Create a temporary art::DexRegisterMap to be able to call
1600 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1601 DexRegisterMap dex_register_map_without_locations(
1602 MemoryRegion(region_.Subregion(dex_register_map_offset_in_code_info,
1603 location_mapping_data_offset_in_dex_register_map)));
1604 size_t number_of_live_dex_registers =
1605 dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
1606 size_t location_mapping_data_size_in_bits =
David Srbecky09ed0982016-02-12 21:58:43 +00001607 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries(encoding))
Roland Levillaina552e1c2015-03-26 15:01:03 +00001608 * number_of_live_dex_registers;
1609 size_t location_mapping_data_size_in_bytes =
1610 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1611 size_t dex_register_map_size =
1612 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1613 return dex_register_map_size;
1614 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001615
Roland Levillaina552e1c2015-03-26 15:01:03 +00001616 // Compute the size of a Dex register location catalog starting at offset `origin`
1617 // in `region_` and containing `number_of_dex_locations` entries.
1618 size_t ComputeDexRegisterLocationCatalogSize(uint32_t origin,
1619 uint32_t number_of_dex_locations) const {
1620 // TODO: Ideally, we would like to use art::DexRegisterLocationCatalog::Size or
1621 // art::DexRegisterLocationCatalog::FindLocationOffset, but the
1622 // DexRegisterLocationCatalog is not yet built. Try to factor common code.
1623 size_t offset = origin + DexRegisterLocationCatalog::kFixedSize;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001624
Roland Levillaina552e1c2015-03-26 15:01:03 +00001625 // Skip the first `number_of_dex_locations - 1` entries.
1626 for (uint16_t i = 0; i < number_of_dex_locations; ++i) {
1627 // Read the first next byte and inspect its first 3 bits to decide
1628 // whether it is a short or a large location.
1629 DexRegisterLocationCatalog::ShortLocation first_byte =
1630 region_.LoadUnaligned<DexRegisterLocationCatalog::ShortLocation>(offset);
1631 DexRegisterLocation::Kind kind =
1632 DexRegisterLocationCatalog::ExtractKindFromShortLocation(first_byte);
1633 if (DexRegisterLocation::IsShortLocationKind(kind)) {
1634 // Short location. Skip the current byte.
1635 offset += DexRegisterLocationCatalog::SingleShortEntrySize();
1636 } else {
1637 // Large location. Skip the 5 next bytes.
1638 offset += DexRegisterLocationCatalog::SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001639 }
1640 }
1641 size_t size = offset - origin;
1642 return size;
1643 }
1644
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001645 MemoryRegion region_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001646 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001647};
1648
Roland Levillain1c1da432015-07-16 11:54:44 +01001649#undef ELEMENT_BYTE_OFFSET_AFTER
1650#undef ELEMENT_BIT_OFFSET_AFTER
1651
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001652} // namespace art
1653
1654#endif // ART_RUNTIME_STACK_MAP_H_