blob: a22498661ef2f1a91e5ca0858c2113d28544e177 [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"
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070026#include "method_info.h"
David Srbecky09ed0982016-02-12 21:58:43 +000027#include "leb128.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010028
29namespace art {
30
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010031class VariableIndentationOutputStream;
32
Roland Levillaina2d8ec62015-03-12 15:25:29 +000033// Size of a frame slot, in bytes. This constant is a signed value,
34// to please the compiler in arithmetic operations involving int32_t
35// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000036static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000037
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000038// Size of Dex virtual registers.
Roland Levillaina552e1c2015-03-26 15:01:03 +000039static constexpr size_t kVRegSize = 4;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000040
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000041class ArtMethod;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000042class CodeInfo;
David Brazdilf677ebf2015-05-29 16:29:43 +010043class StackMapEncoding;
David Srbecky09ed0982016-02-12 21:58:43 +000044struct CodeInfoEncoding;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000045
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010046/**
47 * Classes in the following file are wrapper on stack map information backed
48 * by a MemoryRegion. As such they read and write to the region, they don't have
49 * their own fields.
50 */
51
Roland Levillaina2d8ec62015-03-12 15:25:29 +000052// Dex register location container used by DexRegisterMap and StackMapStream.
53class DexRegisterLocation {
54 public:
55 /*
56 * The location kind used to populate the Dex register information in a
57 * StackMapStream can either be:
David Brazdild9cb68e2015-08-25 13:52:43 +010058 * - kStack: vreg stored on the stack, value holds the stack offset;
59 * - kInRegister: vreg stored in low 32 bits of a core physical register,
60 * value holds the register number;
61 * - kInRegisterHigh: vreg stored in high 32 bits of a core physical register,
62 * value holds the register number;
63 * - kInFpuRegister: vreg stored in low 32 bits of an FPU register,
64 * value holds the register number;
65 * - kInFpuRegisterHigh: vreg stored in high 32 bits of an FPU register,
66 * value holds the register number;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000067 * - kConstant: value holds the constant;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000068 *
69 * In addition, DexRegisterMap also uses these values:
70 * - kInStackLargeOffset: value holds a "large" stack offset (greater than
Roland Levillaina552e1c2015-03-26 15:01:03 +000071 * or equal to 128 bytes);
72 * - kConstantLargeValue: value holds a "large" constant (lower than 0, or
David Brazdild9cb68e2015-08-25 13:52:43 +010073 * or greater than or equal to 32);
74 * - kNone: the register has no location, meaning it has not been set.
Roland Levillaina2d8ec62015-03-12 15:25:29 +000075 */
76 enum class Kind : uint8_t {
77 // Short location kinds, for entries fitting on one byte (3 bits
78 // for the kind, 5 bits for the value) in a DexRegisterMap.
David Brazdild9cb68e2015-08-25 13:52:43 +010079 kInStack = 0, // 0b000
80 kInRegister = 1, // 0b001
81 kInRegisterHigh = 2, // 0b010
Roland Levillaina2d8ec62015-03-12 15:25:29 +000082 kInFpuRegister = 3, // 0b011
David Brazdild9cb68e2015-08-25 13:52:43 +010083 kInFpuRegisterHigh = 4, // 0b100
84 kConstant = 5, // 0b101
Roland Levillaina2d8ec62015-03-12 15:25:29 +000085
86 // Large location kinds, requiring a 5-byte encoding (1 byte for the
87 // kind, 4 bytes for the value).
88
89 // Stack location at a large offset, meaning that the offset value
90 // divided by the stack frame slot size (4 bytes) cannot fit on a
91 // 5-bit unsigned integer (i.e., this offset value is greater than
92 // or equal to 2^5 * 4 = 128 bytes).
David Brazdild9cb68e2015-08-25 13:52:43 +010093 kInStackLargeOffset = 6, // 0b110
Roland Levillaina2d8ec62015-03-12 15:25:29 +000094
95 // Large constant, that cannot fit on a 5-bit signed integer (i.e.,
Roland Levillaina552e1c2015-03-26 15:01:03 +000096 // lower than 0, or greater than or equal to 2^5 = 32).
David Brazdild9cb68e2015-08-25 13:52:43 +010097 kConstantLargeValue = 7, // 0b111
98
99 // Entries with no location are not stored and do not need own marker.
100 kNone = static_cast<uint8_t>(-1),
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000101
102 kLastLocationKind = kConstantLargeValue
103 };
104
105 static_assert(
106 sizeof(Kind) == 1u,
107 "art::DexRegisterLocation::Kind has a size different from one byte.");
108
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000109 static bool IsShortLocationKind(Kind kind) {
110 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000111 case Kind::kInStack:
112 case Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100113 case Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000114 case Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100115 case Kind::kInFpuRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000116 case Kind::kConstant:
117 return true;
118
119 case Kind::kInStackLargeOffset:
120 case Kind::kConstantLargeValue:
121 return false;
122
David Brazdild9cb68e2015-08-25 13:52:43 +0100123 case Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000124 LOG(FATAL) << "Unexpected location kind";
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000125 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100126 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000127 }
128
129 // Convert `kind` to a "surface" kind, i.e. one that doesn't include
130 // any value with a "large" qualifier.
131 // TODO: Introduce another enum type for the surface kind?
132 static Kind ConvertToSurfaceKind(Kind kind) {
133 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000134 case Kind::kInStack:
135 case Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100136 case Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000137 case Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100138 case Kind::kInFpuRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000139 case Kind::kConstant:
140 return kind;
141
142 case Kind::kInStackLargeOffset:
143 return Kind::kInStack;
144
145 case Kind::kConstantLargeValue:
146 return Kind::kConstant;
147
David Brazdild9cb68e2015-08-25 13:52:43 +0100148 case Kind::kNone:
149 return kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000150 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100151 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000152 }
153
Roland Levillaina552e1c2015-03-26 15:01:03 +0000154 // Required by art::StackMapStream::LocationCatalogEntriesIndices.
155 DexRegisterLocation() : kind_(Kind::kNone), value_(0) {}
156
157 DexRegisterLocation(Kind kind, int32_t value) : kind_(kind), value_(value) {}
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000158
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000159 static DexRegisterLocation None() {
160 return DexRegisterLocation(Kind::kNone, 0);
161 }
162
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000163 // Get the "surface" kind of the location, i.e., the one that doesn't
164 // include any value with a "large" qualifier.
165 Kind GetKind() const {
166 return ConvertToSurfaceKind(kind_);
167 }
168
169 // Get the value of the location.
170 int32_t GetValue() const { return value_; }
171
172 // Get the actual kind of the location.
173 Kind GetInternalKind() const { return kind_; }
174
Calin Juravle6ae70962015-03-18 16:31:28 +0000175 bool operator==(DexRegisterLocation other) const {
176 return kind_ == other.kind_ && value_ == other.value_;
177 }
178
179 bool operator!=(DexRegisterLocation other) const {
180 return !(*this == other);
181 }
182
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000183 private:
184 Kind kind_;
185 int32_t value_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000186
187 friend class DexRegisterLocationHashFn;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000188};
189
David Srbecky7dc11782016-02-25 13:23:56 +0000190std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation::Kind& kind);
191
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100192/**
Roland Levillaina552e1c2015-03-26 15:01:03 +0000193 * Store information on unique Dex register locations used in a method.
194 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100195 *
196 * [DexRegisterLocation+].
197 *
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000198 * DexRegisterLocations are either 1- or 5-byte wide (see art::DexRegisterLocation::Kind).
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100199 */
Roland Levillaina552e1c2015-03-26 15:01:03 +0000200class DexRegisterLocationCatalog {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100201 public:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000202 explicit DexRegisterLocationCatalog(MemoryRegion region) : region_(region) {}
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100203
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000204 // Short (compressed) location, fitting on one byte.
205 typedef uint8_t ShortLocation;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100206
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000207 void SetRegisterInfo(size_t offset, const DexRegisterLocation& dex_register_location) {
208 DexRegisterLocation::Kind kind = ComputeCompressedKind(dex_register_location);
209 int32_t value = dex_register_location.GetValue();
210 if (DexRegisterLocation::IsShortLocationKind(kind)) {
211 // Short location. Compress the kind and the value as a single byte.
212 if (kind == DexRegisterLocation::Kind::kInStack) {
213 // Instead of storing stack offsets expressed in bytes for
214 // short stack locations, store slot offsets. A stack offset
215 // is a multiple of 4 (kFrameSlotSize). This means that by
216 // dividing it by 4, we can fit values from the [0, 128)
217 // interval in a short stack location, and not just values
218 // from the [0, 32) interval.
219 DCHECK_EQ(value % kFrameSlotSize, 0);
220 value /= kFrameSlotSize;
221 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000222 DCHECK(IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000223 region_.StoreUnaligned<ShortLocation>(offset, MakeShortLocation(kind, value));
224 } else {
225 // Large location. Write the location on one byte and the value
226 // on 4 bytes.
Roland Levillaina552e1c2015-03-26 15:01:03 +0000227 DCHECK(!IsShortValue(value)) << value;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000228 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
229 // Also divide large stack offsets by 4 for the sake of consistency.
230 DCHECK_EQ(value % kFrameSlotSize, 0);
231 value /= kFrameSlotSize;
232 }
233 // Data can be unaligned as the written Dex register locations can
234 // either be 1-byte or 5-byte wide. Use
235 // art::MemoryRegion::StoreUnaligned instead of
236 // art::MemoryRegion::Store to prevent unligned word accesses on ARM.
237 region_.StoreUnaligned<DexRegisterLocation::Kind>(offset, kind);
238 region_.StoreUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind), value);
Roland Levillain442b46a2015-02-18 16:54:21 +0000239 }
240 }
241
Roland Levillaina552e1c2015-03-26 15:01:03 +0000242 // Find the offset of the location catalog entry number `location_catalog_entry_index`.
243 size_t FindLocationOffset(size_t location_catalog_entry_index) const {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000244 size_t offset = kFixedSize;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000245 // Skip the first `location_catalog_entry_index - 1` entries.
246 for (uint16_t i = 0; i < location_catalog_entry_index; ++i) {
247 // Read the first next byte and inspect its first 3 bits to decide
248 // whether it is a short or a large location.
249 DexRegisterLocation::Kind kind = ExtractKindAtOffset(offset);
250 if (DexRegisterLocation::IsShortLocationKind(kind)) {
251 // Short location. Skip the current byte.
252 offset += SingleShortEntrySize();
253 } else {
254 // Large location. Skip the 5 next bytes.
255 offset += SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000256 }
257 }
258 return offset;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100259 }
260
Roland Levillaina552e1c2015-03-26 15:01:03 +0000261 // Get the internal kind of entry at `location_catalog_entry_index`.
262 DexRegisterLocation::Kind GetLocationInternalKind(size_t location_catalog_entry_index) const {
263 if (location_catalog_entry_index == kNoLocationEntryIndex) {
264 return DexRegisterLocation::Kind::kNone;
265 }
266 return ExtractKindAtOffset(FindLocationOffset(location_catalog_entry_index));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100267 }
268
Roland Levillaina552e1c2015-03-26 15:01:03 +0000269 // Get the (surface) kind and value of entry at `location_catalog_entry_index`.
270 DexRegisterLocation GetDexRegisterLocation(size_t location_catalog_entry_index) const {
271 if (location_catalog_entry_index == kNoLocationEntryIndex) {
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000272 return DexRegisterLocation::None();
273 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000274 size_t offset = FindLocationOffset(location_catalog_entry_index);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000275 // Read the first byte and inspect its first 3 bits to get the location.
276 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
277 DexRegisterLocation::Kind kind = ExtractKindFromShortLocation(first_byte);
278 if (DexRegisterLocation::IsShortLocationKind(kind)) {
279 // Short location. Extract the value from the remaining 5 bits.
280 int32_t value = ExtractValueFromShortLocation(first_byte);
281 if (kind == DexRegisterLocation::Kind::kInStack) {
282 // Convert the stack slot (short) offset to a byte offset value.
283 value *= kFrameSlotSize;
284 }
285 return DexRegisterLocation(kind, value);
286 } else {
287 // Large location. Read the four next bytes to get the value.
288 int32_t value = region_.LoadUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind));
289 if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) {
290 // Convert the stack slot (large) offset to a byte offset value.
291 value *= kFrameSlotSize;
292 }
293 return DexRegisterLocation(kind, value);
294 }
Sebastien Hertz7cde48c2015-01-20 16:06:43 +0100295 }
296
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000297 // Compute the compressed kind of `location`.
298 static DexRegisterLocation::Kind ComputeCompressedKind(const DexRegisterLocation& location) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100299 DexRegisterLocation::Kind kind = location.GetInternalKind();
300 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000301 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000302 return IsShortStackOffsetValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000303 ? DexRegisterLocation::Kind::kInStack
304 : DexRegisterLocation::Kind::kInStackLargeOffset;
305
David Brazdild9cb68e2015-08-25 13:52:43 +0100306 case DexRegisterLocation::Kind::kInRegister:
307 case DexRegisterLocation::Kind::kInRegisterHigh:
308 DCHECK_GE(location.GetValue(), 0);
309 DCHECK_LT(location.GetValue(), 1 << kValueBits);
310 return kind;
311
312 case DexRegisterLocation::Kind::kInFpuRegister:
313 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
314 DCHECK_GE(location.GetValue(), 0);
315 DCHECK_LT(location.GetValue(), 1 << kValueBits);
316 return kind;
317
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000318 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000319 return IsShortConstantValue(location.GetValue())
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000320 ? DexRegisterLocation::Kind::kConstant
321 : DexRegisterLocation::Kind::kConstantLargeValue;
322
David Brazdild9cb68e2015-08-25 13:52:43 +0100323 case DexRegisterLocation::Kind::kConstantLargeValue:
324 case DexRegisterLocation::Kind::kInStackLargeOffset:
325 case DexRegisterLocation::Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000326 LOG(FATAL) << "Unexpected location kind " << kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000327 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100328 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000329 }
330
331 // Can `location` be turned into a short location?
332 static bool CanBeEncodedAsShortLocation(const DexRegisterLocation& location) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100333 DexRegisterLocation::Kind kind = location.GetInternalKind();
334 switch (kind) {
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000335 case DexRegisterLocation::Kind::kInStack:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000336 return IsShortStackOffsetValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000337
David Brazdild9cb68e2015-08-25 13:52:43 +0100338 case DexRegisterLocation::Kind::kInRegister:
339 case DexRegisterLocation::Kind::kInRegisterHigh:
340 case DexRegisterLocation::Kind::kInFpuRegister:
341 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
342 return true;
343
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000344 case DexRegisterLocation::Kind::kConstant:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000345 return IsShortConstantValue(location.GetValue());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000346
David Brazdild9cb68e2015-08-25 13:52:43 +0100347 case DexRegisterLocation::Kind::kConstantLargeValue:
348 case DexRegisterLocation::Kind::kInStackLargeOffset:
349 case DexRegisterLocation::Kind::kNone:
David Srbecky7dc11782016-02-25 13:23:56 +0000350 LOG(FATAL) << "Unexpected location kind " << kind;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000351 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100352 UNREACHABLE();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000353 }
354
355 static size_t EntrySize(const DexRegisterLocation& location) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000356 return CanBeEncodedAsShortLocation(location) ? SingleShortEntrySize() : SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000357 }
358
359 static size_t SingleShortEntrySize() {
360 return sizeof(ShortLocation);
361 }
362
363 static size_t SingleLargeEntrySize() {
364 return sizeof(DexRegisterLocation::Kind) + sizeof(int32_t);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100365 }
366
Roland Levillain12baf472015-03-05 12:41:42 +0000367 size_t Size() const {
368 return region_.size();
369 }
370
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700371 void Dump(VariableIndentationOutputStream* vios,
372 const CodeInfo& code_info);
Roland Levillain0396ed72015-05-27 15:12:19 +0100373
Roland Levillaina552e1c2015-03-26 15:01:03 +0000374 // Special (invalid) Dex register location catalog entry index meaning
375 // that there is no location for a given Dex register (i.e., it is
376 // mapped to a DexRegisterLocation::Kind::kNone location).
377 static constexpr size_t kNoLocationEntryIndex = -1;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100378
Roland Levillain12baf472015-03-05 12:41:42 +0000379 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +0000380 static constexpr int kFixedSize = 0;
381
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000382 // Width of the kind "field" in a short location, in bits.
383 static constexpr size_t kKindBits = 3;
384 // Width of the value "field" in a short location, in bits.
385 static constexpr size_t kValueBits = 5;
386
387 static constexpr uint8_t kKindMask = (1 << kKindBits) - 1;
388 static constexpr int32_t kValueMask = (1 << kValueBits) - 1;
389 static constexpr size_t kKindOffset = 0;
390 static constexpr size_t kValueOffset = kKindBits;
391
Roland Levillaina552e1c2015-03-26 15:01:03 +0000392 static bool IsShortStackOffsetValue(int32_t value) {
393 DCHECK_EQ(value % kFrameSlotSize, 0);
394 return IsShortValue(value / kFrameSlotSize);
395 }
396
397 static bool IsShortConstantValue(int32_t value) {
398 return IsShortValue(value);
399 }
400
401 static bool IsShortValue(int32_t value) {
402 return IsUint<kValueBits>(value);
403 }
404
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000405 static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000406 uint8_t kind_integer_value = static_cast<uint8_t>(kind);
407 DCHECK(IsUint<kKindBits>(kind_integer_value)) << kind_integer_value;
408 DCHECK(IsShortValue(value)) << value;
409 return (kind_integer_value & kKindMask) << kKindOffset
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000410 | (value & kValueMask) << kValueOffset;
411 }
412
413 static DexRegisterLocation::Kind ExtractKindFromShortLocation(ShortLocation location) {
414 uint8_t kind = (location >> kKindOffset) & kKindMask;
415 DCHECK_LE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kLastLocationKind));
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000416 // We do not encode kNone locations in the stack map.
417 DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000418 return static_cast<DexRegisterLocation::Kind>(kind);
419 }
420
421 static int32_t ExtractValueFromShortLocation(ShortLocation location) {
422 return (location >> kValueOffset) & kValueMask;
423 }
424
425 // Extract a location kind from the byte at position `offset`.
426 DexRegisterLocation::Kind ExtractKindAtOffset(size_t offset) const {
427 ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset);
428 return ExtractKindFromShortLocation(first_byte);
429 }
430
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100431 MemoryRegion region_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000432
433 friend class CodeInfo;
434 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100435};
436
Roland Levillaina552e1c2015-03-26 15:01:03 +0000437/* Information on Dex register locations for a specific PC, mapping a
438 * stack map's Dex register to a location entry in a DexRegisterLocationCatalog.
439 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100440 *
441 * [live_bit_mask, entries*]
442 *
Roland Levillaina552e1c2015-03-26 15:01:03 +0000443 * where entries are concatenated unsigned integer values encoded on a number
444 * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending
445 * on the number of entries in the Dex register location catalog
446 * (see DexRegisterMap::SingleEntrySizeInBits). The map is 1-byte aligned.
447 */
448class DexRegisterMap {
449 public:
450 explicit DexRegisterMap(MemoryRegion region) : region_(region) {}
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000451 DexRegisterMap() {}
452
453 bool IsValid() const { return region_.pointer() != nullptr; }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000454
455 // Get the surface kind of Dex register `dex_register_number`.
456 DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number,
457 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100458 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000459 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000460 return DexRegisterLocation::ConvertToSurfaceKind(
David Brazdilf677ebf2015-05-29 16:29:43 +0100461 GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info, enc));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000462 }
463
464 // Get the internal kind of Dex register `dex_register_number`.
465 DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number,
466 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100467 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000468 const CodeInfoEncoding& enc) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000469
470 // Get the Dex register location `dex_register_number`.
471 DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number,
472 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100473 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000474 const CodeInfoEncoding& enc) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000475
476 int32_t GetStackOffsetInBytes(uint16_t dex_register_number,
477 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100478 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000479 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000480 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100481 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000482 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
483 // GetDexRegisterLocation returns the offset in bytes.
484 return location.GetValue();
485 }
486
487 int32_t GetConstant(uint16_t dex_register_number,
488 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100489 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000490 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000491 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100492 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
David Srbecky7dc11782016-02-25 13:23:56 +0000493 DCHECK_EQ(location.GetKind(), DexRegisterLocation::Kind::kConstant);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000494 return location.GetValue();
495 }
496
497 int32_t GetMachineRegister(uint16_t dex_register_number,
498 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +0100499 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000500 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000501 DexRegisterLocation location =
David Brazdilf677ebf2015-05-29 16:29:43 +0100502 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc);
David Brazdild9cb68e2015-08-25 13:52:43 +0100503 DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister ||
504 location.GetInternalKind() == DexRegisterLocation::Kind::kInRegisterHigh ||
505 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister ||
506 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh)
David Srbecky7dc11782016-02-25 13:23:56 +0000507 << location.GetInternalKind();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000508 return location.GetValue();
509 }
510
511 // Get the index of the entry in the Dex register location catalog
512 // corresponding to `dex_register_number`.
513 size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number,
514 uint16_t number_of_dex_registers,
515 size_t number_of_location_catalog_entries) const {
516 if (!IsDexRegisterLive(dex_register_number)) {
517 return DexRegisterLocationCatalog::kNoLocationEntryIndex;
518 }
519
520 if (number_of_location_catalog_entries == 1) {
521 // We do not allocate space for location maps in the case of a
522 // single-entry location catalog, as it is useless. The only valid
523 // entry index is 0;
524 return 0;
525 }
526
527 // The bit offset of the beginning of the map locations.
528 size_t map_locations_offset_in_bits =
529 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
530 size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number);
531 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
532 // The bit size of an entry.
533 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
534 // The bit offset where `index_in_dex_register_map` is located.
535 size_t entry_offset_in_bits =
536 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
537 size_t location_catalog_entry_index =
538 region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits);
539 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
540 return location_catalog_entry_index;
541 }
542
543 // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`.
544 void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map,
545 size_t location_catalog_entry_index,
546 uint16_t number_of_dex_registers,
547 size_t number_of_location_catalog_entries) {
548 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
549 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
550
551 if (number_of_location_catalog_entries == 1) {
552 // We do not allocate space for location maps in the case of a
553 // single-entry location catalog, as it is useless.
554 return;
555 }
556
557 // The bit offset of the beginning of the map locations.
558 size_t map_locations_offset_in_bits =
559 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
560 // The bit size of an entry.
561 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
562 // The bit offset where `index_in_dex_register_map` is located.
563 size_t entry_offset_in_bits =
564 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
565 region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits);
566 }
567
568 void SetLiveBitMask(uint16_t number_of_dex_registers,
569 const BitVector& live_dex_registers_mask) {
570 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
571 for (uint16_t i = 0; i < number_of_dex_registers; ++i) {
572 region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i));
573 }
574 }
575
Mingyao Yang01b47b02017-02-03 12:09:57 -0800576 ALWAYS_INLINE bool IsDexRegisterLive(uint16_t dex_register_number) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000577 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
578 return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number);
579 }
580
581 size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const {
582 size_t number_of_live_dex_registers = 0;
583 for (size_t i = 0; i < number_of_dex_registers; ++i) {
584 if (IsDexRegisterLive(i)) {
585 ++number_of_live_dex_registers;
586 }
587 }
588 return number_of_live_dex_registers;
589 }
590
591 static size_t GetLiveBitMaskOffset() {
592 return kFixedSize;
593 }
594
595 // Compute the size of the live register bit mask (in bytes), for a
596 // method having `number_of_dex_registers` Dex registers.
597 static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) {
598 return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte;
599 }
600
601 static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) {
602 return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers);
603 }
604
605 size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers,
606 size_t number_of_location_catalog_entries) const {
607 size_t location_mapping_data_size_in_bits =
608 GetNumberOfLiveDexRegisters(number_of_dex_registers)
609 * SingleEntrySizeInBits(number_of_location_catalog_entries);
610 return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
611 }
612
613 // Return the size of a map entry in bits. Note that if
614 // `number_of_location_catalog_entries` equals 1, this function returns 0,
615 // which is fine, as there is no need to allocate a map for a
616 // single-entry location catalog; the only valid location catalog entry index
617 // for a live register in this case is 0 and there is no need to
618 // store it.
619 static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) {
620 // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2.
621 return number_of_location_catalog_entries == 0
622 ? 0u
623 : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries));
624 }
625
626 // Return the size of the DexRegisterMap object, in bytes.
627 size_t Size() const {
628 return region_.size();
629 }
630
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100631 void Dump(VariableIndentationOutputStream* vios,
632 const CodeInfo& code_info, uint16_t number_of_dex_registers) const;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100633
Roland Levillaina552e1c2015-03-26 15:01:03 +0000634 private:
635 // Return the index in the Dex register map corresponding to the Dex
636 // register number `dex_register_number`.
637 size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const {
638 if (!IsDexRegisterLive(dex_register_number)) {
639 return kInvalidIndexInDexRegisterMap;
640 }
641 return GetNumberOfLiveDexRegisters(dex_register_number);
642 }
643
644 // Special (invalid) Dex register map entry index meaning that there
645 // is no index in the map for a given Dex register (i.e., it must
646 // have been mapped to a DexRegisterLocation::Kind::kNone location).
647 static constexpr size_t kInvalidIndexInDexRegisterMap = -1;
648
649 static constexpr int kFixedSize = 0;
650
651 MemoryRegion region_;
652
653 friend class CodeInfo;
654 friend class StackMapStream;
655};
656
David Srbecky09ed0982016-02-12 21:58:43 +0000657// Represents bit range of bit-packed integer field.
658// We reuse the idea from ULEB128p1 to support encoding of -1 (aka 0xFFFFFFFF).
659// If min_value is set to -1, we implicitly subtract one from any loaded value,
660// and add one to any stored value. This is generalized to any negative values.
661// In other words, min_value acts as a base and the stored value is added to it.
662struct FieldEncoding {
663 FieldEncoding(size_t start_offset, size_t end_offset, int32_t min_value = 0)
664 : start_offset_(start_offset), end_offset_(end_offset), min_value_(min_value) {
665 DCHECK_LE(start_offset_, end_offset_);
666 DCHECK_LE(BitSize(), 32u);
667 }
668
669 ALWAYS_INLINE size_t BitSize() const { return end_offset_ - start_offset_; }
670
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800671 template <typename Region>
672 ALWAYS_INLINE int32_t Load(const Region& region) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000673 DCHECK_LE(end_offset_, region.size_in_bits());
Mathieu Chartier3ceedc02017-01-25 11:11:02 -0800674 return static_cast<int32_t>(region.LoadBits(start_offset_, BitSize())) + min_value_;
David Srbecky09ed0982016-02-12 21:58:43 +0000675 }
676
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800677 template <typename Region>
678 ALWAYS_INLINE void Store(Region region, int32_t value) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000679 region.StoreBits(start_offset_, value - min_value_, BitSize());
680 DCHECK_EQ(Load(region), value);
681 }
682
683 private:
684 size_t start_offset_;
685 size_t end_offset_;
686 int32_t min_value_;
687};
688
David Brazdilf677ebf2015-05-29 16:29:43 +0100689class StackMapEncoding {
690 public:
Andreas Gamped9911ee2017-03-27 13:27:24 -0700691 StackMapEncoding()
692 : dex_pc_bit_offset_(0),
693 dex_register_map_bit_offset_(0),
694 inline_info_bit_offset_(0),
695 register_mask_index_bit_offset_(0),
696 stack_mask_index_bit_offset_(0),
697 total_bit_size_(0) {}
David Brazdilf677ebf2015-05-29 16:29:43 +0100698
David Srbecky09ed0982016-02-12 21:58:43 +0000699 // Set stack map bit layout based on given sizes.
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800700 // Returns the size of stack map in bits.
David Srbecky09ed0982016-02-12 21:58:43 +0000701 size_t SetFromSizes(size_t native_pc_max,
702 size_t dex_pc_max,
703 size_t dex_register_map_size,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800704 size_t number_of_inline_info,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800705 size_t number_of_register_masks,
David Srbecky45aa5982016-03-18 02:15:09 +0000706 size_t number_of_stack_masks) {
707 total_bit_size_ = 0;
708 DCHECK_EQ(kNativePcBitOffset, total_bit_size_);
709 total_bit_size_ += MinimumBitsToStore(native_pc_max);
David Brazdilf677ebf2015-05-29 16:29:43 +0100710
David Srbecky45aa5982016-03-18 02:15:09 +0000711 dex_pc_bit_offset_ = total_bit_size_;
712 total_bit_size_ += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max);
David Srbecky09ed0982016-02-12 21:58:43 +0000713
714 // We also need +1 for kNoDexRegisterMap, but since the size is strictly
715 // greater than any offset we might try to encode, we already implicitly have it.
David Srbecky45aa5982016-03-18 02:15:09 +0000716 dex_register_map_bit_offset_ = total_bit_size_;
717 total_bit_size_ += MinimumBitsToStore(dex_register_map_size);
David Srbecky09ed0982016-02-12 21:58:43 +0000718
719 // We also need +1 for kNoInlineInfo, but since the inline_info_size is strictly
720 // greater than the offset we might try to encode, we already implicitly have it.
721 // If inline_info_size is zero, we can encode only kNoInlineInfo (in zero bits).
David Srbecky45aa5982016-03-18 02:15:09 +0000722 inline_info_bit_offset_ = total_bit_size_;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800723 total_bit_size_ += MinimumBitsToStore(number_of_inline_info);
David Srbecky09ed0982016-02-12 21:58:43 +0000724
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800725 register_mask_index_bit_offset_ = total_bit_size_;
726 total_bit_size_ += MinimumBitsToStore(number_of_register_masks);
David Srbecky09ed0982016-02-12 21:58:43 +0000727
David Srbecky45aa5982016-03-18 02:15:09 +0000728 stack_mask_index_bit_offset_ = total_bit_size_;
729 total_bit_size_ += MinimumBitsToStore(number_of_stack_masks);
David Srbecky09ed0982016-02-12 21:58:43 +0000730
David Srbecky45aa5982016-03-18 02:15:09 +0000731 return total_bit_size_;
David Brazdilf677ebf2015-05-29 16:29:43 +0100732 }
733
David Srbecky09ed0982016-02-12 21:58:43 +0000734 ALWAYS_INLINE FieldEncoding GetNativePcEncoding() const {
735 return FieldEncoding(kNativePcBitOffset, dex_pc_bit_offset_);
736 }
737 ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const {
738 return FieldEncoding(dex_pc_bit_offset_, dex_register_map_bit_offset_, -1 /* min_value */);
739 }
740 ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const {
741 return FieldEncoding(dex_register_map_bit_offset_, inline_info_bit_offset_, -1 /* min_value */);
742 }
743 ALWAYS_INLINE FieldEncoding GetInlineInfoEncoding() const {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800744 return FieldEncoding(inline_info_bit_offset_,
745 register_mask_index_bit_offset_,
746 -1 /* min_value */);
David Srbecky09ed0982016-02-12 21:58:43 +0000747 }
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800748 ALWAYS_INLINE FieldEncoding GetRegisterMaskIndexEncoding() const {
749 return FieldEncoding(register_mask_index_bit_offset_, stack_mask_index_bit_offset_);
David Srbecky09ed0982016-02-12 21:58:43 +0000750 }
David Srbecky45aa5982016-03-18 02:15:09 +0000751 ALWAYS_INLINE FieldEncoding GetStackMaskIndexEncoding() const {
752 return FieldEncoding(stack_mask_index_bit_offset_, total_bit_size_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100753 }
David Srbecky45aa5982016-03-18 02:15:09 +0000754 ALWAYS_INLINE size_t BitSize() const {
755 return total_bit_size_;
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800756 }
David Brazdilf677ebf2015-05-29 16:29:43 +0100757
Mathieu Chartierc420a802017-02-14 15:16:19 -0800758 // Encode the encoding into the vector.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800759 template<typename Vector>
760 void Encode(Vector* dest) const {
761 static_assert(alignof(StackMapEncoding) == 1, "Should not require alignment");
762 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
763 dest->insert(dest->end(), ptr, ptr + sizeof(*this));
764 }
765
Mathieu Chartierc420a802017-02-14 15:16:19 -0800766 // Decode the encoding from a pointer, updates the pointer.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800767 void Decode(const uint8_t** ptr) {
768 *this = *reinterpret_cast<const StackMapEncoding*>(*ptr);
769 *ptr += sizeof(*this);
770 }
771
David Srbecky09ed0982016-02-12 21:58:43 +0000772 void Dump(VariableIndentationOutputStream* vios) const;
David Brazdilf677ebf2015-05-29 16:29:43 +0100773
774 private:
David Srbecky09ed0982016-02-12 21:58:43 +0000775 static constexpr size_t kNativePcBitOffset = 0;
776 uint8_t dex_pc_bit_offset_;
777 uint8_t dex_register_map_bit_offset_;
778 uint8_t inline_info_bit_offset_;
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800779 uint8_t register_mask_index_bit_offset_;
David Srbecky45aa5982016-03-18 02:15:09 +0000780 uint8_t stack_mask_index_bit_offset_;
781 uint8_t total_bit_size_;
David Brazdilf677ebf2015-05-29 16:29:43 +0100782};
783
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100784/**
785 * A Stack Map holds compilation information for a specific PC necessary for:
786 * - Mapping it to a dex PC,
787 * - Knowing which stack entries are objects,
788 * - Knowing which registers hold objects,
789 * - Knowing the inlining information,
790 * - Knowing the values of dex registers.
791 *
792 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100793 *
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800794 * [native_pc_offset, dex_pc, dex_register_map_offset, inlining_info_index, register_mask_index,
David Srbecky45aa5982016-03-18 02:15:09 +0000795 * stack_mask_index].
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100796 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100797class StackMap {
798 public:
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100799 StackMap() {}
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800800 explicit StackMap(BitMemoryRegion region) : region_(region) {}
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100801
David Srbecky09ed0982016-02-12 21:58:43 +0000802 ALWAYS_INLINE bool IsValid() const { return region_.pointer() != nullptr; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100803
David Srbecky09ed0982016-02-12 21:58:43 +0000804 ALWAYS_INLINE uint32_t GetDexPc(const StackMapEncoding& encoding) const {
805 return encoding.GetDexPcEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100806 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100807
David Srbecky09ed0982016-02-12 21:58:43 +0000808 ALWAYS_INLINE void SetDexPc(const StackMapEncoding& encoding, uint32_t dex_pc) {
809 encoding.GetDexPcEncoding().Store(region_, dex_pc);
David Brazdilf677ebf2015-05-29 16:29:43 +0100810 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100811
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800812 ALWAYS_INLINE uint32_t GetNativePcOffset(const StackMapEncoding& encoding,
813 InstructionSet instruction_set) const {
814 CodeOffset offset(
815 CodeOffset::FromCompressedOffset(encoding.GetNativePcEncoding().Load(region_)));
816 return offset.Uint32Value(instruction_set);
David Brazdilf677ebf2015-05-29 16:29:43 +0100817 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100818
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800819 ALWAYS_INLINE void SetNativePcCodeOffset(const StackMapEncoding& encoding,
820 CodeOffset native_pc_offset) {
821 encoding.GetNativePcEncoding().Store(region_, native_pc_offset.CompressedValue());
David Brazdilf677ebf2015-05-29 16:29:43 +0100822 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100823
David Srbecky09ed0982016-02-12 21:58:43 +0000824 ALWAYS_INLINE uint32_t GetDexRegisterMapOffset(const StackMapEncoding& encoding) const {
825 return encoding.GetDexRegisterMapEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100826 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100827
David Srbecky09ed0982016-02-12 21:58:43 +0000828 ALWAYS_INLINE void SetDexRegisterMapOffset(const StackMapEncoding& encoding, uint32_t offset) {
829 encoding.GetDexRegisterMapEncoding().Store(region_, offset);
David Brazdilf677ebf2015-05-29 16:29:43 +0100830 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100831
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800832 ALWAYS_INLINE uint32_t GetInlineInfoIndex(const StackMapEncoding& encoding) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000833 return encoding.GetInlineInfoEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100834 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100835
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800836 ALWAYS_INLINE void SetInlineInfoIndex(const StackMapEncoding& encoding, uint32_t index) {
837 encoding.GetInlineInfoEncoding().Store(region_, index);
David Brazdilf677ebf2015-05-29 16:29:43 +0100838 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100839
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800840 ALWAYS_INLINE uint32_t GetRegisterMaskIndex(const StackMapEncoding& encoding) const {
841 return encoding.GetRegisterMaskIndexEncoding().Load(region_);
David Brazdilf677ebf2015-05-29 16:29:43 +0100842 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100843
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800844 ALWAYS_INLINE void SetRegisterMaskIndex(const StackMapEncoding& encoding, uint32_t mask) {
845 encoding.GetRegisterMaskIndexEncoding().Store(region_, mask);
David Brazdilf677ebf2015-05-29 16:29:43 +0100846 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100847
David Srbecky45aa5982016-03-18 02:15:09 +0000848 ALWAYS_INLINE uint32_t GetStackMaskIndex(const StackMapEncoding& encoding) const {
849 return encoding.GetStackMaskIndexEncoding().Load(region_);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100850 }
851
David Srbecky45aa5982016-03-18 02:15:09 +0000852 ALWAYS_INLINE void SetStackMaskIndex(const StackMapEncoding& encoding, uint32_t mask) {
853 encoding.GetStackMaskIndexEncoding().Store(region_, mask);
David Srbecky09ed0982016-02-12 21:58:43 +0000854 }
855
856 ALWAYS_INLINE bool HasDexRegisterMap(const StackMapEncoding& encoding) const {
David Brazdilf677ebf2015-05-29 16:29:43 +0100857 return GetDexRegisterMapOffset(encoding) != kNoDexRegisterMap;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100858 }
859
David Srbecky09ed0982016-02-12 21:58:43 +0000860 ALWAYS_INLINE bool HasInlineInfo(const StackMapEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800861 return GetInlineInfoIndex(encoding) != kNoInlineInfo;
Roland Levillain442b46a2015-02-18 16:54:21 +0000862 }
863
David Srbecky09ed0982016-02-12 21:58:43 +0000864 ALWAYS_INLINE bool Equals(const StackMap& other) const {
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800865 return region_.pointer() == other.region_.pointer() &&
866 region_.size() == other.region_.size() &&
867 region_.BitOffset() == other.region_.BitOffset();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100868 }
869
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100870 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100871 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000872 const CodeInfoEncoding& encoding,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700873 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100874 uint32_t code_offset,
875 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800876 InstructionSet instruction_set,
Roland Levillainf2650d12015-05-28 14:53:28 +0100877 const std::string& header_suffix = "") const;
878
Roland Levillain442b46a2015-02-18 16:54:21 +0000879 // Special (invalid) offset for the DexRegisterMapOffset field meaning
880 // that there is no Dex register map for this stack map.
881 static constexpr uint32_t kNoDexRegisterMap = -1;
882
883 // Special (invalid) offset for the InlineDescriptorOffset field meaning
884 // that there is no inline info for this stack map.
885 static constexpr uint32_t kNoInlineInfo = -1;
886
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100887 private:
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100888 static constexpr int kFixedSize = 0;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100889
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800890 BitMemoryRegion region_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100891
Nicolas Geoffray39468442014-09-02 15:17:15 +0100892 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100893};
894
David Srbecky61b28a12016-02-25 21:55:03 +0000895class InlineInfoEncoding {
896 public:
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700897 void SetFromSizes(size_t method_index_idx_max,
David Srbecky61b28a12016-02-25 21:55:03 +0000898 size_t dex_pc_max,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000899 size_t extra_data_max,
David Srbecky61b28a12016-02-25 21:55:03 +0000900 size_t dex_register_map_size) {
901 total_bit_size_ = kMethodIndexBitOffset;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700902 total_bit_size_ += MinimumBitsToStore(method_index_idx_max);
David Srbecky61b28a12016-02-25 21:55:03 +0000903
904 dex_pc_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100905 // Note: We're not encoding the dex pc if there is none. That's the case
906 // for an intrinsified native method, such as String.charAt().
907 if (dex_pc_max != DexFile::kDexNoIndex) {
908 total_bit_size_ += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max);
909 }
David Srbecky61b28a12016-02-25 21:55:03 +0000910
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000911 extra_data_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
912 total_bit_size_ += MinimumBitsToStore(extra_data_max);
David Srbecky61b28a12016-02-25 21:55:03 +0000913
914 // We also need +1 for kNoDexRegisterMap, but since the size is strictly
915 // greater than any offset we might try to encode, we already implicitly have it.
916 dex_register_map_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_);
917 total_bit_size_ += MinimumBitsToStore(dex_register_map_size);
918 }
919
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700920 ALWAYS_INLINE FieldEncoding GetMethodIndexIdxEncoding() const {
David Srbecky61b28a12016-02-25 21:55:03 +0000921 return FieldEncoding(kMethodIndexBitOffset, dex_pc_bit_offset_);
922 }
923 ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000924 return FieldEncoding(dex_pc_bit_offset_, extra_data_bit_offset_, -1 /* min_value */);
David Srbecky61b28a12016-02-25 21:55:03 +0000925 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000926 ALWAYS_INLINE FieldEncoding GetExtraDataEncoding() const {
927 return FieldEncoding(extra_data_bit_offset_, dex_register_map_bit_offset_);
David Srbecky61b28a12016-02-25 21:55:03 +0000928 }
929 ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const {
930 return FieldEncoding(dex_register_map_bit_offset_, total_bit_size_, -1 /* min_value */);
931 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800932 ALWAYS_INLINE size_t BitSize() const {
933 return total_bit_size_;
David Srbecky61b28a12016-02-25 21:55:03 +0000934 }
935
936 void Dump(VariableIndentationOutputStream* vios) const;
937
Mathieu Chartierc420a802017-02-14 15:16:19 -0800938 // Encode the encoding into the vector.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800939 template<typename Vector>
940 void Encode(Vector* dest) const {
941 static_assert(alignof(InlineInfoEncoding) == 1, "Should not require alignment");
942 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
943 dest->insert(dest->end(), ptr, ptr + sizeof(*this));
944 }
945
Mathieu Chartierc420a802017-02-14 15:16:19 -0800946 // Decode the encoding from a pointer, updates the pointer.
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800947 void Decode(const uint8_t** ptr) {
948 *this = *reinterpret_cast<const InlineInfoEncoding*>(*ptr);
949 *ptr += sizeof(*this);
950 }
951
David Srbecky61b28a12016-02-25 21:55:03 +0000952 private:
953 static constexpr uint8_t kIsLastBitOffset = 0;
954 static constexpr uint8_t kMethodIndexBitOffset = 1;
955 uint8_t dex_pc_bit_offset_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000956 uint8_t extra_data_bit_offset_;
David Srbecky61b28a12016-02-25 21:55:03 +0000957 uint8_t dex_register_map_bit_offset_;
958 uint8_t total_bit_size_;
959};
960
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100961/**
962 * Inline information for a specific PC. The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100963 *
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000964 * [is_last,
965 * method_index (or ArtMethod high bits),
966 * dex_pc,
967 * extra_data (ArtMethod low bits or 1),
968 * dex_register_map_offset]+.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100969 */
970class InlineInfo {
971 public:
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800972 explicit InlineInfo(BitMemoryRegion region) : region_(region) {}
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100973
David Srbecky61b28a12016-02-25 21:55:03 +0000974 ALWAYS_INLINE uint32_t GetDepth(const InlineInfoEncoding& encoding) const {
975 size_t depth = 0;
976 while (!GetRegionAtDepth(encoding, depth++).LoadBit(0)) { } // Check is_last bit.
977 return depth;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100978 }
979
David Srbecky61b28a12016-02-25 21:55:03 +0000980 ALWAYS_INLINE void SetDepth(const InlineInfoEncoding& encoding, uint32_t depth) {
981 DCHECK_GT(depth, 0u);
982 for (size_t d = 0; d < depth; ++d) {
983 GetRegionAtDepth(encoding, d).StoreBit(0, d == depth - 1); // Set is_last bit.
984 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100985 }
986
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700987 ALWAYS_INLINE uint32_t GetMethodIndexIdxAtDepth(const InlineInfoEncoding& encoding,
988 uint32_t depth) const {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000989 DCHECK(!EncodesArtMethodAtDepth(encoding, depth));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700990 return encoding.GetMethodIndexIdxEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100991 }
992
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700993 ALWAYS_INLINE void SetMethodIndexIdxAtDepth(const InlineInfoEncoding& encoding,
994 uint32_t depth,
995 uint32_t index) {
996 encoding.GetMethodIndexIdxEncoding().Store(GetRegionAtDepth(encoding, depth), index);
997 }
998
999
1000 ALWAYS_INLINE uint32_t GetMethodIndexAtDepth(const InlineInfoEncoding& encoding,
1001 const MethodInfo& method_info,
1002 uint32_t depth) const {
1003 return method_info.GetMethodIndex(GetMethodIndexIdxAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001004 }
1005
David Srbecky61b28a12016-02-25 21:55:03 +00001006 ALWAYS_INLINE uint32_t GetDexPcAtDepth(const InlineInfoEncoding& encoding,
1007 uint32_t depth) const {
1008 return encoding.GetDexPcEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001009 }
1010
David Srbecky61b28a12016-02-25 21:55:03 +00001011 ALWAYS_INLINE void SetDexPcAtDepth(const InlineInfoEncoding& encoding,
1012 uint32_t depth,
1013 uint32_t dex_pc) {
1014 encoding.GetDexPcEncoding().Store(GetRegionAtDepth(encoding, depth), dex_pc);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001015 }
1016
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001017 ALWAYS_INLINE bool EncodesArtMethodAtDepth(const InlineInfoEncoding& encoding,
1018 uint32_t depth) const {
1019 return (encoding.GetExtraDataEncoding().Load(GetRegionAtDepth(encoding, depth)) & 1) == 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001020 }
1021
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001022 ALWAYS_INLINE void SetExtraDataAtDepth(const InlineInfoEncoding& encoding,
1023 uint32_t depth,
1024 uint32_t extra_data) {
1025 encoding.GetExtraDataEncoding().Store(GetRegionAtDepth(encoding, depth), extra_data);
1026 }
1027
1028 ALWAYS_INLINE ArtMethod* GetArtMethodAtDepth(const InlineInfoEncoding& encoding,
1029 uint32_t depth) const {
1030 uint32_t low_bits = encoding.GetExtraDataEncoding().Load(GetRegionAtDepth(encoding, depth));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001031 uint32_t high_bits = encoding.GetMethodIndexIdxEncoding().Load(
1032 GetRegionAtDepth(encoding, depth));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001033 if (high_bits == 0) {
1034 return reinterpret_cast<ArtMethod*>(low_bits);
1035 } else {
1036 uint64_t address = high_bits;
1037 address = address << 32;
1038 return reinterpret_cast<ArtMethod*>(address | low_bits);
1039 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001040 }
1041
David Srbecky61b28a12016-02-25 21:55:03 +00001042 ALWAYS_INLINE uint32_t GetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding,
1043 uint32_t depth) const {
1044 return encoding.GetDexRegisterMapEncoding().Load(GetRegionAtDepth(encoding, depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001045 }
1046
David Srbecky61b28a12016-02-25 21:55:03 +00001047 ALWAYS_INLINE void SetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding,
1048 uint32_t depth,
1049 uint32_t offset) {
1050 encoding.GetDexRegisterMapEncoding().Store(GetRegionAtDepth(encoding, depth), offset);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001051 }
1052
David Srbecky61b28a12016-02-25 21:55:03 +00001053 ALWAYS_INLINE bool HasDexRegisterMapAtDepth(const InlineInfoEncoding& encoding,
1054 uint32_t depth) const {
1055 return GetDexRegisterMapOffsetAtDepth(encoding, depth) != StackMap::kNoDexRegisterMap;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001056 }
1057
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001058 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +00001059 const CodeInfo& info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001060 const MethodInfo& method_info,
David Srbecky61b28a12016-02-25 21:55:03 +00001061 uint16_t* number_of_dex_registers) const;
Roland Levillain1c1da432015-07-16 11:54:44 +01001062
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001063 private:
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001064 ALWAYS_INLINE BitMemoryRegion GetRegionAtDepth(const InlineInfoEncoding& encoding,
1065 uint32_t depth) const {
1066 size_t entry_size = encoding.BitSize();
David Srbecky61b28a12016-02-25 21:55:03 +00001067 DCHECK_GT(entry_size, 0u);
1068 return region_.Subregion(depth * entry_size, entry_size);
1069 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001070
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001071 BitMemoryRegion region_;
1072};
1073
1074// Bit sized region encoding, may be more than 255 bits.
1075class BitRegionEncoding {
1076 public:
1077 uint32_t num_bits = 0;
1078
1079 ALWAYS_INLINE size_t BitSize() const {
1080 return num_bits;
1081 }
1082
1083 template<typename Vector>
1084 void Encode(Vector* dest) const {
1085 EncodeUnsignedLeb128(dest, num_bits); // Use leb in case num_bits is greater than 255.
1086 }
1087
1088 void Decode(const uint8_t** ptr) {
1089 num_bits = DecodeUnsignedLeb128(ptr);
1090 }
1091};
1092
1093// A table of bit sized encodings.
1094template <typename Encoding>
1095struct BitEncodingTable {
1096 static constexpr size_t kInvalidOffset = static_cast<size_t>(-1);
1097 // How the encoding is laid out (serialized).
1098 Encoding encoding;
1099
1100 // Number of entries in the table (serialized).
1101 size_t num_entries;
1102
1103 // Bit offset for the base of the table (computed).
1104 size_t bit_offset = kInvalidOffset;
1105
1106 template<typename Vector>
1107 void Encode(Vector* dest) const {
1108 EncodeUnsignedLeb128(dest, num_entries);
1109 encoding.Encode(dest);
1110 }
1111
1112 ALWAYS_INLINE void Decode(const uint8_t** ptr) {
1113 num_entries = DecodeUnsignedLeb128(ptr);
1114 encoding.Decode(ptr);
1115 }
1116
1117 // Set the bit offset in the table and adds the space used by the table to offset.
1118 void UpdateBitOffset(size_t* offset) {
1119 DCHECK(offset != nullptr);
1120 bit_offset = *offset;
1121 *offset += encoding.BitSize() * num_entries;
1122 }
1123
1124 // Return the bit region for the map at index i.
1125 ALWAYS_INLINE BitMemoryRegion BitRegion(MemoryRegion region, size_t index) const {
1126 DCHECK_NE(bit_offset, kInvalidOffset) << "Invalid table offset";
1127 DCHECK_LT(index, num_entries);
1128 const size_t map_size = encoding.BitSize();
1129 return BitMemoryRegion(region, bit_offset + index * map_size, map_size);
1130 }
1131};
1132
1133// A byte sized table of possible variable sized encodings.
1134struct ByteSizedTable {
1135 static constexpr size_t kInvalidOffset = static_cast<size_t>(-1);
1136
1137 // Number of entries in the table (serialized).
1138 size_t num_entries = 0;
1139
1140 // Number of bytes of the table (serialized).
1141 size_t num_bytes;
1142
1143 // Bit offset for the base of the table (computed).
1144 size_t byte_offset = kInvalidOffset;
1145
1146 template<typename Vector>
1147 void Encode(Vector* dest) const {
1148 EncodeUnsignedLeb128(dest, num_entries);
1149 EncodeUnsignedLeb128(dest, num_bytes);
1150 }
1151
1152 ALWAYS_INLINE void Decode(const uint8_t** ptr) {
1153 num_entries = DecodeUnsignedLeb128(ptr);
1154 num_bytes = DecodeUnsignedLeb128(ptr);
1155 }
1156
1157 // Set the bit offset of the table. Adds the total bit size of the table to offset.
1158 void UpdateBitOffset(size_t* offset) {
1159 DCHECK(offset != nullptr);
1160 DCHECK_ALIGNED(*offset, kBitsPerByte);
1161 byte_offset = *offset / kBitsPerByte;
1162 *offset += num_bytes * kBitsPerByte;
1163 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001164};
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001165
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001166// Format is [native pc, invoke type, method index].
1167class InvokeInfoEncoding {
1168 public:
1169 void SetFromSizes(size_t native_pc_max,
1170 size_t invoke_type_max,
1171 size_t method_index_max) {
1172 total_bit_size_ = 0;
1173 DCHECK_EQ(kNativePcBitOffset, total_bit_size_);
1174 total_bit_size_ += MinimumBitsToStore(native_pc_max);
1175 invoke_type_bit_offset_ = total_bit_size_;
1176 total_bit_size_ += MinimumBitsToStore(invoke_type_max);
1177 method_index_bit_offset_ = total_bit_size_;
1178 total_bit_size_ += MinimumBitsToStore(method_index_max);
1179 }
1180
1181 ALWAYS_INLINE FieldEncoding GetNativePcEncoding() const {
1182 return FieldEncoding(kNativePcBitOffset, invoke_type_bit_offset_);
1183 }
1184
1185 ALWAYS_INLINE FieldEncoding GetInvokeTypeEncoding() const {
1186 return FieldEncoding(invoke_type_bit_offset_, method_index_bit_offset_);
1187 }
1188
1189 ALWAYS_INLINE FieldEncoding GetMethodIndexEncoding() const {
1190 return FieldEncoding(method_index_bit_offset_, total_bit_size_);
1191 }
1192
1193 ALWAYS_INLINE size_t BitSize() const {
1194 return total_bit_size_;
1195 }
1196
1197 template<typename Vector>
1198 void Encode(Vector* dest) const {
1199 static_assert(alignof(InvokeInfoEncoding) == 1, "Should not require alignment");
1200 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(this);
1201 dest->insert(dest->end(), ptr, ptr + sizeof(*this));
1202 }
1203
1204 void Decode(const uint8_t** ptr) {
1205 *this = *reinterpret_cast<const InvokeInfoEncoding*>(*ptr);
1206 *ptr += sizeof(*this);
1207 }
1208
1209 private:
1210 static constexpr uint8_t kNativePcBitOffset = 0;
1211 uint8_t invoke_type_bit_offset_;
1212 uint8_t method_index_bit_offset_;
1213 uint8_t total_bit_size_;
1214};
1215
1216class InvokeInfo {
1217 public:
1218 explicit InvokeInfo(BitMemoryRegion region) : region_(region) {}
1219
1220 ALWAYS_INLINE uint32_t GetNativePcOffset(const InvokeInfoEncoding& encoding,
1221 InstructionSet instruction_set) const {
1222 CodeOffset offset(
1223 CodeOffset::FromCompressedOffset(encoding.GetNativePcEncoding().Load(region_)));
1224 return offset.Uint32Value(instruction_set);
1225 }
1226
1227 ALWAYS_INLINE void SetNativePcCodeOffset(const InvokeInfoEncoding& encoding,
1228 CodeOffset native_pc_offset) {
1229 encoding.GetNativePcEncoding().Store(region_, native_pc_offset.CompressedValue());
1230 }
1231
1232 ALWAYS_INLINE uint32_t GetInvokeType(const InvokeInfoEncoding& encoding) const {
1233 return encoding.GetInvokeTypeEncoding().Load(region_);
1234 }
1235
1236 ALWAYS_INLINE void SetInvokeType(const InvokeInfoEncoding& encoding, uint32_t invoke_type) {
1237 encoding.GetInvokeTypeEncoding().Store(region_, invoke_type);
1238 }
1239
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001240 ALWAYS_INLINE uint32_t GetMethodIndexIdx(const InvokeInfoEncoding& encoding) const {
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001241 return encoding.GetMethodIndexEncoding().Load(region_);
1242 }
1243
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001244 ALWAYS_INLINE void SetMethodIndexIdx(const InvokeInfoEncoding& encoding,
1245 uint32_t method_index_idx) {
1246 encoding.GetMethodIndexEncoding().Store(region_, method_index_idx);
1247 }
1248
1249 ALWAYS_INLINE uint32_t GetMethodIndex(const InvokeInfoEncoding& encoding,
1250 MethodInfo method_info) const {
1251 return method_info.GetMethodIndex(GetMethodIndexIdx(encoding));
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001252 }
1253
1254 bool IsValid() const { return region_.pointer() != nullptr; }
1255
1256 private:
1257 BitMemoryRegion region_;
1258};
1259
David Srbecky09ed0982016-02-12 21:58:43 +00001260// Most of the fields are encoded as ULEB128 to save space.
1261struct CodeInfoEncoding {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001262 static constexpr uint32_t kInvalidSize = static_cast<size_t>(-1);
1263 // Byte sized tables go first to avoid unnecessary alignment bits.
1264 ByteSizedTable dex_register_map;
1265 ByteSizedTable location_catalog;
1266 BitEncodingTable<StackMapEncoding> stack_map;
1267 BitEncodingTable<BitRegionEncoding> register_mask;
1268 BitEncodingTable<BitRegionEncoding> stack_mask;
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001269 BitEncodingTable<InvokeInfoEncoding> invoke_info;
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001270 BitEncodingTable<InlineInfoEncoding> inline_info;
David Srbecky09ed0982016-02-12 21:58:43 +00001271
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001272 CodeInfoEncoding() {}
David Srbecky09ed0982016-02-12 21:58:43 +00001273
1274 explicit CodeInfoEncoding(const void* data) {
1275 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001276 dex_register_map.Decode(&ptr);
1277 location_catalog.Decode(&ptr);
1278 stack_map.Decode(&ptr);
1279 register_mask.Decode(&ptr);
1280 stack_mask.Decode(&ptr);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001281 invoke_info.Decode(&ptr);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001282 if (stack_map.encoding.GetInlineInfoEncoding().BitSize() > 0) {
1283 inline_info.Decode(&ptr);
David Srbecky61b28a12016-02-25 21:55:03 +00001284 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001285 inline_info = BitEncodingTable<InlineInfoEncoding>();
David Srbecky61b28a12016-02-25 21:55:03 +00001286 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001287 cache_header_size =
1288 dchecked_integral_cast<uint32_t>(ptr - reinterpret_cast<const uint8_t*>(data));
1289 ComputeTableOffsets();
David Srbecky09ed0982016-02-12 21:58:43 +00001290 }
1291
Mathieu Chartierc420a802017-02-14 15:16:19 -08001292 // Compress is not const since it calculates cache_header_size. This is used by PrepareForFillIn.
David Srbecky09ed0982016-02-12 21:58:43 +00001293 template<typename Vector>
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001294 void Compress(Vector* dest) {
1295 dex_register_map.Encode(dest);
1296 location_catalog.Encode(dest);
1297 stack_map.Encode(dest);
1298 register_mask.Encode(dest);
1299 stack_mask.Encode(dest);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001300 invoke_info.Encode(dest);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001301 if (stack_map.encoding.GetInlineInfoEncoding().BitSize() > 0) {
1302 inline_info.Encode(dest);
David Srbecky61b28a12016-02-25 21:55:03 +00001303 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001304 cache_header_size = dest->size();
David Srbecky09ed0982016-02-12 21:58:43 +00001305 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001306
1307 ALWAYS_INLINE void ComputeTableOffsets() {
1308 // Skip the header.
1309 size_t bit_offset = HeaderSize() * kBitsPerByte;
1310 // The byte tables must be aligned so they must go first.
1311 dex_register_map.UpdateBitOffset(&bit_offset);
1312 location_catalog.UpdateBitOffset(&bit_offset);
1313 // Other tables don't require alignment.
1314 stack_map.UpdateBitOffset(&bit_offset);
1315 register_mask.UpdateBitOffset(&bit_offset);
1316 stack_mask.UpdateBitOffset(&bit_offset);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001317 invoke_info.UpdateBitOffset(&bit_offset);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001318 inline_info.UpdateBitOffset(&bit_offset);
1319 cache_non_header_size = RoundUp(bit_offset, kBitsPerByte) / kBitsPerByte - HeaderSize();
1320 }
1321
1322 ALWAYS_INLINE size_t HeaderSize() const {
1323 DCHECK_NE(cache_header_size, kInvalidSize) << "Uninitialized";
1324 return cache_header_size;
1325 }
1326
1327 ALWAYS_INLINE size_t NonHeaderSize() const {
1328 DCHECK_NE(cache_non_header_size, kInvalidSize) << "Uninitialized";
1329 return cache_non_header_size;
1330 }
1331
1332 private:
1333 // Computed fields (not serialized).
Mathieu Chartierc420a802017-02-14 15:16:19 -08001334 // Header size in bytes, cached to avoid needing to re-decoding the encoding in HeaderSize.
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001335 uint32_t cache_header_size = kInvalidSize;
Mathieu Chartierc420a802017-02-14 15:16:19 -08001336 // Non header size in bytes, cached to avoid needing to re-decoding the encoding in NonHeaderSize.
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001337 uint32_t cache_non_header_size = kInvalidSize;
David Srbecky09ed0982016-02-12 21:58:43 +00001338};
1339
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001340/**
1341 * Wrapper around all compiler information collected for a method.
1342 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +01001343 *
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001344 * [CodeInfoEncoding, DexRegisterMap+, DexLocationCatalog+, StackMap+, RegisterMask+, StackMask+,
Mathieu Chartierc420a802017-02-14 15:16:19 -08001345 * InlineInfo*]
1346 *
1347 * where CodeInfoEncoding is of the form:
1348 *
1349 * [ByteSizedTable(dex_register_map), ByteSizedTable(location_catalog),
1350 * BitEncodingTable<StackMapEncoding>, BitEncodingTable<BitRegionEncoding>,
1351 * BitEncodingTable<BitRegionEncoding>, BitEncodingTable<InlineInfoEncoding>]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001352 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001353class CodeInfo {
1354 public:
David Srbecky09ed0982016-02-12 21:58:43 +00001355 explicit CodeInfo(MemoryRegion region) : region_(region) {
1356 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001357
Nicolas Geoffray39468442014-09-02 15:17:15 +01001358 explicit CodeInfo(const void* data) {
David Srbecky09ed0982016-02-12 21:58:43 +00001359 CodeInfoEncoding encoding = CodeInfoEncoding(data);
1360 region_ = MemoryRegion(const_cast<void*>(data),
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001361 encoding.HeaderSize() + encoding.NonHeaderSize());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001362 }
1363
David Srbecky09ed0982016-02-12 21:58:43 +00001364 CodeInfoEncoding ExtractEncoding() const {
David Srbecky45aa5982016-03-18 02:15:09 +00001365 CodeInfoEncoding encoding(region_.begin());
Mathieu Chartier01c78142017-01-05 10:17:55 -08001366 AssertValidStackMap(encoding);
1367 return encoding;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001368 }
1369
David Srbecky09ed0982016-02-12 21:58:43 +00001370 bool HasInlineInfo(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001371 return encoding.stack_map.encoding.GetInlineInfoEncoding().BitSize() > 0;
Nicolas Geoffray896f8f72015-03-30 15:44:25 +01001372 }
1373
David Srbecky09ed0982016-02-12 21:58:43 +00001374 DexRegisterLocationCatalog GetDexRegisterLocationCatalog(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001375 return DexRegisterLocationCatalog(region_.Subregion(encoding.location_catalog.byte_offset,
1376 encoding.location_catalog.num_bytes));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001377 }
1378
Mathieu Chartier12f1b992017-01-19 18:00:45 -08001379 ALWAYS_INLINE size_t GetNumberOfStackMaskBits(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001380 return encoding.stack_mask.encoding.BitSize();
Mathieu Chartier12f1b992017-01-19 18:00:45 -08001381 }
1382
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001383 ALWAYS_INLINE StackMap GetStackMapAt(size_t index, const CodeInfoEncoding& encoding) const {
1384 return StackMap(encoding.stack_map.BitRegion(region_, index));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001385 }
1386
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001387 BitMemoryRegion GetStackMask(size_t index, const CodeInfoEncoding& encoding) const {
1388 return encoding.stack_mask.BitRegion(region_, index);
David Srbecky45aa5982016-03-18 02:15:09 +00001389 }
1390
1391 BitMemoryRegion GetStackMaskOf(const CodeInfoEncoding& encoding,
1392 const StackMap& stack_map) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001393 return GetStackMask(stack_map.GetStackMaskIndex(encoding.stack_map.encoding), encoding);
David Srbecky45aa5982016-03-18 02:15:09 +00001394 }
1395
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001396 BitMemoryRegion GetRegisterMask(size_t index, const CodeInfoEncoding& encoding) const {
1397 return encoding.register_mask.BitRegion(region_, index);
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001398 }
1399
1400 uint32_t GetRegisterMaskOf(const CodeInfoEncoding& encoding, const StackMap& stack_map) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001401 size_t index = stack_map.GetRegisterMaskIndex(encoding.stack_map.encoding);
1402 return GetRegisterMask(index, encoding).LoadBits(0u, encoding.register_mask.encoding.BitSize());
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001403 }
1404
David Srbecky09ed0982016-02-12 21:58:43 +00001405 uint32_t GetNumberOfLocationCatalogEntries(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001406 return encoding.location_catalog.num_entries;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001407 }
1408
David Srbecky09ed0982016-02-12 21:58:43 +00001409 uint32_t GetDexRegisterLocationCatalogSize(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001410 return encoding.location_catalog.num_bytes;
Roland Levillaina552e1c2015-03-26 15:01:03 +00001411 }
1412
David Srbecky09ed0982016-02-12 21:58:43 +00001413 uint32_t GetNumberOfStackMaps(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001414 return encoding.stack_map.num_entries;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001415 }
1416
David Srbecky45aa5982016-03-18 02:15:09 +00001417 // Get the size of all the stack maps of this CodeInfo object, in bits. Not byte aligned.
1418 ALWAYS_INLINE size_t GetStackMapsSizeInBits(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001419 return encoding.stack_map.encoding.BitSize() * GetNumberOfStackMaps(encoding);
Nicolas Geoffray6530baf2015-05-26 15:22:58 +01001420 }
1421
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001422 InvokeInfo GetInvokeInfo(const CodeInfoEncoding& encoding, size_t index) const {
1423 return InvokeInfo(encoding.invoke_info.BitRegion(region_, index));
1424 }
1425
David Brazdilf677ebf2015-05-29 16:29:43 +01001426 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map,
David Srbecky09ed0982016-02-12 21:58:43 +00001427 const CodeInfoEncoding& encoding,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001428 size_t number_of_dex_registers) const {
1429 if (!stack_map.HasDexRegisterMap(encoding.stack_map.encoding)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001430 return DexRegisterMap();
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001431 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001432 const uint32_t offset = encoding.dex_register_map.byte_offset +
1433 stack_map.GetDexRegisterMapOffset(encoding.stack_map.encoding);
1434 size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers);
1435 return DexRegisterMap(region_.Subregion(offset, size));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001436 }
1437
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001438 size_t GetDexRegisterMapsSize(const CodeInfoEncoding& encoding,
1439 uint32_t number_of_dex_registers) const {
1440 size_t total = 0;
1441 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
1442 StackMap stack_map = GetStackMapAt(i, encoding);
1443 DexRegisterMap map(GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers));
1444 total += map.Size();
1445 }
1446 return total;
1447 }
1448
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001449 // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`.
1450 DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
1451 InlineInfo inline_info,
David Srbecky09ed0982016-02-12 21:58:43 +00001452 const CodeInfoEncoding& encoding,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001453 uint32_t number_of_dex_registers) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001454 if (!inline_info.HasDexRegisterMapAtDepth(encoding.inline_info.encoding, depth)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001455 return DexRegisterMap();
1456 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001457 uint32_t offset = encoding.dex_register_map.byte_offset +
1458 inline_info.GetDexRegisterMapOffsetAtDepth(encoding.inline_info.encoding, depth);
David Srbecky09ed0982016-02-12 21:58:43 +00001459 size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +00001460 return DexRegisterMap(region_.Subregion(offset, size));
1461 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001462 }
1463
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001464 InlineInfo GetInlineInfo(size_t index, const CodeInfoEncoding& encoding) const {
Mathieu Chartierc420a802017-02-14 15:16:19 -08001465 // Since we do not know the depth, we just return the whole remaining map. The caller may
1466 // access the inline info for arbitrary depths. To return the precise inline info we would need
1467 // to count the depth before returning.
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001468 // TODO: Clean this up.
1469 const size_t bit_offset = encoding.inline_info.bit_offset +
1470 index * encoding.inline_info.encoding.BitSize();
1471 return InlineInfo(BitMemoryRegion(region_, bit_offset, region_.size_in_bits() - bit_offset));
1472 }
1473
David Srbecky09ed0982016-02-12 21:58:43 +00001474 InlineInfo GetInlineInfoOf(StackMap stack_map, const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001475 DCHECK(stack_map.HasInlineInfo(encoding.stack_map.encoding));
1476 uint32_t index = stack_map.GetInlineInfoIndex(encoding.stack_map.encoding);
1477 return GetInlineInfo(index, encoding);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001478 }
1479
David Srbecky09ed0982016-02-12 21:58:43 +00001480 StackMap GetStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1481 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +01001482 StackMap stack_map = GetStackMapAt(i, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001483 if (stack_map.GetDexPc(encoding.stack_map.encoding) == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001484 return stack_map;
1485 }
1486 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001487 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001488 }
1489
David Brazdil77a48ae2015-09-15 12:34:04 +00001490 // Searches the stack map list backwards because catch stack maps are stored
1491 // at the end.
David Srbecky09ed0982016-02-12 21:58:43 +00001492 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1493 for (size_t i = GetNumberOfStackMaps(encoding); i > 0; --i) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001494 StackMap stack_map = GetStackMapAt(i - 1, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001495 if (stack_map.GetDexPc(encoding.stack_map.encoding) == dex_pc) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001496 return stack_map;
1497 }
1498 }
1499 return StackMap();
1500 }
1501
David Srbecky09ed0982016-02-12 21:58:43 +00001502 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const {
1503 size_t e = GetNumberOfStackMaps(encoding);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001504 if (e == 0) {
1505 // There cannot be OSR stack map if there is no stack map.
1506 return StackMap();
1507 }
1508 // Walk over all stack maps. If two consecutive stack maps are identical, then we
1509 // have found a stack map suitable for OSR.
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001510 const StackMapEncoding& stack_map_encoding = encoding.stack_map.encoding;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001511 for (size_t i = 0; i < e - 1; ++i) {
1512 StackMap stack_map = GetStackMapAt(i, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001513 if (stack_map.GetDexPc(stack_map_encoding) == dex_pc) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001514 StackMap other = GetStackMapAt(i + 1, encoding);
David Srbecky09ed0982016-02-12 21:58:43 +00001515 if (other.GetDexPc(stack_map_encoding) == dex_pc &&
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001516 other.GetNativePcOffset(stack_map_encoding, kRuntimeISA) ==
1517 stack_map.GetNativePcOffset(stack_map_encoding, kRuntimeISA)) {
David Srbecky09ed0982016-02-12 21:58:43 +00001518 DCHECK_EQ(other.GetDexRegisterMapOffset(stack_map_encoding),
1519 stack_map.GetDexRegisterMapOffset(stack_map_encoding));
1520 DCHECK(!stack_map.HasInlineInfo(stack_map_encoding));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001521 if (i < e - 2) {
1522 // Make sure there are not three identical stack maps following each other.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001523 DCHECK_NE(
1524 stack_map.GetNativePcOffset(stack_map_encoding, kRuntimeISA),
1525 GetStackMapAt(i + 2, encoding).GetNativePcOffset(stack_map_encoding, kRuntimeISA));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001526 }
1527 return stack_map;
1528 }
1529 }
1530 }
1531 return StackMap();
1532 }
1533
David Brazdilf677ebf2015-05-29 16:29:43 +01001534 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset,
David Srbecky09ed0982016-02-12 21:58:43 +00001535 const CodeInfoEncoding& encoding) const {
David Brazdil77a48ae2015-09-15 12:34:04 +00001536 // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack
1537 // maps are not. If we knew that the method does not have try/catch,
1538 // we could do binary search.
David Srbecky09ed0982016-02-12 21:58:43 +00001539 for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +01001540 StackMap stack_map = GetStackMapAt(i, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001541 if (stack_map.GetNativePcOffset(encoding.stack_map.encoding, kRuntimeISA) ==
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001542 native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001543 return stack_map;
1544 }
1545 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +01001546 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001547 }
1548
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001549 InvokeInfo GetInvokeInfoForNativePcOffset(uint32_t native_pc_offset,
1550 const CodeInfoEncoding& encoding) {
1551 for (size_t index = 0; index < encoding.invoke_info.num_entries; index++) {
1552 InvokeInfo item = GetInvokeInfo(encoding, index);
1553 if (item.GetNativePcOffset(encoding.invoke_info.encoding, kRuntimeISA) == native_pc_offset) {
1554 return item;
1555 }
1556 }
1557 return InvokeInfo(BitMemoryRegion());
1558 }
1559
Roland Levillainf2650d12015-05-28 14:53:28 +01001560 // Dump this CodeInfo object on `os`. `code_offset` is the (absolute)
1561 // native PC of the compiled method and `number_of_dex_registers` the
1562 // number of Dex virtual registers used in this method. If
1563 // `dump_stack_maps` is true, also dump the stack maps and the
1564 // associated Dex register maps.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001565 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001566 uint32_t code_offset,
1567 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001568 bool dump_stack_maps,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001569 InstructionSet instruction_set,
1570 const MethodInfo& method_info) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001571
Mathieu Chartier01c78142017-01-05 10:17:55 -08001572 // Check that the code info has valid stack map and abort if it does not.
1573 void AssertValidStackMap(const CodeInfoEncoding& encoding) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001574 if (region_.size() != 0 && region_.size_in_bits() < GetStackMapsSizeInBits(encoding)) {
Mathieu Chartier01c78142017-01-05 10:17:55 -08001575 LOG(FATAL) << region_.size() << "\n"
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001576 << encoding.HeaderSize() << "\n"
1577 << encoding.NonHeaderSize() << "\n"
1578 << encoding.location_catalog.num_entries << "\n"
1579 << encoding.stack_map.num_entries << "\n"
1580 << encoding.stack_map.encoding.BitSize();
Mathieu Chartier01c78142017-01-05 10:17:55 -08001581 }
1582 }
1583
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001584 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +00001585 // Compute the size of the Dex register map associated to the stack map at
1586 // `dex_register_map_offset_in_code_info`.
David Srbecky09ed0982016-02-12 21:58:43 +00001587 size_t ComputeDexRegisterMapSizeOf(const CodeInfoEncoding& encoding,
1588 uint32_t dex_register_map_offset_in_code_info,
Roland Levillaina552e1c2015-03-26 15:01:03 +00001589 uint16_t number_of_dex_registers) const {
1590 // Offset where the actual mapping data starts within art::DexRegisterMap.
1591 size_t location_mapping_data_offset_in_dex_register_map =
1592 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1593 // Create a temporary art::DexRegisterMap to be able to call
1594 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1595 DexRegisterMap dex_register_map_without_locations(
1596 MemoryRegion(region_.Subregion(dex_register_map_offset_in_code_info,
1597 location_mapping_data_offset_in_dex_register_map)));
1598 size_t number_of_live_dex_registers =
1599 dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
1600 size_t location_mapping_data_size_in_bits =
David Srbecky09ed0982016-02-12 21:58:43 +00001601 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries(encoding))
Roland Levillaina552e1c2015-03-26 15:01:03 +00001602 * number_of_live_dex_registers;
1603 size_t location_mapping_data_size_in_bytes =
1604 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1605 size_t dex_register_map_size =
1606 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1607 return dex_register_map_size;
1608 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001609
Roland Levillaina552e1c2015-03-26 15:01:03 +00001610 // Compute the size of a Dex register location catalog starting at offset `origin`
1611 // in `region_` and containing `number_of_dex_locations` entries.
1612 size_t ComputeDexRegisterLocationCatalogSize(uint32_t origin,
1613 uint32_t number_of_dex_locations) const {
1614 // TODO: Ideally, we would like to use art::DexRegisterLocationCatalog::Size or
1615 // art::DexRegisterLocationCatalog::FindLocationOffset, but the
1616 // DexRegisterLocationCatalog is not yet built. Try to factor common code.
1617 size_t offset = origin + DexRegisterLocationCatalog::kFixedSize;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001618
Roland Levillaina552e1c2015-03-26 15:01:03 +00001619 // Skip the first `number_of_dex_locations - 1` entries.
1620 for (uint16_t i = 0; i < number_of_dex_locations; ++i) {
1621 // Read the first next byte and inspect its first 3 bits to decide
1622 // whether it is a short or a large location.
1623 DexRegisterLocationCatalog::ShortLocation first_byte =
1624 region_.LoadUnaligned<DexRegisterLocationCatalog::ShortLocation>(offset);
1625 DexRegisterLocation::Kind kind =
1626 DexRegisterLocationCatalog::ExtractKindFromShortLocation(first_byte);
1627 if (DexRegisterLocation::IsShortLocationKind(kind)) {
1628 // Short location. Skip the current byte.
1629 offset += DexRegisterLocationCatalog::SingleShortEntrySize();
1630 } else {
1631 // Large location. Skip the 5 next bytes.
1632 offset += DexRegisterLocationCatalog::SingleLargeEntrySize();
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001633 }
1634 }
1635 size_t size = offset - origin;
1636 return size;
1637 }
1638
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001639 MemoryRegion region_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001640 friend class StackMapStream;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001641};
1642
Roland Levillain1c1da432015-07-16 11:54:44 +01001643#undef ELEMENT_BYTE_OFFSET_AFTER
1644#undef ELEMENT_BIT_OFFSET_AFTER
1645
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001646} // namespace art
1647
1648#endif // ART_RUNTIME_STACK_MAP_H_