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