Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1 | /* |
| 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 Gampe | 69489fa | 2017-06-08 18:03:25 -0700 | [diff] [blame] | 20 | #include <limits> |
| 21 | |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 22 | #include "arch/code_offset.h" |
David Sehr | 1ce2b3b | 2018-04-05 11:02:03 -0700 | [diff] [blame] | 23 | #include "base/bit_memory_region.h" |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 24 | #include "base/bit_table.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "base/bit_utils.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 26 | #include "base/bit_vector.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 27 | #include "base/leb128.h" |
David Sehr | 1ce2b3b | 2018-04-05 11:02:03 -0700 | [diff] [blame] | 28 | #include "base/memory_region.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 29 | #include "dex/dex_file_types.h" |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 30 | #include "method_info.h" |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 31 | #include "oat_quick_method_header.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 35 | class VariableIndentationOutputStream; |
| 36 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 37 | // 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 Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 40 | static constexpr ssize_t kFrameSlotSize = 4; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 41 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 42 | class ArtMethod; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 43 | class CodeInfo; |
| 44 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 45 | /** |
| 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 51 | // Dex register location container used by DexRegisterMap and StackMapStream. |
| 52 | class DexRegisterLocation { |
| 53 | public: |
| 54 | /* |
| 55 | * The location kind used to populate the Dex register information in a |
| 56 | * StackMapStream can either be: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 57 | * - 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 66 | * - kConstant: value holds the constant; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 67 | * |
| 68 | * In addition, DexRegisterMap also uses these values: |
| 69 | * - kInStackLargeOffset: value holds a "large" stack offset (greater than |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 70 | * or equal to 128 bytes); |
| 71 | * - kConstantLargeValue: value holds a "large" constant (lower than 0, or |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 72 | * or greater than or equal to 32); |
| 73 | * - kNone: the register has no location, meaning it has not been set. |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 74 | */ |
| 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 Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 78 | kInStack = 0, // 0b000 |
| 79 | kInRegister = 1, // 0b001 |
| 80 | kInRegisterHigh = 2, // 0b010 |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 81 | kInFpuRegister = 3, // 0b011 |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 82 | kInFpuRegisterHigh = 4, // 0b100 |
| 83 | kConstant = 5, // 0b101 |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 84 | |
| 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 Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 92 | kInStackLargeOffset = 6, // 0b110 |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 93 | |
| 94 | // Large constant, that cannot fit on a 5-bit signed integer (i.e., |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 95 | // lower than 0, or greater than or equal to 2^5 = 32). |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 96 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 100 | |
| 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 108 | static bool IsShortLocationKind(Kind kind) { |
| 109 | switch (kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 110 | case Kind::kInStack: |
| 111 | case Kind::kInRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 112 | case Kind::kInRegisterHigh: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 113 | case Kind::kInFpuRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 114 | case Kind::kInFpuRegisterHigh: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 115 | case Kind::kConstant: |
| 116 | return true; |
| 117 | |
| 118 | case Kind::kInStackLargeOffset: |
| 119 | case Kind::kConstantLargeValue: |
| 120 | return false; |
| 121 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 122 | case Kind::kNone: |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 123 | LOG(FATAL) << "Unexpected location kind"; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 124 | } |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 125 | UNREACHABLE(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 126 | } |
| 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 133 | case Kind::kInStack: |
| 134 | case Kind::kInRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 135 | case Kind::kInRegisterHigh: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 136 | case Kind::kInFpuRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 137 | case Kind::kInFpuRegisterHigh: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 138 | 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 Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 147 | case Kind::kNone: |
| 148 | return kind; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 149 | } |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 150 | UNREACHABLE(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 153 | // 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 157 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 158 | static DexRegisterLocation None() { |
| 159 | return DexRegisterLocation(Kind::kNone, 0); |
| 160 | } |
| 161 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 162 | // 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 Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 174 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 182 | private: |
| 183 | Kind kind_; |
| 184 | int32_t value_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 185 | |
| 186 | friend class DexRegisterLocationHashFn; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 189 | std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation::Kind& kind); |
| 190 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 191 | /** |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 192 | * Store information on unique Dex register locations used in a method. |
| 193 | * The information is of the form: |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 194 | * |
| 195 | * [DexRegisterLocation+]. |
| 196 | * |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 197 | * DexRegisterLocations are either 1- or 5-byte wide (see art::DexRegisterLocation::Kind). |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 198 | */ |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 199 | class DexRegisterLocationCatalog { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 200 | public: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 201 | explicit DexRegisterLocationCatalog(MemoryRegion region) : region_(region) {} |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 202 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 203 | // Short (compressed) location, fitting on one byte. |
| 204 | typedef uint8_t ShortLocation; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 205 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 206 | 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 Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 221 | DCHECK(IsShortValue(value)) << value; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 222 | 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 Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 226 | DCHECK(!IsShortValue(value)) << value; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 227 | 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 Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 241 | // 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 243 | size_t offset = kFixedSize; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 244 | // 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | return offset; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 260 | // 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 266 | } |
| 267 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 268 | // 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 Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 271 | return DexRegisterLocation::None(); |
| 272 | } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 273 | size_t offset = FindLocationOffset(location_catalog_entry_index); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 274 | // 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 Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 296 | // Compute the compressed kind of `location`. |
| 297 | static DexRegisterLocation::Kind ComputeCompressedKind(const DexRegisterLocation& location) { |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 298 | DexRegisterLocation::Kind kind = location.GetInternalKind(); |
| 299 | switch (kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 300 | case DexRegisterLocation::Kind::kInStack: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 301 | return IsShortStackOffsetValue(location.GetValue()) |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 302 | ? DexRegisterLocation::Kind::kInStack |
| 303 | : DexRegisterLocation::Kind::kInStackLargeOffset; |
| 304 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 305 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 317 | case DexRegisterLocation::Kind::kConstant: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 318 | return IsShortConstantValue(location.GetValue()) |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 319 | ? DexRegisterLocation::Kind::kConstant |
| 320 | : DexRegisterLocation::Kind::kConstantLargeValue; |
| 321 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 322 | case DexRegisterLocation::Kind::kConstantLargeValue: |
| 323 | case DexRegisterLocation::Kind::kInStackLargeOffset: |
| 324 | case DexRegisterLocation::Kind::kNone: |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 325 | LOG(FATAL) << "Unexpected location kind " << kind; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 326 | } |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 327 | UNREACHABLE(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // Can `location` be turned into a short location? |
| 331 | static bool CanBeEncodedAsShortLocation(const DexRegisterLocation& location) { |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 332 | DexRegisterLocation::Kind kind = location.GetInternalKind(); |
| 333 | switch (kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 334 | case DexRegisterLocation::Kind::kInStack: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 335 | return IsShortStackOffsetValue(location.GetValue()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 336 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 337 | case DexRegisterLocation::Kind::kInRegister: |
| 338 | case DexRegisterLocation::Kind::kInRegisterHigh: |
| 339 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 340 | case DexRegisterLocation::Kind::kInFpuRegisterHigh: |
| 341 | return true; |
| 342 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 343 | case DexRegisterLocation::Kind::kConstant: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 344 | return IsShortConstantValue(location.GetValue()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 345 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 346 | case DexRegisterLocation::Kind::kConstantLargeValue: |
| 347 | case DexRegisterLocation::Kind::kInStackLargeOffset: |
| 348 | case DexRegisterLocation::Kind::kNone: |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 349 | LOG(FATAL) << "Unexpected location kind " << kind; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 350 | } |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 351 | UNREACHABLE(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static size_t EntrySize(const DexRegisterLocation& location) { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 355 | return CanBeEncodedAsShortLocation(location) ? SingleShortEntrySize() : SingleLargeEntrySize(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 356 | } |
| 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 366 | size_t Size() const { |
| 367 | return region_.size(); |
| 368 | } |
| 369 | |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 370 | void Dump(VariableIndentationOutputStream* vios, |
| 371 | const CodeInfo& code_info); |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 372 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 373 | // 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 377 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 378 | private: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 379 | static constexpr int kFixedSize = 0; |
| 380 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 381 | // 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 Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 391 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 404 | static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 405 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 409 | | (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 Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 415 | // We do not encode kNone locations in the stack map. |
| 416 | DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone)); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 417 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 430 | MemoryRegion region_; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 431 | |
| 432 | friend class CodeInfo; |
| 433 | friend class StackMapStream; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 434 | }; |
| 435 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 436 | /* 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 Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 439 | * |
| 440 | * [live_bit_mask, entries*] |
| 441 | * |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 442 | * 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 | */ |
| 447 | class DexRegisterMap { |
| 448 | public: |
| 449 | explicit DexRegisterMap(MemoryRegion region) : region_(region) {} |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 450 | DexRegisterMap() {} |
| 451 | |
David Srbecky | 68fefac | 2018-05-10 17:49:33 +0100 | [diff] [blame] | 452 | bool IsValid() const { return region_.IsValid(); } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 453 | |
| 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 Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 457 | const CodeInfo& code_info) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 458 | return DexRegisterLocation::ConvertToSurfaceKind( |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 459 | GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info)); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 460 | } |
| 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 Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 465 | const CodeInfo& code_info) const; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 466 | |
| 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 Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 470 | const CodeInfo& code_info) const; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 471 | |
| 472 | int32_t GetStackOffsetInBytes(uint16_t dex_register_number, |
| 473 | uint16_t number_of_dex_registers, |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 474 | const CodeInfo& code_info) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 475 | DexRegisterLocation location = |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 476 | GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 477 | 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 Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 484 | const CodeInfo& code_info) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 485 | DexRegisterLocation location = |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 486 | GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info); |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 487 | DCHECK_EQ(location.GetKind(), DexRegisterLocation::Kind::kConstant); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 488 | return location.GetValue(); |
| 489 | } |
| 490 | |
| 491 | int32_t GetMachineRegister(uint16_t dex_register_number, |
| 492 | uint16_t number_of_dex_registers, |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 493 | const CodeInfo& code_info) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 494 | DexRegisterLocation location = |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 495 | GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info); |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 496 | 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 Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 500 | << location.GetInternalKind(); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 501 | 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 Yang | 01b47b0 | 2017-02-03 12:09:57 -0800 | [diff] [blame] | 569 | ALWAYS_INLINE bool IsDexRegisterLive(uint16_t dex_register_number) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 570 | 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 Srbecky | 68fefac | 2018-05-10 17:49:33 +0100 | [diff] [blame] | 621 | return BitsToBytesRoundUp(region_.size_in_bits()); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 624 | void Dump(VariableIndentationOutputStream* vios, |
| 625 | const CodeInfo& code_info, uint16_t number_of_dex_registers) const; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 626 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 627 | 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 Srbecky | 68fefac | 2018-05-10 17:49:33 +0100 | [diff] [blame] | 644 | BitMemoryRegion region_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 645 | |
| 646 | friend class CodeInfo; |
| 647 | friend class StackMapStream; |
| 648 | }; |
| 649 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 650 | /** |
| 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 657 | */ |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 658 | class StackMap : public BitTable<6>::Accessor { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 659 | public: |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 660 | enum Field { |
| 661 | kNativePcOffset, |
| 662 | kDexPc, |
| 663 | kDexRegisterMapOffset, |
| 664 | kInlineInfoIndex, |
| 665 | kRegisterMaskIndex, |
| 666 | kStackMaskIndex, |
| 667 | kCount, |
| 668 | }; |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 669 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 670 | StackMap() : BitTable<kCount>::Accessor(nullptr, -1) {} |
| 671 | StackMap(const BitTable<kCount>* table, uint32_t row) |
| 672 | : BitTable<kCount>::Accessor(table, row) {} |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 673 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 674 | ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const { |
| 675 | CodeOffset offset(CodeOffset::FromCompressedOffset(Get<kNativePcOffset>())); |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 676 | return offset.Uint32Value(instruction_set); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 677 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 678 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 679 | uint32_t GetDexPc() const { return Get<kDexPc>(); } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 680 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 681 | uint32_t GetDexRegisterMapOffset() const { return Get<kDexRegisterMapOffset>(); } |
| 682 | bool HasDexRegisterMap() const { return GetDexRegisterMapOffset() != kNoValue; } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 683 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 684 | uint32_t GetInlineInfoIndex() const { return Get<kInlineInfoIndex>(); } |
| 685 | bool HasInlineInfo() const { return GetInlineInfoIndex() != kNoValue; } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 686 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 687 | uint32_t GetRegisterMaskIndex() const { return Get<kRegisterMaskIndex>(); } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 688 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 689 | uint32_t GetStackMaskIndex() const { return Get<kStackMaskIndex>(); } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 690 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 691 | static void DumpEncoding(const BitTable<6>& table, VariableIndentationOutputStream* vios); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 692 | void Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 693 | const CodeInfo& code_info, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 694 | const MethodInfo& method_info, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 695 | uint32_t code_offset, |
| 696 | uint16_t number_of_dex_registers, |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 697 | InstructionSet instruction_set, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 698 | const std::string& header_suffix = "") const; |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 699 | }; |
| 700 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 701 | /** |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 702 | * 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 Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 705 | */ |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 706 | class InlineInfo : public BitTable<5>::Accessor { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 707 | public: |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 708 | 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 Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 718 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 719 | 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 Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 727 | size_t depth = 0; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 728 | while (AtDepth(depth++).Get<kIsLast>() == kMore) { } |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 729 | return depth; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 730 | } |
| 731 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 732 | uint32_t GetMethodIndexIdxAtDepth(uint32_t depth) const { |
| 733 | DCHECK(!EncodesArtMethodAtDepth(depth)); |
| 734 | return AtDepth(depth).Get<kMethodIndexIdx>(); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 735 | } |
| 736 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 737 | uint32_t GetMethodIndexAtDepth(const MethodInfo& method_info, uint32_t depth) const { |
| 738 | return method_info.GetMethodIndex(GetMethodIndexIdxAtDepth(depth)); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 739 | } |
| 740 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 741 | uint32_t GetDexPcAtDepth(uint32_t depth) const { |
| 742 | return AtDepth(depth).Get<kDexPc>(); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 743 | } |
| 744 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 745 | bool EncodesArtMethodAtDepth(uint32_t depth) const { |
| 746 | return (AtDepth(depth).Get<kExtraData>() & 1) == 0; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 747 | } |
| 748 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 749 | 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 Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 752 | 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 Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 759 | } |
| 760 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 761 | uint32_t GetDexRegisterMapOffsetAtDepth(uint32_t depth) const { |
| 762 | return AtDepth(depth).Get<kDexRegisterMapOffset>(); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 763 | } |
| 764 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 765 | bool HasDexRegisterMapAtDepth(uint32_t depth) const { |
| 766 | return GetDexRegisterMapOffsetAtDepth(depth) != StackMap::kNoValue; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 767 | } |
| 768 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 769 | static void DumpEncoding(const BitTable<5>& table, VariableIndentationOutputStream* vios); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 770 | void Dump(VariableIndentationOutputStream* vios, |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 771 | const CodeInfo& info, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 772 | const MethodInfo& method_info, |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 773 | uint16_t* number_of_dex_registers) const; |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 774 | }; |
| 775 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 776 | class InvokeInfo : public BitTable<3>::Accessor { |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 777 | public: |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 778 | enum Field { |
| 779 | kNativePcOffset, |
| 780 | kInvokeType, |
| 781 | kMethodIndexIdx, |
| 782 | kCount, |
| 783 | }; |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 784 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 785 | InvokeInfo(const BitTable<kCount>* table, uint32_t row) |
| 786 | : BitTable<kCount>::Accessor(table, row) {} |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 787 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 788 | ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const { |
| 789 | CodeOffset offset(CodeOffset::FromCompressedOffset(Get<kNativePcOffset>())); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 790 | return offset.Uint32Value(instruction_set); |
| 791 | } |
| 792 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 793 | 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 Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 799 | } |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 800 | }; |
| 801 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 802 | /** |
| 803 | * Wrapper around all compiler information collected for a method. |
| 804 | * The information is of the form: |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 805 | * |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 806 | * [BitTable<Header>, BitTable<StackMap>, BitTable<RegisterMask>, BitTable<InlineInfo>, |
| 807 | * BitTable<InvokeInfo>, BitTable<StackMask>, DexRegisterMap, DexLocationCatalog] |
Mathieu Chartier | c420a80 | 2017-02-14 15:16:19 -0800 | [diff] [blame] | 808 | * |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 809 | */ |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 810 | class CodeInfo { |
| 811 | public: |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 812 | explicit CodeInfo(const void* data) { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 813 | Decode(reinterpret_cast<const uint8_t*>(data)); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 814 | } |
| 815 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 816 | explicit CodeInfo(MemoryRegion region) : CodeInfo(region.begin()) { |
| 817 | DCHECK_EQ(size_, region.size()); |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 818 | } |
| 819 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 820 | explicit CodeInfo(const OatQuickMethodHeader* header) |
| 821 | : CodeInfo(header->GetOptimizedCodeInfoPtr()) { |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 822 | } |
| 823 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 824 | size_t Size() const { |
| 825 | return size_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 826 | } |
| 827 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 828 | bool HasInlineInfo() const { |
| 829 | return stack_maps_.NumColumnBits(StackMap::kInlineInfoIndex) != 0; |
Mathieu Chartier | 12f1b99 | 2017-01-19 18:00:45 -0800 | [diff] [blame] | 830 | } |
| 831 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 832 | DexRegisterLocationCatalog GetDexRegisterLocationCatalog() const { |
| 833 | return DexRegisterLocationCatalog(location_catalog_); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 834 | } |
| 835 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 836 | ALWAYS_INLINE size_t GetNumberOfStackMaskBits() const { |
| 837 | return stack_mask_bits_; |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 838 | } |
| 839 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 840 | ALWAYS_INLINE StackMap GetStackMapAt(size_t index) const { |
| 841 | return StackMap(&stack_maps_, index); |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 842 | } |
| 843 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 844 | BitMemoryRegion GetStackMask(size_t index) const { |
| 845 | return stack_masks_.Subregion(index * stack_mask_bits_, stack_mask_bits_); |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 846 | } |
| 847 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 848 | BitMemoryRegion GetStackMaskOf(const StackMap& stack_map) const { |
| 849 | return GetStackMask(stack_map.GetStackMaskIndex()); |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 850 | } |
| 851 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 852 | uint32_t GetRegisterMaskOf(const StackMap& stack_map) const { |
| 853 | return register_masks_.Get(stack_map.GetRegisterMaskIndex()); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 854 | } |
| 855 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 856 | uint32_t GetNumberOfLocationCatalogEntries() const { |
| 857 | return location_catalog_entries_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 858 | } |
| 859 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 860 | uint32_t GetDexRegisterLocationCatalogSize() const { |
| 861 | return location_catalog_.size(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 862 | } |
| 863 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 864 | uint32_t GetNumberOfStackMaps() const { |
| 865 | return stack_maps_.NumRows(); |
Nicolas Geoffray | 6530baf | 2015-05-26 15:22:58 +0100 | [diff] [blame] | 866 | } |
| 867 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 868 | InvokeInfo GetInvokeInfo(size_t index) const { |
| 869 | return InvokeInfo(&invoke_infos_, index); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 870 | } |
| 871 | |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 872 | DexRegisterMap GetDexRegisterMapOf(StackMap stack_map, |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 873 | size_t number_of_dex_registers) const { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 874 | if (!stack_map.HasDexRegisterMap()) { |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 875 | return DexRegisterMap(); |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 876 | } |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 877 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 880 | } |
| 881 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 882 | size_t GetDexRegisterMapsSize(uint32_t number_of_dex_registers) const { |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 883 | size_t total = 0; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 884 | 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 Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 887 | total += map.Size(); |
| 888 | } |
| 889 | return total; |
| 890 | } |
| 891 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 892 | // 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 Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 896 | if (!inline_info.HasDexRegisterMapAtDepth(depth)) { |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 897 | return DexRegisterMap(); |
| 898 | } else { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 899 | 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 Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 902 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 903 | } |
| 904 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 905 | InlineInfo GetInlineInfo(size_t index) const { |
| 906 | return InlineInfo(&inline_infos_, index); |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 907 | } |
| 908 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 909 | InlineInfo GetInlineInfoOf(StackMap stack_map) const { |
| 910 | DCHECK(stack_map.HasInlineInfo()); |
| 911 | uint32_t index = stack_map.GetInlineInfoIndex(); |
| 912 | return GetInlineInfo(index); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 913 | } |
| 914 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 915 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 919 | return stack_map; |
| 920 | } |
| 921 | } |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 922 | return StackMap(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 923 | } |
| 924 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 925 | // Searches the stack map list backwards because catch stack maps are stored |
| 926 | // at the end. |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 927 | 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 Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 931 | return stack_map; |
| 932 | } |
| 933 | } |
| 934 | return StackMap(); |
| 935 | } |
| 936 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 937 | StackMap GetOsrStackMapForDexPc(uint32_t dex_pc) const { |
| 938 | size_t e = GetNumberOfStackMaps(); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 939 | 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 Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 946 | 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 Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 955 | if (i < e - 2) { |
| 956 | // Make sure there are not three identical stack maps following each other. |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 957 | DCHECK_NE( |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 958 | stack_map.GetNativePcOffset(kRuntimeISA), |
| 959 | GetStackMapAt(i + 2).GetNativePcOffset(kRuntimeISA)); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 960 | } |
| 961 | return stack_map; |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | return StackMap(); |
| 966 | } |
| 967 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 968 | StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 969 | // 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 Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 972 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 975 | return stack_map; |
| 976 | } |
| 977 | } |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 978 | return StackMap(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 979 | } |
| 980 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 981 | 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 Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 985 | return item; |
| 986 | } |
| 987 | } |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 988 | return InvokeInfo(&invoke_infos_, -1); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 989 | } |
| 990 | |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 991 | // 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 Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 996 | void Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 997 | uint32_t code_offset, |
| 998 | uint16_t number_of_dex_registers, |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 999 | bool dump_stack_maps, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1000 | InstructionSet instruction_set, |
| 1001 | const MethodInfo& method_info) const; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 1002 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1003 | private: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1004 | // Compute the size of the Dex register map associated to the stack map at |
| 1005 | // `dex_register_map_offset_in_code_info`. |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 1006 | size_t ComputeDexRegisterMapSizeOf(uint32_t dex_register_map_offset, |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1007 | 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 Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 1014 | MemoryRegion(dex_register_maps_.Subregion(dex_register_map_offset, |
| 1015 | location_mapping_data_offset_in_dex_register_map))); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1016 | 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 Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 1019 | DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries()) |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1020 | * 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 Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 1027 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 1028 | 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 Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame^] | 1035 | 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 Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1064 | }; |
| 1065 | |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 1066 | #undef ELEMENT_BYTE_OFFSET_AFTER |
| 1067 | #undef ELEMENT_BIT_OFFSET_AFTER |
| 1068 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1069 | } // namespace art |
| 1070 | |
| 1071 | #endif // ART_RUNTIME_STACK_MAP_H_ |