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" |
| 21 | #include "memory_region.h" |
Nicolas Geoffray | 376b2bb | 2014-12-09 14:26:32 +0000 | [diff] [blame] | 22 | #include "utils.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 26 | // Size of a frame slot, in bytes. This constant is a signed value, |
| 27 | // to please the compiler in arithmetic operations involving int32_t |
| 28 | // (signed) values. |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 29 | static constexpr ssize_t kFrameSlotSize = 4; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 30 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 31 | // Size of Dex virtual registers. |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 32 | static constexpr size_t kVRegSize = 4; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 33 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 34 | class CodeInfo; |
| 35 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 36 | /** |
| 37 | * Classes in the following file are wrapper on stack map information backed |
| 38 | * by a MemoryRegion. As such they read and write to the region, they don't have |
| 39 | * their own fields. |
| 40 | */ |
| 41 | |
| 42 | /** |
| 43 | * Inline information for a specific PC. The information is of the form: |
| 44 | * [inlining_depth, [method_dex reference]+] |
| 45 | */ |
| 46 | class InlineInfo { |
| 47 | public: |
| 48 | explicit InlineInfo(MemoryRegion region) : region_(region) {} |
| 49 | |
| 50 | uint8_t GetDepth() const { |
Roland Levillain | ede7bf8 | 2015-03-13 12:23:04 +0000 | [diff] [blame] | 51 | return region_.LoadUnaligned<uint8_t>(kDepthOffset); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void SetDepth(uint8_t depth) { |
Roland Levillain | ede7bf8 | 2015-03-13 12:23:04 +0000 | [diff] [blame] | 55 | region_.StoreUnaligned<uint8_t>(kDepthOffset, depth); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | uint32_t GetMethodReferenceIndexAtDepth(uint8_t depth) const { |
Roland Levillain | ede7bf8 | 2015-03-13 12:23:04 +0000 | [diff] [blame] | 59 | return region_.LoadUnaligned<uint32_t>(kFixedSize + depth * SingleEntrySize()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void SetMethodReferenceIndexAtDepth(uint8_t depth, uint32_t index) { |
Roland Levillain | ede7bf8 | 2015-03-13 12:23:04 +0000 | [diff] [blame] | 63 | region_.StoreUnaligned<uint32_t>(kFixedSize + depth * SingleEntrySize(), index); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static size_t SingleEntrySize() { |
| 67 | return sizeof(uint32_t); |
| 68 | } |
| 69 | |
| 70 | private: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 71 | // TODO: Instead of plain types such as "uint8_t", introduce |
| 72 | // typedefs (and document the memory layout of InlineInfo). |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 73 | static constexpr int kDepthOffset = 0; |
| 74 | static constexpr int kFixedSize = kDepthOffset + sizeof(uint8_t); |
| 75 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 76 | MemoryRegion region_; |
| 77 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 78 | friend class CodeInfo; |
| 79 | friend class StackMap; |
| 80 | friend class StackMapStream; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 81 | }; |
| 82 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 83 | // Dex register location container used by DexRegisterMap and StackMapStream. |
| 84 | class DexRegisterLocation { |
| 85 | public: |
| 86 | /* |
| 87 | * The location kind used to populate the Dex register information in a |
| 88 | * StackMapStream can either be: |
| 89 | * - kNone: the register has no location yet, meaning it has not been set; |
| 90 | * - kConstant: value holds the constant; |
| 91 | * - kStack: value holds the stack offset; |
| 92 | * - kRegister: value holds the physical register number; |
| 93 | * - kFpuRegister: value holds the physical register number. |
| 94 | * |
| 95 | * In addition, DexRegisterMap also uses these values: |
| 96 | * - kInStackLargeOffset: value holds a "large" stack offset (greater than |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 97 | * or equal to 128 bytes); |
| 98 | * - kConstantLargeValue: value holds a "large" constant (lower than 0, or |
| 99 | * or greater than or equal to 32). |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 100 | */ |
| 101 | enum class Kind : uint8_t { |
| 102 | // Short location kinds, for entries fitting on one byte (3 bits |
| 103 | // for the kind, 5 bits for the value) in a DexRegisterMap. |
| 104 | kNone = 0, // 0b000 |
| 105 | kInStack = 1, // 0b001 |
| 106 | kInRegister = 2, // 0b010 |
| 107 | kInFpuRegister = 3, // 0b011 |
| 108 | kConstant = 4, // 0b100 |
| 109 | |
| 110 | // Large location kinds, requiring a 5-byte encoding (1 byte for the |
| 111 | // kind, 4 bytes for the value). |
| 112 | |
| 113 | // Stack location at a large offset, meaning that the offset value |
| 114 | // divided by the stack frame slot size (4 bytes) cannot fit on a |
| 115 | // 5-bit unsigned integer (i.e., this offset value is greater than |
| 116 | // or equal to 2^5 * 4 = 128 bytes). |
| 117 | kInStackLargeOffset = 5, // 0b101 |
| 118 | |
| 119 | // 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] | 120 | // lower than 0, or greater than or equal to 2^5 = 32). |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 121 | kConstantLargeValue = 6, // 0b110 |
| 122 | |
| 123 | kLastLocationKind = kConstantLargeValue |
| 124 | }; |
| 125 | |
| 126 | static_assert( |
| 127 | sizeof(Kind) == 1u, |
| 128 | "art::DexRegisterLocation::Kind has a size different from one byte."); |
| 129 | |
| 130 | static const char* PrettyDescriptor(Kind kind) { |
| 131 | switch (kind) { |
| 132 | case Kind::kNone: |
| 133 | return "none"; |
| 134 | case Kind::kInStack: |
| 135 | return "in stack"; |
| 136 | case Kind::kInRegister: |
| 137 | return "in register"; |
| 138 | case Kind::kInFpuRegister: |
| 139 | return "in fpu register"; |
| 140 | case Kind::kConstant: |
| 141 | return "as constant"; |
| 142 | case Kind::kInStackLargeOffset: |
| 143 | return "in stack (large offset)"; |
| 144 | case Kind::kConstantLargeValue: |
| 145 | return "as constant (large value)"; |
| 146 | default: |
| 147 | UNREACHABLE(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | static bool IsShortLocationKind(Kind kind) { |
| 152 | switch (kind) { |
| 153 | case Kind::kNone: |
| 154 | case Kind::kInStack: |
| 155 | case Kind::kInRegister: |
| 156 | case Kind::kInFpuRegister: |
| 157 | case Kind::kConstant: |
| 158 | return true; |
| 159 | |
| 160 | case Kind::kInStackLargeOffset: |
| 161 | case Kind::kConstantLargeValue: |
| 162 | return false; |
| 163 | |
| 164 | default: |
| 165 | UNREACHABLE(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // Convert `kind` to a "surface" kind, i.e. one that doesn't include |
| 170 | // any value with a "large" qualifier. |
| 171 | // TODO: Introduce another enum type for the surface kind? |
| 172 | static Kind ConvertToSurfaceKind(Kind kind) { |
| 173 | switch (kind) { |
| 174 | case Kind::kNone: |
| 175 | case Kind::kInStack: |
| 176 | case Kind::kInRegister: |
| 177 | case Kind::kInFpuRegister: |
| 178 | case Kind::kConstant: |
| 179 | return kind; |
| 180 | |
| 181 | case Kind::kInStackLargeOffset: |
| 182 | return Kind::kInStack; |
| 183 | |
| 184 | case Kind::kConstantLargeValue: |
| 185 | return Kind::kConstant; |
| 186 | |
| 187 | default: |
| 188 | UNREACHABLE(); |
| 189 | } |
| 190 | } |
| 191 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 192 | // Required by art::StackMapStream::LocationCatalogEntriesIndices. |
| 193 | DexRegisterLocation() : kind_(Kind::kNone), value_(0) {} |
| 194 | |
| 195 | DexRegisterLocation(Kind kind, int32_t value) : kind_(kind), value_(value) {} |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 196 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 197 | static DexRegisterLocation None() { |
| 198 | return DexRegisterLocation(Kind::kNone, 0); |
| 199 | } |
| 200 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 201 | // Get the "surface" kind of the location, i.e., the one that doesn't |
| 202 | // include any value with a "large" qualifier. |
| 203 | Kind GetKind() const { |
| 204 | return ConvertToSurfaceKind(kind_); |
| 205 | } |
| 206 | |
| 207 | // Get the value of the location. |
| 208 | int32_t GetValue() const { return value_; } |
| 209 | |
| 210 | // Get the actual kind of the location. |
| 211 | Kind GetInternalKind() const { return kind_; } |
| 212 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 213 | bool operator==(DexRegisterLocation other) const { |
| 214 | return kind_ == other.kind_ && value_ == other.value_; |
| 215 | } |
| 216 | |
| 217 | bool operator!=(DexRegisterLocation other) const { |
| 218 | return !(*this == other); |
| 219 | } |
| 220 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 221 | private: |
| 222 | Kind kind_; |
| 223 | int32_t value_; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 224 | |
| 225 | friend class DexRegisterLocationHashFn; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 228 | /** |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 229 | * Store information on unique Dex register locations used in a method. |
| 230 | * The information is of the form: |
| 231 | * [DexRegisterLocation+]. |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 232 | * DexRegisterLocations are either 1- or 5-byte wide (see art::DexRegisterLocation::Kind). |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 233 | */ |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 234 | class DexRegisterLocationCatalog { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 235 | public: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 236 | explicit DexRegisterLocationCatalog(MemoryRegion region) : region_(region) {} |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 237 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 238 | // Short (compressed) location, fitting on one byte. |
| 239 | typedef uint8_t ShortLocation; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 240 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 241 | void SetRegisterInfo(size_t offset, const DexRegisterLocation& dex_register_location) { |
| 242 | DexRegisterLocation::Kind kind = ComputeCompressedKind(dex_register_location); |
| 243 | int32_t value = dex_register_location.GetValue(); |
| 244 | if (DexRegisterLocation::IsShortLocationKind(kind)) { |
| 245 | // Short location. Compress the kind and the value as a single byte. |
| 246 | if (kind == DexRegisterLocation::Kind::kInStack) { |
| 247 | // Instead of storing stack offsets expressed in bytes for |
| 248 | // short stack locations, store slot offsets. A stack offset |
| 249 | // is a multiple of 4 (kFrameSlotSize). This means that by |
| 250 | // dividing it by 4, we can fit values from the [0, 128) |
| 251 | // interval in a short stack location, and not just values |
| 252 | // from the [0, 32) interval. |
| 253 | DCHECK_EQ(value % kFrameSlotSize, 0); |
| 254 | value /= kFrameSlotSize; |
| 255 | } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 256 | DCHECK(IsShortValue(value)) << value; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 257 | region_.StoreUnaligned<ShortLocation>(offset, MakeShortLocation(kind, value)); |
| 258 | } else { |
| 259 | // Large location. Write the location on one byte and the value |
| 260 | // on 4 bytes. |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 261 | DCHECK(!IsShortValue(value)) << value; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 262 | if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) { |
| 263 | // Also divide large stack offsets by 4 for the sake of consistency. |
| 264 | DCHECK_EQ(value % kFrameSlotSize, 0); |
| 265 | value /= kFrameSlotSize; |
| 266 | } |
| 267 | // Data can be unaligned as the written Dex register locations can |
| 268 | // either be 1-byte or 5-byte wide. Use |
| 269 | // art::MemoryRegion::StoreUnaligned instead of |
| 270 | // art::MemoryRegion::Store to prevent unligned word accesses on ARM. |
| 271 | region_.StoreUnaligned<DexRegisterLocation::Kind>(offset, kind); |
| 272 | region_.StoreUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind), value); |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 276 | // Find the offset of the location catalog entry number `location_catalog_entry_index`. |
| 277 | size_t FindLocationOffset(size_t location_catalog_entry_index) const { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 278 | size_t offset = kFixedSize; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 279 | // Skip the first `location_catalog_entry_index - 1` entries. |
| 280 | for (uint16_t i = 0; i < location_catalog_entry_index; ++i) { |
| 281 | // Read the first next byte and inspect its first 3 bits to decide |
| 282 | // whether it is a short or a large location. |
| 283 | DexRegisterLocation::Kind kind = ExtractKindAtOffset(offset); |
| 284 | if (DexRegisterLocation::IsShortLocationKind(kind)) { |
| 285 | // Short location. Skip the current byte. |
| 286 | offset += SingleShortEntrySize(); |
| 287 | } else { |
| 288 | // Large location. Skip the 5 next bytes. |
| 289 | offset += SingleLargeEntrySize(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | return offset; |
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 internal kind of entry at `location_catalog_entry_index`. |
| 296 | DexRegisterLocation::Kind GetLocationInternalKind(size_t location_catalog_entry_index) const { |
| 297 | if (location_catalog_entry_index == kNoLocationEntryIndex) { |
| 298 | return DexRegisterLocation::Kind::kNone; |
| 299 | } |
| 300 | return ExtractKindAtOffset(FindLocationOffset(location_catalog_entry_index)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 303 | // Get the (surface) kind and value of entry at `location_catalog_entry_index`. |
| 304 | DexRegisterLocation GetDexRegisterLocation(size_t location_catalog_entry_index) const { |
| 305 | if (location_catalog_entry_index == kNoLocationEntryIndex) { |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 306 | return DexRegisterLocation::None(); |
| 307 | } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 308 | size_t offset = FindLocationOffset(location_catalog_entry_index); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 309 | // Read the first byte and inspect its first 3 bits to get the location. |
| 310 | ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset); |
| 311 | DexRegisterLocation::Kind kind = ExtractKindFromShortLocation(first_byte); |
| 312 | if (DexRegisterLocation::IsShortLocationKind(kind)) { |
| 313 | // Short location. Extract the value from the remaining 5 bits. |
| 314 | int32_t value = ExtractValueFromShortLocation(first_byte); |
| 315 | if (kind == DexRegisterLocation::Kind::kInStack) { |
| 316 | // Convert the stack slot (short) offset to a byte offset value. |
| 317 | value *= kFrameSlotSize; |
| 318 | } |
| 319 | return DexRegisterLocation(kind, value); |
| 320 | } else { |
| 321 | // Large location. Read the four next bytes to get the value. |
| 322 | int32_t value = region_.LoadUnaligned<int32_t>(offset + sizeof(DexRegisterLocation::Kind)); |
| 323 | if (kind == DexRegisterLocation::Kind::kInStackLargeOffset) { |
| 324 | // Convert the stack slot (large) offset to a byte offset value. |
| 325 | value *= kFrameSlotSize; |
| 326 | } |
| 327 | return DexRegisterLocation(kind, value); |
| 328 | } |
Sebastien Hertz | 7cde48c | 2015-01-20 16:06:43 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 331 | // Compute the compressed kind of `location`. |
| 332 | static DexRegisterLocation::Kind ComputeCompressedKind(const DexRegisterLocation& location) { |
| 333 | switch (location.GetInternalKind()) { |
| 334 | case DexRegisterLocation::Kind::kNone: |
| 335 | DCHECK_EQ(location.GetValue(), 0); |
| 336 | return DexRegisterLocation::Kind::kNone; |
| 337 | |
| 338 | case DexRegisterLocation::Kind::kInRegister: |
| 339 | DCHECK_GE(location.GetValue(), 0); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 340 | DCHECK_LT(location.GetValue(), 1 << kValueBits); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 341 | return DexRegisterLocation::Kind::kInRegister; |
| 342 | |
| 343 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 344 | DCHECK_GE(location.GetValue(), 0); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 345 | DCHECK_LT(location.GetValue(), 1 << kValueBits); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 346 | return DexRegisterLocation::Kind::kInFpuRegister; |
| 347 | |
| 348 | case DexRegisterLocation::Kind::kInStack: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 349 | return IsShortStackOffsetValue(location.GetValue()) |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 350 | ? DexRegisterLocation::Kind::kInStack |
| 351 | : DexRegisterLocation::Kind::kInStackLargeOffset; |
| 352 | |
| 353 | case DexRegisterLocation::Kind::kConstant: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 354 | return IsShortConstantValue(location.GetValue()) |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 355 | ? DexRegisterLocation::Kind::kConstant |
| 356 | : DexRegisterLocation::Kind::kConstantLargeValue; |
| 357 | |
| 358 | default: |
| 359 | LOG(FATAL) << "Unexpected location kind" |
| 360 | << DexRegisterLocation::PrettyDescriptor(location.GetInternalKind()); |
| 361 | UNREACHABLE(); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // Can `location` be turned into a short location? |
| 366 | static bool CanBeEncodedAsShortLocation(const DexRegisterLocation& location) { |
| 367 | switch (location.GetInternalKind()) { |
| 368 | case DexRegisterLocation::Kind::kNone: |
| 369 | case DexRegisterLocation::Kind::kInRegister: |
| 370 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 371 | return true; |
| 372 | |
| 373 | case DexRegisterLocation::Kind::kInStack: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 374 | return IsShortStackOffsetValue(location.GetValue()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 375 | |
| 376 | case DexRegisterLocation::Kind::kConstant: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 377 | return IsShortConstantValue(location.GetValue()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 378 | |
| 379 | default: |
| 380 | UNREACHABLE(); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static size_t EntrySize(const DexRegisterLocation& location) { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 385 | return CanBeEncodedAsShortLocation(location) ? SingleShortEntrySize() : SingleLargeEntrySize(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static size_t SingleShortEntrySize() { |
| 389 | return sizeof(ShortLocation); |
| 390 | } |
| 391 | |
| 392 | static size_t SingleLargeEntrySize() { |
| 393 | return sizeof(DexRegisterLocation::Kind) + sizeof(int32_t); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 394 | } |
| 395 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 396 | size_t Size() const { |
| 397 | return region_.size(); |
| 398 | } |
| 399 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 400 | // Special (invalid) Dex register location catalog entry index meaning |
| 401 | // that there is no location for a given Dex register (i.e., it is |
| 402 | // mapped to a DexRegisterLocation::Kind::kNone location). |
| 403 | static constexpr size_t kNoLocationEntryIndex = -1; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 404 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 405 | private: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 406 | static constexpr int kFixedSize = 0; |
| 407 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 408 | // Width of the kind "field" in a short location, in bits. |
| 409 | static constexpr size_t kKindBits = 3; |
| 410 | // Width of the value "field" in a short location, in bits. |
| 411 | static constexpr size_t kValueBits = 5; |
| 412 | |
| 413 | static constexpr uint8_t kKindMask = (1 << kKindBits) - 1; |
| 414 | static constexpr int32_t kValueMask = (1 << kValueBits) - 1; |
| 415 | static constexpr size_t kKindOffset = 0; |
| 416 | static constexpr size_t kValueOffset = kKindBits; |
| 417 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 418 | static bool IsShortStackOffsetValue(int32_t value) { |
| 419 | DCHECK_EQ(value % kFrameSlotSize, 0); |
| 420 | return IsShortValue(value / kFrameSlotSize); |
| 421 | } |
| 422 | |
| 423 | static bool IsShortConstantValue(int32_t value) { |
| 424 | return IsShortValue(value); |
| 425 | } |
| 426 | |
| 427 | static bool IsShortValue(int32_t value) { |
| 428 | return IsUint<kValueBits>(value); |
| 429 | } |
| 430 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 431 | static ShortLocation MakeShortLocation(DexRegisterLocation::Kind kind, int32_t value) { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 432 | uint8_t kind_integer_value = static_cast<uint8_t>(kind); |
| 433 | DCHECK(IsUint<kKindBits>(kind_integer_value)) << kind_integer_value; |
| 434 | DCHECK(IsShortValue(value)) << value; |
| 435 | return (kind_integer_value & kKindMask) << kKindOffset |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 436 | | (value & kValueMask) << kValueOffset; |
| 437 | } |
| 438 | |
| 439 | static DexRegisterLocation::Kind ExtractKindFromShortLocation(ShortLocation location) { |
| 440 | uint8_t kind = (location >> kKindOffset) & kKindMask; |
| 441 | DCHECK_LE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kLastLocationKind)); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 442 | // We do not encode kNone locations in the stack map. |
| 443 | DCHECK_NE(kind, static_cast<uint8_t>(DexRegisterLocation::Kind::kNone)); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 444 | return static_cast<DexRegisterLocation::Kind>(kind); |
| 445 | } |
| 446 | |
| 447 | static int32_t ExtractValueFromShortLocation(ShortLocation location) { |
| 448 | return (location >> kValueOffset) & kValueMask; |
| 449 | } |
| 450 | |
| 451 | // Extract a location kind from the byte at position `offset`. |
| 452 | DexRegisterLocation::Kind ExtractKindAtOffset(size_t offset) const { |
| 453 | ShortLocation first_byte = region_.LoadUnaligned<ShortLocation>(offset); |
| 454 | return ExtractKindFromShortLocation(first_byte); |
| 455 | } |
| 456 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 457 | MemoryRegion region_; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 458 | |
| 459 | friend class CodeInfo; |
| 460 | friend class StackMapStream; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 461 | }; |
| 462 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 463 | /* Information on Dex register locations for a specific PC, mapping a |
| 464 | * stack map's Dex register to a location entry in a DexRegisterLocationCatalog. |
| 465 | * The information is of the form: |
| 466 | * [live_bit_mask, entries*] |
| 467 | * where entries are concatenated unsigned integer values encoded on a number |
| 468 | * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending |
| 469 | * on the number of entries in the Dex register location catalog |
| 470 | * (see DexRegisterMap::SingleEntrySizeInBits). The map is 1-byte aligned. |
| 471 | */ |
| 472 | class DexRegisterMap { |
| 473 | public: |
| 474 | explicit DexRegisterMap(MemoryRegion region) : region_(region) {} |
| 475 | |
| 476 | // Get the surface kind of Dex register `dex_register_number`. |
| 477 | DexRegisterLocation::Kind GetLocationKind(uint16_t dex_register_number, |
| 478 | uint16_t number_of_dex_registers, |
| 479 | const CodeInfo& code_info) const { |
| 480 | return DexRegisterLocation::ConvertToSurfaceKind( |
| 481 | GetLocationInternalKind(dex_register_number, number_of_dex_registers, code_info)); |
| 482 | } |
| 483 | |
| 484 | // Get the internal kind of Dex register `dex_register_number`. |
| 485 | DexRegisterLocation::Kind GetLocationInternalKind(uint16_t dex_register_number, |
| 486 | uint16_t number_of_dex_registers, |
| 487 | const CodeInfo& code_info) const; |
| 488 | |
| 489 | // Get the Dex register location `dex_register_number`. |
| 490 | DexRegisterLocation GetDexRegisterLocation(uint16_t dex_register_number, |
| 491 | uint16_t number_of_dex_registers, |
| 492 | const CodeInfo& code_info) const; |
| 493 | |
| 494 | int32_t GetStackOffsetInBytes(uint16_t dex_register_number, |
| 495 | uint16_t number_of_dex_registers, |
| 496 | const CodeInfo& code_info) const { |
| 497 | DexRegisterLocation location = |
| 498 | GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info); |
| 499 | DCHECK(location.GetKind() == DexRegisterLocation::Kind::kInStack); |
| 500 | // GetDexRegisterLocation returns the offset in bytes. |
| 501 | return location.GetValue(); |
| 502 | } |
| 503 | |
| 504 | int32_t GetConstant(uint16_t dex_register_number, |
| 505 | uint16_t number_of_dex_registers, |
| 506 | const CodeInfo& code_info) const { |
| 507 | DexRegisterLocation location = |
| 508 | GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info); |
| 509 | DCHECK(location.GetKind() == DexRegisterLocation::Kind::kConstant); |
| 510 | return location.GetValue(); |
| 511 | } |
| 512 | |
| 513 | int32_t GetMachineRegister(uint16_t dex_register_number, |
| 514 | uint16_t number_of_dex_registers, |
| 515 | const CodeInfo& code_info) const { |
| 516 | DexRegisterLocation location = |
| 517 | GetDexRegisterLocation(dex_register_number, number_of_dex_registers, code_info); |
| 518 | DCHECK(location.GetInternalKind() == DexRegisterLocation::Kind::kInRegister |
| 519 | || location.GetInternalKind() == DexRegisterLocation::Kind::kInFpuRegister) |
| 520 | << DexRegisterLocation::PrettyDescriptor(location.GetInternalKind()); |
| 521 | return location.GetValue(); |
| 522 | } |
| 523 | |
| 524 | // Get the index of the entry in the Dex register location catalog |
| 525 | // corresponding to `dex_register_number`. |
| 526 | size_t GetLocationCatalogEntryIndex(uint16_t dex_register_number, |
| 527 | uint16_t number_of_dex_registers, |
| 528 | size_t number_of_location_catalog_entries) const { |
| 529 | if (!IsDexRegisterLive(dex_register_number)) { |
| 530 | return DexRegisterLocationCatalog::kNoLocationEntryIndex; |
| 531 | } |
| 532 | |
| 533 | if (number_of_location_catalog_entries == 1) { |
| 534 | // We do not allocate space for location maps in the case of a |
| 535 | // single-entry location catalog, as it is useless. The only valid |
| 536 | // entry index is 0; |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | // The bit offset of the beginning of the map locations. |
| 541 | size_t map_locations_offset_in_bits = |
| 542 | GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte; |
| 543 | size_t index_in_dex_register_map = GetIndexInDexRegisterMap(dex_register_number); |
| 544 | DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers)); |
| 545 | // The bit size of an entry. |
| 546 | size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries); |
| 547 | // The bit offset where `index_in_dex_register_map` is located. |
| 548 | size_t entry_offset_in_bits = |
| 549 | map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits; |
| 550 | size_t location_catalog_entry_index = |
| 551 | region_.LoadBits(entry_offset_in_bits, map_entry_size_in_bits); |
| 552 | DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries); |
| 553 | return location_catalog_entry_index; |
| 554 | } |
| 555 | |
| 556 | // Map entry at `index_in_dex_register_map` to `location_catalog_entry_index`. |
| 557 | void SetLocationCatalogEntryIndex(size_t index_in_dex_register_map, |
| 558 | size_t location_catalog_entry_index, |
| 559 | uint16_t number_of_dex_registers, |
| 560 | size_t number_of_location_catalog_entries) { |
| 561 | DCHECK_LT(index_in_dex_register_map, GetNumberOfLiveDexRegisters(number_of_dex_registers)); |
| 562 | DCHECK_LT(location_catalog_entry_index, number_of_location_catalog_entries); |
| 563 | |
| 564 | if (number_of_location_catalog_entries == 1) { |
| 565 | // We do not allocate space for location maps in the case of a |
| 566 | // single-entry location catalog, as it is useless. |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | // The bit offset of the beginning of the map locations. |
| 571 | size_t map_locations_offset_in_bits = |
| 572 | GetLocationMappingDataOffset(number_of_dex_registers) * kBitsPerByte; |
| 573 | // The bit size of an entry. |
| 574 | size_t map_entry_size_in_bits = SingleEntrySizeInBits(number_of_location_catalog_entries); |
| 575 | // The bit offset where `index_in_dex_register_map` is located. |
| 576 | size_t entry_offset_in_bits = |
| 577 | map_locations_offset_in_bits + index_in_dex_register_map * map_entry_size_in_bits; |
| 578 | region_.StoreBits(entry_offset_in_bits, location_catalog_entry_index, map_entry_size_in_bits); |
| 579 | } |
| 580 | |
| 581 | void SetLiveBitMask(uint16_t number_of_dex_registers, |
| 582 | const BitVector& live_dex_registers_mask) { |
| 583 | size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte; |
| 584 | for (uint16_t i = 0; i < number_of_dex_registers; ++i) { |
| 585 | region_.StoreBit(live_bit_mask_offset_in_bits + i, live_dex_registers_mask.IsBitSet(i)); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | bool IsDexRegisterLive(uint16_t dex_register_number) const { |
| 590 | size_t live_bit_mask_offset_in_bits = GetLiveBitMaskOffset() * kBitsPerByte; |
| 591 | return region_.LoadBit(live_bit_mask_offset_in_bits + dex_register_number); |
| 592 | } |
| 593 | |
| 594 | size_t GetNumberOfLiveDexRegisters(uint16_t number_of_dex_registers) const { |
| 595 | size_t number_of_live_dex_registers = 0; |
| 596 | for (size_t i = 0; i < number_of_dex_registers; ++i) { |
| 597 | if (IsDexRegisterLive(i)) { |
| 598 | ++number_of_live_dex_registers; |
| 599 | } |
| 600 | } |
| 601 | return number_of_live_dex_registers; |
| 602 | } |
| 603 | |
| 604 | static size_t GetLiveBitMaskOffset() { |
| 605 | return kFixedSize; |
| 606 | } |
| 607 | |
| 608 | // Compute the size of the live register bit mask (in bytes), for a |
| 609 | // method having `number_of_dex_registers` Dex registers. |
| 610 | static size_t GetLiveBitMaskSize(uint16_t number_of_dex_registers) { |
| 611 | return RoundUp(number_of_dex_registers, kBitsPerByte) / kBitsPerByte; |
| 612 | } |
| 613 | |
| 614 | static size_t GetLocationMappingDataOffset(uint16_t number_of_dex_registers) { |
| 615 | return GetLiveBitMaskOffset() + GetLiveBitMaskSize(number_of_dex_registers); |
| 616 | } |
| 617 | |
| 618 | size_t GetLocationMappingDataSize(uint16_t number_of_dex_registers, |
| 619 | size_t number_of_location_catalog_entries) const { |
| 620 | size_t location_mapping_data_size_in_bits = |
| 621 | GetNumberOfLiveDexRegisters(number_of_dex_registers) |
| 622 | * SingleEntrySizeInBits(number_of_location_catalog_entries); |
| 623 | return RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte; |
| 624 | } |
| 625 | |
| 626 | // Return the size of a map entry in bits. Note that if |
| 627 | // `number_of_location_catalog_entries` equals 1, this function returns 0, |
| 628 | // which is fine, as there is no need to allocate a map for a |
| 629 | // single-entry location catalog; the only valid location catalog entry index |
| 630 | // for a live register in this case is 0 and there is no need to |
| 631 | // store it. |
| 632 | static size_t SingleEntrySizeInBits(size_t number_of_location_catalog_entries) { |
| 633 | // Handle the case of 0, as we cannot pass 0 to art::WhichPowerOf2. |
| 634 | return number_of_location_catalog_entries == 0 |
| 635 | ? 0u |
| 636 | : WhichPowerOf2(RoundUpToPowerOfTwo(number_of_location_catalog_entries)); |
| 637 | } |
| 638 | |
| 639 | // Return the size of the DexRegisterMap object, in bytes. |
| 640 | size_t Size() const { |
| 641 | return region_.size(); |
| 642 | } |
| 643 | |
| 644 | private: |
| 645 | // Return the index in the Dex register map corresponding to the Dex |
| 646 | // register number `dex_register_number`. |
| 647 | size_t GetIndexInDexRegisterMap(uint16_t dex_register_number) const { |
| 648 | if (!IsDexRegisterLive(dex_register_number)) { |
| 649 | return kInvalidIndexInDexRegisterMap; |
| 650 | } |
| 651 | return GetNumberOfLiveDexRegisters(dex_register_number); |
| 652 | } |
| 653 | |
| 654 | // Special (invalid) Dex register map entry index meaning that there |
| 655 | // is no index in the map for a given Dex register (i.e., it must |
| 656 | // have been mapped to a DexRegisterLocation::Kind::kNone location). |
| 657 | static constexpr size_t kInvalidIndexInDexRegisterMap = -1; |
| 658 | |
| 659 | static constexpr int kFixedSize = 0; |
| 660 | |
| 661 | MemoryRegion region_; |
| 662 | |
| 663 | friend class CodeInfo; |
| 664 | friend class StackMapStream; |
| 665 | }; |
| 666 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 667 | /** |
| 668 | * A Stack Map holds compilation information for a specific PC necessary for: |
| 669 | * - Mapping it to a dex PC, |
| 670 | * - Knowing which stack entries are objects, |
| 671 | * - Knowing which registers hold objects, |
| 672 | * - Knowing the inlining information, |
| 673 | * - Knowing the values of dex registers. |
| 674 | * |
| 675 | * The information is of the form: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 676 | * [dex_pc, native_pc_offset, dex_register_map_offset, inlining_info_offset, register_mask, |
| 677 | * stack_mask]. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 678 | * |
| 679 | * Note that register_mask is fixed size, but stack_mask is variable size, depending on the |
| 680 | * stack size of a method. |
| 681 | */ |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 682 | class StackMap { |
| 683 | public: |
| 684 | explicit StackMap(MemoryRegion region) : region_(region) {} |
| 685 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 686 | uint32_t GetDexPc(const CodeInfo& info) const; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 687 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 688 | void SetDexPc(const CodeInfo& info, uint32_t dex_pc); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 689 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 690 | uint32_t GetNativePcOffset(const CodeInfo& info) const; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 691 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 692 | void SetNativePcOffset(const CodeInfo& info, uint32_t native_pc_offset); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 693 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 694 | uint32_t GetDexRegisterMapOffset(const CodeInfo& info) const; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 695 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 696 | void SetDexRegisterMapOffset(const CodeInfo& info, uint32_t offset); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 697 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 698 | uint32_t GetInlineDescriptorOffset(const CodeInfo& info) const; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 699 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 700 | void SetInlineDescriptorOffset(const CodeInfo& info, uint32_t offset); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 701 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 702 | uint32_t GetRegisterMask(const CodeInfo& info) const; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 703 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 704 | void SetRegisterMask(const CodeInfo& info, uint32_t mask); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 705 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 706 | MemoryRegion GetStackMask(const CodeInfo& info) const; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 707 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 708 | void SetStackMask(const CodeInfo& info, const BitVector& sp_map) { |
| 709 | MemoryRegion region = GetStackMask(info); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 710 | for (size_t i = 0; i < region.size_in_bits(); i++) { |
| 711 | region.StoreBit(i, sp_map.IsBitSet(i)); |
| 712 | } |
| 713 | } |
| 714 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 715 | bool HasDexRegisterMap(const CodeInfo& info) const { |
| 716 | return GetDexRegisterMapOffset(info) != kNoDexRegisterMap; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 717 | } |
| 718 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 719 | bool HasInlineInfo(const CodeInfo& info) const { |
| 720 | return GetInlineDescriptorOffset(info) != kNoInlineInfo; |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | bool Equals(const StackMap& other) const { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 724 | return region_.pointer() == other.region_.pointer() |
| 725 | && region_.size() == other.region_.size(); |
| 726 | } |
| 727 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 728 | static size_t ComputeStackMapSize(size_t stack_mask_size, |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 729 | size_t inline_info_size, |
| 730 | size_t dex_register_map_size, |
| 731 | size_t dex_pc_max, |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 732 | size_t native_pc_max, |
| 733 | size_t register_mask_max); |
Nicolas Geoffray | 376b2bb | 2014-12-09 14:26:32 +0000 | [diff] [blame] | 734 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 735 | // Special (invalid) offset for the DexRegisterMapOffset field meaning |
| 736 | // that there is no Dex register map for this stack map. |
| 737 | static constexpr uint32_t kNoDexRegisterMap = -1; |
| 738 | |
| 739 | // Special (invalid) offset for the InlineDescriptorOffset field meaning |
| 740 | // that there is no inline info for this stack map. |
| 741 | static constexpr uint32_t kNoInlineInfo = -1; |
| 742 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 743 | private: |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 744 | static size_t ComputeStackMapSizeInternal(size_t stack_mask_size, |
| 745 | size_t number_of_bytes_for_inline_info, |
| 746 | size_t number_of_bytes_for_dex_map, |
| 747 | size_t number_of_bytes_for_dex_pc, |
| 748 | size_t number_of_bytes_for_native_pc, |
| 749 | size_t number_of_bytes_for_register_mask); |
| 750 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 751 | // TODO: Instead of plain types such as "uint32_t", introduce |
| 752 | // typedefs (and document the memory layout of StackMap). |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 753 | static constexpr int kRegisterMaskOffset = 0; |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 754 | static constexpr int kFixedSize = 0; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 755 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 756 | MemoryRegion region_; |
| 757 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 758 | friend class CodeInfo; |
| 759 | friend class StackMapStream; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 760 | }; |
| 761 | |
| 762 | |
| 763 | /** |
| 764 | * Wrapper around all compiler information collected for a method. |
| 765 | * The information is of the form: |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 766 | * [overall_size, number_of_location_catalog_entries, number_of_stack_maps, stack_mask_size, |
| 767 | * DexRegisterLocationCatalog+, StackMap+, DexRegisterMap+, InlineInfo*]. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 768 | */ |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 769 | class CodeInfo { |
| 770 | public: |
| 771 | explicit CodeInfo(MemoryRegion region) : region_(region) {} |
| 772 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 773 | explicit CodeInfo(const void* data) { |
| 774 | uint32_t size = reinterpret_cast<const uint32_t*>(data)[0]; |
| 775 | region_ = MemoryRegion(const_cast<void*>(data), size); |
| 776 | } |
| 777 | |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 778 | static size_t EncodingSizeInBytes(size_t max_element) { |
| 779 | DCHECK(IsUint<32>(max_element)); |
| 780 | return (max_element == 0) ? 0 |
| 781 | : IsUint<8>(max_element) ? 1 |
| 782 | : IsUint<16>(max_element) ? 2 |
| 783 | : IsUint<24>(max_element) ? 3 |
| 784 | : 4; |
| 785 | } |
| 786 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 787 | void SetEncoding(size_t inline_info_size, |
| 788 | size_t dex_register_map_size, |
| 789 | size_t dex_pc_max, |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 790 | size_t native_pc_max, |
| 791 | size_t register_mask_max) { |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 792 | if (inline_info_size != 0) { |
| 793 | region_.StoreBit(kHasInlineInfoBitOffset, 1); |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 794 | // + 1 to also encode kNoInlineInfo: if an inline info offset |
| 795 | // is at 0xFF, we want to overflow to a larger encoding, because it will |
| 796 | // conflict with kNoInlineInfo. |
| 797 | // The offset is relative to the dex register map. TODO: Change this. |
| 798 | SetEncodingAt(kInlineInfoBitOffset, |
| 799 | EncodingSizeInBytes(dex_register_map_size + inline_info_size + 1)); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 800 | } else { |
| 801 | region_.StoreBit(kHasInlineInfoBitOffset, 0); |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 802 | SetEncodingAt(kInlineInfoBitOffset, 0); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 803 | } |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 804 | // + 1 to also encode kNoDexRegisterMap: if a dex register map offset |
| 805 | // is at 0xFF, we want to overflow to a larger encoding, because it will |
| 806 | // conflict with kNoDexRegisterMap. |
| 807 | SetEncodingAt(kDexRegisterMapBitOffset, EncodingSizeInBytes(dex_register_map_size + 1)); |
| 808 | SetEncodingAt(kDexPcBitOffset, EncodingSizeInBytes(dex_pc_max)); |
| 809 | SetEncodingAt(kNativePcBitOffset, EncodingSizeInBytes(native_pc_max)); |
| 810 | SetEncodingAt(kRegisterMaskBitOffset, EncodingSizeInBytes(register_mask_max)); |
| 811 | } |
| 812 | |
| 813 | void SetEncodingAt(size_t bit_offset, size_t number_of_bytes) { |
| 814 | // We encode the number of bytes needed for writing a value on 3 bits, |
| 815 | // for values that we know are maximum 32bits. |
| 816 | region_.StoreBit(bit_offset, (number_of_bytes & 1)); |
| 817 | region_.StoreBit(bit_offset + 1, (number_of_bytes & 2)); |
| 818 | region_.StoreBit(bit_offset + 2, (number_of_bytes & 4)); |
| 819 | } |
| 820 | |
| 821 | size_t GetNumberOfBytesForEncoding(size_t bit_offset) const { |
| 822 | return region_.LoadBit(bit_offset) |
| 823 | + (region_.LoadBit(bit_offset + 1) << 1) |
| 824 | + (region_.LoadBit(bit_offset + 2) << 2); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | bool HasInlineInfo() const { |
| 828 | return region_.LoadBit(kHasInlineInfoBitOffset); |
| 829 | } |
| 830 | |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 831 | size_t NumberOfBytesForInlineInfo() const { |
| 832 | return GetNumberOfBytesForEncoding(kInlineInfoBitOffset); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 835 | size_t NumberOfBytesForDexRegisterMap() const { |
| 836 | return GetNumberOfBytesForEncoding(kDexRegisterMapBitOffset); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 839 | size_t NumberOfBytesForRegisterMask() const { |
| 840 | return GetNumberOfBytesForEncoding(kRegisterMaskBitOffset); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 843 | size_t NumberOfBytesForNativePc() const { |
| 844 | return GetNumberOfBytesForEncoding(kNativePcBitOffset); |
| 845 | } |
| 846 | |
| 847 | size_t NumberOfBytesForDexPc() const { |
| 848 | return GetNumberOfBytesForEncoding(kDexPcBitOffset); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | size_t ComputeStackMapRegisterMaskOffset() const { |
| 852 | return StackMap::kRegisterMaskOffset; |
| 853 | } |
| 854 | |
| 855 | size_t ComputeStackMapStackMaskOffset() const { |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 856 | return ComputeStackMapRegisterMaskOffset() |
| 857 | + (NumberOfBytesForRegisterMask() * sizeof(uint8_t)); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | size_t ComputeStackMapDexPcOffset() const { |
| 861 | return ComputeStackMapStackMaskOffset() + GetStackMaskSize(); |
| 862 | } |
| 863 | |
| 864 | size_t ComputeStackMapNativePcOffset() const { |
| 865 | return ComputeStackMapDexPcOffset() |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 866 | + (NumberOfBytesForDexPc() * sizeof(uint8_t)); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | size_t ComputeStackMapDexRegisterMapOffset() const { |
| 870 | return ComputeStackMapNativePcOffset() |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 871 | + (NumberOfBytesForNativePc() * sizeof(uint8_t)); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | size_t ComputeStackMapInlineInfoOffset() const { |
| 875 | CHECK(HasInlineInfo()); |
| 876 | return ComputeStackMapDexRegisterMapOffset() |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 877 | + (NumberOfBytesForDexRegisterMap() * sizeof(uint8_t)); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 880 | uint32_t GetDexRegisterLocationCatalogOffset() const { |
| 881 | return kFixedSize; |
| 882 | } |
| 883 | |
| 884 | DexRegisterLocationCatalog GetDexRegisterLocationCatalog() const { |
| 885 | return DexRegisterLocationCatalog(region_.Subregion( |
| 886 | GetDexRegisterLocationCatalogOffset(), |
| 887 | GetDexRegisterLocationCatalogSize())); |
| 888 | } |
| 889 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 890 | StackMap GetStackMapAt(size_t i) const { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 891 | size_t size = StackMapSize(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 892 | return StackMap(GetStackMaps().Subregion(i * size, size)); |
| 893 | } |
| 894 | |
| 895 | uint32_t GetOverallSize() const { |
Nicolas Geoffray | aec8f93 | 2015-03-18 10:42:22 +0000 | [diff] [blame] | 896 | return region_.LoadUnaligned<uint32_t>(kOverallSizeOffset); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | void SetOverallSize(uint32_t size) { |
Nicolas Geoffray | aec8f93 | 2015-03-18 10:42:22 +0000 | [diff] [blame] | 900 | region_.StoreUnaligned<uint32_t>(kOverallSizeOffset, size); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 901 | } |
| 902 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 903 | uint32_t GetNumberOfDexRegisterLocationCatalogEntries() const { |
| 904 | return region_.LoadUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset); |
| 905 | } |
| 906 | |
| 907 | void SetNumberOfDexRegisterLocationCatalogEntries(uint32_t num_entries) { |
| 908 | region_.StoreUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset, num_entries); |
| 909 | } |
| 910 | |
| 911 | uint32_t GetDexRegisterLocationCatalogSize() const { |
| 912 | return ComputeDexRegisterLocationCatalogSize(GetDexRegisterLocationCatalogOffset(), |
| 913 | GetNumberOfDexRegisterLocationCatalogEntries()); |
| 914 | } |
| 915 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 916 | uint32_t GetStackMaskSize() const { |
Nicolas Geoffray | aec8f93 | 2015-03-18 10:42:22 +0000 | [diff] [blame] | 917 | return region_.LoadUnaligned<uint32_t>(kStackMaskSizeOffset); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | void SetStackMaskSize(uint32_t size) { |
Nicolas Geoffray | aec8f93 | 2015-03-18 10:42:22 +0000 | [diff] [blame] | 921 | region_.StoreUnaligned<uint32_t>(kStackMaskSizeOffset, size); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | size_t GetNumberOfStackMaps() const { |
Nicolas Geoffray | aec8f93 | 2015-03-18 10:42:22 +0000 | [diff] [blame] | 925 | return region_.LoadUnaligned<uint32_t>(kNumberOfStackMapsOffset); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | void SetNumberOfStackMaps(uint32_t number_of_stack_maps) { |
Nicolas Geoffray | aec8f93 | 2015-03-18 10:42:22 +0000 | [diff] [blame] | 929 | region_.StoreUnaligned<uint32_t>(kNumberOfStackMapsOffset, number_of_stack_maps); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 930 | } |
| 931 | |
Roland Levillain | 29ba1b0 | 2015-03-13 11:45:07 +0000 | [diff] [blame] | 932 | // Get the size of one stack map of this CodeInfo object, in bytes. |
| 933 | // All stack maps of a CodeInfo have the same size. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 934 | size_t StackMapSize() const { |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 935 | return StackMap::ComputeStackMapSizeInternal(GetStackMaskSize(), |
| 936 | NumberOfBytesForInlineInfo(), |
| 937 | NumberOfBytesForDexRegisterMap(), |
| 938 | NumberOfBytesForDexPc(), |
| 939 | NumberOfBytesForNativePc(), |
| 940 | NumberOfBytesForRegisterMask()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 941 | } |
| 942 | |
Roland Levillain | 29ba1b0 | 2015-03-13 11:45:07 +0000 | [diff] [blame] | 943 | // Get the size all the stack maps of this CodeInfo object, in bytes. |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 944 | size_t GetStackMapsSize() const { |
Roland Levillain | 29ba1b0 | 2015-03-13 11:45:07 +0000 | [diff] [blame] | 945 | return StackMapSize() * GetNumberOfStackMaps(); |
| 946 | } |
| 947 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 948 | size_t GetDexRegisterMapsOffset() const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 949 | return GetStackMapsOffset() + GetStackMapsSize(); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 952 | uint32_t GetStackMapsOffset() const { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 953 | return GetDexRegisterLocationCatalogOffset() + GetDexRegisterLocationCatalogSize(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 956 | DexRegisterMap GetDexRegisterMapOf(StackMap stack_map, uint32_t number_of_dex_registers) const { |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 957 | DCHECK(stack_map.HasDexRegisterMap(*this)); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 958 | uint32_t offset = GetDexRegisterMapsOffset() + stack_map.GetDexRegisterMapOffset(*this); |
| 959 | size_t size = ComputeDexRegisterMapSizeOf(offset, number_of_dex_registers); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 960 | return DexRegisterMap(region_.Subregion(offset, size)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 961 | } |
| 962 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 963 | InlineInfo GetInlineInfoOf(StackMap stack_map) const { |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 964 | DCHECK(stack_map.HasInlineInfo(*this)); |
| 965 | uint32_t offset = stack_map.GetInlineDescriptorOffset(*this) + GetDexRegisterMapsOffset(); |
| 966 | uint8_t depth = region_.LoadUnaligned<uint8_t>(offset); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 967 | return InlineInfo(region_.Subregion(offset, |
| 968 | InlineInfo::kFixedSize + depth * InlineInfo::SingleEntrySize())); |
| 969 | } |
| 970 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 971 | StackMap GetStackMapForDexPc(uint32_t dex_pc) const { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 972 | for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 973 | StackMap stack_map = GetStackMapAt(i); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 974 | if (stack_map.GetDexPc(*this) == dex_pc) { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 975 | return stack_map; |
| 976 | } |
| 977 | } |
| 978 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 979 | UNREACHABLE(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 980 | } |
| 981 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 982 | StackMap GetStackMapForNativePcOffset(uint32_t native_pc_offset) const { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 983 | // TODO: stack maps are sorted by native pc, we can do a binary search. |
| 984 | for (size_t i = 0, e = GetNumberOfStackMaps(); i < e; ++i) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 985 | StackMap stack_map = GetStackMapAt(i); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 986 | if (stack_map.GetNativePcOffset(*this) == native_pc_offset) { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 987 | return stack_map; |
| 988 | } |
| 989 | } |
| 990 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 991 | UNREACHABLE(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 992 | } |
| 993 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 994 | void Dump(std::ostream& os, uint16_t number_of_dex_registers) const; |
| 995 | void DumpStackMapHeader(std::ostream& os, size_t stack_map_num) const; |
| 996 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 997 | private: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 998 | // TODO: Instead of plain types such as "uint32_t", introduce |
| 999 | // typedefs (and document the memory layout of CodeInfo). |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1000 | static constexpr int kOverallSizeOffset = 0; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 1001 | static constexpr int kEncodingInfoOffset = kOverallSizeOffset + sizeof(uint32_t); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1002 | static constexpr int kNumberOfDexRegisterLocationCatalogEntriesOffset = |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 1003 | kEncodingInfoOffset + sizeof(uint16_t); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1004 | static constexpr int kNumberOfStackMapsOffset = |
| 1005 | kNumberOfDexRegisterLocationCatalogEntriesOffset + sizeof(uint32_t); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1006 | static constexpr int kStackMaskSizeOffset = kNumberOfStackMapsOffset + sizeof(uint32_t); |
| 1007 | static constexpr int kFixedSize = kStackMaskSizeOffset + sizeof(uint32_t); |
| 1008 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 1009 | static constexpr int kHasInlineInfoBitOffset = (kEncodingInfoOffset * kBitsPerByte); |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 1010 | static constexpr int kInlineInfoBitOffset = kHasInlineInfoBitOffset + 1; |
| 1011 | static constexpr int kDexRegisterMapBitOffset = kInlineInfoBitOffset + 3; |
| 1012 | static constexpr int kDexPcBitOffset = kDexRegisterMapBitOffset + 3; |
| 1013 | static constexpr int kNativePcBitOffset = kDexPcBitOffset + 3; |
| 1014 | static constexpr int kRegisterMaskBitOffset = kNativePcBitOffset + 3; |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 1015 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1016 | MemoryRegion GetStackMaps() const { |
| 1017 | return region_.size() == 0 |
| 1018 | ? MemoryRegion() |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1019 | : region_.Subregion(GetStackMapsOffset(), GetStackMapsSize()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1020 | } |
| 1021 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1022 | // Compute the size of the Dex register map associated to the stack map at |
| 1023 | // `dex_register_map_offset_in_code_info`. |
| 1024 | size_t ComputeDexRegisterMapSizeOf(uint32_t dex_register_map_offset_in_code_info, |
| 1025 | uint16_t number_of_dex_registers) const { |
| 1026 | // Offset where the actual mapping data starts within art::DexRegisterMap. |
| 1027 | size_t location_mapping_data_offset_in_dex_register_map = |
| 1028 | DexRegisterMap::GetLocationMappingDataOffset(number_of_dex_registers); |
| 1029 | // Create a temporary art::DexRegisterMap to be able to call |
| 1030 | // art::DexRegisterMap::GetNumberOfLiveDexRegisters and |
| 1031 | DexRegisterMap dex_register_map_without_locations( |
| 1032 | MemoryRegion(region_.Subregion(dex_register_map_offset_in_code_info, |
| 1033 | location_mapping_data_offset_in_dex_register_map))); |
| 1034 | size_t number_of_live_dex_registers = |
| 1035 | dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers); |
| 1036 | size_t location_mapping_data_size_in_bits = |
| 1037 | DexRegisterMap::SingleEntrySizeInBits(GetNumberOfDexRegisterLocationCatalogEntries()) |
| 1038 | * number_of_live_dex_registers; |
| 1039 | size_t location_mapping_data_size_in_bytes = |
| 1040 | RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte; |
| 1041 | size_t dex_register_map_size = |
| 1042 | location_mapping_data_offset_in_dex_register_map + location_mapping_data_size_in_bytes; |
| 1043 | return dex_register_map_size; |
| 1044 | } |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 1045 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1046 | // Compute the size of a Dex register location catalog starting at offset `origin` |
| 1047 | // in `region_` and containing `number_of_dex_locations` entries. |
| 1048 | size_t ComputeDexRegisterLocationCatalogSize(uint32_t origin, |
| 1049 | uint32_t number_of_dex_locations) const { |
| 1050 | // TODO: Ideally, we would like to use art::DexRegisterLocationCatalog::Size or |
| 1051 | // art::DexRegisterLocationCatalog::FindLocationOffset, but the |
| 1052 | // DexRegisterLocationCatalog is not yet built. Try to factor common code. |
| 1053 | size_t offset = origin + DexRegisterLocationCatalog::kFixedSize; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 1054 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 1055 | // Skip the first `number_of_dex_locations - 1` entries. |
| 1056 | for (uint16_t i = 0; i < number_of_dex_locations; ++i) { |
| 1057 | // Read the first next byte and inspect its first 3 bits to decide |
| 1058 | // whether it is a short or a large location. |
| 1059 | DexRegisterLocationCatalog::ShortLocation first_byte = |
| 1060 | region_.LoadUnaligned<DexRegisterLocationCatalog::ShortLocation>(offset); |
| 1061 | DexRegisterLocation::Kind kind = |
| 1062 | DexRegisterLocationCatalog::ExtractKindFromShortLocation(first_byte); |
| 1063 | if (DexRegisterLocation::IsShortLocationKind(kind)) { |
| 1064 | // Short location. Skip the current byte. |
| 1065 | offset += DexRegisterLocationCatalog::SingleShortEntrySize(); |
| 1066 | } else { |
| 1067 | // Large location. Skip the 5 next bytes. |
| 1068 | offset += DexRegisterLocationCatalog::SingleLargeEntrySize(); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | size_t size = offset - origin; |
| 1072 | return size; |
| 1073 | } |
| 1074 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1075 | MemoryRegion region_; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1076 | friend class StackMapStream; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1077 | }; |
| 1078 | |
| 1079 | } // namespace art |
| 1080 | |
| 1081 | #endif // ART_RUNTIME_STACK_MAP_H_ |