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