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: |
| 448 | explicit DexRegisterMap(MemoryRegion region) : region_(region) {} |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 449 | DexRegisterMap() {} |
| 450 | |
David Srbecky | 68fefac | 2018-05-10 17:49:33 +0100 | [diff] [blame] | 451 | bool IsValid() const { return region_.IsValid(); } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 452 | |
| 453 | // Get the surface kind of Dex register `dex_register_number`. |
| 454 | DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number, |
| 455 | uint16_t number_of_dex_registers, |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 456 | const CodeInfo& code_info) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 457 | return DexRegisterLocation::ConvertToSurfaceKind( |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 458 | GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info)); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // Get the internal kind of Dex register `dex_register_number`. |
| 462 | DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number, |
| 463 | uint16_t number_of_dex_registers, |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 464 | const CodeInfo& code_info) const; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 465 | |
| 466 | // Get the Dex register location `dex_register_number`. |
| 467 | DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number, |
| 468 | uint16_t number_of_dex_registers, |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 469 | const CodeInfo& code_info) const; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 470 | |
| 471 | int32_t GetStackOffsetInBytes(uint16_t dex_register_number, |
| 472 | uint16_t number_of_dex_registers, |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 473 | const CodeInfo& code_info) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 474 | DexRegisterLocation location = |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 475 | GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 476 | DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack); |
| 477 | // GetDexRegisterLocation returns the offset in bytes. |
| 478 | return location.GetValue(); |
| 479 | } |
| 480 | |
| 481 | int32_t GetConstant(uint16_t dex_register_number, |
| 482 | uint16_t number_of_dex_registers, |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 483 | const CodeInfo& code_info) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 484 | DexRegisterLocation location = |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 485 | GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info); |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 486 | DCHECK_EQ(location.GetKind(), DexRegisterLocation::Kind::kConstant); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 487 | return location.GetValue(); |
| 488 | } |
| 489 | |
| 490 | int32_t GetMachineRegister(uint16_t dex_register_number, |
| 491 | uint16_t number_of_dex_registers, |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 492 | const CodeInfo& code_info) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 493 | DexRegisterLocation location = |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 494 | GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info); |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 495 | DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister || |
| 496 | location.GetInternalKind() == DexRegisterLocation::Kind::kInRegisterHigh || |
| 497 | location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister || |
| 498 | location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh) |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 499 | << location.GetInternalKind(); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 500 | return location.GetValue(); |
| 501 | } |
| 502 | |
| 503 | // Get the index of the entry in the Dex register location catalog |
| 504 | // corresponding to `dex_register_number`. |
| 505 | size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number, |
| 506 | uint16_t number_of_dex_registers, |
| 507 | size_t number_of_location_catalog_entries) const { |
| 508 | if (!IsDexRegisterLive(dex_register_number)) { |
| 509 | return DexRegisterLocationCatalog::kNoLocationEntryIndex; |
| 510 | } |
| 511 | |
| 512 | if (number_of_location_catalog_entries == 1) { |
| 513 | // We do not allocate space for location maps in the case of a |
| 514 | // single-entry location catalog, as it is useless. The only valid |
| 515 | // entry index is 0; |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | // The bit offset of the beginning of the map locations. |
| 520 | size_t map_locations_offset_in_bits = |
| 521 | GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte; |
| 522 | size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number); |
| 523 | DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers)); |
| 524 | // The bit size of an entry. |
| 525 | size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries); |
| 526 | // The bit offset where `index_in_dex_register_map` is located. |
| 527 | size_t entry_offset_in_bits = |
| 528 | map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits; |
| 529 | size_t location_catalog_entry_index = |
| 530 | region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits); |
| 531 | DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries); |
| 532 | return location_catalog_entry_index; |
| 533 | } |
| 534 | |
| 535 | // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`. |
| 536 | void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map, |
| 537 | size_t location_catalog_entry_index, |
| 538 | uint16_t number_of_dex_registers, |
| 539 | size_t number_of_location_catalog_entries) { |
| 540 | DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers)); |
| 541 | DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries); |
| 542 | |
| 543 | if (number_of_location_catalog_entries == 1) { |
| 544 | // We do not allocate space for location maps in the case of a |
| 545 | // single-entry location catalog, as it is useless. |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | // The bit offset of the beginning of the map locations. |
| 550 | size_t map_locations_offset_in_bits = |
| 551 | GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte; |
| 552 | // The bit size of an entry. |
| 553 | size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries); |
| 554 | // The bit offset where `index_in_dex_register_map` is located. |
| 555 | size_t entry_offset_in_bits = |
| 556 | map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits; |
| 557 | region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits); |
| 558 | } |
| 559 | |
| 560 | void SetLiveBitMask(uint16_t number_of_dex_registers, |
| 561 | const BitVector& live_dex_registers_mask) { |
| 562 | size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte; |
| 563 | for (uint16_t i = 0; i < number_of_dex_registers; ++i) { |
| 564 | region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i)); |
| 565 | } |
| 566 | } |
| 567 | |
Mingyao Yang | 01b47b0 | 2017-02-03 12:09:57 -0800 | [diff] [blame] | 568 | ALWAYS_INLINE bool IsDexRegisterLive(uint16_t dex_register_number) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 569 | size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte; |
| 570 | return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number); |
| 571 | } |
| 572 | |
| 573 | size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const { |
| 574 | size_t number_of_live_dex_registers = 0; |
| 575 | for (size_t i = 0; i < number_of_dex_registers; ++i) { |
| 576 | if (IsDexRegisterLive(i)) { |
| 577 | ++number_of_live_dex_registers; |
| 578 | } |
| 579 | } |
| 580 | return number_of_live_dex_registers; |
| 581 | } |
| 582 | |
| 583 | static size_t GetLiveBitMaskOffset() { |
| 584 | return kFixedSize; |
| 585 | } |
| 586 | |
| 587 | // Compute the size of the live register bit mask (in bytes), for a |
| 588 | // method having `number_of_dex_registers` Dex registers. |
| 589 | static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) { |
| 590 | return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte; |
| 591 | } |
| 592 | |
| 593 | static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) { |
| 594 | return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers); |
| 595 | } |
| 596 | |
| 597 | size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers, |
| 598 | size_t number_of_location_catalog_entries) const { |
| 599 | size_t location_mapping_data_size_in_bits = |
| 600 | GetNumberOfLiveDexRegisters(number_of_dex_registers) |
| 601 | * SingleEntrySizeInBits(number_of_location_catalog_entries); |
| 602 | return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte; |
| 603 | } |
| 604 | |
| 605 | // Return the size of a map entry in bits. Note that if |
| 606 | // `number_of_location_catalog_entries` equals 1, this function returns 0, |
| 607 | // which is fine, as there is no need to allocate a map for a |
| 608 | // single-entry location catalog; the only valid location catalog entry index |
| 609 | // for a live register in this case is 0 and there is no need to |
| 610 | // store it. |
| 611 | static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) { |
| 612 | // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2. |
| 613 | return number_of_location_catalog_entries == 0 |
| 614 | ? 0u |
| 615 | : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries)); |
| 616 | } |
| 617 | |
| 618 | // Return the size of the DexRegisterMap object, in bytes. |
| 619 | size_t Size() const { |
David Srbecky | 68fefac | 2018-05-10 17:49:33 +0100 | [diff] [blame] | 620 | return BitsToBytesRoundUp(region_.size_in_bits()); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 623 | void Dump(VariableIndentationOutputStream* vios, |
| 624 | const CodeInfo& code_info, uint16_t number_of_dex_registers) const; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 625 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 626 | private: |
| 627 | // Return the index in the Dex register map corresponding to the Dex |
| 628 | // register number `dex_register_number`. |
| 629 | size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const { |
| 630 | if (!IsDexRegisterLive(dex_register_number)) { |
| 631 | return kInvalidIndexInDexRegisterMap; |
| 632 | } |
| 633 | return GetNumberOfLiveDexRegisters(dex_register_number); |
| 634 | } |
| 635 | |
| 636 | // Special (invalid) Dex register map entry index meaning that there |
| 637 | // is no index in the map for a given Dex register (i.e., it must |
| 638 | // have been mapped to a DexRegisterLocation::Kind::kNone location). |
| 639 | static constexpr size_t kInvalidIndexInDexRegisterMap = -1; |
| 640 | |
| 641 | static constexpr int kFixedSize = 0; |
| 642 | |
David Srbecky | 68fefac | 2018-05-10 17:49:33 +0100 | [diff] [blame] | 643 | BitMemoryRegion region_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 644 | |
| 645 | friend class CodeInfo; |
| 646 | friend class StackMapStream; |
| 647 | }; |
| 648 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 649 | /** |
| 650 | * A Stack Map holds compilation information for a specific PC necessary for: |
| 651 | * - Mapping it to a dex PC, |
| 652 | * - Knowing which stack entries are objects, |
| 653 | * - Knowing which registers hold objects, |
| 654 | * - Knowing the inlining information, |
| 655 | * - Knowing the values of dex registers. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 656 | */ |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 657 | class StackMap : public BitTable<6>::Accessor { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 658 | public: |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 659 | enum Field { |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame^] | 660 | kPackedNativePc, |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 661 | kDexPc, |
| 662 | kDexRegisterMapOffset, |
| 663 | kInlineInfoIndex, |
| 664 | kRegisterMaskIndex, |
| 665 | kStackMaskIndex, |
| 666 | kCount, |
| 667 | }; |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 668 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 669 | StackMap() : BitTable<kCount>::Accessor(nullptr, -1) {} |
| 670 | StackMap(const BitTable<kCount>* table, uint32_t row) |
| 671 | : BitTable<kCount>::Accessor(table, row) {} |
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 | ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const { |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame^] | 674 | return UnpackNativePc(Get<kPackedNativePc>(), instruction_set); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 675 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 676 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 677 | uint32_t GetDexPc() const { return Get<kDexPc>(); } |
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 GetDexRegisterMapOffset() const { return Get<kDexRegisterMapOffset>(); } |
| 680 | bool HasDexRegisterMap() const { return GetDexRegisterMapOffset() != kNoValue; } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 681 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 682 | uint32_t GetInlineInfoIndex() const { return Get<kInlineInfoIndex>(); } |
| 683 | bool HasInlineInfo() const { return GetInlineInfoIndex() != kNoValue; } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 684 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 685 | uint32_t GetRegisterMaskIndex() const { return Get<kRegisterMaskIndex>(); } |
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 GetStackMaskIndex() const { return Get<kStackMaskIndex>(); } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 688 | |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame^] | 689 | static uint32_t PackNativePc(uint32_t native_pc, InstructionSet isa) { |
| 690 | // TODO: DCHECK_ALIGNED_PARAM(native_pc, GetInstructionSetInstructionAlignment(isa)); |
| 691 | return native_pc / GetInstructionSetInstructionAlignment(isa); |
| 692 | } |
| 693 | |
| 694 | static uint32_t UnpackNativePc(uint32_t packed_native_pc, InstructionSet isa) { |
| 695 | uint32_t native_pc = packed_native_pc * GetInstructionSetInstructionAlignment(isa); |
| 696 | DCHECK_EQ(native_pc / GetInstructionSetInstructionAlignment(isa), packed_native_pc); |
| 697 | return native_pc; |
| 698 | } |
| 699 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 700 | static void DumpEncoding(const BitTable<6>& table, VariableIndentationOutputStream* vios); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 701 | void Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 702 | const CodeInfo& code_info, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 703 | const MethodInfo& method_info, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 704 | uint32_t code_offset, |
| 705 | uint16_t number_of_dex_registers, |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 706 | InstructionSet instruction_set, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 707 | const std::string& header_suffix = "") const; |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 708 | }; |
| 709 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 710 | /** |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 711 | * Inline information for a specific PC. |
| 712 | * The row referenced from the StackMap holds information at depth 0. |
| 713 | * Following rows hold information for further depths. |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 714 | */ |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 715 | class InlineInfo : public BitTable<5>::Accessor { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 716 | public: |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 717 | enum Field { |
| 718 | kIsLast, // Determines if there are further rows for further depths. |
| 719 | kMethodIndexIdx, // Method index or ArtMethod high bits. |
| 720 | kDexPc, |
| 721 | kExtraData, // ArtMethod low bits or 1. |
| 722 | kDexRegisterMapOffset, |
| 723 | kCount, |
| 724 | }; |
| 725 | static constexpr uint32_t kLast = -1; |
| 726 | static constexpr uint32_t kMore = 0; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 727 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 728 | InlineInfo(const BitTable<kCount>* table, uint32_t row) |
| 729 | : BitTable<kCount>::Accessor(table, row) {} |
| 730 | |
| 731 | ALWAYS_INLINE InlineInfo AtDepth(uint32_t depth) const { |
| 732 | return InlineInfo(table_, this->row_ + depth); |
| 733 | } |
| 734 | |
| 735 | uint32_t GetDepth() const { |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 736 | size_t depth = 0; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 737 | while (AtDepth(depth++).Get<kIsLast>() == kMore) { } |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 738 | return 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 GetMethodIndexIdxAtDepth(uint32_t depth) const { |
| 742 | DCHECK(!EncodesArtMethodAtDepth(depth)); |
| 743 | return AtDepth(depth).Get<kMethodIndexIdx>(); |
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 | uint32_t GetMethodIndexAtDepth(const MethodInfo& method_info, uint32_t depth) const { |
| 747 | return method_info.GetMethodIndex(GetMethodIndexIdxAtDepth(depth)); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 748 | } |
| 749 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 750 | uint32_t GetDexPcAtDepth(uint32_t depth) const { |
| 751 | return AtDepth(depth).Get<kDexPc>(); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 752 | } |
| 753 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 754 | bool EncodesArtMethodAtDepth(uint32_t depth) const { |
| 755 | return (AtDepth(depth).Get<kExtraData>() & 1) == 0; |
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 | ArtMethod* GetArtMethodAtDepth(uint32_t depth) const { |
| 759 | uint32_t low_bits = AtDepth(depth).Get<kExtraData>(); |
| 760 | uint32_t high_bits = AtDepth(depth).Get<kMethodIndexIdx>(); |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 761 | if (high_bits == 0) { |
| 762 | return reinterpret_cast<ArtMethod*>(low_bits); |
| 763 | } else { |
| 764 | uint64_t address = high_bits; |
| 765 | address = address << 32; |
| 766 | return reinterpret_cast<ArtMethod*>(address | low_bits); |
| 767 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 768 | } |
| 769 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 770 | uint32_t GetDexRegisterMapOffsetAtDepth(uint32_t depth) const { |
| 771 | return AtDepth(depth).Get<kDexRegisterMapOffset>(); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 772 | } |
| 773 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 774 | bool HasDexRegisterMapAtDepth(uint32_t depth) const { |
| 775 | return GetDexRegisterMapOffsetAtDepth(depth) != StackMap::kNoValue; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 776 | } |
| 777 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 778 | static void DumpEncoding(const BitTable<5>& table, VariableIndentationOutputStream* vios); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 779 | void Dump(VariableIndentationOutputStream* vios, |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 780 | const CodeInfo& info, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 781 | const MethodInfo& method_info, |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 782 | uint16_t* number_of_dex_registers) const; |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 783 | }; |
| 784 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 785 | class InvokeInfo : public BitTable<3>::Accessor { |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 786 | public: |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 787 | enum Field { |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame^] | 788 | kPackedNativePc, |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 789 | kInvokeType, |
| 790 | kMethodIndexIdx, |
| 791 | kCount, |
| 792 | }; |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 793 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 794 | InvokeInfo(const BitTable<kCount>* table, uint32_t row) |
| 795 | : BitTable<kCount>::Accessor(table, row) {} |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 796 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 797 | ALWAYS_INLINE uint32_t GetNativePcOffset(InstructionSet instruction_set) const { |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame^] | 798 | return StackMap::UnpackNativePc(Get<kPackedNativePc>(), instruction_set); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 799 | } |
| 800 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 801 | uint32_t GetInvokeType() const { return Get<kInvokeType>(); } |
| 802 | |
| 803 | uint32_t GetMethodIndexIdx() const { return Get<kMethodIndexIdx>(); } |
| 804 | |
| 805 | uint32_t GetMethodIndex(MethodInfo method_info) const { |
| 806 | return method_info.GetMethodIndex(GetMethodIndexIdx()); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 807 | } |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 808 | }; |
| 809 | |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 810 | // Register masks tend to have many trailing zero bits (caller-saves are usually not encoded), |
| 811 | // therefore it is worth encoding the mask as value+shift. |
| 812 | class RegisterMask : public BitTable<2>::Accessor { |
| 813 | public: |
| 814 | enum Field { |
| 815 | kValue, |
| 816 | kShift, |
| 817 | kCount, |
| 818 | }; |
| 819 | |
| 820 | RegisterMask(const BitTable<kCount>* table, uint32_t row) |
| 821 | : BitTable<kCount>::Accessor(table, row) {} |
| 822 | |
| 823 | ALWAYS_INLINE uint32_t GetMask() const { |
| 824 | return Get<kValue>() << Get<kShift>(); |
| 825 | } |
| 826 | }; |
| 827 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 828 | /** |
| 829 | * Wrapper around all compiler information collected for a method. |
| 830 | * The information is of the form: |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 831 | * |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 832 | * [BitTable<Header>, BitTable<StackMap>, BitTable<RegisterMask>, BitTable<InlineInfo>, |
| 833 | * BitTable<InvokeInfo>, BitTable<StackMask>, DexRegisterMap, DexLocationCatalog] |
Mathieu Chartier | c420a80 | 2017-02-14 15:16:19 -0800 | [diff] [blame] | 834 | * |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 835 | */ |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 836 | class CodeInfo { |
| 837 | public: |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 838 | explicit CodeInfo(const void* data) { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 839 | Decode(reinterpret_cast<const uint8_t*>(data)); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 840 | } |
| 841 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 842 | explicit CodeInfo(MemoryRegion region) : CodeInfo(region.begin()) { |
| 843 | DCHECK_EQ(size_, region.size()); |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 844 | } |
| 845 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 846 | explicit CodeInfo(const OatQuickMethodHeader* header) |
| 847 | : CodeInfo(header->GetOptimizedCodeInfoPtr()) { |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 848 | } |
| 849 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 850 | size_t Size() const { |
| 851 | return size_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 852 | } |
| 853 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 854 | bool HasInlineInfo() const { |
| 855 | return stack_maps_.NumColumnBits(StackMap::kInlineInfoIndex) != 0; |
Mathieu Chartier | 12f1b99 | 2017-01-19 18:00:45 -0800 | [diff] [blame] | 856 | } |
| 857 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 858 | DexRegisterLocationCatalog GetDexRegisterLocationCatalog() const { |
| 859 | return DexRegisterLocationCatalog(location_catalog_); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 860 | } |
| 861 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 862 | ALWAYS_INLINE StackMap GetStackMapAt(size_t index) const { |
| 863 | return StackMap(&stack_maps_, index); |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 864 | } |
| 865 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 866 | BitMemoryRegion GetStackMask(size_t index) const { |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 867 | return stack_masks_.GetBitMemoryRegion(index); |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 868 | } |
| 869 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 870 | BitMemoryRegion GetStackMaskOf(const StackMap& stack_map) const { |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 871 | uint32_t index = stack_map.GetStackMaskIndex(); |
| 872 | return (index == StackMap::kNoValue) ? BitMemoryRegion() : GetStackMask(index); |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 873 | } |
| 874 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 875 | uint32_t GetRegisterMaskOf(const StackMap& stack_map) const { |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 876 | uint32_t index = stack_map.GetRegisterMaskIndex(); |
| 877 | return (index == StackMap::kNoValue) ? 0 : RegisterMask(®ister_masks_, index).GetMask(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 878 | } |
| 879 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 880 | uint32_t GetNumberOfLocationCatalogEntries() const { |
| 881 | return location_catalog_entries_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 882 | } |
| 883 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 884 | uint32_t GetDexRegisterLocationCatalogSize() const { |
| 885 | return location_catalog_.size(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 886 | } |
| 887 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 888 | uint32_t GetNumberOfStackMaps() const { |
| 889 | return stack_maps_.NumRows(); |
Nicolas Geoffray | 6530baf | 2015-05-26 15:22:58 +0100 | [diff] [blame] | 890 | } |
| 891 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 892 | InvokeInfo GetInvokeInfo(size_t index) const { |
| 893 | return InvokeInfo(&invoke_infos_, index); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 894 | } |
| 895 | |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 896 | DexRegisterMap GetDexRegisterMapOf(StackMap stack_map, |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 897 | size_t number_of_dex_registers) const { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 898 | if (!stack_map.HasDexRegisterMap()) { |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 899 | return DexRegisterMap(); |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 900 | } |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 901 | const uint32_t offset = stack_map.GetDexRegisterMapOffset(); |
| 902 | size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers); |
| 903 | return DexRegisterMap(dex_register_maps_.Subregion(offset, size)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 904 | } |
| 905 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 906 | size_t GetDexRegisterMapsSize(uint32_t number_of_dex_registers) const { |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 907 | size_t total = 0; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 908 | for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) { |
| 909 | StackMap stack_map = GetStackMapAt(i); |
| 910 | DexRegisterMap map(GetDexRegisterMapOf(stack_map, number_of_dex_registers)); |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 911 | total += map.Size(); |
| 912 | } |
| 913 | return total; |
| 914 | } |
| 915 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 916 | // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`. |
| 917 | DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth, |
| 918 | InlineInfo inline_info, |
| 919 | uint32_t number_of_dex_registers) const { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 920 | if (!inline_info.HasDexRegisterMapAtDepth(depth)) { |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 921 | return DexRegisterMap(); |
| 922 | } else { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 923 | uint32_t offset = inline_info.GetDexRegisterMapOffsetAtDepth(depth); |
| 924 | size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers); |
| 925 | return DexRegisterMap(dex_register_maps_.Subregion(offset, size)); |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 926 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 927 | } |
| 928 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 929 | InlineInfo GetInlineInfo(size_t index) const { |
| 930 | return InlineInfo(&inline_infos_, index); |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 931 | } |
| 932 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 933 | InlineInfo GetInlineInfoOf(StackMap stack_map) const { |
| 934 | DCHECK(stack_map.HasInlineInfo()); |
| 935 | uint32_t index = stack_map.GetInlineInfoIndex(); |
| 936 | return GetInlineInfo(index); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 937 | } |
| 938 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 939 | StackMap GetStackMapForDexPc(uint32_t dex_pc) const { |
| 940 | for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) { |
| 941 | StackMap stack_map = GetStackMapAt(i); |
| 942 | if (stack_map.GetDexPc() == dex_pc) { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 943 | return stack_map; |
| 944 | } |
| 945 | } |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 946 | return StackMap(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 947 | } |
| 948 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 949 | // Searches the stack map list backwards because catch stack maps are stored |
| 950 | // at the end. |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 951 | StackMap GetCatchStackMapForDexPc(uint32_t dex_pc) const { |
| 952 | for (size_t i = GetNumberOfStackMaps(); i > 0; --i) { |
| 953 | StackMap stack_map = GetStackMapAt(i - 1); |
| 954 | if (stack_map.GetDexPc() == dex_pc) { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 955 | return stack_map; |
| 956 | } |
| 957 | } |
| 958 | return StackMap(); |
| 959 | } |
| 960 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 961 | StackMap GetOsrStackMapForDexPc(uint32_t dex_pc) const { |
| 962 | size_t e = GetNumberOfStackMaps(); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 963 | if (e == 0) { |
| 964 | // There cannot be OSR stack map if there is no stack map. |
| 965 | return StackMap(); |
| 966 | } |
| 967 | // Walk over all stack maps. If two consecutive stack maps are identical, then we |
| 968 | // have found a stack map suitable for OSR. |
| 969 | for (size_t i = 0; i < e - 1; ++i) { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 970 | StackMap stack_map = GetStackMapAt(i); |
| 971 | if (stack_map.GetDexPc() == dex_pc) { |
| 972 | StackMap other = GetStackMapAt(i + 1); |
| 973 | if (other.GetDexPc() == dex_pc && |
| 974 | other.GetNativePcOffset(kRuntimeISA) == |
| 975 | stack_map.GetNativePcOffset(kRuntimeISA)) { |
| 976 | DCHECK_EQ(other.GetDexRegisterMapOffset(), |
| 977 | stack_map.GetDexRegisterMapOffset()); |
| 978 | DCHECK(!stack_map.HasInlineInfo()); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 979 | if (i < e - 2) { |
| 980 | // Make sure there are not three identical stack maps following each other. |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 981 | DCHECK_NE( |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 982 | stack_map.GetNativePcOffset(kRuntimeISA), |
| 983 | GetStackMapAt(i + 2).GetNativePcOffset(kRuntimeISA)); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 984 | } |
| 985 | return stack_map; |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | return StackMap(); |
| 990 | } |
| 991 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 992 | StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 993 | // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack |
| 994 | // maps are not. If we knew that the method does not have try/catch, |
| 995 | // we could do binary search. |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 996 | for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) { |
| 997 | StackMap stack_map = GetStackMapAt(i); |
| 998 | if (stack_map.GetNativePcOffset(kRuntimeISA) == native_pc_offset) { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 999 | return stack_map; |
| 1000 | } |
| 1001 | } |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 1002 | return StackMap(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1003 | } |
| 1004 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1005 | InvokeInfo GetInvokeInfoForNativePcOffset(uint32_t native_pc_offset) { |
| 1006 | for (size_t index = 0; index < invoke_infos_.NumRows(); index++) { |
| 1007 | InvokeInfo item = GetInvokeInfo(index); |
| 1008 | if (item.GetNativePcOffset(kRuntimeISA) == native_pc_offset) { |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 1009 | return item; |
| 1010 | } |
| 1011 | } |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1012 | return InvokeInfo(&invoke_infos_, -1); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1015 | // Dump this CodeInfo object on `os`. `code_offset` is the (absolute) |
| 1016 | // native PC of the compiled method and `number_of_dex_registers` the |
| 1017 | // number of Dex virtual registers used in this method. If |
| 1018 | // `dump_stack_maps` is true, also dump the stack maps and the |
| 1019 | // associated Dex register maps. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1020 | void Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1021 | uint32_t code_offset, |
| 1022 | uint16_t number_of_dex_registers, |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1023 | bool dump_stack_maps, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1024 | InstructionSet instruction_set, |
| 1025 | const MethodInfo& method_info) const; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 1026 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1027 | private: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1028 | // Compute the size of the Dex register map associated to the stack map at |
| 1029 | // `dex_register_map_offset_in_code_info`. |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1030 | size_t ComputeDexRegisterMapSizeOf(uint32_t dex_register_map_offset, |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1031 | uint16_t number_of_dex_registers) const { |
| 1032 | // Offset where the actual mapping data starts within art::DexRegisterMap. |
| 1033 | size_t location_mapping_data_offset_in_dex_register_map = |
| 1034 | DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers); |
| 1035 | // Create a temporary art::DexRegisterMap to be able to call |
| 1036 | // art::DexRegisterMap::GetNumberOfLiveDexRegisters and |
| 1037 | DexRegisterMap dex_register_map_without_locations( |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1038 | MemoryRegion(dex_register_maps_.Subregion(dex_register_map_offset, |
| 1039 | location_mapping_data_offset_in_dex_register_map))); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1040 | size_t number_of_live_dex_registers = |
| 1041 | dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers); |
| 1042 | size_t location_mapping_data_size_in_bits = |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1043 | DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries()) |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1044 | * number_of_live_dex_registers; |
| 1045 | size_t location_mapping_data_size_in_bytes = |
| 1046 | RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte; |
| 1047 | size_t dex_register_map_size = |
| 1048 | location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes; |
| 1049 | return dex_register_map_size; |
| 1050 | } |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 1051 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1052 | MemoryRegion DecodeMemoryRegion(MemoryRegion& region, size_t* bit_offset) { |
| 1053 | size_t length = DecodeVarintBits(BitMemoryRegion(region), bit_offset); |
| 1054 | size_t offset = BitsToBytesRoundUp(*bit_offset);; |
| 1055 | *bit_offset = (offset + length) * kBitsPerByte; |
| 1056 | return region.Subregion(offset, length); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1059 | void Decode(const uint8_t* data) { |
| 1060 | size_t non_header_size = DecodeUnsignedLeb128(&data); |
| 1061 | MemoryRegion region(const_cast<uint8_t*>(data), non_header_size); |
| 1062 | BitMemoryRegion bit_region(region); |
| 1063 | size_t bit_offset = 0; |
| 1064 | size_ = UnsignedLeb128Size(non_header_size) + non_header_size; |
| 1065 | dex_register_maps_ = DecodeMemoryRegion(region, &bit_offset); |
| 1066 | location_catalog_entries_ = DecodeVarintBits(bit_region, &bit_offset); |
| 1067 | location_catalog_ = DecodeMemoryRegion(region, &bit_offset); |
| 1068 | stack_maps_.Decode(bit_region, &bit_offset); |
| 1069 | invoke_infos_.Decode(bit_region, &bit_offset); |
| 1070 | inline_infos_.Decode(bit_region, &bit_offset); |
| 1071 | register_masks_.Decode(bit_region, &bit_offset); |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 1072 | stack_masks_.Decode(bit_region, &bit_offset); |
| 1073 | CHECK_EQ(BitsToBytesRoundUp(bit_offset), non_header_size); |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | size_t size_; |
| 1077 | MemoryRegion dex_register_maps_; |
| 1078 | uint32_t location_catalog_entries_; |
| 1079 | MemoryRegion location_catalog_; |
| 1080 | BitTable<StackMap::Field::kCount> stack_maps_; |
| 1081 | BitTable<InvokeInfo::Field::kCount> invoke_infos_; |
| 1082 | BitTable<InlineInfo::Field::kCount> inline_infos_; |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 1083 | BitTable<RegisterMask::Field::kCount> register_masks_; |
| 1084 | BitTable<1> stack_masks_; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1085 | |
| 1086 | friend class OatDumper; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1087 | }; |
| 1088 | |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 1089 | #undef ELEMENT_BYTE_OFFSET_AFTER |
| 1090 | #undef ELEMENT_BIT_OFFSET_AFTER |
| 1091 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1092 | } // namespace art |
| 1093 | |
| 1094 | #endif // ART_RUNTIME_STACK_MAP_H_ |