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