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