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