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 | |
| 20 | #include "base/bit_vector.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 21 | #include "base/bit_utils.h" |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 22 | #include "dex_file.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 23 | #include "memory_region.h" |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 24 | #include "leb128.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 28 | class VariableIndentationOutputStream; |
| 29 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 30 | // Size of a frame slot, in bytes. This constant is a signed value, |
| 31 | // to please the compiler in arithmetic operations involving int32_t |
| 32 | // (signed) values. |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 33 | static constexpr ssize_t kFrameSlotSize = 4; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 34 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 35 | // Size of Dex virtual registers. |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 36 | static constexpr size_t kVRegSize = 4; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 37 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 38 | class ArtMethod; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 39 | class CodeInfo; |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 40 | class StackMapEncoding; |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 41 | struct CodeInfoEncoding; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 42 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 43 | /** |
| 44 | * Classes in the following file are wrapper on stack map information backed |
| 45 | * by a MemoryRegion. As such they read and write to the region, they don't have |
| 46 | * their own fields. |
| 47 | */ |
| 48 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 49 | // Dex register location container used by DexRegisterMap and StackMapStream. |
| 50 | class DexRegisterLocation { |
| 51 | public: |
| 52 | /* |
| 53 | * The location kind used to populate the Dex register information in a |
| 54 | * StackMapStream can either be: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 55 | * - kStack: vreg stored on the stack, value holds the stack offset; |
| 56 | * - kInRegister: vreg stored in low 32 bits of a core physical register, |
| 57 | * value holds the register number; |
| 58 | * - kInRegisterHigh: vreg stored in high 32 bits of a core physical register, |
| 59 | * value holds the register number; |
| 60 | * - kInFpuRegister: vreg stored in low 32 bits of an FPU register, |
| 61 | * value holds the register number; |
| 62 | * - kInFpuRegisterHigh: vreg stored in high 32 bits of an FPU register, |
| 63 | * value holds the register number; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 64 | * - kConstant: value holds the constant; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 65 | * |
| 66 | * In addition, DexRegisterMap also uses these values: |
| 67 | * - kInStackLargeOffset: value holds a "large" stack offset (greater than |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 68 | * or equal to 128 bytes); |
| 69 | * - kConstantLargeValue: value holds a "large" constant (lower than 0, or |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 70 | * or greater than or equal to 32); |
| 71 | * - kNone: the register has no location, meaning it has not been set. |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 72 | */ |
| 73 | enum class Kind : uint8_t { |
| 74 | // Short location kinds, for entries fitting on one byte (3 bits |
| 75 | // for the kind, 5 bits for the value) in a DexRegisterMap. |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 76 | kInStack = 0, // 0b000 |
| 77 | kInRegister = 1, // 0b001 |
| 78 | kInRegisterHigh = 2, // 0b010 |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 79 | kInFpuRegister = 3, // 0b011 |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 80 | kInFpuRegisterHigh = 4, // 0b100 |
| 81 | kConstant = 5, // 0b101 |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 82 | |
| 83 | // Large location kinds, requiring a 5-byte encoding (1 byte for the |
| 84 | // kind, 4 bytes for the value). |
| 85 | |
| 86 | // Stack location at a large offset, meaning that the offset value |
| 87 | // divided by the stack frame slot size (4 bytes) cannot fit on a |
| 88 | // 5-bit unsigned integer (i.e., this offset value is greater than |
| 89 | // or equal to 2^5 * 4 = 128 bytes). |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 90 | kInStackLargeOffset = 6, // 0b110 |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 91 | |
| 92 | // 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] | 93 | // lower than 0, or greater than or equal to 2^5 = 32). |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 94 | kConstantLargeValue = 7, // 0b111 |
| 95 | |
| 96 | // Entries with no location are not stored and do not need own marker. |
| 97 | kNone = static_cast<uint8_t>(-1), |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 98 | |
| 99 | kLastLocationKind = kConstantLargeValue |
| 100 | }; |
| 101 | |
| 102 | static_assert( |
| 103 | sizeof(Kind) == 1u, |
| 104 | "art::DexRegisterLocation::Kind has a size different from one byte."); |
| 105 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 106 | static bool IsShortLocationKind(Kind kind) { |
| 107 | switch (kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 108 | case Kind::kInStack: |
| 109 | case Kind::kInRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 110 | case Kind::kInRegisterHigh: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 111 | case Kind::kInFpuRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 112 | case Kind::kInFpuRegisterHigh: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 113 | case Kind::kConstant: |
| 114 | return true; |
| 115 | |
| 116 | case Kind::kInStackLargeOffset: |
| 117 | case Kind::kConstantLargeValue: |
| 118 | return false; |
| 119 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 120 | case Kind::kNone: |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 121 | LOG(FATAL) << "Unexpected location kind"; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 122 | } |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 123 | UNREACHABLE(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Convert `kind` to a "surface" kind, i.e. one that doesn't include |
| 127 | // any value with a "large" qualifier. |
| 128 | // TODO: Introduce another enum type for the surface kind? |
| 129 | static Kind ConvertToSurfaceKind(Kind kind) { |
| 130 | switch (kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 131 | case Kind::kInStack: |
| 132 | case Kind::kInRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 133 | case Kind::kInRegisterHigh: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 134 | case Kind::kInFpuRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 135 | case Kind::kInFpuRegisterHigh: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 136 | case Kind::kConstant: |
| 137 | return kind; |
| 138 | |
| 139 | case Kind::kInStackLargeOffset: |
| 140 | return Kind::kInStack; |
| 141 | |
| 142 | case Kind::kConstantLargeValue: |
| 143 | return Kind::kConstant; |
| 144 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 145 | case Kind::kNone: |
| 146 | return kind; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 147 | } |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 148 | UNREACHABLE(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 151 | // Required by art::StackMapStream::LocationCatalogEntriesIndices. |
| 152 | DexRegisterLocation() : kind_(Kind::kNone), value_(0) {} |
| 153 | |
| 154 | DexRegisterLocation(Kind kind, int32_t value) : kind_(kind), value_(value) {} |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 155 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 156 | static DexRegisterLocation None() { |
| 157 | return DexRegisterLocation(Kind::kNone, 0); |
| 158 | } |
| 159 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 160 | // Get the "surface" kind of the location, i.e., the one that doesn't |
| 161 | // include any value with a "large" qualifier. |
| 162 | Kind GetKind() const { |
| 163 | return ConvertToSurfaceKind(kind_); |
| 164 | } |
| 165 | |
| 166 | // Get the value of the location. |
| 167 | int32_t GetValue() const { return value_; } |
| 168 | |
| 169 | // Get the actual kind of the location. |
| 170 | Kind GetInternalKind() const { return kind_; } |
| 171 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 172 | bool operator==(DexRegisterLocation other) const { |
| 173 | return kind_ == other.kind_ && value_ == other.value_; |
| 174 | } |
| 175 | |
| 176 | bool operator!=(DexRegisterLocation other) const { |
| 177 | return !(*this == other); |
| 178 | } |
| 179 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 180 | private: |
| 181 | Kind kind_; |
| 182 | int32_t value_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 183 | |
| 184 | friend class DexRegisterLocationHashFn; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 187 | std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation::Kind& kind); |
| 188 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 189 | /** |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 190 | * Store information on unique Dex register locations used in a method. |
| 191 | * The information is of the form: |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 192 | * |
| 193 | * [DexRegisterLocation+]. |
| 194 | * |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 195 | * DexRegisterLocations are either 1- or 5-byte wide (see art::DexRegisterLocation::Kind). |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 196 | */ |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 197 | class DexRegisterLocationCatalog { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 198 | public: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 199 | explicit DexRegisterLocationCatalog(MemoryRegion region) : region_(region) {} |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 200 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 201 | // Short (compressed) location, fitting on one byte. |
| 202 | typedef uint8_t ShortLocation; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 203 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 204 | void SetRegisterInfo(size_t offset, const DexRegisterLocation& dex_register_location) { |
| 205 | DexRegisterLocation::Kind kind = ComputeCompressedKind(dex_register_location); |
| 206 | int32_t value = dex_register_location.GetValue(); |
| 207 | if (DexRegisterLocation::IsShortLocationKind(kind)) { |
| 208 | // Short location. Compress the kind and the value as a single byte. |
| 209 | if (kind == DexRegisterLocation::Kind::kInStack) { |
| 210 | // Instead of storing stack offsets expressed in bytes for |
| 211 | // short stack locations, store slot offsets. A stack offset |
| 212 | // is a multiple of 4 (kFrameSlotSize). This means that by |
| 213 | // dividing it by 4, we can fit values from the [0, 128) |
| 214 | // interval in a short stack location, and not just values |
| 215 | // from the [0, 32) interval. |
| 216 | DCHECK_EQ(value % kFrameSlotSize, 0); |
| 217 | value /= kFrameSlotSize; |
| 218 | } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 219 | DCHECK(IsShortValue(value)) << value; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 220 | region_.StoreUnaligned<ShortLocation>(offset, MakeShortLocation(kind, value)); |
| 221 | } else { |
| 222 | // Large location. Write the location on one byte and the value |
| 223 | // on 4 bytes. |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 224 | DCHECK(!IsShortValue(value)) << value; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 225 | if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) { |
| 226 | // Also divide large stack offsets by 4 for the sake of consistency. |
| 227 | DCHECK_EQ(value % kFrameSlotSize, 0); |
| 228 | value /= kFrameSlotSize; |
| 229 | } |
| 230 | // Data can be unaligned as the written Dex register locations can |
| 231 | // either be 1-byte or 5-byte wide. Use |
| 232 | // art::MemoryRegion::StoreUnaligned instead of |
| 233 | // art::MemoryRegion::Store to prevent unligned word accesses on ARM. |
| 234 | region_.StoreUnaligned<DexRegisterLocation::Kind>(offset, kind); |
| 235 | region_.StoreUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind), value); |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 239 | // Find the offset of the location catalog entry number `location_catalog_entry_index`. |
| 240 | size_t FindLocationOffset(size_t location_catalog_entry_index) const { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 241 | size_t offset = kFixedSize; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 242 | // Skip the first `location_catalog_entry_index - 1` entries. |
| 243 | for (uint16_t i = 0; i < location_catalog_entry_index; ++i) { |
| 244 | // Read the first next byte and inspect its first 3 bits to decide |
| 245 | // whether it is a short or a large location. |
| 246 | DexRegisterLocation::Kind kind = ExtractKindAtOffset(offset); |
| 247 | if (DexRegisterLocation::IsShortLocationKind(kind)) { |
| 248 | // Short location. Skip the current byte. |
| 249 | offset += SingleShortEntrySize(); |
| 250 | } else { |
| 251 | // Large location. Skip the 5 next bytes. |
| 252 | offset += SingleLargeEntrySize(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | return offset; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 258 | // Get the internal kind of entry at `location_catalog_entry_index`. |
| 259 | DexRegisterLocation::Kind GetLocationInternalKind(size_t location_catalog_entry_index) const { |
| 260 | if (location_catalog_entry_index == kNoLocationEntryIndex) { |
| 261 | return DexRegisterLocation::Kind::kNone; |
| 262 | } |
| 263 | return ExtractKindAtOffset(FindLocationOffset(location_catalog_entry_index)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 266 | // Get the (surface) kind and value of entry at `location_catalog_entry_index`. |
| 267 | DexRegisterLocation GetDexRegisterLocation(size_t location_catalog_entry_index) const { |
| 268 | if (location_catalog_entry_index == kNoLocationEntryIndex) { |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 269 | return DexRegisterLocation::None(); |
| 270 | } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 271 | size_t offset = FindLocationOffset(location_catalog_entry_index); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 272 | // Read the first byte and inspect its first 3 bits to get the location. |
| 273 | ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset); |
| 274 | DexRegisterLocation::Kind kind = ExtractKindFromShortLocation(first_byte); |
| 275 | if (DexRegisterLocation::IsShortLocationKind(kind)) { |
| 276 | // Short location. Extract the value from the remaining 5 bits. |
| 277 | int32_t value = ExtractValueFromShortLocation(first_byte); |
| 278 | if (kind == DexRegisterLocation::Kind::kInStack) { |
| 279 | // Convert the stack slot (short) offset to a byte offset value. |
| 280 | value *= kFrameSlotSize; |
| 281 | } |
| 282 | return DexRegisterLocation(kind, value); |
| 283 | } else { |
| 284 | // Large location. Read the four next bytes to get the value. |
| 285 | int32_t value = region_.LoadUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind)); |
| 286 | if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) { |
| 287 | // Convert the stack slot (large) offset to a byte offset value. |
| 288 | value *= kFrameSlotSize; |
| 289 | } |
| 290 | return DexRegisterLocation(kind, value); |
| 291 | } |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 294 | // Compute the compressed kind of `location`. |
| 295 | static DexRegisterLocation::Kind ComputeCompressedKind(const DexRegisterLocation& location) { |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 296 | DexRegisterLocation::Kind kind = location.GetInternalKind(); |
| 297 | switch (kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 298 | case DexRegisterLocation::Kind::kInStack: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 299 | return IsShortStackOffsetValue(location.GetValue()) |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 300 | ? DexRegisterLocation::Kind::kInStack |
| 301 | : DexRegisterLocation::Kind::kInStackLargeOffset; |
| 302 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 303 | case DexRegisterLocation::Kind::kInRegister: |
| 304 | case DexRegisterLocation::Kind::kInRegisterHigh: |
| 305 | DCHECK_GE(location.GetValue(), 0); |
| 306 | DCHECK_LT(location.GetValue(), 1 << kValueBits); |
| 307 | return kind; |
| 308 | |
| 309 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 310 | case DexRegisterLocation::Kind::kInFpuRegisterHigh: |
| 311 | DCHECK_GE(location.GetValue(), 0); |
| 312 | DCHECK_LT(location.GetValue(), 1 << kValueBits); |
| 313 | return kind; |
| 314 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 315 | case DexRegisterLocation::Kind::kConstant: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 316 | return IsShortConstantValue(location.GetValue()) |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 317 | ? DexRegisterLocation::Kind::kConstant |
| 318 | : DexRegisterLocation::Kind::kConstantLargeValue; |
| 319 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 320 | case DexRegisterLocation::Kind::kConstantLargeValue: |
| 321 | case DexRegisterLocation::Kind::kInStackLargeOffset: |
| 322 | case DexRegisterLocation::Kind::kNone: |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 323 | LOG(FATAL) << "Unexpected location kind " << kind; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 324 | } |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 325 | UNREACHABLE(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // Can `location` be turned into a short location? |
| 329 | static bool CanBeEncodedAsShortLocation(const DexRegisterLocation& location) { |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 330 | DexRegisterLocation::Kind kind = location.GetInternalKind(); |
| 331 | switch (kind) { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 332 | case DexRegisterLocation::Kind::kInStack: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 333 | return IsShortStackOffsetValue(location.GetValue()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 334 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 335 | case DexRegisterLocation::Kind::kInRegister: |
| 336 | case DexRegisterLocation::Kind::kInRegisterHigh: |
| 337 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 338 | case DexRegisterLocation::Kind::kInFpuRegisterHigh: |
| 339 | return true; |
| 340 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 341 | case DexRegisterLocation::Kind::kConstant: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 342 | return IsShortConstantValue(location.GetValue()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 343 | |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 344 | case DexRegisterLocation::Kind::kConstantLargeValue: |
| 345 | case DexRegisterLocation::Kind::kInStackLargeOffset: |
| 346 | case DexRegisterLocation::Kind::kNone: |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 347 | LOG(FATAL) << "Unexpected location kind " << kind; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 348 | } |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 349 | UNREACHABLE(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static size_t EntrySize(const DexRegisterLocation& location) { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 353 | return CanBeEncodedAsShortLocation(location) ? SingleShortEntrySize() : SingleLargeEntrySize(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static size_t SingleShortEntrySize() { |
| 357 | return sizeof(ShortLocation); |
| 358 | } |
| 359 | |
| 360 | static size_t SingleLargeEntrySize() { |
| 361 | return sizeof(DexRegisterLocation::Kind) + sizeof(int32_t); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 364 | size_t Size() const { |
| 365 | return region_.size(); |
| 366 | } |
| 367 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 368 | void Dump(VariableIndentationOutputStream* vios, const CodeInfo& code_info); |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 369 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 370 | // Special (invalid) Dex register location catalog entry index meaning |
| 371 | // that there is no location for a given Dex register (i.e., it is |
| 372 | // mapped to a DexRegisterLocation::Kind::kNone location). |
| 373 | static constexpr size_t kNoLocationEntryIndex = -1; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 374 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 375 | private: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 376 | static constexpr int kFixedSize = 0; |
| 377 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 378 | // Width of the kind "field" in a short location, in bits. |
| 379 | static constexpr size_t kKindBits = 3; |
| 380 | // Width of the value "field" in a short location, in bits. |
| 381 | static constexpr size_t kValueBits = 5; |
| 382 | |
| 383 | static constexpr uint8_t kKindMask = (1 << kKindBits) - 1; |
| 384 | static constexpr int32_t kValueMask = (1 << kValueBits) - 1; |
| 385 | static constexpr size_t kKindOffset = 0; |
| 386 | static constexpr size_t kValueOffset = kKindBits; |
| 387 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 388 | static bool IsShortStackOffsetValue(int32_t value) { |
| 389 | DCHECK_EQ(value % kFrameSlotSize, 0); |
| 390 | return IsShortValue(value / kFrameSlotSize); |
| 391 | } |
| 392 | |
| 393 | static bool IsShortConstantValue(int32_t value) { |
| 394 | return IsShortValue(value); |
| 395 | } |
| 396 | |
| 397 | static bool IsShortValue(int32_t value) { |
| 398 | return IsUint<kValueBits>(value); |
| 399 | } |
| 400 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 401 | static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 402 | uint8_t kind_integer_value = static_cast<uint8_t>(kind); |
| 403 | DCHECK(IsUint<kKindBits>(kind_integer_value)) << kind_integer_value; |
| 404 | DCHECK(IsShortValue(value)) << value; |
| 405 | return (kind_integer_value & kKindMask) << kKindOffset |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 406 | | (value & kValueMask) << kValueOffset; |
| 407 | } |
| 408 | |
| 409 | static DexRegisterLocation::Kind ExtractKindFromShortLocation(ShortLocation location) { |
| 410 | uint8_t kind = (location >> kKindOffset) & kKindMask; |
| 411 | DCHECK_LE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kLastLocationKind)); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 412 | // We do not encode kNone locations in the stack map. |
| 413 | DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone)); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 414 | return static_cast<DexRegisterLocation::Kind>(kind); |
| 415 | } |
| 416 | |
| 417 | static int32_t ExtractValueFromShortLocation(ShortLocation location) { |
| 418 | return (location >> kValueOffset) & kValueMask; |
| 419 | } |
| 420 | |
| 421 | // Extract a location kind from the byte at position `offset`. |
| 422 | DexRegisterLocation::Kind ExtractKindAtOffset(size_t offset) const { |
| 423 | ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset); |
| 424 | return ExtractKindFromShortLocation(first_byte); |
| 425 | } |
| 426 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 427 | MemoryRegion region_; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 428 | |
| 429 | friend class CodeInfo; |
| 430 | friend class StackMapStream; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 431 | }; |
| 432 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 433 | /* Information on Dex register locations for a specific PC, mapping a |
| 434 | * stack map's Dex register to a location entry in a DexRegisterLocationCatalog. |
| 435 | * The information is of the form: |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 436 | * |
| 437 | * [live_bit_mask, entries*] |
| 438 | * |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 439 | * where entries are concatenated unsigned integer values encoded on a number |
| 440 | * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending |
| 441 | * on the number of entries in the Dex register location catalog |
| 442 | * (see DexRegisterMap::SingleEntrySizeInBits). The map is 1-byte aligned. |
| 443 | */ |
| 444 | class DexRegisterMap { |
| 445 | public: |
| 446 | explicit DexRegisterMap(MemoryRegion region) : region_(region) {} |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 447 | DexRegisterMap() {} |
| 448 | |
| 449 | bool IsValid() const { return region_.pointer() != nullptr; } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 450 | |
| 451 | // Get the surface kind of Dex register `dex_register_number`. |
| 452 | DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number, |
| 453 | uint16_t number_of_dex_registers, |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 454 | const CodeInfo& code_info, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 455 | const CodeInfoEncoding& enc) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 456 | return DexRegisterLocation::ConvertToSurfaceKind( |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 457 | GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info, enc)); |
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`. |
| 461 | DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number, |
| 462 | uint16_t number_of_dex_registers, |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 463 | const CodeInfo& code_info, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 464 | const CodeInfoEncoding& enc) 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 Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 469 | const CodeInfo& code_info, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 470 | const CodeInfoEncoding& enc) const; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 471 | |
| 472 | int32_t GetStackOffsetInBytes(uint16_t dex_register_number, |
| 473 | uint16_t number_of_dex_registers, |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 474 | const CodeInfo& code_info, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 475 | const CodeInfoEncoding& enc) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 476 | DexRegisterLocation location = |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 477 | GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 478 | DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack); |
| 479 | // GetDexRegisterLocation returns the offset in bytes. |
| 480 | return location.GetValue(); |
| 481 | } |
| 482 | |
| 483 | int32_t GetConstant(uint16_t dex_register_number, |
| 484 | uint16_t number_of_dex_registers, |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 485 | const CodeInfo& code_info, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 486 | const CodeInfoEncoding& enc) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 487 | DexRegisterLocation location = |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 488 | GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc); |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 489 | DCHECK_EQ(location.GetKind(), DexRegisterLocation::Kind::kConstant); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 490 | return location.GetValue(); |
| 491 | } |
| 492 | |
| 493 | int32_t GetMachineRegister(uint16_t dex_register_number, |
| 494 | uint16_t number_of_dex_registers, |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 495 | const CodeInfo& code_info, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 496 | const CodeInfoEncoding& enc) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 497 | DexRegisterLocation location = |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 498 | GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info, enc); |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 499 | DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister || |
| 500 | location.GetInternalKind() == DexRegisterLocation::Kind::kInRegisterHigh || |
| 501 | location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister || |
| 502 | location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegisterHigh) |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 503 | << location.GetInternalKind(); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 504 | return location.GetValue(); |
| 505 | } |
| 506 | |
| 507 | // Get the index of the entry in the Dex register location catalog |
| 508 | // corresponding to `dex_register_number`. |
| 509 | size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number, |
| 510 | uint16_t number_of_dex_registers, |
| 511 | size_t number_of_location_catalog_entries) const { |
| 512 | if (!IsDexRegisterLive(dex_register_number)) { |
| 513 | return DexRegisterLocationCatalog::kNoLocationEntryIndex; |
| 514 | } |
| 515 | |
| 516 | if (number_of_location_catalog_entries == 1) { |
| 517 | // We do not allocate space for location maps in the case of a |
| 518 | // single-entry location catalog, as it is useless. The only valid |
| 519 | // entry index is 0; |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | // The bit offset of the beginning of the map locations. |
| 524 | size_t map_locations_offset_in_bits = |
| 525 | GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte; |
| 526 | size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number); |
| 527 | DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers)); |
| 528 | // The bit size of an entry. |
| 529 | size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries); |
| 530 | // The bit offset where `index_in_dex_register_map` is located. |
| 531 | size_t entry_offset_in_bits = |
| 532 | map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits; |
| 533 | size_t location_catalog_entry_index = |
| 534 | region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits); |
| 535 | DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries); |
| 536 | return location_catalog_entry_index; |
| 537 | } |
| 538 | |
| 539 | // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`. |
| 540 | void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map, |
| 541 | size_t location_catalog_entry_index, |
| 542 | uint16_t number_of_dex_registers, |
| 543 | size_t number_of_location_catalog_entries) { |
| 544 | DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers)); |
| 545 | DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries); |
| 546 | |
| 547 | if (number_of_location_catalog_entries == 1) { |
| 548 | // We do not allocate space for location maps in the case of a |
| 549 | // single-entry location catalog, as it is useless. |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | // The bit offset of the beginning of the map locations. |
| 554 | size_t map_locations_offset_in_bits = |
| 555 | GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte; |
| 556 | // The bit size of an entry. |
| 557 | size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries); |
| 558 | // The bit offset where `index_in_dex_register_map` is located. |
| 559 | size_t entry_offset_in_bits = |
| 560 | map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits; |
| 561 | region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits); |
| 562 | } |
| 563 | |
| 564 | void SetLiveBitMask(uint16_t number_of_dex_registers, |
| 565 | const BitVector& live_dex_registers_mask) { |
| 566 | size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte; |
| 567 | for (uint16_t i = 0; i < number_of_dex_registers; ++i) { |
| 568 | region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i)); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | bool IsDexRegisterLive(uint16_t dex_register_number) const { |
| 573 | size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte; |
| 574 | return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number); |
| 575 | } |
| 576 | |
| 577 | size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const { |
| 578 | size_t number_of_live_dex_registers = 0; |
| 579 | for (size_t i = 0; i < number_of_dex_registers; ++i) { |
| 580 | if (IsDexRegisterLive(i)) { |
| 581 | ++number_of_live_dex_registers; |
| 582 | } |
| 583 | } |
| 584 | return number_of_live_dex_registers; |
| 585 | } |
| 586 | |
| 587 | static size_t GetLiveBitMaskOffset() { |
| 588 | return kFixedSize; |
| 589 | } |
| 590 | |
| 591 | // Compute the size of the live register bit mask (in bytes), for a |
| 592 | // method having `number_of_dex_registers` Dex registers. |
| 593 | static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) { |
| 594 | return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte; |
| 595 | } |
| 596 | |
| 597 | static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) { |
| 598 | return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers); |
| 599 | } |
| 600 | |
| 601 | size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers, |
| 602 | size_t number_of_location_catalog_entries) const { |
| 603 | size_t location_mapping_data_size_in_bits = |
| 604 | GetNumberOfLiveDexRegisters(number_of_dex_registers) |
| 605 | * SingleEntrySizeInBits(number_of_location_catalog_entries); |
| 606 | return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte; |
| 607 | } |
| 608 | |
| 609 | // Return the size of a map entry in bits. Note that if |
| 610 | // `number_of_location_catalog_entries` equals 1, this function returns 0, |
| 611 | // which is fine, as there is no need to allocate a map for a |
| 612 | // single-entry location catalog; the only valid location catalog entry index |
| 613 | // for a live register in this case is 0 and there is no need to |
| 614 | // store it. |
| 615 | static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) { |
| 616 | // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2. |
| 617 | return number_of_location_catalog_entries == 0 |
| 618 | ? 0u |
| 619 | : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries)); |
| 620 | } |
| 621 | |
| 622 | // Return the size of the DexRegisterMap object, in bytes. |
| 623 | size_t Size() const { |
| 624 | return region_.size(); |
| 625 | } |
| 626 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 627 | void Dump(VariableIndentationOutputStream* vios, |
| 628 | const CodeInfo& code_info, uint16_t number_of_dex_registers) const; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 629 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 630 | private: |
| 631 | // Return the index in the Dex register map corresponding to the Dex |
| 632 | // register number `dex_register_number`. |
| 633 | size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const { |
| 634 | if (!IsDexRegisterLive(dex_register_number)) { |
| 635 | return kInvalidIndexInDexRegisterMap; |
| 636 | } |
| 637 | return GetNumberOfLiveDexRegisters(dex_register_number); |
| 638 | } |
| 639 | |
| 640 | // Special (invalid) Dex register map entry index meaning that there |
| 641 | // is no index in the map for a given Dex register (i.e., it must |
| 642 | // have been mapped to a DexRegisterLocation::Kind::kNone location). |
| 643 | static constexpr size_t kInvalidIndexInDexRegisterMap = -1; |
| 644 | |
| 645 | static constexpr int kFixedSize = 0; |
| 646 | |
| 647 | MemoryRegion region_; |
| 648 | |
| 649 | friend class CodeInfo; |
| 650 | friend class StackMapStream; |
| 651 | }; |
| 652 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 653 | // Represents bit range of bit-packed integer field. |
| 654 | // We reuse the idea from ULEB128p1 to support encoding of -1 (aka 0xFFFFFFFF). |
| 655 | // If min_value is set to -1, we implicitly subtract one from any loaded value, |
| 656 | // and add one to any stored value. This is generalized to any negative values. |
| 657 | // In other words, min_value acts as a base and the stored value is added to it. |
| 658 | struct FieldEncoding { |
| 659 | FieldEncoding(size_t start_offset, size_t end_offset, int32_t min_value = 0) |
| 660 | : start_offset_(start_offset), end_offset_(end_offset), min_value_(min_value) { |
| 661 | DCHECK_LE(start_offset_, end_offset_); |
| 662 | DCHECK_LE(BitSize(), 32u); |
| 663 | } |
| 664 | |
| 665 | ALWAYS_INLINE size_t BitSize() const { return end_offset_ - start_offset_; } |
| 666 | |
| 667 | ALWAYS_INLINE int32_t Load(const MemoryRegion& region) const { |
| 668 | DCHECK_LE(end_offset_, region.size_in_bits()); |
| 669 | const size_t bit_count = BitSize(); |
| 670 | if (bit_count == 0) { |
| 671 | // Do not touch any memory if the range is empty. |
| 672 | return min_value_; |
| 673 | } |
| 674 | uint8_t* address = region.start() + start_offset_ / kBitsPerByte; |
| 675 | const uint32_t shift = start_offset_ & (kBitsPerByte - 1); |
| 676 | // Load the value (reading only the strictly needed bytes). |
| 677 | const uint32_t load_bit_count = shift + bit_count; |
| 678 | uint32_t value = *address++ >> shift; |
| 679 | if (load_bit_count > 8) { |
| 680 | value |= static_cast<uint32_t>(*address++) << (8 - shift); |
| 681 | if (load_bit_count > 16) { |
| 682 | value |= static_cast<uint32_t>(*address++) << (16 - shift); |
| 683 | if (load_bit_count > 24) { |
| 684 | value |= static_cast<uint32_t>(*address++) << (24 - shift); |
| 685 | if (load_bit_count > 32) { |
| 686 | value |= static_cast<uint32_t>(*address++) << (32 - shift); |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | // Clear unwanted most significant bits. |
| 692 | uint32_t clear_bit_count = 32 - bit_count; |
| 693 | value = (value << clear_bit_count) >> clear_bit_count; |
| 694 | return value + min_value_; |
| 695 | } |
| 696 | |
| 697 | ALWAYS_INLINE void Store(MemoryRegion region, int32_t value) const { |
| 698 | region.StoreBits(start_offset_, value - min_value_, BitSize()); |
| 699 | DCHECK_EQ(Load(region), value); |
| 700 | } |
| 701 | |
| 702 | private: |
| 703 | size_t start_offset_; |
| 704 | size_t end_offset_; |
| 705 | int32_t min_value_; |
| 706 | }; |
| 707 | |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 708 | class StackMapEncoding { |
| 709 | public: |
| 710 | StackMapEncoding() {} |
| 711 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 712 | // Set stack map bit layout based on given sizes. |
| 713 | // Returns the size of stack map in bytes. |
| 714 | size_t SetFromSizes(size_t native_pc_max, |
| 715 | size_t dex_pc_max, |
| 716 | size_t dex_register_map_size, |
| 717 | size_t inline_info_size, |
| 718 | size_t register_mask_max, |
| 719 | size_t stack_mask_bit_size) { |
| 720 | size_t bit_offset = 0; |
| 721 | DCHECK_EQ(kNativePcBitOffset, bit_offset); |
| 722 | bit_offset += MinimumBitsToStore(native_pc_max); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 723 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 724 | dex_pc_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset); |
| 725 | bit_offset += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max); |
| 726 | |
| 727 | // We also need +1 for kNoDexRegisterMap, but since the size is strictly |
| 728 | // greater than any offset we might try to encode, we already implicitly have it. |
| 729 | dex_register_map_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset); |
| 730 | bit_offset += MinimumBitsToStore(dex_register_map_size); |
| 731 | |
| 732 | // We also need +1 for kNoInlineInfo, but since the inline_info_size is strictly |
| 733 | // greater than the offset we might try to encode, we already implicitly have it. |
| 734 | // If inline_info_size is zero, we can encode only kNoInlineInfo (in zero bits). |
| 735 | inline_info_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset); |
| 736 | if (inline_info_size != 0) { |
| 737 | bit_offset += MinimumBitsToStore(dex_register_map_size + inline_info_size); |
| 738 | } |
| 739 | |
| 740 | register_mask_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset); |
| 741 | bit_offset += MinimumBitsToStore(register_mask_max); |
| 742 | |
| 743 | stack_mask_bit_offset_ = dchecked_integral_cast<uint8_t>(bit_offset); |
| 744 | bit_offset += stack_mask_bit_size; |
| 745 | |
| 746 | return RoundUp(bit_offset, kBitsPerByte) / kBitsPerByte; |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 747 | } |
| 748 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 749 | ALWAYS_INLINE FieldEncoding GetNativePcEncoding() const { |
| 750 | return FieldEncoding(kNativePcBitOffset, dex_pc_bit_offset_); |
| 751 | } |
| 752 | ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const { |
| 753 | return FieldEncoding(dex_pc_bit_offset_, dex_register_map_bit_offset_, -1 /* min_value */); |
| 754 | } |
| 755 | ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const { |
| 756 | return FieldEncoding(dex_register_map_bit_offset_, inline_info_bit_offset_, -1 /* min_value */); |
| 757 | } |
| 758 | ALWAYS_INLINE FieldEncoding GetInlineInfoEncoding() const { |
| 759 | return FieldEncoding(inline_info_bit_offset_, register_mask_bit_offset_, -1 /* min_value */); |
| 760 | } |
| 761 | ALWAYS_INLINE FieldEncoding GetRegisterMaskEncoding() const { |
| 762 | return FieldEncoding(register_mask_bit_offset_, stack_mask_bit_offset_); |
| 763 | } |
| 764 | ALWAYS_INLINE size_t GetStackMaskBitOffset() const { |
| 765 | // The end offset is not encoded. It is implicitly the end of stack map entry. |
| 766 | return stack_mask_bit_offset_; |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 767 | } |
| 768 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 769 | void Dump(VariableIndentationOutputStream* vios) const; |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 770 | |
| 771 | private: |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 772 | static constexpr size_t kNativePcBitOffset = 0; |
| 773 | uint8_t dex_pc_bit_offset_; |
| 774 | uint8_t dex_register_map_bit_offset_; |
| 775 | uint8_t inline_info_bit_offset_; |
| 776 | uint8_t register_mask_bit_offset_; |
| 777 | uint8_t stack_mask_bit_offset_; |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 778 | }; |
| 779 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 780 | /** |
| 781 | * A Stack Map holds compilation information for a specific PC necessary for: |
| 782 | * - Mapping it to a dex PC, |
| 783 | * - Knowing which stack entries are objects, |
| 784 | * - Knowing which registers hold objects, |
| 785 | * - Knowing the inlining information, |
| 786 | * - Knowing the values of dex registers. |
| 787 | * |
| 788 | * The information is of the form: |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 789 | * |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 790 | * [native_pc_offset, dex_pc, dex_register_map_offset, inlining_info_offset, register_mask, |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 791 | * stack_mask]. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 792 | */ |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 793 | class StackMap { |
| 794 | public: |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 795 | StackMap() {} |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 796 | explicit StackMap(MemoryRegion region) : region_(region) {} |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 797 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 798 | ALWAYS_INLINE bool IsValid() const { return region_.pointer() != nullptr; } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 799 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 800 | ALWAYS_INLINE uint32_t GetDexPc(const StackMapEncoding& encoding) const { |
| 801 | return encoding.GetDexPcEncoding().Load(region_); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 802 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 803 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 804 | ALWAYS_INLINE void SetDexPc(const StackMapEncoding& encoding, uint32_t dex_pc) { |
| 805 | encoding.GetDexPcEncoding().Store(region_, dex_pc); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 806 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 807 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 808 | ALWAYS_INLINE uint32_t GetNativePcOffset(const StackMapEncoding& encoding) const { |
| 809 | return encoding.GetNativePcEncoding().Load(region_); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 810 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 811 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 812 | ALWAYS_INLINE void SetNativePcOffset(const StackMapEncoding& encoding, uint32_t native_pc_offset) { |
| 813 | encoding.GetNativePcEncoding().Store(region_, native_pc_offset); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 814 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 815 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 816 | ALWAYS_INLINE uint32_t GetDexRegisterMapOffset(const StackMapEncoding& encoding) const { |
| 817 | return encoding.GetDexRegisterMapEncoding().Load(region_); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 818 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 819 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 820 | ALWAYS_INLINE void SetDexRegisterMapOffset(const StackMapEncoding& encoding, uint32_t offset) { |
| 821 | encoding.GetDexRegisterMapEncoding().Store(region_, offset); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 822 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 823 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 824 | ALWAYS_INLINE uint32_t GetInlineDescriptorOffset(const StackMapEncoding& encoding) const { |
| 825 | return encoding.GetInlineInfoEncoding().Load(region_); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 826 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 827 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 828 | ALWAYS_INLINE void SetInlineDescriptorOffset(const StackMapEncoding& encoding, uint32_t offset) { |
| 829 | encoding.GetInlineInfoEncoding().Store(region_, offset); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 830 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 831 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 832 | ALWAYS_INLINE uint32_t GetRegisterMask(const StackMapEncoding& encoding) const { |
| 833 | return encoding.GetRegisterMaskEncoding().Load(region_); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 834 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 835 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 836 | ALWAYS_INLINE void SetRegisterMask(const StackMapEncoding& encoding, uint32_t mask) { |
| 837 | encoding.GetRegisterMaskEncoding().Store(region_, mask); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 838 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 839 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 840 | ALWAYS_INLINE size_t GetNumberOfStackMaskBits(const StackMapEncoding& encoding) const { |
| 841 | return region_.size_in_bits() - encoding.GetStackMaskBitOffset(); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 842 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 843 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 844 | ALWAYS_INLINE bool GetStackMaskBit(const StackMapEncoding& encoding, size_t index) const { |
| 845 | return region_.LoadBit(encoding.GetStackMaskBitOffset() + index); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 846 | } |
| 847 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 848 | ALWAYS_INLINE void SetStackMaskBit(const StackMapEncoding& encoding, size_t index, bool value) { |
| 849 | region_.StoreBit(encoding.GetStackMaskBitOffset() + index, value); |
| 850 | } |
| 851 | |
| 852 | ALWAYS_INLINE bool HasDexRegisterMap(const StackMapEncoding& encoding) const { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 853 | return GetDexRegisterMapOffset(encoding) != kNoDexRegisterMap; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 854 | } |
| 855 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 856 | ALWAYS_INLINE bool HasInlineInfo(const StackMapEncoding& encoding) const { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 857 | return GetInlineDescriptorOffset(encoding) != kNoInlineInfo; |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 858 | } |
| 859 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 860 | ALWAYS_INLINE bool Equals(const StackMap& other) const { |
| 861 | return region_.pointer() == other.region_.pointer() && region_.size() == other.region_.size(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 862 | } |
| 863 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 864 | void Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 865 | const CodeInfo& code_info, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 866 | const CodeInfoEncoding& encoding, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 867 | uint32_t code_offset, |
| 868 | uint16_t number_of_dex_registers, |
| 869 | const std::string& header_suffix = "") const; |
| 870 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 871 | // Special (invalid) offset for the DexRegisterMapOffset field meaning |
| 872 | // that there is no Dex register map for this stack map. |
| 873 | static constexpr uint32_t kNoDexRegisterMap = -1; |
| 874 | |
| 875 | // Special (invalid) offset for the InlineDescriptorOffset field meaning |
| 876 | // that there is no inline info for this stack map. |
| 877 | static constexpr uint32_t kNoInlineInfo = -1; |
| 878 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 879 | private: |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 880 | static constexpr int kFixedSize = 0; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 881 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 882 | MemoryRegion region_; |
| 883 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 884 | friend class StackMapStream; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 885 | }; |
| 886 | |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 887 | class InlineInfoEncoding { |
| 888 | public: |
| 889 | void SetFromSizes(size_t method_index_max, |
| 890 | size_t dex_pc_max, |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 891 | size_t extra_data_max, |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 892 | size_t dex_register_map_size) { |
| 893 | total_bit_size_ = kMethodIndexBitOffset; |
| 894 | total_bit_size_ += MinimumBitsToStore(method_index_max); |
| 895 | |
| 896 | dex_pc_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_); |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 897 | // Note: We're not encoding the dex pc if there is none. That's the case |
| 898 | // for an intrinsified native method, such as String.charAt(). |
| 899 | if (dex_pc_max != DexFile::kDexNoIndex) { |
| 900 | total_bit_size_ += MinimumBitsToStore(1 /* kNoDexPc */ + dex_pc_max); |
| 901 | } |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 902 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 903 | extra_data_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_); |
| 904 | total_bit_size_ += MinimumBitsToStore(extra_data_max); |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 905 | |
| 906 | // We also need +1 for kNoDexRegisterMap, but since the size is strictly |
| 907 | // greater than any offset we might try to encode, we already implicitly have it. |
| 908 | dex_register_map_bit_offset_ = dchecked_integral_cast<uint8_t>(total_bit_size_); |
| 909 | total_bit_size_ += MinimumBitsToStore(dex_register_map_size); |
| 910 | } |
| 911 | |
| 912 | ALWAYS_INLINE FieldEncoding GetMethodIndexEncoding() const { |
| 913 | return FieldEncoding(kMethodIndexBitOffset, dex_pc_bit_offset_); |
| 914 | } |
| 915 | ALWAYS_INLINE FieldEncoding GetDexPcEncoding() const { |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 916 | return FieldEncoding(dex_pc_bit_offset_, extra_data_bit_offset_, -1 /* min_value */); |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 917 | } |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 918 | ALWAYS_INLINE FieldEncoding GetExtraDataEncoding() const { |
| 919 | return FieldEncoding(extra_data_bit_offset_, dex_register_map_bit_offset_); |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 920 | } |
| 921 | ALWAYS_INLINE FieldEncoding GetDexRegisterMapEncoding() const { |
| 922 | return FieldEncoding(dex_register_map_bit_offset_, total_bit_size_, -1 /* min_value */); |
| 923 | } |
| 924 | ALWAYS_INLINE size_t GetEntrySize() const { |
| 925 | return RoundUp(total_bit_size_, kBitsPerByte) / kBitsPerByte; |
| 926 | } |
| 927 | |
| 928 | void Dump(VariableIndentationOutputStream* vios) const; |
| 929 | |
| 930 | private: |
| 931 | static constexpr uint8_t kIsLastBitOffset = 0; |
| 932 | static constexpr uint8_t kMethodIndexBitOffset = 1; |
| 933 | uint8_t dex_pc_bit_offset_; |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 934 | uint8_t extra_data_bit_offset_; |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 935 | uint8_t dex_register_map_bit_offset_; |
| 936 | uint8_t total_bit_size_; |
| 937 | }; |
| 938 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 939 | /** |
| 940 | * Inline information for a specific PC. The information is of the form: |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 941 | * |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 942 | * [is_last, |
| 943 | * method_index (or ArtMethod high bits), |
| 944 | * dex_pc, |
| 945 | * extra_data (ArtMethod low bits or 1), |
| 946 | * dex_register_map_offset]+. |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 947 | */ |
| 948 | class InlineInfo { |
| 949 | public: |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 950 | explicit InlineInfo(MemoryRegion region) : region_(region) { |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 951 | } |
| 952 | |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 953 | ALWAYS_INLINE uint32_t GetDepth(const InlineInfoEncoding& encoding) const { |
| 954 | size_t depth = 0; |
| 955 | while (!GetRegionAtDepth(encoding, depth++).LoadBit(0)) { } // Check is_last bit. |
| 956 | return depth; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 957 | } |
| 958 | |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 959 | ALWAYS_INLINE void SetDepth(const InlineInfoEncoding& encoding, uint32_t depth) { |
| 960 | DCHECK_GT(depth, 0u); |
| 961 | for (size_t d = 0; d < depth; ++d) { |
| 962 | GetRegionAtDepth(encoding, d).StoreBit(0, d == depth - 1); // Set is_last bit. |
| 963 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 964 | } |
| 965 | |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 966 | ALWAYS_INLINE uint32_t GetMethodIndexAtDepth(const InlineInfoEncoding& encoding, |
| 967 | uint32_t depth) const { |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 968 | DCHECK(!EncodesArtMethodAtDepth(encoding, depth)); |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 969 | return encoding.GetMethodIndexEncoding().Load(GetRegionAtDepth(encoding, depth)); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 970 | } |
| 971 | |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 972 | ALWAYS_INLINE void SetMethodIndexAtDepth(const InlineInfoEncoding& encoding, |
| 973 | uint32_t depth, |
| 974 | uint32_t index) { |
| 975 | encoding.GetMethodIndexEncoding().Store(GetRegionAtDepth(encoding, depth), index); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 976 | } |
| 977 | |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 978 | ALWAYS_INLINE uint32_t GetDexPcAtDepth(const InlineInfoEncoding& encoding, |
| 979 | uint32_t depth) const { |
| 980 | return encoding.GetDexPcEncoding().Load(GetRegionAtDepth(encoding, depth)); |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 981 | } |
| 982 | |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 983 | ALWAYS_INLINE void SetDexPcAtDepth(const InlineInfoEncoding& encoding, |
| 984 | uint32_t depth, |
| 985 | uint32_t dex_pc) { |
| 986 | encoding.GetDexPcEncoding().Store(GetRegionAtDepth(encoding, depth), dex_pc); |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 987 | } |
| 988 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 989 | ALWAYS_INLINE bool EncodesArtMethodAtDepth(const InlineInfoEncoding& encoding, |
| 990 | uint32_t depth) const { |
| 991 | return (encoding.GetExtraDataEncoding().Load(GetRegionAtDepth(encoding, depth)) & 1) == 0; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 992 | } |
| 993 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 994 | ALWAYS_INLINE void SetExtraDataAtDepth(const InlineInfoEncoding& encoding, |
| 995 | uint32_t depth, |
| 996 | uint32_t extra_data) { |
| 997 | encoding.GetExtraDataEncoding().Store(GetRegionAtDepth(encoding, depth), extra_data); |
| 998 | } |
| 999 | |
| 1000 | ALWAYS_INLINE ArtMethod* GetArtMethodAtDepth(const InlineInfoEncoding& encoding, |
| 1001 | uint32_t depth) const { |
| 1002 | uint32_t low_bits = encoding.GetExtraDataEncoding().Load(GetRegionAtDepth(encoding, depth)); |
| 1003 | uint32_t high_bits = encoding.GetMethodIndexEncoding().Load(GetRegionAtDepth(encoding, depth)); |
| 1004 | if (high_bits == 0) { |
| 1005 | return reinterpret_cast<ArtMethod*>(low_bits); |
| 1006 | } else { |
| 1007 | uint64_t address = high_bits; |
| 1008 | address = address << 32; |
| 1009 | return reinterpret_cast<ArtMethod*>(address | low_bits); |
| 1010 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1011 | } |
| 1012 | |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 1013 | ALWAYS_INLINE uint32_t GetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding, |
| 1014 | uint32_t depth) const { |
| 1015 | return encoding.GetDexRegisterMapEncoding().Load(GetRegionAtDepth(encoding, depth)); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1016 | } |
| 1017 | |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 1018 | ALWAYS_INLINE void SetDexRegisterMapOffsetAtDepth(const InlineInfoEncoding& encoding, |
| 1019 | uint32_t depth, |
| 1020 | uint32_t offset) { |
| 1021 | encoding.GetDexRegisterMapEncoding().Store(GetRegionAtDepth(encoding, depth), offset); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1022 | } |
| 1023 | |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 1024 | ALWAYS_INLINE bool HasDexRegisterMapAtDepth(const InlineInfoEncoding& encoding, |
| 1025 | uint32_t depth) const { |
| 1026 | return GetDexRegisterMapOffsetAtDepth(encoding, depth) != StackMap::kNoDexRegisterMap; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1027 | } |
| 1028 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1029 | void Dump(VariableIndentationOutputStream* vios, |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 1030 | const CodeInfo& info, |
| 1031 | uint16_t* number_of_dex_registers) const; |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 1032 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1033 | private: |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 1034 | ALWAYS_INLINE MemoryRegion GetRegionAtDepth(const InlineInfoEncoding& encoding, |
| 1035 | uint32_t depth) const { |
| 1036 | size_t entry_size = encoding.GetEntrySize(); |
| 1037 | DCHECK_GT(entry_size, 0u); |
| 1038 | return region_.Subregion(depth * entry_size, entry_size); |
| 1039 | } |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1040 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1041 | MemoryRegion region_; |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1042 | }; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1043 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1044 | // Most of the fields are encoded as ULEB128 to save space. |
| 1045 | struct CodeInfoEncoding { |
| 1046 | uint32_t non_header_size; |
| 1047 | uint32_t number_of_stack_maps; |
| 1048 | uint32_t stack_map_size_in_bytes; |
| 1049 | uint32_t number_of_location_catalog_entries; |
| 1050 | StackMapEncoding stack_map_encoding; |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 1051 | InlineInfoEncoding inline_info_encoding; |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1052 | uint8_t header_size; |
| 1053 | |
| 1054 | CodeInfoEncoding() { } |
| 1055 | |
| 1056 | explicit CodeInfoEncoding(const void* data) { |
| 1057 | const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data); |
| 1058 | non_header_size = DecodeUnsignedLeb128(&ptr); |
| 1059 | number_of_stack_maps = DecodeUnsignedLeb128(&ptr); |
| 1060 | stack_map_size_in_bytes = DecodeUnsignedLeb128(&ptr); |
| 1061 | number_of_location_catalog_entries = DecodeUnsignedLeb128(&ptr); |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 1062 | static_assert(alignof(StackMapEncoding) == 1, |
| 1063 | "StackMapEncoding should not require alignment"); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1064 | stack_map_encoding = *reinterpret_cast<const StackMapEncoding*>(ptr); |
| 1065 | ptr += sizeof(StackMapEncoding); |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 1066 | if (stack_map_encoding.GetInlineInfoEncoding().BitSize() > 0) { |
| 1067 | static_assert(alignof(InlineInfoEncoding) == 1, |
| 1068 | "InlineInfoEncoding should not require alignment"); |
| 1069 | inline_info_encoding = *reinterpret_cast<const InlineInfoEncoding*>(ptr); |
| 1070 | ptr += sizeof(InlineInfoEncoding); |
| 1071 | } else { |
Roland Levillain | 7cbd27f | 2016-08-11 23:53:33 +0100 | [diff] [blame] | 1072 | inline_info_encoding = InlineInfoEncoding{}; // NOLINT. |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 1073 | } |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1074 | header_size = dchecked_integral_cast<uint8_t>(ptr - reinterpret_cast<const uint8_t*>(data)); |
| 1075 | } |
| 1076 | |
| 1077 | template<typename Vector> |
| 1078 | void Compress(Vector* dest) const { |
| 1079 | EncodeUnsignedLeb128(dest, non_header_size); |
| 1080 | EncodeUnsignedLeb128(dest, number_of_stack_maps); |
| 1081 | EncodeUnsignedLeb128(dest, stack_map_size_in_bytes); |
| 1082 | EncodeUnsignedLeb128(dest, number_of_location_catalog_entries); |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 1083 | const uint8_t* stack_map_ptr = reinterpret_cast<const uint8_t*>(&stack_map_encoding); |
| 1084 | dest->insert(dest->end(), stack_map_ptr, stack_map_ptr + sizeof(StackMapEncoding)); |
| 1085 | if (stack_map_encoding.GetInlineInfoEncoding().BitSize() > 0) { |
| 1086 | const uint8_t* inline_info_ptr = reinterpret_cast<const uint8_t*>(&inline_info_encoding); |
| 1087 | dest->insert(dest->end(), inline_info_ptr, inline_info_ptr + sizeof(InlineInfoEncoding)); |
| 1088 | } |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1089 | } |
| 1090 | }; |
| 1091 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1092 | /** |
| 1093 | * Wrapper around all compiler information collected for a method. |
| 1094 | * The information is of the form: |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 1095 | * |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1096 | * [CodeInfoEncoding, StackMap+, DexRegisterLocationCatalog+, DexRegisterMap+, InlineInfo*] |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 1097 | * |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1098 | * where CodeInfoEncoding is of the form: |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 1099 | * |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1100 | * [non_header_size, number_of_stack_maps, stack_map_size_in_bytes, |
| 1101 | * number_of_location_catalog_entries, StackMapEncoding] |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1102 | */ |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1103 | class CodeInfo { |
| 1104 | public: |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1105 | explicit CodeInfo(MemoryRegion region) : region_(region) { |
| 1106 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1107 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1108 | explicit CodeInfo(const void* data) { |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1109 | CodeInfoEncoding encoding = CodeInfoEncoding(data); |
| 1110 | region_ = MemoryRegion(const_cast<void*>(data), |
| 1111 | encoding.header_size + encoding.non_header_size); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1112 | } |
| 1113 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1114 | CodeInfoEncoding ExtractEncoding() const { |
Mathieu Chartier | 01c7814 | 2017-01-05 10:17:55 -0800 | [diff] [blame] | 1115 | CodeInfoEncoding encoding(region_.start()); |
| 1116 | AssertValidStackMap(encoding); |
| 1117 | return encoding; |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 1118 | } |
| 1119 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1120 | bool HasInlineInfo(const CodeInfoEncoding& encoding) const { |
| 1121 | return encoding.stack_map_encoding.GetInlineInfoEncoding().BitSize() > 0; |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 1122 | } |
| 1123 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1124 | DexRegisterLocationCatalog GetDexRegisterLocationCatalog(const CodeInfoEncoding& encoding) const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1125 | return DexRegisterLocationCatalog(region_.Subregion( |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 1126 | GetDexRegisterLocationCatalogOffset(encoding), |
| 1127 | GetDexRegisterLocationCatalogSize(encoding))); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Mingyao Yang | ccfa885 | 2017-01-18 14:51:59 -0800 | [diff] [blame^] | 1130 | ALWAYS_INLINE StackMap GetStackMapAt(size_t i, const CodeInfoEncoding& encoding) const { |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1131 | size_t stack_map_size = encoding.stack_map_size_in_bytes; |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 1132 | return StackMap(GetStackMaps(encoding).Subregion(i * stack_map_size, stack_map_size)); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1133 | } |
| 1134 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1135 | uint32_t GetNumberOfLocationCatalogEntries(const CodeInfoEncoding& encoding) const { |
| 1136 | return encoding.number_of_location_catalog_entries; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1137 | } |
| 1138 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1139 | uint32_t GetDexRegisterLocationCatalogSize(const CodeInfoEncoding& encoding) const { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 1140 | return ComputeDexRegisterLocationCatalogSize(GetDexRegisterLocationCatalogOffset(encoding), |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1141 | GetNumberOfLocationCatalogEntries(encoding)); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1144 | uint32_t GetNumberOfStackMaps(const CodeInfoEncoding& encoding) const { |
| 1145 | return encoding.number_of_stack_maps; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 1148 | // Get the size of all the stack maps of this CodeInfo object, in bytes. |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1149 | size_t GetStackMapsSize(const CodeInfoEncoding& encoding) const { |
| 1150 | return encoding.stack_map_size_in_bytes * GetNumberOfStackMaps(encoding); |
Roland Levillain | 29ba1b0 | 2015-03-13 11:45:07 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1153 | uint32_t GetDexRegisterLocationCatalogOffset(const CodeInfoEncoding& encoding) const { |
| 1154 | return GetStackMapsOffset(encoding) + GetStackMapsSize(encoding); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1157 | size_t GetDexRegisterMapsOffset(const CodeInfoEncoding& encoding) const { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 1158 | return GetDexRegisterLocationCatalogOffset(encoding) |
| 1159 | + GetDexRegisterLocationCatalogSize(encoding); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1162 | uint32_t GetStackMapsOffset(const CodeInfoEncoding& encoding) const { |
| 1163 | return encoding.header_size; |
Nicolas Geoffray | 6530baf | 2015-05-26 15:22:58 +0100 | [diff] [blame] | 1164 | } |
| 1165 | |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 1166 | DexRegisterMap GetDexRegisterMapOf(StackMap stack_map, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1167 | const CodeInfoEncoding& encoding, |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 1168 | uint32_t number_of_dex_registers) const { |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1169 | if (!stack_map.HasDexRegisterMap(encoding.stack_map_encoding)) { |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 1170 | return DexRegisterMap(); |
| 1171 | } else { |
| 1172 | uint32_t offset = GetDexRegisterMapsOffset(encoding) |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1173 | + stack_map.GetDexRegisterMapOffset(encoding.stack_map_encoding); |
| 1174 | size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers); |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 1175 | return DexRegisterMap(region_.Subregion(offset, size)); |
| 1176 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1177 | } |
| 1178 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1179 | // Return the `DexRegisterMap` pointed by `inline_info` at depth `depth`. |
| 1180 | DexRegisterMap GetDexRegisterMapAtDepth(uint8_t depth, |
| 1181 | InlineInfo inline_info, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1182 | const CodeInfoEncoding& encoding, |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1183 | uint32_t number_of_dex_registers) const { |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 1184 | if (!inline_info.HasDexRegisterMapAtDepth(encoding.inline_info_encoding, depth)) { |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 1185 | return DexRegisterMap(); |
| 1186 | } else { |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 1187 | uint32_t offset = GetDexRegisterMapsOffset(encoding) + |
| 1188 | inline_info.GetDexRegisterMapOffsetAtDepth(encoding.inline_info_encoding, depth); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1189 | size_t size = ComputeDexRegisterMapSizeOf(encoding, offset, number_of_dex_registers); |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 1190 | return DexRegisterMap(region_.Subregion(offset, size)); |
| 1191 | } |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 1192 | } |
| 1193 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1194 | InlineInfo GetInlineInfoOf(StackMap stack_map, const CodeInfoEncoding& encoding) const { |
| 1195 | DCHECK(stack_map.HasInlineInfo(encoding.stack_map_encoding)); |
| 1196 | uint32_t offset = stack_map.GetInlineDescriptorOffset(encoding.stack_map_encoding) |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 1197 | + GetDexRegisterMapsOffset(encoding); |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 1198 | return InlineInfo(region_.Subregion(offset, region_.size() - offset)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1199 | } |
| 1200 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1201 | StackMap GetStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const { |
| 1202 | for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 1203 | StackMap stack_map = GetStackMapAt(i, encoding); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1204 | if (stack_map.GetDexPc(encoding.stack_map_encoding) == dex_pc) { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1205 | return stack_map; |
| 1206 | } |
| 1207 | } |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 1208 | return StackMap(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1209 | } |
| 1210 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 1211 | // Searches the stack map list backwards because catch stack maps are stored |
| 1212 | // at the end. |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1213 | StackMap GetCatchStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const { |
| 1214 | for (size_t i = GetNumberOfStackMaps(encoding); i > 0; --i) { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 1215 | StackMap stack_map = GetStackMapAt(i - 1, encoding); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1216 | if (stack_map.GetDexPc(encoding.stack_map_encoding) == dex_pc) { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 1217 | return stack_map; |
| 1218 | } |
| 1219 | } |
| 1220 | return StackMap(); |
| 1221 | } |
| 1222 | |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1223 | StackMap GetOsrStackMapForDexPc(uint32_t dex_pc, const CodeInfoEncoding& encoding) const { |
| 1224 | size_t e = GetNumberOfStackMaps(encoding); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1225 | if (e == 0) { |
| 1226 | // There cannot be OSR stack map if there is no stack map. |
| 1227 | return StackMap(); |
| 1228 | } |
| 1229 | // Walk over all stack maps. If two consecutive stack maps are identical, then we |
| 1230 | // have found a stack map suitable for OSR. |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1231 | const StackMapEncoding& stack_map_encoding = encoding.stack_map_encoding; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1232 | for (size_t i = 0; i < e - 1; ++i) { |
| 1233 | StackMap stack_map = GetStackMapAt(i, encoding); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1234 | if (stack_map.GetDexPc(stack_map_encoding) == dex_pc) { |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1235 | StackMap other = GetStackMapAt(i + 1, encoding); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1236 | if (other.GetDexPc(stack_map_encoding) == dex_pc && |
| 1237 | other.GetNativePcOffset(stack_map_encoding) == |
| 1238 | stack_map.GetNativePcOffset(stack_map_encoding)) { |
| 1239 | DCHECK_EQ(other.GetDexRegisterMapOffset(stack_map_encoding), |
| 1240 | stack_map.GetDexRegisterMapOffset(stack_map_encoding)); |
| 1241 | DCHECK(!stack_map.HasInlineInfo(stack_map_encoding)); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1242 | if (i < e - 2) { |
| 1243 | // Make sure there are not three identical stack maps following each other. |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1244 | DCHECK_NE(stack_map.GetNativePcOffset(stack_map_encoding), |
| 1245 | GetStackMapAt(i + 2, encoding).GetNativePcOffset(stack_map_encoding)); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1246 | } |
| 1247 | return stack_map; |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | return StackMap(); |
| 1252 | } |
| 1253 | |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 1254 | StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset, |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1255 | const CodeInfoEncoding& encoding) const { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 1256 | // TODO: Safepoint stack maps are sorted by native_pc_offset but catch stack |
| 1257 | // maps are not. If we knew that the method does not have try/catch, |
| 1258 | // we could do binary search. |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1259 | for (size_t i = 0, e = GetNumberOfStackMaps(encoding); i < e; ++i) { |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 1260 | StackMap stack_map = GetStackMapAt(i, encoding); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1261 | if (stack_map.GetNativePcOffset(encoding.stack_map_encoding) == native_pc_offset) { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1262 | return stack_map; |
| 1263 | } |
| 1264 | } |
Nicolas Geoffray | e12997f | 2015-05-22 14:01:33 +0100 | [diff] [blame] | 1265 | return StackMap(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1266 | } |
| 1267 | |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1268 | // Dump this CodeInfo object on `os`. `code_offset` is the (absolute) |
| 1269 | // native PC of the compiled method and `number_of_dex_registers` the |
| 1270 | // number of Dex virtual registers used in this method. If |
| 1271 | // `dump_stack_maps` is true, also dump the stack maps and the |
| 1272 | // associated Dex register maps. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1273 | void Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1274 | uint32_t code_offset, |
| 1275 | uint16_t number_of_dex_registers, |
| 1276 | bool dump_stack_maps) const; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 1277 | |
Mathieu Chartier | 01c7814 | 2017-01-05 10:17:55 -0800 | [diff] [blame] | 1278 | // Check that the code info has valid stack map and abort if it does not. |
| 1279 | void AssertValidStackMap(const CodeInfoEncoding& encoding) const { |
| 1280 | if (region_.size() != 0 && region_.size() < GetStackMapsSize(encoding)) { |
| 1281 | LOG(FATAL) << region_.size() << "\n" |
| 1282 | << encoding.header_size << "\n" |
| 1283 | << encoding.non_header_size << "\n" |
| 1284 | << encoding.number_of_location_catalog_entries << "\n" |
| 1285 | << encoding.number_of_stack_maps << "\n" |
| 1286 | << encoding.stack_map_size_in_bytes; |
| 1287 | } |
| 1288 | } |
| 1289 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1290 | private: |
Mingyao Yang | ccfa885 | 2017-01-18 14:51:59 -0800 | [diff] [blame^] | 1291 | ALWAYS_INLINE MemoryRegion GetStackMaps(const CodeInfoEncoding& encoding) const { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1292 | return region_.size() == 0 |
| 1293 | ? MemoryRegion() |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1294 | : region_.Subregion(GetStackMapsOffset(encoding), GetStackMapsSize(encoding)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1295 | } |
| 1296 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1297 | // Compute the size of the Dex register map associated to the stack map at |
| 1298 | // `dex_register_map_offset_in_code_info`. |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1299 | size_t ComputeDexRegisterMapSizeOf(const CodeInfoEncoding& encoding, |
| 1300 | uint32_t dex_register_map_offset_in_code_info, |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1301 | uint16_t number_of_dex_registers) const { |
| 1302 | // Offset where the actual mapping data starts within art::DexRegisterMap. |
| 1303 | size_t location_mapping_data_offset_in_dex_register_map = |
| 1304 | DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers); |
| 1305 | // Create a temporary art::DexRegisterMap to be able to call |
| 1306 | // art::DexRegisterMap::GetNumberOfLiveDexRegisters and |
| 1307 | DexRegisterMap dex_register_map_without_locations( |
| 1308 | MemoryRegion(region_.Subregion(dex_register_map_offset_in_code_info, |
| 1309 | location_mapping_data_offset_in_dex_register_map))); |
| 1310 | size_t number_of_live_dex_registers = |
| 1311 | dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers); |
| 1312 | size_t location_mapping_data_size_in_bits = |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 1313 | DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries(encoding)) |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1314 | * number_of_live_dex_registers; |
| 1315 | size_t location_mapping_data_size_in_bytes = |
| 1316 | RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte; |
| 1317 | size_t dex_register_map_size = |
| 1318 | location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes; |
| 1319 | return dex_register_map_size; |
| 1320 | } |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 1321 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1322 | // Compute the size of a Dex register location catalog starting at offset `origin` |
| 1323 | // in `region_` and containing `number_of_dex_locations` entries. |
| 1324 | size_t ComputeDexRegisterLocationCatalogSize(uint32_t origin, |
| 1325 | uint32_t number_of_dex_locations) const { |
| 1326 | // TODO: Ideally, we would like to use art::DexRegisterLocationCatalog::Size or |
| 1327 | // art::DexRegisterLocationCatalog::FindLocationOffset, but the |
| 1328 | // DexRegisterLocationCatalog is not yet built. Try to factor common code. |
| 1329 | size_t offset = origin + DexRegisterLocationCatalog::kFixedSize; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 1330 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1331 | // Skip the first `number_of_dex_locations - 1` entries. |
| 1332 | for (uint16_t i = 0; i < number_of_dex_locations; ++i) { |
| 1333 | // Read the first next byte and inspect its first 3 bits to decide |
| 1334 | // whether it is a short or a large location. |
| 1335 | DexRegisterLocationCatalog::ShortLocation first_byte = |
| 1336 | region_.LoadUnaligned<DexRegisterLocationCatalog::ShortLocation>(offset); |
| 1337 | DexRegisterLocation::Kind kind = |
| 1338 | DexRegisterLocationCatalog::ExtractKindFromShortLocation(first_byte); |
| 1339 | if (DexRegisterLocation::IsShortLocationKind(kind)) { |
| 1340 | // Short location. Skip the current byte. |
| 1341 | offset += DexRegisterLocationCatalog::SingleShortEntrySize(); |
| 1342 | } else { |
| 1343 | // Large location. Skip the 5 next bytes. |
| 1344 | offset += DexRegisterLocationCatalog::SingleLargeEntrySize(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 1345 | } |
| 1346 | } |
| 1347 | size_t size = offset - origin; |
| 1348 | return size; |
| 1349 | } |
| 1350 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1351 | MemoryRegion region_; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1352 | friend class StackMapStream; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1353 | }; |
| 1354 | |
Roland Levillain | 1c1da43 | 2015-07-16 11:54:44 +0100 | [diff] [blame] | 1355 | #undef ELEMENT_BYTE_OFFSET_AFTER |
| 1356 | #undef ELEMENT_BIT_OFFSET_AFTER |
| 1357 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1358 | } // namespace art |
| 1359 | |
| 1360 | #endif // ART_RUNTIME_STACK_MAP_H_ |