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 | |
David Sehr | 1ce2b3b | 2018-04-05 11:02:03 -0700 | [diff] [blame] | 22 | #include "base/bit_memory_region.h" |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 23 | #include "base/bit_table.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 24 | #include "base/bit_utils.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 25 | #include "base/bit_vector.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 26 | #include "base/leb128.h" |
David Sehr | 1ce2b3b | 2018-04-05 11:02:03 -0700 | [diff] [blame] | 27 | #include "base/memory_region.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 28 | #include "dex/dex_file_types.h" |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 29 | #include "method_info.h" |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 30 | #include "oat_quick_method_header.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 34 | class VariableIndentationOutputStream; |
| 35 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 36 | // Size of a frame slot, in bytes. This constant is a signed value, |
| 37 | // to please the compiler in arithmetic operations involving int32_t |
| 38 | // (signed) values. |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 39 | static constexpr ssize_t kFrameSlotSize = 4; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 40 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 41 | class ArtMethod; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 42 | class CodeInfo; |
| 43 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 44 | /** |
| 45 | * Classes in the following file are wrapper on stack map information backed |
| 46 | * by a MemoryRegion. As such they read and write to the region, they don't have |
| 47 | * their own fields. |
| 48 | */ |
| 49 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 50 | // Dex register location container used by DexRegisterMap and StackMapStream. |
| 51 | class DexRegisterLocation { |
| 52 | public: |
| 53 | /* |
| 54 | * The location kind used to populate the Dex register information in a |
| 55 | * StackMapStream can either be: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 56 | * - kStack: vreg stored on the stack, value holds the stack offset; |
| 57 | * - kInRegister: vreg stored in low 32 bits of a core physical register, |
| 58 | * value holds the register number; |
| 59 | * - kInRegisterHigh: vreg stored in high 32 bits of a core physical register, |
| 60 | * value holds the register number; |
| 61 | * - kInFpuRegister: vreg stored in low 32 bits of an FPU register, |
| 62 | * value holds the register number; |
| 63 | * - kInFpuRegisterHigh: vreg stored in high 32 bits of an FPU register, |
| 64 | * value holds the register number; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 65 | * - kConstant: value holds the constant; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 66 | * |
| 67 | * In addition, DexRegisterMap also uses these values: |
| 68 | * - kInStackLargeOffset: value holds a "large" stack offset (greater than |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 69 | * or equal to 128 bytes); |
| 70 | * - kConstantLargeValue: value holds a "large" constant (lower than 0, or |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 71 | * or greater than or equal to 32); |
| 72 | * - kNone: the register has no location, meaning it has not been set. |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 73 | */ |
| 74 | enum class Kind : uint8_t { |
| 75 | // Short location kinds, for entries fitting on one byte (3 bits |
| 76 | // for the kind, 5 bits for the value) in a DexRegisterMap. |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 77 | kInStack = 0, // 0b000 |
| 78 | kInRegister = 1, // 0b001 |
| 79 | kInRegisterHigh = 2, // 0b010 |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 80 | kInFpuRegister = 3, // 0b011 |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 81 | kInFpuRegisterHigh = 4, // 0b100 |
| 82 | kConstant = 5, // 0b101 |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 83 | |
| 84 | // Large location kinds, requiring a 5-byte encoding (1 byte for the |
| 85 | // kind, 4 bytes for the value). |
| 86 | |
| 87 | // Stack location at a large offset, meaning that the offset value |
| 88 | // divided by the stack frame slot size (4 bytes) cannot fit on a |
| 89 | // 5-bit unsigned integer (i.e., this offset value is greater than |
| 90 | // or equal to 2^5 * 4 = 128 bytes). |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 91 | kInStackLargeOffset = 6, // 0b110 |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 92 | |
| 93 | // 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] | 94 | // lower than 0, or greater than or equal to 2^5 = 32). |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 95 | kConstantLargeValue = 7, // 0b111 |
| 96 | |
| 97 | // Entries with no location are not stored and do not need own marker. |
| 98 | kNone = static_cast<uint8_t>(-1), |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 99 | |
| 100 | kLastLocationKind = kConstantLargeValue |
| 101 | }; |
| 102 | |
| 103 | static_assert( |
| 104 | sizeof(Kind) == 1u, |
| 105 | "art::DexRegisterLocation::Kind has a size different from one byte."); |
| 106 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 107 | static bool IsShortLocationKind(Kind kind) { |
| 108 | switch (kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 109 | case Kind::kInStack: |
| 110 | case Kind::kInRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 111 | case Kind::kInRegisterHigh: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 112 | case Kind::kInFpuRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 113 | case Kind::kInFpuRegisterHigh: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 114 | case Kind::kConstant: |
| 115 | return true; |
| 116 | |
| 117 | case Kind::kInStackLargeOffset: |
| 118 | case Kind::kConstantLargeValue: |
| 119 | return false; |
| 120 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 121 | case Kind::kNone: |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 122 | LOG(FATAL) << "Unexpected location kind"; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 123 | } |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 124 | UNREACHABLE(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // Convert `kind` to a "surface" kind, i.e. one that doesn't include |
| 128 | // any value with a "large" qualifier. |
| 129 | // TODO: Introduce another enum type for the surface kind? |
| 130 | static Kind ConvertToSurfaceKind(Kind kind) { |
| 131 | switch (kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 132 | case Kind::kInStack: |
| 133 | case Kind::kInRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 134 | case Kind::kInRegisterHigh: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 135 | case Kind::kInFpuRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 136 | case Kind::kInFpuRegisterHigh: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 137 | case Kind::kConstant: |
| 138 | return kind; |
| 139 | |
| 140 | case Kind::kInStackLargeOffset: |
| 141 | return Kind::kInStack; |
| 142 | |
| 143 | case Kind::kConstantLargeValue: |
| 144 | return Kind::kConstant; |
| 145 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 146 | case Kind::kNone: |
| 147 | return kind; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 148 | } |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 149 | UNREACHABLE(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 152 | // Required by art::StackMapStream::LocationCatalogEntriesIndices. |
| 153 | DexRegisterLocation() : kind_(Kind::kNone), value_(0) {} |
| 154 | |
| 155 | DexRegisterLocation(Kind kind, int32_t value) : kind_(kind), value_(value) {} |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 156 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 157 | static DexRegisterLocation None() { |
| 158 | return DexRegisterLocation(Kind::kNone, 0); |
| 159 | } |
| 160 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 161 | // Get the "surface" kind of the location, i.e., the one that doesn't |
| 162 | // include any value with a "large" qualifier. |
| 163 | Kind GetKind() const { |
| 164 | return ConvertToSurfaceKind(kind_); |
| 165 | } |
| 166 | |
| 167 | // Get the value of the location. |
| 168 | int32_t GetValue() const { return value_; } |
| 169 | |
| 170 | // Get the actual kind of the location. |
| 171 | Kind GetInternalKind() const { return kind_; } |
| 172 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 173 | bool operator==(DexRegisterLocation other) const { |
| 174 | return kind_ == other.kind_ && value_ == other.value_; |
| 175 | } |
| 176 | |
| 177 | bool operator!=(DexRegisterLocation other) const { |
| 178 | return !(*this == other); |
| 179 | } |
| 180 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 181 | private: |
| 182 | Kind kind_; |
| 183 | int32_t value_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 184 | |
| 185 | friend class DexRegisterLocationHashFn; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 188 | std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation::Kind& kind); |
| 189 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 190 | /** |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 191 | * Store information on unique Dex register locations used in a method. |
| 192 | * The information is of the form: |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 193 | * |
| 194 | * [DexRegisterLocation+]. |
| 195 | * |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 196 | * DexRegisterLocations are either 1- or 5-byte wide (see art::DexRegisterLocation::Kind). |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 197 | */ |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 198 | class DexRegisterLocationCatalog { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 199 | public: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 200 | explicit DexRegisterLocationCatalog(MemoryRegion region) : region_(region) {} |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 201 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 202 | // Short (compressed) location, fitting on one byte. |
| 203 | typedef uint8_t ShortLocation; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 204 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 205 | void SetRegisterInfo(size_t offset, const DexRegisterLocation& dex_register_location) { |
| 206 | DexRegisterLocation::Kind kind = ComputeCompressedKind(dex_register_location); |
| 207 | int32_t value = dex_register_location.GetValue(); |
| 208 | if (DexRegisterLocation::IsShortLocationKind(kind)) { |
| 209 | // Short location. Compress the kind and the value as a single byte. |
| 210 | if (kind == DexRegisterLocation::Kind::kInStack) { |
| 211 | // Instead of storing stack offsets expressed in bytes for |
| 212 | // short stack locations, store slot offsets. A stack offset |
| 213 | // is a multiple of 4 (kFrameSlotSize). This means that by |
| 214 | // dividing it by 4, we can fit values from the [0, 128) |
| 215 | // interval in a short stack location, and not just values |
| 216 | // from the [0, 32) interval. |
| 217 | DCHECK_EQ(value % kFrameSlotSize, 0); |
| 218 | value /= kFrameSlotSize; |
| 219 | } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 220 | DCHECK(IsShortValue(value)) << value; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 221 | region_.StoreUnaligned<ShortLocation>(offset, MakeShortLocation(kind, value)); |
| 222 | } else { |
| 223 | // Large location. Write the location on one byte and the value |
| 224 | // on 4 bytes. |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 225 | DCHECK(!IsShortValue(value)) << value; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 226 | if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) { |
| 227 | // Also divide large stack offsets by 4 for the sake of consistency. |
| 228 | DCHECK_EQ(value % kFrameSlotSize, 0); |
| 229 | value /= kFrameSlotSize; |
| 230 | } |
| 231 | // Data can be unaligned as the written Dex register locations can |
| 232 | // either be 1-byte or 5-byte wide. Use |
| 233 | // art::MemoryRegion::StoreUnaligned instead of |
| 234 | // art::MemoryRegion::Store to prevent unligned word accesses on ARM. |
| 235 | region_.StoreUnaligned<DexRegisterLocation::Kind>(offset, kind); |
| 236 | region_.StoreUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind), value); |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 240 | // Find the offset of the location catalog entry number `location_catalog_entry_index`. |
| 241 | size_t FindLocationOffset(size_t location_catalog_entry_index) const { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 242 | size_t offset = kFixedSize; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 243 | // Skip the first `location_catalog_entry_index - 1` entries. |
| 244 | for (uint16_t i = 0; i < location_catalog_entry_index; ++i) { |
| 245 | // Read the first next byte and inspect its first 3 bits to decide |
| 246 | // whether it is a short or a large location. |
| 247 | DexRegisterLocation::Kind kind = ExtractKindAtOffset(offset); |
| 248 | if (DexRegisterLocation::IsShortLocationKind(kind)) { |
| 249 | // Short location. Skip the current byte. |
| 250 | offset += SingleShortEntrySize(); |
| 251 | } else { |
| 252 | // Large location. Skip the 5 next bytes. |
| 253 | offset += SingleLargeEntrySize(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | return offset; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 259 | // Get the internal kind of entry at `location_catalog_entry_index`. |
| 260 | DexRegisterLocation::Kind GetLocationInternalKind(size_t location_catalog_entry_index) const { |
| 261 | if (location_catalog_entry_index == kNoLocationEntryIndex) { |
| 262 | return DexRegisterLocation::Kind::kNone; |
| 263 | } |
| 264 | return ExtractKindAtOffset(FindLocationOffset(location_catalog_entry_index)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 267 | // Get the (surface) kind and value of entry at `location_catalog_entry_index`. |
| 268 | DexRegisterLocation GetDexRegisterLocation(size_t location_catalog_entry_index) const { |
| 269 | if (location_catalog_entry_index == kNoLocationEntryIndex) { |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 270 | return DexRegisterLocation::None(); |
| 271 | } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 272 | size_t offset = FindLocationOffset(location_catalog_entry_index); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 273 | // Read the first byte and inspect its first 3 bits to get the location. |
| 274 | ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset); |
| 275 | DexRegisterLocation::Kind kind = ExtractKindFromShortLocation(first_byte); |
| 276 | if (DexRegisterLocation::IsShortLocationKind(kind)) { |
| 277 | // Short location. Extract the value from the remaining 5 bits. |
| 278 | int32_t value = ExtractValueFromShortLocation(first_byte); |
| 279 | if (kind == DexRegisterLocation::Kind::kInStack) { |
| 280 | // Convert the stack slot (short) offset to a byte offset value. |
| 281 | value *= kFrameSlotSize; |
| 282 | } |
| 283 | return DexRegisterLocation(kind, value); |
| 284 | } else { |
| 285 | // Large location. Read the four next bytes to get the value. |
| 286 | int32_t value = region_.LoadUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind)); |
| 287 | if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) { |
| 288 | // Convert the stack slot (large) offset to a byte offset value. |
| 289 | value *= kFrameSlotSize; |
| 290 | } |
| 291 | return DexRegisterLocation(kind, value); |
| 292 | } |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 295 | // Compute the compressed kind of `location`. |
| 296 | static DexRegisterLocation::Kind ComputeCompressedKind(const DexRegisterLocation& location) { |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 297 | DexRegisterLocation::Kind kind = location.GetInternalKind(); |
| 298 | switch (kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 299 | case DexRegisterLocation::Kind::kInStack: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 300 | return IsShortStackOffsetValue(location.GetValue()) |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 301 | ? DexRegisterLocation::Kind::kInStack |
| 302 | : DexRegisterLocation::Kind::kInStackLargeOffset; |
| 303 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 304 | case DexRegisterLocation::Kind::kInRegister: |
| 305 | case DexRegisterLocation::Kind::kInRegisterHigh: |
| 306 | DCHECK_GE(location.GetValue(), 0); |
| 307 | DCHECK_LT(location.GetValue(), 1 << kValueBits); |
| 308 | return kind; |
| 309 | |
| 310 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 311 | case DexRegisterLocation::Kind::kInFpuRegisterHigh: |
| 312 | DCHECK_GE(location.GetValue(), 0); |
| 313 | DCHECK_LT(location.GetValue(), 1 << kValueBits); |
| 314 | return kind; |
| 315 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 316 | case DexRegisterLocation::Kind::kConstant: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 317 | return IsShortConstantValue(location.GetValue()) |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 318 | ? DexRegisterLocation::Kind::kConstant |
| 319 | : DexRegisterLocation::Kind::kConstantLargeValue; |
| 320 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 321 | case DexRegisterLocation::Kind::kConstantLargeValue: |
| 322 | case DexRegisterLocation::Kind::kInStackLargeOffset: |
| 323 | case DexRegisterLocation::Kind::kNone: |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 324 | LOG(FATAL) << "Unexpected location kind " << kind; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 325 | } |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 326 | UNREACHABLE(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // Can `location` be turned into a short location? |
| 330 | static bool CanBeEncodedAsShortLocation(const DexRegisterLocation& location) { |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 331 | DexRegisterLocation::Kind kind = location.GetInternalKind(); |
| 332 | switch (kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 333 | case DexRegisterLocation::Kind::kInStack: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 334 | return IsShortStackOffsetValue(location.GetValue()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 335 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 336 | case DexRegisterLocation::Kind::kInRegister: |
| 337 | case DexRegisterLocation::Kind::kInRegisterHigh: |
| 338 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 339 | case DexRegisterLocation::Kind::kInFpuRegisterHigh: |
| 340 | return true; |
| 341 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 342 | case DexRegisterLocation::Kind::kConstant: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 343 | return IsShortConstantValue(location.GetValue()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 344 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 345 | case DexRegisterLocation::Kind::kConstantLargeValue: |
| 346 | case DexRegisterLocation::Kind::kInStackLargeOffset: |
| 347 | case DexRegisterLocation::Kind::kNone: |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 348 | LOG(FATAL) << "Unexpected location kind " << kind; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 349 | } |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 350 | UNREACHABLE(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static size_t EntrySize(const DexRegisterLocation& location) { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 354 | return CanBeEncodedAsShortLocation(location) ? SingleShortEntrySize() : SingleLargeEntrySize(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static size_t SingleShortEntrySize() { |
| 358 | return sizeof(ShortLocation); |
| 359 | } |
| 360 | |
| 361 | static size_t SingleLargeEntrySize() { |
| 362 | return sizeof(DexRegisterLocation::Kind) + sizeof(int32_t); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 363 | } |
| 364 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 365 | size_t Size() const { |
| 366 | return region_.size(); |
| 367 | } |
| 368 | |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 369 | void Dump(VariableIndentationOutputStream* vios, |
| 370 | const CodeInfo& code_info); |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 371 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 372 | // Special (invalid) Dex register location catalog entry index meaning |
| 373 | // that there is no location for a given Dex register (i.e., it is |
| 374 | // mapped to a DexRegisterLocation::Kind::kNone location). |
| 375 | static constexpr size_t kNoLocationEntryIndex = -1; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 376 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 377 | private: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 378 | static constexpr int kFixedSize = 0; |
| 379 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 380 | // Width of the kind "field" in a short location, in bits. |
| 381 | static constexpr size_t kKindBits = 3; |
| 382 | // Width of the value "field" in a short location, in bits. |
| 383 | static constexpr size_t kValueBits = 5; |
| 384 | |
| 385 | static constexpr uint8_t kKindMask = (1 << kKindBits) - 1; |
| 386 | static constexpr int32_t kValueMask = (1 << kValueBits) - 1; |
| 387 | static constexpr size_t kKindOffset = 0; |
| 388 | static constexpr size_t kValueOffset = kKindBits; |
| 389 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 390 | static bool IsShortStackOffsetValue(int32_t value) { |
| 391 | DCHECK_EQ(value % kFrameSlotSize, 0); |
| 392 | return IsShortValue(value / kFrameSlotSize); |
| 393 | } |
| 394 | |
| 395 | static bool IsShortConstantValue(int32_t value) { |
| 396 | return IsShortValue(value); |
| 397 | } |
| 398 | |
| 399 | static bool IsShortValue(int32_t value) { |
| 400 | return IsUint<kValueBits>(value); |
| 401 | } |
| 402 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 403 | static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 404 | uint8_t kind_integer_value = static_cast<uint8_t>(kind); |
| 405 | DCHECK(IsUint<kKindBits>(kind_integer_value)) << kind_integer_value; |
| 406 | DCHECK(IsShortValue(value)) << value; |
| 407 | return (kind_integer_value & kKindMask) << kKindOffset |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 408 | | (value & kValueMask) << kValueOffset; |
| 409 | } |
| 410 | |
| 411 | static DexRegisterLocation::Kind ExtractKindFromShortLocation(ShortLocation location) { |
| 412 | uint8_t kind = (location >> kKindOffset) & kKindMask; |
| 413 | DCHECK_LE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kLastLocationKind)); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 414 | // We do not encode kNone locations in the stack map. |
| 415 | DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone)); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 416 | return static_cast<DexRegisterLocation::Kind>(kind); |
| 417 | } |
| 418 | |
| 419 | static int32_t ExtractValueFromShortLocation(ShortLocation location) { |
| 420 | return (location >> kValueOffset) & kValueMask; |
| 421 | } |
| 422 | |
| 423 | // Extract a location kind from the byte at position `offset`. |
| 424 | DexRegisterLocation::Kind ExtractKindAtOffset(size_t offset) const { |
| 425 | ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset); |
| 426 | return ExtractKindFromShortLocation(first_byte); |
| 427 | } |
| 428 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 429 | MemoryRegion region_; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 430 | |
| 431 | friend class CodeInfo; |
| 432 | friend class StackMapStream; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 433 | }; |
| 434 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 435 | /* Information on Dex register locations for a specific PC, mapping a |
| 436 | * stack map's Dex register to a location entry in a DexRegisterLocationCatalog. |
| 437 | * The information is of the form: |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 438 | * |
| 439 | * [live_bit_mask, entries*] |
| 440 | * |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 441 | * where entries are concatenated unsigned integer values encoded on a number |
| 442 | * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending |
| 443 | * on the number of entries in the Dex register location catalog |
| 444 | * (see DexRegisterMap::SingleEntrySizeInBits). The map is 1-byte aligned. |
| 445 | */ |
| 446 | class DexRegisterMap { |
| 447 | public: |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 448 | DexRegisterMap(MemoryRegion region, uint16_t number_of_dex_registers, const CodeInfo& code_info) |
| 449 | : region_(region), |
| 450 | number_of_dex_registers_(number_of_dex_registers), |
| 451 | code_info_(code_info) {} |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 452 | |
David Srbecky | 68fefac | 2018-05-10 17:49:33 +0100 | [diff] [blame] | 453 | bool IsValid() const { return region_.IsValid(); } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 454 | |
| 455 | // Get the surface kind of Dex register `dex_register_number`. |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 456 | DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number) const { |
| 457 | return DexRegisterLocation::ConvertToSurfaceKind(GetLocationInternalKind(dex_register_number)); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | // Get the internal kind of Dex register `dex_register_number`. |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 461 | DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number) const; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 462 | |
| 463 | // Get the Dex register location `dex_register_number`. |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 464 | DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number) const; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 465 | |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 466 | int32_t GetStackOffsetInBytes(uint16_t dex_register_number) const { |
| 467 | DexRegisterLocation location = GetDexRegisterLocation(dex_register_number); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 468 | DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack); |
| 469 | // GetDexRegisterLocation returns the offset in bytes. |
| 470 | return location.GetValue(); |
| 471 | } |
| 472 | |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 473 | int32_t GetConstant(uint16_t dex_register_number) const { |
| 474 | DexRegisterLocation location = GetDexRegisterLocation(dex_register_number); |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 475 | DCHECK_EQ(location.GetKind(), DexRegisterLocation::Kind::kConstant); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 476 | return location.GetValue(); |
| 477 | } |
| 478 | |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 479 | int32_t GetMachineRegister(uint16_t dex_register_number) const { |
| 480 | DexRegisterLocation location = GetDexRegisterLocation(dex_register_number); |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 481 | DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister || |
| 482 | location.GetInternalKind() == DexRegisterLocation::Kind::kInRegisterHigh || |
| 483 | location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister || |
| 484 | location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh) |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 485 | << location.GetInternalKind(); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 486 | return location.GetValue(); |
| 487 | } |
| 488 | |
| 489 | // Get the index of the entry in the Dex register location catalog |
| 490 | // corresponding to `dex_register_number`. |
| 491 | size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number, |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 492 | size_t number_of_location_catalog_entries) const { |
| 493 | if (!IsDexRegisterLive(dex_register_number)) { |
| 494 | return DexRegisterLocationCatalog::kNoLocationEntryIndex; |
| 495 | } |
| 496 | |
| 497 | if (number_of_location_catalog_entries == 1) { |
| 498 | // We do not allocate space for location maps in the case of a |
| 499 | // single-entry location catalog, as it is useless. The only valid |
| 500 | // entry index is 0; |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | // The bit offset of the beginning of the map locations. |
| 505 | size_t map_locations_offset_in_bits = |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 506 | GetLocationMappingDataOffset(number_of_dex_registers_) * kBitsPerByte; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 507 | size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number); |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 508 | DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters()); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 509 | // The bit size of an entry. |
| 510 | size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries); |
| 511 | // The bit offset where `index_in_dex_register_map` is located. |
| 512 | size_t entry_offset_in_bits = |
| 513 | map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits; |
| 514 | size_t location_catalog_entry_index = |
| 515 | region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits); |
| 516 | DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries); |
| 517 | return location_catalog_entry_index; |
| 518 | } |
| 519 | |
| 520 | // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`. |
| 521 | void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map, |
| 522 | size_t location_catalog_entry_index, |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 523 | size_t number_of_location_catalog_entries) { |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 524 | DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters()); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 525 | DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries); |
| 526 | |
| 527 | if (number_of_location_catalog_entries == 1) { |
| 528 | // We do not allocate space for location maps in the case of a |
| 529 | // single-entry location catalog, as it is useless. |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | // The bit offset of the beginning of the map locations. |
| 534 | size_t map_locations_offset_in_bits = |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 535 | GetLocationMappingDataOffset(number_of_dex_registers_) * kBitsPerByte; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 536 | // The bit size of an entry. |
| 537 | size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries); |
| 538 | // The bit offset where `index_in_dex_register_map` is located. |
| 539 | size_t entry_offset_in_bits = |
| 540 | map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits; |
| 541 | region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits); |
| 542 | } |
| 543 | |
| 544 | void SetLiveBitMask(uint16_t number_of_dex_registers, |
| 545 | const BitVector& live_dex_registers_mask) { |
| 546 | size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte; |
| 547 | for (uint16_t i = 0; i < number_of_dex_registers; ++i) { |
| 548 | region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i)); |
| 549 | } |
| 550 | } |
| 551 | |
Mingyao Yang | 01b47b0 | 2017-02-03 12:09:57 -0800 | [diff] [blame] | 552 | ALWAYS_INLINE bool IsDexRegisterLive(uint16_t dex_register_number) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 553 | size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte; |
| 554 | return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number); |
| 555 | } |
| 556 | |
| 557 | size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const { |
| 558 | size_t number_of_live_dex_registers = 0; |
| 559 | for (size_t i = 0; i < number_of_dex_registers; ++i) { |
| 560 | if (IsDexRegisterLive(i)) { |
| 561 | ++number_of_live_dex_registers; |
| 562 | } |
| 563 | } |
| 564 | return number_of_live_dex_registers; |
| 565 | } |
| 566 | |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 567 | size_t GetNumberOfLiveDexRegisters() const { |
| 568 | return GetNumberOfLiveDexRegisters(number_of_dex_registers_); |
| 569 | } |
| 570 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 571 | static size_t GetLiveBitMaskOffset() { |
| 572 | return kFixedSize; |
| 573 | } |
| 574 | |
| 575 | // Compute the size of the live register bit mask (in bytes), for a |
| 576 | // method having `number_of_dex_registers` Dex registers. |
| 577 | static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) { |
| 578 | return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte; |
| 579 | } |
| 580 | |
| 581 | static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) { |
| 582 | return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers); |
| 583 | } |
| 584 | |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 585 | size_t GetLocationMappingDataSize(size_t number_of_location_catalog_entries) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 586 | size_t location_mapping_data_size_in_bits = |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 587 | GetNumberOfLiveDexRegisters() |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 588 | * SingleEntrySizeInBits(number_of_location_catalog_entries); |
| 589 | return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte; |
| 590 | } |
| 591 | |
| 592 | // Return the size of a map entry in bits. Note that if |
| 593 | // `number_of_location_catalog_entries` equals 1, this function returns 0, |
| 594 | // which is fine, as there is no need to allocate a map for a |
| 595 | // single-entry location catalog; the only valid location catalog entry index |
| 596 | // for a live register in this case is 0 and there is no need to |
| 597 | // store it. |
| 598 | static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) { |
| 599 | // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2. |
| 600 | return number_of_location_catalog_entries == 0 |
| 601 | ? 0u |
| 602 | : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries)); |
| 603 | } |
| 604 | |
| 605 | // Return the size of the DexRegisterMap object, in bytes. |
| 606 | size_t Size() const { |
David Srbecky | 68fefac | 2018-05-10 17:49:33 +0100 | [diff] [blame] | 607 | return BitsToBytesRoundUp(region_.size_in_bits()); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 608 | } |
| 609 | |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 610 | void Dump(VariableIndentationOutputStream* vios) const; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 611 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 612 | private: |
| 613 | // Return the index in the Dex register map corresponding to the Dex |
| 614 | // register number `dex_register_number`. |
| 615 | size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const { |
| 616 | if (!IsDexRegisterLive(dex_register_number)) { |
| 617 | return kInvalidIndexInDexRegisterMap; |
| 618 | } |
| 619 | return GetNumberOfLiveDexRegisters(dex_register_number); |
| 620 | } |
| 621 | |
| 622 | // Special (invalid) Dex register map entry index meaning that there |
| 623 | // is no index in the map for a given Dex register (i.e., it must |
| 624 | // have been mapped to a DexRegisterLocation::Kind::kNone location). |
| 625 | static constexpr size_t kInvalidIndexInDexRegisterMap = -1; |
| 626 | |
| 627 | static constexpr int kFixedSize = 0; |
| 628 | |
David Srbecky | 68fefac | 2018-05-10 17:49:33 +0100 | [diff] [blame] | 629 | BitMemoryRegion region_; |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 630 | uint16_t number_of_dex_registers_; |
| 631 | const CodeInfo& code_info_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 632 | |
| 633 | friend class CodeInfo; |
| 634 | friend class StackMapStream; |
| 635 | }; |
| 636 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 637 | /** |
| 638 | * A Stack Map holds compilation information for a specific PC necessary for: |
| 639 | * - Mapping it to a dex PC, |
| 640 | * - Knowing which stack entries are objects, |
| 641 | * - Knowing which registers hold objects, |
| 642 | * - Knowing the inlining information, |
| 643 | * - Knowing the values of dex registers. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 644 | */ |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 645 | class StackMap : public BitTable<6>::Accessor { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 646 | public: |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 647 | enum Field { |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame] | 648 | kPackedNativePc, |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 649 | kDexPc, |
| 650 | kDexRegisterMapOffset, |
| 651 | kInlineInfoIndex, |
| 652 | kRegisterMaskIndex, |
| 653 | kStackMaskIndex, |
| 654 | kCount, |
| 655 | }; |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 656 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 657 | StackMap() : BitTable<kCount>::Accessor(nullptr, -1) {} |
| 658 | StackMap(const BitTable<kCount>* table, uint32_t row) |
| 659 | : BitTable<kCount>::Accessor(table, row) {} |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 660 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 661 | ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const { |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame] | 662 | return UnpackNativePc(Get<kPackedNativePc>(), instruction_set); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 663 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 664 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 665 | uint32_t GetDexPc() const { return Get<kDexPc>(); } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 666 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 667 | uint32_t GetDexRegisterMapOffset() const { return Get<kDexRegisterMapOffset>(); } |
| 668 | bool HasDexRegisterMap() const { return GetDexRegisterMapOffset() != kNoValue; } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 669 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 670 | uint32_t GetInlineInfoIndex() const { return Get<kInlineInfoIndex>(); } |
| 671 | bool HasInlineInfo() const { return GetInlineInfoIndex() != kNoValue; } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 672 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 673 | uint32_t GetRegisterMaskIndex() const { return Get<kRegisterMaskIndex>(); } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 674 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 675 | uint32_t GetStackMaskIndex() const { return Get<kStackMaskIndex>(); } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 676 | |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame] | 677 | static uint32_t PackNativePc(uint32_t native_pc, InstructionSet isa) { |
David Srbecky | d775f96 | 2018-05-30 18:12:52 +0100 | [diff] [blame^] | 678 | DCHECK_ALIGNED_PARAM(native_pc, GetInstructionSetInstructionAlignment(isa)); |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame] | 679 | return native_pc / GetInstructionSetInstructionAlignment(isa); |
| 680 | } |
| 681 | |
| 682 | static uint32_t UnpackNativePc(uint32_t packed_native_pc, InstructionSet isa) { |
| 683 | uint32_t native_pc = packed_native_pc * GetInstructionSetInstructionAlignment(isa); |
| 684 | DCHECK_EQ(native_pc / GetInstructionSetInstructionAlignment(isa), packed_native_pc); |
| 685 | return native_pc; |
| 686 | } |
| 687 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 688 | static void DumpEncoding(const BitTable<6>& table, VariableIndentationOutputStream* vios); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 689 | void Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 690 | const CodeInfo& code_info, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 691 | const MethodInfo& method_info, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 692 | uint32_t code_offset, |
| 693 | uint16_t number_of_dex_registers, |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 694 | InstructionSet instruction_set, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 695 | const std::string& header_suffix = "") const; |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 696 | }; |
| 697 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 698 | /** |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 699 | * Inline information for a specific PC. |
| 700 | * The row referenced from the StackMap holds information at depth 0. |
| 701 | * Following rows hold information for further depths. |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 702 | */ |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 703 | class InlineInfo : public BitTable<5>::Accessor { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 704 | public: |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 705 | enum Field { |
| 706 | kIsLast, // Determines if there are further rows for further depths. |
| 707 | kMethodIndexIdx, // Method index or ArtMethod high bits. |
| 708 | kDexPc, |
| 709 | kExtraData, // ArtMethod low bits or 1. |
| 710 | kDexRegisterMapOffset, |
| 711 | kCount, |
| 712 | }; |
| 713 | static constexpr uint32_t kLast = -1; |
| 714 | static constexpr uint32_t kMore = 0; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 715 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 716 | InlineInfo(const BitTable<kCount>* table, uint32_t row) |
| 717 | : BitTable<kCount>::Accessor(table, row) {} |
| 718 | |
| 719 | ALWAYS_INLINE InlineInfo AtDepth(uint32_t depth) const { |
| 720 | return InlineInfo(table_, this->row_ + depth); |
| 721 | } |
| 722 | |
| 723 | uint32_t GetDepth() const { |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 724 | size_t depth = 0; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 725 | while (AtDepth(depth++).Get<kIsLast>() == kMore) { } |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 726 | return depth; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 727 | } |
| 728 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 729 | uint32_t GetMethodIndexIdxAtDepth(uint32_t depth) const { |
| 730 | DCHECK(!EncodesArtMethodAtDepth(depth)); |
| 731 | return AtDepth(depth).Get<kMethodIndexIdx>(); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 732 | } |
| 733 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 734 | uint32_t GetMethodIndexAtDepth(const MethodInfo& method_info, uint32_t depth) const { |
| 735 | return method_info.GetMethodIndex(GetMethodIndexIdxAtDepth(depth)); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 736 | } |
| 737 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 738 | uint32_t GetDexPcAtDepth(uint32_t depth) const { |
| 739 | return AtDepth(depth).Get<kDexPc>(); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 740 | } |
| 741 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 742 | bool EncodesArtMethodAtDepth(uint32_t depth) const { |
| 743 | return (AtDepth(depth).Get<kExtraData>() & 1) == 0; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 744 | } |
| 745 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 746 | ArtMethod* GetArtMethodAtDepth(uint32_t depth) const { |
| 747 | uint32_t low_bits = AtDepth(depth).Get<kExtraData>(); |
| 748 | uint32_t high_bits = AtDepth(depth).Get<kMethodIndexIdx>(); |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 749 | if (high_bits == 0) { |
| 750 | return reinterpret_cast<ArtMethod*>(low_bits); |
| 751 | } else { |
| 752 | uint64_t address = high_bits; |
| 753 | address = address << 32; |
| 754 | return reinterpret_cast<ArtMethod*>(address | low_bits); |
| 755 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 756 | } |
| 757 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 758 | uint32_t GetDexRegisterMapOffsetAtDepth(uint32_t depth) const { |
| 759 | return AtDepth(depth).Get<kDexRegisterMapOffset>(); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 760 | } |
| 761 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 762 | bool HasDexRegisterMapAtDepth(uint32_t depth) const { |
| 763 | return GetDexRegisterMapOffsetAtDepth(depth) != StackMap::kNoValue; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 764 | } |
| 765 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 766 | static void DumpEncoding(const BitTable<5>& table, VariableIndentationOutputStream* vios); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 767 | void Dump(VariableIndentationOutputStream* vios, |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 768 | const CodeInfo& info, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 769 | const MethodInfo& method_info, |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 770 | uint16_t* number_of_dex_registers) const; |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 771 | }; |
| 772 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 773 | class InvokeInfo : public BitTable<3>::Accessor { |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 774 | public: |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 775 | enum Field { |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame] | 776 | kPackedNativePc, |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 777 | kInvokeType, |
| 778 | kMethodIndexIdx, |
| 779 | kCount, |
| 780 | }; |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 781 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 782 | InvokeInfo(const BitTable<kCount>* table, uint32_t row) |
| 783 | : BitTable<kCount>::Accessor(table, row) {} |
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 | ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const { |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame] | 786 | return StackMap::UnpackNativePc(Get<kPackedNativePc>(), instruction_set); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 787 | } |
| 788 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 789 | uint32_t GetInvokeType() const { return Get<kInvokeType>(); } |
| 790 | |
| 791 | uint32_t GetMethodIndexIdx() const { return Get<kMethodIndexIdx>(); } |
| 792 | |
| 793 | uint32_t GetMethodIndex(MethodInfo method_info) const { |
| 794 | return method_info.GetMethodIndex(GetMethodIndexIdx()); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 795 | } |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 796 | }; |
| 797 | |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 798 | // Register masks tend to have many trailing zero bits (caller-saves are usually not encoded), |
| 799 | // therefore it is worth encoding the mask as value+shift. |
| 800 | class RegisterMask : public BitTable<2>::Accessor { |
| 801 | public: |
| 802 | enum Field { |
| 803 | kValue, |
| 804 | kShift, |
| 805 | kCount, |
| 806 | }; |
| 807 | |
| 808 | RegisterMask(const BitTable<kCount>* table, uint32_t row) |
| 809 | : BitTable<kCount>::Accessor(table, row) {} |
| 810 | |
| 811 | ALWAYS_INLINE uint32_t GetMask() const { |
| 812 | return Get<kValue>() << Get<kShift>(); |
| 813 | } |
| 814 | }; |
| 815 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 816 | /** |
| 817 | * Wrapper around all compiler information collected for a method. |
| 818 | * The information is of the form: |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 819 | * |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 820 | * [BitTable<Header>, BitTable<StackMap>, BitTable<RegisterMask>, BitTable<InlineInfo>, |
| 821 | * BitTable<InvokeInfo>, BitTable<StackMask>, DexRegisterMap, DexLocationCatalog] |
Mathieu Chartier | c420a80 | 2017-02-14 15:16:19 -0800 | [diff] [blame] | 822 | * |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 823 | */ |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 824 | class CodeInfo { |
| 825 | public: |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 826 | explicit CodeInfo(const void* data) { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 827 | Decode(reinterpret_cast<const uint8_t*>(data)); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 828 | } |
| 829 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 830 | explicit CodeInfo(MemoryRegion region) : CodeInfo(region.begin()) { |
| 831 | DCHECK_EQ(size_, region.size()); |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 832 | } |
| 833 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 834 | explicit CodeInfo(const OatQuickMethodHeader* header) |
| 835 | : CodeInfo(header->GetOptimizedCodeInfoPtr()) { |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 836 | } |
| 837 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 838 | size_t Size() const { |
| 839 | return size_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 840 | } |
| 841 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 842 | bool HasInlineInfo() const { |
| 843 | return stack_maps_.NumColumnBits(StackMap::kInlineInfoIndex) != 0; |
Mathieu Chartier | 12f1b99 | 2017-01-19 18:00:45 -0800 | [diff] [blame] | 844 | } |
| 845 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 846 | DexRegisterLocationCatalog GetDexRegisterLocationCatalog() const { |
| 847 | return DexRegisterLocationCatalog(location_catalog_); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 848 | } |
| 849 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 850 | ALWAYS_INLINE StackMap GetStackMapAt(size_t index) const { |
| 851 | return StackMap(&stack_maps_, index); |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 852 | } |
| 853 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 854 | BitMemoryRegion GetStackMask(size_t index) const { |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 855 | return stack_masks_.GetBitMemoryRegion(index); |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 856 | } |
| 857 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 858 | BitMemoryRegion GetStackMaskOf(const StackMap& stack_map) const { |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 859 | uint32_t index = stack_map.GetStackMaskIndex(); |
| 860 | return (index == StackMap::kNoValue) ? BitMemoryRegion() : GetStackMask(index); |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 861 | } |
| 862 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 863 | uint32_t GetRegisterMaskOf(const StackMap& stack_map) const { |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 864 | uint32_t index = stack_map.GetRegisterMaskIndex(); |
| 865 | return (index == StackMap::kNoValue) ? 0 : RegisterMask(®ister_masks_, index).GetMask(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 866 | } |
| 867 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 868 | uint32_t GetNumberOfLocationCatalogEntries() const { |
| 869 | return location_catalog_entries_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 870 | } |
| 871 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 872 | uint32_t GetDexRegisterLocationCatalogSize() const { |
| 873 | return location_catalog_.size(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 874 | } |
| 875 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 876 | uint32_t GetNumberOfStackMaps() const { |
| 877 | return stack_maps_.NumRows(); |
Nicolas Geoffray | 6530baf | 2015-05-26 15:22:58 +0100 | [diff] [blame] | 878 | } |
| 879 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 880 | InvokeInfo GetInvokeInfo(size_t index) const { |
| 881 | return InvokeInfo(&invoke_infos_, index); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 882 | } |
| 883 | |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 884 | DexRegisterMap GetDexRegisterMapOf(StackMap stack_map, |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 885 | size_t number_of_dex_registers) const { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 886 | if (!stack_map.HasDexRegisterMap()) { |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 887 | return DexRegisterMap(MemoryRegion(), 0, *this); |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 888 | } |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 889 | const uint32_t offset = stack_map.GetDexRegisterMapOffset(); |
| 890 | size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers); |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 891 | return DexRegisterMap(dex_register_maps_.Subregion(offset, size), |
| 892 | number_of_dex_registers, |
| 893 | *this); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 894 | } |
| 895 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 896 | size_t GetDexRegisterMapsSize(uint32_t number_of_dex_registers) const { |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 897 | size_t total = 0; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 898 | for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) { |
| 899 | StackMap stack_map = GetStackMapAt(i); |
| 900 | DexRegisterMap map(GetDexRegisterMapOf(stack_map, number_of_dex_registers)); |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 901 | total += map.Size(); |
| 902 | } |
| 903 | return total; |
| 904 | } |
| 905 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 906 | // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`. |
| 907 | DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth, |
| 908 | InlineInfo inline_info, |
| 909 | uint32_t number_of_dex_registers) const { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 910 | if (!inline_info.HasDexRegisterMapAtDepth(depth)) { |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 911 | return DexRegisterMap(MemoryRegion(), 0, *this); |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 912 | } else { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 913 | uint32_t offset = inline_info.GetDexRegisterMapOffsetAtDepth(depth); |
| 914 | size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers); |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 915 | return DexRegisterMap(dex_register_maps_.Subregion(offset, size), |
| 916 | number_of_dex_registers, |
| 917 | *this); |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 918 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 919 | } |
| 920 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 921 | InlineInfo GetInlineInfo(size_t index) const { |
| 922 | return InlineInfo(&inline_infos_, index); |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 923 | } |
| 924 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 925 | InlineInfo GetInlineInfoOf(StackMap stack_map) const { |
| 926 | DCHECK(stack_map.HasInlineInfo()); |
| 927 | uint32_t index = stack_map.GetInlineInfoIndex(); |
| 928 | return GetInlineInfo(index); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 929 | } |
| 930 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 931 | StackMap GetStackMapForDexPc(uint32_t dex_pc) const { |
| 932 | for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) { |
| 933 | StackMap stack_map = GetStackMapAt(i); |
| 934 | if (stack_map.GetDexPc() == dex_pc) { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 935 | return stack_map; |
| 936 | } |
| 937 | } |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 938 | return StackMap(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 939 | } |
| 940 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 941 | // Searches the stack map list backwards because catch stack maps are stored |
| 942 | // at the end. |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 943 | StackMap GetCatchStackMapForDexPc(uint32_t dex_pc) const { |
| 944 | for (size_t i = GetNumberOfStackMaps(); i > 0; --i) { |
| 945 | StackMap stack_map = GetStackMapAt(i - 1); |
| 946 | if (stack_map.GetDexPc() == dex_pc) { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 947 | return stack_map; |
| 948 | } |
| 949 | } |
| 950 | return StackMap(); |
| 951 | } |
| 952 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 953 | StackMap GetOsrStackMapForDexPc(uint32_t dex_pc) const { |
| 954 | size_t e = GetNumberOfStackMaps(); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 955 | if (e == 0) { |
| 956 | // There cannot be OSR stack map if there is no stack map. |
| 957 | return StackMap(); |
| 958 | } |
| 959 | // Walk over all stack maps. If two consecutive stack maps are identical, then we |
| 960 | // have found a stack map suitable for OSR. |
| 961 | for (size_t i = 0; i < e - 1; ++i) { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 962 | StackMap stack_map = GetStackMapAt(i); |
| 963 | if (stack_map.GetDexPc() == dex_pc) { |
| 964 | StackMap other = GetStackMapAt(i + 1); |
| 965 | if (other.GetDexPc() == dex_pc && |
| 966 | other.GetNativePcOffset(kRuntimeISA) == |
| 967 | stack_map.GetNativePcOffset(kRuntimeISA)) { |
| 968 | DCHECK_EQ(other.GetDexRegisterMapOffset(), |
| 969 | stack_map.GetDexRegisterMapOffset()); |
| 970 | DCHECK(!stack_map.HasInlineInfo()); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 971 | if (i < e - 2) { |
| 972 | // Make sure there are not three identical stack maps following each other. |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 973 | DCHECK_NE( |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 974 | stack_map.GetNativePcOffset(kRuntimeISA), |
| 975 | GetStackMapAt(i + 2).GetNativePcOffset(kRuntimeISA)); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 976 | } |
| 977 | return stack_map; |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | return StackMap(); |
| 982 | } |
| 983 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 984 | StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 985 | // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack |
| 986 | // maps are not. If we knew that the method does not have try/catch, |
| 987 | // we could do binary search. |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 988 | for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) { |
| 989 | StackMap stack_map = GetStackMapAt(i); |
| 990 | if (stack_map.GetNativePcOffset(kRuntimeISA) == native_pc_offset) { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 991 | return stack_map; |
| 992 | } |
| 993 | } |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 994 | return StackMap(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 995 | } |
| 996 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 997 | InvokeInfo GetInvokeInfoForNativePcOffset(uint32_t native_pc_offset) { |
| 998 | for (size_t index = 0; index < invoke_infos_.NumRows(); index++) { |
| 999 | InvokeInfo item = GetInvokeInfo(index); |
| 1000 | if (item.GetNativePcOffset(kRuntimeISA) == native_pc_offset) { |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 1001 | return item; |
| 1002 | } |
| 1003 | } |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1004 | return InvokeInfo(&invoke_infos_, -1); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1007 | // Dump this CodeInfo object on `os`. `code_offset` is the (absolute) |
| 1008 | // native PC of the compiled method and `number_of_dex_registers` the |
| 1009 | // number of Dex virtual registers used in this method. If |
| 1010 | // `dump_stack_maps` is true, also dump the stack maps and the |
| 1011 | // associated Dex register maps. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1012 | void Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1013 | uint32_t code_offset, |
| 1014 | uint16_t number_of_dex_registers, |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1015 | bool dump_stack_maps, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1016 | InstructionSet instruction_set, |
| 1017 | const MethodInfo& method_info) const; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 1018 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1019 | private: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1020 | // Compute the size of the Dex register map associated to the stack map at |
| 1021 | // `dex_register_map_offset_in_code_info`. |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1022 | size_t ComputeDexRegisterMapSizeOf(uint32_t dex_register_map_offset, |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1023 | uint16_t number_of_dex_registers) const { |
| 1024 | // Offset where the actual mapping data starts within art::DexRegisterMap. |
| 1025 | size_t location_mapping_data_offset_in_dex_register_map = |
| 1026 | DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers); |
| 1027 | // Create a temporary art::DexRegisterMap to be able to call |
| 1028 | // art::DexRegisterMap::GetNumberOfLiveDexRegisters and |
| 1029 | DexRegisterMap dex_register_map_without_locations( |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1030 | MemoryRegion(dex_register_maps_.Subregion(dex_register_map_offset, |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 1031 | location_mapping_data_offset_in_dex_register_map)), |
| 1032 | number_of_dex_registers, |
| 1033 | *this); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1034 | size_t number_of_live_dex_registers = |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 1035 | dex_register_map_without_locations.GetNumberOfLiveDexRegisters(); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1036 | size_t location_mapping_data_size_in_bits = |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1037 | DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries()) |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1038 | * number_of_live_dex_registers; |
| 1039 | size_t location_mapping_data_size_in_bytes = |
| 1040 | RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte; |
| 1041 | size_t dex_register_map_size = |
| 1042 | location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes; |
| 1043 | return dex_register_map_size; |
| 1044 | } |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 1045 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1046 | MemoryRegion DecodeMemoryRegion(MemoryRegion& region, size_t* bit_offset) { |
| 1047 | size_t length = DecodeVarintBits(BitMemoryRegion(region), bit_offset); |
| 1048 | size_t offset = BitsToBytesRoundUp(*bit_offset);; |
| 1049 | *bit_offset = (offset + length) * kBitsPerByte; |
| 1050 | return region.Subregion(offset, length); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1053 | void Decode(const uint8_t* data) { |
| 1054 | size_t non_header_size = DecodeUnsignedLeb128(&data); |
| 1055 | MemoryRegion region(const_cast<uint8_t*>(data), non_header_size); |
| 1056 | BitMemoryRegion bit_region(region); |
| 1057 | size_t bit_offset = 0; |
| 1058 | size_ = UnsignedLeb128Size(non_header_size) + non_header_size; |
| 1059 | dex_register_maps_ = DecodeMemoryRegion(region, &bit_offset); |
| 1060 | location_catalog_entries_ = DecodeVarintBits(bit_region, &bit_offset); |
| 1061 | location_catalog_ = DecodeMemoryRegion(region, &bit_offset); |
| 1062 | stack_maps_.Decode(bit_region, &bit_offset); |
| 1063 | invoke_infos_.Decode(bit_region, &bit_offset); |
| 1064 | inline_infos_.Decode(bit_region, &bit_offset); |
| 1065 | register_masks_.Decode(bit_region, &bit_offset); |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 1066 | stack_masks_.Decode(bit_region, &bit_offset); |
| 1067 | CHECK_EQ(BitsToBytesRoundUp(bit_offset), non_header_size); |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | size_t size_; |
| 1071 | MemoryRegion dex_register_maps_; |
| 1072 | uint32_t location_catalog_entries_; |
| 1073 | MemoryRegion location_catalog_; |
| 1074 | BitTable<StackMap::Field::kCount> stack_maps_; |
| 1075 | BitTable<InvokeInfo::Field::kCount> invoke_infos_; |
| 1076 | BitTable<InlineInfo::Field::kCount> inline_infos_; |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 1077 | BitTable<RegisterMask::Field::kCount> register_masks_; |
| 1078 | BitTable<1> stack_masks_; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1079 | |
| 1080 | friend class OatDumper; |
David Srbecky | 21d45b4 | 2018-05-30 06:35:05 +0100 | [diff] [blame] | 1081 | friend class StackMapStream; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1082 | }; |
| 1083 | |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 1084 | #undef ELEMENT_BYTE_OFFSET_AFTER |
| 1085 | #undef ELEMENT_BIT_OFFSET_AFTER |
| 1086 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1087 | } // namespace art |
| 1088 | |
| 1089 | #endif // ART_RUNTIME_STACK_MAP_H_ |