blob: 91cecf0690486d9186b96f0ef5fcc4d7b2f8ef78 [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"
David Srbecky052f8ca2018-04-26 15:42:54 +010024#include "base/bit_table.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/bit_utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "base/bit_vector.h"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "base/leb128.h"
David Sehr1ce2b3b2018-04-05 11:02:03 -070028#include "base/memory_region.h"
David Sehr9e734c72018-01-04 17:56:19 -080029#include "dex/dex_file_types.h"
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070030#include "method_info.h"
David Srbecky052f8ca2018-04-26 15:42:54 +010031#include "oat_quick_method_header.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010032
33namespace art {
34
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010035class VariableIndentationOutputStream;
36
Roland Levillaina2d8ec62015-03-12 15:25:29 +000037// Size of a frame slot, in bytes. This constant is a signed value,
38// to please the compiler in arithmetic operations involving int32_t
39// (signed) values.
Roland Levillaina552e1c2015-03-26 15:01:03 +000040static constexpr ssize_t kFrameSlotSize = 4;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000041
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000042class ArtMethod;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000043class CodeInfo;
44
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
David Srbecky68fefac2018-05-10 17:49:33 +0100452 bool IsValid() const { return region_.IsValid(); }
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 Srbecky052f8ca2018-04-26 15:42:54 +0100457 const CodeInfo& code_info) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000458 return DexRegisterLocation::ConvertToSurfaceKind(
David Srbecky052f8ca2018-04-26 15:42:54 +0100459 GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000460 }
461
462 // Get the internal kind of Dex register `dex_register_number`.
463 DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number,
464 uint16_t number_of_dex_registers,
David Srbecky052f8ca2018-04-26 15:42:54 +0100465 const CodeInfo& code_info) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000466
467 // Get the Dex register location `dex_register_number`.
468 DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number,
469 uint16_t number_of_dex_registers,
David Srbecky052f8ca2018-04-26 15:42:54 +0100470 const CodeInfo& code_info) const;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000471
472 int32_t GetStackOffsetInBytes(uint16_t dex_register_number,
473 uint16_t number_of_dex_registers,
David Srbecky052f8ca2018-04-26 15:42:54 +0100474 const CodeInfo& code_info) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000475 DexRegisterLocation location =
David Srbecky052f8ca2018-04-26 15:42:54 +0100476 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000477 DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack);
478 // GetDexRegisterLocation returns the offset in bytes.
479 return location.GetValue();
480 }
481
482 int32_t GetConstant(uint16_t dex_register_number,
483 uint16_t number_of_dex_registers,
David Srbecky052f8ca2018-04-26 15:42:54 +0100484 const CodeInfo& code_info) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000485 DexRegisterLocation location =
David Srbecky052f8ca2018-04-26 15:42:54 +0100486 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
David Srbecky7dc11782016-02-25 13:23:56 +0000487 DCHECK_EQ(location.GetKind(), DexRegisterLocation::Kind::kConstant);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000488 return location.GetValue();
489 }
490
491 int32_t GetMachineRegister(uint16_t dex_register_number,
492 uint16_t number_of_dex_registers,
David Srbecky052f8ca2018-04-26 15:42:54 +0100493 const CodeInfo& code_info) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000494 DexRegisterLocation location =
David Srbecky052f8ca2018-04-26 15:42:54 +0100495 GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info);
David Brazdild9cb68e2015-08-25 13:52:43 +0100496 DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister ||
497 location.GetInternalKind() == DexRegisterLocation::Kind::kInRegisterHigh ||
498 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister ||
499 location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh)
David Srbecky7dc11782016-02-25 13:23:56 +0000500 << location.GetInternalKind();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000501 return location.GetValue();
502 }
503
504 // Get the index of the entry in the Dex register location catalog
505 // corresponding to `dex_register_number`.
506 size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number,
507 uint16_t number_of_dex_registers,
508 size_t number_of_location_catalog_entries) const {
509 if (!IsDexRegisterLive(dex_register_number)) {
510 return DexRegisterLocationCatalog::kNoLocationEntryIndex;
511 }
512
513 if (number_of_location_catalog_entries == 1) {
514 // We do not allocate space for location maps in the case of a
515 // single-entry location catalog, as it is useless. The only valid
516 // entry index is 0;
517 return 0;
518 }
519
520 // The bit offset of the beginning of the map locations.
521 size_t map_locations_offset_in_bits =
522 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
523 size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number);
524 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
525 // The bit size of an entry.
526 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
527 // The bit offset where `index_in_dex_register_map` is located.
528 size_t entry_offset_in_bits =
529 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
530 size_t location_catalog_entry_index =
531 region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits);
532 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
533 return location_catalog_entry_index;
534 }
535
536 // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`.
537 void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map,
538 size_t location_catalog_entry_index,
539 uint16_t number_of_dex_registers,
540 size_t number_of_location_catalog_entries) {
541 DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers));
542 DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries);
543
544 if (number_of_location_catalog_entries == 1) {
545 // We do not allocate space for location maps in the case of a
546 // single-entry location catalog, as it is useless.
547 return;
548 }
549
550 // The bit offset of the beginning of the map locations.
551 size_t map_locations_offset_in_bits =
552 GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte;
553 // The bit size of an entry.
554 size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries);
555 // The bit offset where `index_in_dex_register_map` is located.
556 size_t entry_offset_in_bits =
557 map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits;
558 region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits);
559 }
560
561 void SetLiveBitMask(uint16_t number_of_dex_registers,
562 const BitVector& live_dex_registers_mask) {
563 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
564 for (uint16_t i = 0; i < number_of_dex_registers; ++i) {
565 region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i));
566 }
567 }
568
Mingyao Yang01b47b02017-02-03 12:09:57 -0800569 ALWAYS_INLINE bool IsDexRegisterLive(uint16_t dex_register_number) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +0000570 size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte;
571 return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number);
572 }
573
574 size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const {
575 size_t number_of_live_dex_registers = 0;
576 for (size_t i = 0; i < number_of_dex_registers; ++i) {
577 if (IsDexRegisterLive(i)) {
578 ++number_of_live_dex_registers;
579 }
580 }
581 return number_of_live_dex_registers;
582 }
583
584 static size_t GetLiveBitMaskOffset() {
585 return kFixedSize;
586 }
587
588 // Compute the size of the live register bit mask (in bytes), for a
589 // method having `number_of_dex_registers` Dex registers.
590 static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) {
591 return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte;
592 }
593
594 static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) {
595 return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers);
596 }
597
598 size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers,
599 size_t number_of_location_catalog_entries) const {
600 size_t location_mapping_data_size_in_bits =
601 GetNumberOfLiveDexRegisters(number_of_dex_registers)
602 * SingleEntrySizeInBits(number_of_location_catalog_entries);
603 return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
604 }
605
606 // Return the size of a map entry in bits. Note that if
607 // `number_of_location_catalog_entries` equals 1, this function returns 0,
608 // which is fine, as there is no need to allocate a map for a
609 // single-entry location catalog; the only valid location catalog entry index
610 // for a live register in this case is 0 and there is no need to
611 // store it.
612 static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) {
613 // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2.
614 return number_of_location_catalog_entries == 0
615 ? 0u
616 : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries));
617 }
618
619 // Return the size of the DexRegisterMap object, in bytes.
620 size_t Size() const {
David Srbecky68fefac2018-05-10 17:49:33 +0100621 return BitsToBytesRoundUp(region_.size_in_bits());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000622 }
623
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100624 void Dump(VariableIndentationOutputStream* vios,
625 const CodeInfo& code_info, uint16_t number_of_dex_registers) const;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100626
Roland Levillaina552e1c2015-03-26 15:01:03 +0000627 private:
628 // Return the index in the Dex register map corresponding to the Dex
629 // register number `dex_register_number`.
630 size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const {
631 if (!IsDexRegisterLive(dex_register_number)) {
632 return kInvalidIndexInDexRegisterMap;
633 }
634 return GetNumberOfLiveDexRegisters(dex_register_number);
635 }
636
637 // Special (invalid) Dex register map entry index meaning that there
638 // is no index in the map for a given Dex register (i.e., it must
639 // have been mapped to a DexRegisterLocation::Kind::kNone location).
640 static constexpr size_t kInvalidIndexInDexRegisterMap = -1;
641
642 static constexpr int kFixedSize = 0;
643
David Srbecky68fefac2018-05-10 17:49:33 +0100644 BitMemoryRegion region_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000645
646 friend class CodeInfo;
647 friend class StackMapStream;
648};
649
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100650/**
651 * A Stack Map holds compilation information for a specific PC necessary for:
652 * - Mapping it to a dex PC,
653 * - Knowing which stack entries are objects,
654 * - Knowing which registers hold objects,
655 * - Knowing the inlining information,
656 * - Knowing the values of dex registers.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100657 */
David Srbecky052f8ca2018-04-26 15:42:54 +0100658class StackMap : public BitTable<6>::Accessor {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100659 public:
David Srbecky052f8ca2018-04-26 15:42:54 +0100660 enum Field {
661 kNativePcOffset,
662 kDexPc,
663 kDexRegisterMapOffset,
664 kInlineInfoIndex,
665 kRegisterMaskIndex,
666 kStackMaskIndex,
667 kCount,
668 };
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100669
David Srbecky052f8ca2018-04-26 15:42:54 +0100670 StackMap() : BitTable<kCount>::Accessor(nullptr, -1) {}
671 StackMap(const BitTable<kCount>* table, uint32_t row)
672 : BitTable<kCount>::Accessor(table, row) {}
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100673
David Srbecky052f8ca2018-04-26 15:42:54 +0100674 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
675 CodeOffset offset(CodeOffset::FromCompressedOffset(Get<kNativePcOffset>()));
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800676 return offset.Uint32Value(instruction_set);
David Brazdilf677ebf2015-05-29 16:29:43 +0100677 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100678
David Srbecky052f8ca2018-04-26 15:42:54 +0100679 uint32_t GetDexPc() const { return Get<kDexPc>(); }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100680
David Srbecky052f8ca2018-04-26 15:42:54 +0100681 uint32_t GetDexRegisterMapOffset() const { return Get<kDexRegisterMapOffset>(); }
682 bool HasDexRegisterMap() const { return GetDexRegisterMapOffset() != kNoValue; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100683
David Srbecky052f8ca2018-04-26 15:42:54 +0100684 uint32_t GetInlineInfoIndex() const { return Get<kInlineInfoIndex>(); }
685 bool HasInlineInfo() const { return GetInlineInfoIndex() != kNoValue; }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100686
David Srbecky052f8ca2018-04-26 15:42:54 +0100687 uint32_t GetRegisterMaskIndex() const { return Get<kRegisterMaskIndex>(); }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100688
David Srbecky052f8ca2018-04-26 15:42:54 +0100689 uint32_t GetStackMaskIndex() const { return Get<kStackMaskIndex>(); }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100690
David Srbecky052f8ca2018-04-26 15:42:54 +0100691 static void DumpEncoding(const BitTable<6>& table, VariableIndentationOutputStream* vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100692 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100693 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700694 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100695 uint32_t code_offset,
696 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800697 InstructionSet instruction_set,
Roland Levillainf2650d12015-05-28 14:53:28 +0100698 const std::string& header_suffix = "") const;
David Srbecky61b28a12016-02-25 21:55:03 +0000699};
700
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100701/**
David Srbecky052f8ca2018-04-26 15:42:54 +0100702 * Inline information for a specific PC.
703 * The row referenced from the StackMap holds information at depth 0.
704 * Following rows hold information for further depths.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100705 */
David Srbecky052f8ca2018-04-26 15:42:54 +0100706class InlineInfo : public BitTable<5>::Accessor {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100707 public:
David Srbecky052f8ca2018-04-26 15:42:54 +0100708 enum Field {
709 kIsLast, // Determines if there are further rows for further depths.
710 kMethodIndexIdx, // Method index or ArtMethod high bits.
711 kDexPc,
712 kExtraData, // ArtMethod low bits or 1.
713 kDexRegisterMapOffset,
714 kCount,
715 };
716 static constexpr uint32_t kLast = -1;
717 static constexpr uint32_t kMore = 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100718
David Srbecky052f8ca2018-04-26 15:42:54 +0100719 InlineInfo(const BitTable<kCount>* table, uint32_t row)
720 : BitTable<kCount>::Accessor(table, row) {}
721
722 ALWAYS_INLINE InlineInfo AtDepth(uint32_t depth) const {
723 return InlineInfo(table_, this->row_ + depth);
724 }
725
726 uint32_t GetDepth() const {
David Srbecky61b28a12016-02-25 21:55:03 +0000727 size_t depth = 0;
David Srbecky052f8ca2018-04-26 15:42:54 +0100728 while (AtDepth(depth++).Get<kIsLast>() == kMore) { }
David Srbecky61b28a12016-02-25 21:55:03 +0000729 return depth;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100730 }
731
David Srbecky052f8ca2018-04-26 15:42:54 +0100732 uint32_t GetMethodIndexIdxAtDepth(uint32_t depth) const {
733 DCHECK(!EncodesArtMethodAtDepth(depth));
734 return AtDepth(depth).Get<kMethodIndexIdx>();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100735 }
736
David Srbecky052f8ca2018-04-26 15:42:54 +0100737 uint32_t GetMethodIndexAtDepth(const MethodInfo& method_info, uint32_t depth) const {
738 return method_info.GetMethodIndex(GetMethodIndexIdxAtDepth(depth));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100739 }
740
David Srbecky052f8ca2018-04-26 15:42:54 +0100741 uint32_t GetDexPcAtDepth(uint32_t depth) const {
742 return AtDepth(depth).Get<kDexPc>();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700743 }
744
David Srbecky052f8ca2018-04-26 15:42:54 +0100745 bool EncodesArtMethodAtDepth(uint32_t depth) const {
746 return (AtDepth(depth).Get<kExtraData>() & 1) == 0;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100747 }
748
David Srbecky052f8ca2018-04-26 15:42:54 +0100749 ArtMethod* GetArtMethodAtDepth(uint32_t depth) const {
750 uint32_t low_bits = AtDepth(depth).Get<kExtraData>();
751 uint32_t high_bits = AtDepth(depth).Get<kMethodIndexIdx>();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000752 if (high_bits == 0) {
753 return reinterpret_cast<ArtMethod*>(low_bits);
754 } else {
755 uint64_t address = high_bits;
756 address = address << 32;
757 return reinterpret_cast<ArtMethod*>(address | low_bits);
758 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100759 }
760
David Srbecky052f8ca2018-04-26 15:42:54 +0100761 uint32_t GetDexRegisterMapOffsetAtDepth(uint32_t depth) const {
762 return AtDepth(depth).Get<kDexRegisterMapOffset>();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100763 }
764
David Srbecky052f8ca2018-04-26 15:42:54 +0100765 bool HasDexRegisterMapAtDepth(uint32_t depth) const {
766 return GetDexRegisterMapOffsetAtDepth(depth) != StackMap::kNoValue;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100767 }
768
David Srbecky052f8ca2018-04-26 15:42:54 +0100769 static void DumpEncoding(const BitTable<5>& table, VariableIndentationOutputStream* vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100770 void Dump(VariableIndentationOutputStream* vios,
David Srbecky61b28a12016-02-25 21:55:03 +0000771 const CodeInfo& info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700772 const MethodInfo& method_info,
David Srbecky61b28a12016-02-25 21:55:03 +0000773 uint16_t* number_of_dex_registers) const;
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800774};
775
David Srbecky052f8ca2018-04-26 15:42:54 +0100776class InvokeInfo : public BitTable<3>::Accessor {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800777 public:
David Srbecky052f8ca2018-04-26 15:42:54 +0100778 enum Field {
779 kNativePcOffset,
780 kInvokeType,
781 kMethodIndexIdx,
782 kCount,
783 };
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800784
David Srbecky052f8ca2018-04-26 15:42:54 +0100785 InvokeInfo(const BitTable<kCount>* table, uint32_t row)
786 : BitTable<kCount>::Accessor(table, row) {}
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800787
David Srbecky052f8ca2018-04-26 15:42:54 +0100788 ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const {
789 CodeOffset offset(CodeOffset::FromCompressedOffset(Get<kNativePcOffset>()));
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800790 return offset.Uint32Value(instruction_set);
791 }
792
David Srbecky052f8ca2018-04-26 15:42:54 +0100793 uint32_t GetInvokeType() const { return Get<kInvokeType>(); }
794
795 uint32_t GetMethodIndexIdx() const { return Get<kMethodIndexIdx>(); }
796
797 uint32_t GetMethodIndex(MethodInfo method_info) const {
798 return method_info.GetMethodIndex(GetMethodIndexIdx());
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800799 }
David Srbecky09ed0982016-02-12 21:58:43 +0000800};
801
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100802/**
803 * Wrapper around all compiler information collected for a method.
804 * The information is of the form:
Roland Levillain1c1da432015-07-16 11:54:44 +0100805 *
David Srbecky052f8ca2018-04-26 15:42:54 +0100806 * [BitTable<Header>, BitTable<StackMap>, BitTable<RegisterMask>, BitTable<InlineInfo>,
807 * BitTable<InvokeInfo>, BitTable<StackMask>, DexRegisterMap, DexLocationCatalog]
Mathieu Chartierc420a802017-02-14 15:16:19 -0800808 *
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100809 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100810class CodeInfo {
811 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100812 explicit CodeInfo(const void* data) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100813 Decode(reinterpret_cast<const uint8_t*>(data));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100814 }
815
David Srbecky052f8ca2018-04-26 15:42:54 +0100816 explicit CodeInfo(MemoryRegion region) : CodeInfo(region.begin()) {
817 DCHECK_EQ(size_, region.size());
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100818 }
819
David Srbecky052f8ca2018-04-26 15:42:54 +0100820 explicit CodeInfo(const OatQuickMethodHeader* header)
821 : CodeInfo(header->GetOptimizedCodeInfoPtr()) {
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100822 }
823
David Srbecky052f8ca2018-04-26 15:42:54 +0100824 size_t Size() const {
825 return size_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000826 }
827
David Srbecky052f8ca2018-04-26 15:42:54 +0100828 bool HasInlineInfo() const {
829 return stack_maps_.NumColumnBits(StackMap::kInlineInfoIndex) != 0;
Mathieu Chartier12f1b992017-01-19 18:00:45 -0800830 }
831
David Srbecky052f8ca2018-04-26 15:42:54 +0100832 DexRegisterLocationCatalog GetDexRegisterLocationCatalog() const {
833 return DexRegisterLocationCatalog(location_catalog_);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100834 }
835
David Srbecky052f8ca2018-04-26 15:42:54 +0100836 ALWAYS_INLINE size_t GetNumberOfStackMaskBits() const {
837 return stack_mask_bits_;
David Srbecky45aa5982016-03-18 02:15:09 +0000838 }
839
David Srbecky052f8ca2018-04-26 15:42:54 +0100840 ALWAYS_INLINE StackMap GetStackMapAt(size_t index) const {
841 return StackMap(&stack_maps_, index);
David Srbecky45aa5982016-03-18 02:15:09 +0000842 }
843
David Srbecky052f8ca2018-04-26 15:42:54 +0100844 BitMemoryRegion GetStackMask(size_t index) const {
845 return stack_masks_.Subregion(index * stack_mask_bits_, stack_mask_bits_);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800846 }
847
David Srbecky052f8ca2018-04-26 15:42:54 +0100848 BitMemoryRegion GetStackMaskOf(const StackMap& stack_map) const {
849 return GetStackMask(stack_map.GetStackMaskIndex());
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800850 }
851
David Srbecky052f8ca2018-04-26 15:42:54 +0100852 uint32_t GetRegisterMaskOf(const StackMap& stack_map) const {
853 return register_masks_.Get(stack_map.GetRegisterMaskIndex());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100854 }
855
David Srbecky052f8ca2018-04-26 15:42:54 +0100856 uint32_t GetNumberOfLocationCatalogEntries() const {
857 return location_catalog_entries_;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000858 }
859
David Srbecky052f8ca2018-04-26 15:42:54 +0100860 uint32_t GetDexRegisterLocationCatalogSize() const {
861 return location_catalog_.size();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100862 }
863
David Srbecky052f8ca2018-04-26 15:42:54 +0100864 uint32_t GetNumberOfStackMaps() const {
865 return stack_maps_.NumRows();
Nicolas Geoffray6530baf2015-05-26 15:22:58 +0100866 }
867
David Srbecky052f8ca2018-04-26 15:42:54 +0100868 InvokeInfo GetInvokeInfo(size_t index) const {
869 return InvokeInfo(&invoke_infos_, index);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800870 }
871
David Brazdilf677ebf2015-05-29 16:29:43 +0100872 DexRegisterMap GetDexRegisterMapOf(StackMap stack_map,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800873 size_t number_of_dex_registers) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100874 if (!stack_map.HasDexRegisterMap()) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000875 return DexRegisterMap();
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000876 }
David Srbecky052f8ca2018-04-26 15:42:54 +0100877 const uint32_t offset = stack_map.GetDexRegisterMapOffset();
878 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
879 return DexRegisterMap(dex_register_maps_.Subregion(offset, size));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100880 }
881
David Srbecky052f8ca2018-04-26 15:42:54 +0100882 size_t GetDexRegisterMapsSize(uint32_t number_of_dex_registers) const {
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800883 size_t total = 0;
David Srbecky052f8ca2018-04-26 15:42:54 +0100884 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
885 StackMap stack_map = GetStackMapAt(i);
886 DexRegisterMap map(GetDexRegisterMapOf(stack_map, number_of_dex_registers));
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800887 total += map.Size();
888 }
889 return total;
890 }
891
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100892 // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`.
893 DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth,
894 InlineInfo inline_info,
895 uint32_t number_of_dex_registers) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100896 if (!inline_info.HasDexRegisterMapAtDepth(depth)) {
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000897 return DexRegisterMap();
898 } else {
David Srbecky052f8ca2018-04-26 15:42:54 +0100899 uint32_t offset = inline_info.GetDexRegisterMapOffsetAtDepth(depth);
900 size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers);
901 return DexRegisterMap(dex_register_maps_.Subregion(offset, size));
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000902 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100903 }
904
David Srbecky052f8ca2018-04-26 15:42:54 +0100905 InlineInfo GetInlineInfo(size_t index) const {
906 return InlineInfo(&inline_infos_, index);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800907 }
908
David Srbecky052f8ca2018-04-26 15:42:54 +0100909 InlineInfo GetInlineInfoOf(StackMap stack_map) const {
910 DCHECK(stack_map.HasInlineInfo());
911 uint32_t index = stack_map.GetInlineInfoIndex();
912 return GetInlineInfo(index);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100913 }
914
David Srbecky052f8ca2018-04-26 15:42:54 +0100915 StackMap GetStackMapForDexPc(uint32_t dex_pc) const {
916 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
917 StackMap stack_map = GetStackMapAt(i);
918 if (stack_map.GetDexPc() == dex_pc) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100919 return stack_map;
920 }
921 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100922 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100923 }
924
David Brazdil77a48ae2015-09-15 12:34:04 +0000925 // Searches the stack map list backwards because catch stack maps are stored
926 // at the end.
David Srbecky052f8ca2018-04-26 15:42:54 +0100927 StackMap GetCatchStackMapForDexPc(uint32_t dex_pc) const {
928 for (size_t i = GetNumberOfStackMaps(); i > 0; --i) {
929 StackMap stack_map = GetStackMapAt(i - 1);
930 if (stack_map.GetDexPc() == dex_pc) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000931 return stack_map;
932 }
933 }
934 return StackMap();
935 }
936
David Srbecky052f8ca2018-04-26 15:42:54 +0100937 StackMap GetOsrStackMapForDexPc(uint32_t dex_pc) const {
938 size_t e = GetNumberOfStackMaps();
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000939 if (e == 0) {
940 // There cannot be OSR stack map if there is no stack map.
941 return StackMap();
942 }
943 // Walk over all stack maps. If two consecutive stack maps are identical, then we
944 // have found a stack map suitable for OSR.
945 for (size_t i = 0; i < e - 1; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100946 StackMap stack_map = GetStackMapAt(i);
947 if (stack_map.GetDexPc() == dex_pc) {
948 StackMap other = GetStackMapAt(i + 1);
949 if (other.GetDexPc() == dex_pc &&
950 other.GetNativePcOffset(kRuntimeISA) ==
951 stack_map.GetNativePcOffset(kRuntimeISA)) {
952 DCHECK_EQ(other.GetDexRegisterMapOffset(),
953 stack_map.GetDexRegisterMapOffset());
954 DCHECK(!stack_map.HasInlineInfo());
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000955 if (i < e - 2) {
956 // Make sure there are not three identical stack maps following each other.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800957 DCHECK_NE(
David Srbecky052f8ca2018-04-26 15:42:54 +0100958 stack_map.GetNativePcOffset(kRuntimeISA),
959 GetStackMapAt(i + 2).GetNativePcOffset(kRuntimeISA));
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000960 }
961 return stack_map;
962 }
963 }
964 }
965 return StackMap();
966 }
967
David Srbecky052f8ca2018-04-26 15:42:54 +0100968 StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const {
David Brazdil77a48ae2015-09-15 12:34:04 +0000969 // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack
970 // maps are not. If we knew that the method does not have try/catch,
971 // we could do binary search.
David Srbecky052f8ca2018-04-26 15:42:54 +0100972 for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) {
973 StackMap stack_map = GetStackMapAt(i);
974 if (stack_map.GetNativePcOffset(kRuntimeISA) == native_pc_offset) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100975 return stack_map;
976 }
977 }
Nicolas Geoffraye12997f2015-05-22 14:01:33 +0100978 return StackMap();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100979 }
980
David Srbecky052f8ca2018-04-26 15:42:54 +0100981 InvokeInfo GetInvokeInfoForNativePcOffset(uint32_t native_pc_offset) {
982 for (size_t index = 0; index < invoke_infos_.NumRows(); index++) {
983 InvokeInfo item = GetInvokeInfo(index);
984 if (item.GetNativePcOffset(kRuntimeISA) == native_pc_offset) {
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800985 return item;
986 }
987 }
David Srbecky052f8ca2018-04-26 15:42:54 +0100988 return InvokeInfo(&invoke_infos_, -1);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800989 }
990
Roland Levillainf2650d12015-05-28 14:53:28 +0100991 // Dump this CodeInfo object on `os`. `code_offset` is the (absolute)
992 // native PC of the compiled method and `number_of_dex_registers` the
993 // number of Dex virtual registers used in this method. If
994 // `dump_stack_maps` is true, also dump the stack maps and the
995 // associated Dex register maps.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100996 void Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100997 uint32_t code_offset,
998 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800999 bool dump_stack_maps,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001000 InstructionSet instruction_set,
1001 const MethodInfo& method_info) const;
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001002
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001003 private:
Roland Levillaina552e1c2015-03-26 15:01:03 +00001004 // Compute the size of the Dex register map associated to the stack map at
1005 // `dex_register_map_offset_in_code_info`.
David Srbecky052f8ca2018-04-26 15:42:54 +01001006 size_t ComputeDexRegisterMapSizeOf(uint32_t dex_register_map_offset,
Roland Levillaina552e1c2015-03-26 15:01:03 +00001007 uint16_t number_of_dex_registers) const {
1008 // Offset where the actual mapping data starts within art::DexRegisterMap.
1009 size_t location_mapping_data_offset_in_dex_register_map =
1010 DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers);
1011 // Create a temporary art::DexRegisterMap to be able to call
1012 // art::DexRegisterMap::GetNumberOfLiveDexRegisters and
1013 DexRegisterMap dex_register_map_without_locations(
David Srbecky052f8ca2018-04-26 15:42:54 +01001014 MemoryRegion(dex_register_maps_.Subregion(dex_register_map_offset,
1015 location_mapping_data_offset_in_dex_register_map)));
Roland Levillaina552e1c2015-03-26 15:01:03 +00001016 size_t number_of_live_dex_registers =
1017 dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
1018 size_t location_mapping_data_size_in_bits =
David Srbecky052f8ca2018-04-26 15:42:54 +01001019 DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries())
Roland Levillaina552e1c2015-03-26 15:01:03 +00001020 * number_of_live_dex_registers;
1021 size_t location_mapping_data_size_in_bytes =
1022 RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
1023 size_t dex_register_map_size =
1024 location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes;
1025 return dex_register_map_size;
1026 }
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +00001027
David Srbecky052f8ca2018-04-26 15:42:54 +01001028 MemoryRegion DecodeMemoryRegion(MemoryRegion& region, size_t* bit_offset) {
1029 size_t length = DecodeVarintBits(BitMemoryRegion(region), bit_offset);
1030 size_t offset = BitsToBytesRoundUp(*bit_offset);;
1031 *bit_offset = (offset + length) * kBitsPerByte;
1032 return region.Subregion(offset, length);
Roland Levillaina2d8ec62015-03-12 15:25:29 +00001033 }
1034
David Srbecky052f8ca2018-04-26 15:42:54 +01001035 void Decode(const uint8_t* data) {
1036 size_t non_header_size = DecodeUnsignedLeb128(&data);
1037 MemoryRegion region(const_cast<uint8_t*>(data), non_header_size);
1038 BitMemoryRegion bit_region(region);
1039 size_t bit_offset = 0;
1040 size_ = UnsignedLeb128Size(non_header_size) + non_header_size;
1041 dex_register_maps_ = DecodeMemoryRegion(region, &bit_offset);
1042 location_catalog_entries_ = DecodeVarintBits(bit_region, &bit_offset);
1043 location_catalog_ = DecodeMemoryRegion(region, &bit_offset);
1044 stack_maps_.Decode(bit_region, &bit_offset);
1045 invoke_infos_.Decode(bit_region, &bit_offset);
1046 inline_infos_.Decode(bit_region, &bit_offset);
1047 register_masks_.Decode(bit_region, &bit_offset);
1048 stack_mask_bits_ = DecodeVarintBits(bit_region, &bit_offset);
1049 stack_masks_ = bit_region.Subregion(bit_offset, non_header_size * kBitsPerByte - bit_offset);
1050 }
1051
1052 size_t size_;
1053 MemoryRegion dex_register_maps_;
1054 uint32_t location_catalog_entries_;
1055 MemoryRegion location_catalog_;
1056 BitTable<StackMap::Field::kCount> stack_maps_;
1057 BitTable<InvokeInfo::Field::kCount> invoke_infos_;
1058 BitTable<InlineInfo::Field::kCount> inline_infos_;
1059 BitTable<1> register_masks_;
1060 uint32_t stack_mask_bits_ = 0;
1061 BitMemoryRegion stack_masks_;
1062
1063 friend class OatDumper;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001064};
1065
Roland Levillain1c1da432015-07-16 11:54:44 +01001066#undef ELEMENT_BYTE_OFFSET_AFTER
1067#undef ELEMENT_BIT_OFFSET_AFTER
1068
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001069} // namespace art
1070
1071#endif // ART_RUNTIME_STACK_MAP_H_