Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 17 | #include "reg_type_cache-inl.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 18 | |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 19 | #include "base/casts.h" |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 20 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 21 | #include "dex_file-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "mirror/class-inl.h" |
| 23 | #include "mirror/object-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 24 | #include "reg_type-inl.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | namespace verifier { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 28 | |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 29 | bool RegTypeCache::primitive_initialized_ = false; |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 30 | uint16_t RegTypeCache::primitive_count_ = 0; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 31 | const PreciseConstType* RegTypeCache::small_precise_constants_[kMaxSmallConstant - kMinSmallConstant + 1]; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 32 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 33 | static bool MatchingPrecisionForClass(const RegType* entry, bool precise) |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 34 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 35 | if (entry->IsPreciseReference() == precise) { |
| 36 | // We were or weren't looking for a precise reference and we found what we need. |
| 37 | return true; |
| 38 | } else { |
| 39 | if (!precise && entry->GetClass()->CannotBeAssignedFromOtherTypes()) { |
| 40 | // We weren't looking for a precise reference, as we're looking up based on a descriptor, but |
| 41 | // we found a matching entry based on the descriptor. Return the precise entry in that case. |
| 42 | return true; |
| 43 | } |
| 44 | return false; |
| 45 | } |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 48 | void RegTypeCache::FillPrimitiveAndSmallConstantTypes() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 49 | entries_.push_back(UndefinedType::GetInstance()); |
| 50 | entries_.push_back(ConflictType::GetInstance()); |
| 51 | entries_.push_back(BooleanType::GetInstance()); |
| 52 | entries_.push_back(ByteType::GetInstance()); |
| 53 | entries_.push_back(ShortType::GetInstance()); |
| 54 | entries_.push_back(CharType::GetInstance()); |
| 55 | entries_.push_back(IntegerType::GetInstance()); |
| 56 | entries_.push_back(LongLoType::GetInstance()); |
| 57 | entries_.push_back(LongHiType::GetInstance()); |
| 58 | entries_.push_back(FloatType::GetInstance()); |
| 59 | entries_.push_back(DoubleLoType::GetInstance()); |
| 60 | entries_.push_back(DoubleHiType::GetInstance()); |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 61 | for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) { |
| 62 | int32_t i = value - kMinSmallConstant; |
| 63 | DCHECK_EQ(entries_.size(), small_precise_constants_[i]->GetId()); |
| 64 | entries_.push_back(small_precise_constants_[i]); |
| 65 | } |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 66 | DCHECK_EQ(entries_.size(), primitive_count_); |
| 67 | } |
| 68 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 69 | const RegType& RegTypeCache::FromDescriptor(mirror::ClassLoader* loader, const char* descriptor, |
| 70 | bool precise) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 71 | DCHECK(RegTypeCache::primitive_initialized_); |
| 72 | if (descriptor[1] == '\0') { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 73 | switch (descriptor[0]) { |
| 74 | case 'Z': |
| 75 | return Boolean(); |
| 76 | case 'B': |
| 77 | return Byte(); |
| 78 | case 'S': |
| 79 | return Short(); |
| 80 | case 'C': |
| 81 | return Char(); |
| 82 | case 'I': |
| 83 | return Integer(); |
| 84 | case 'J': |
| 85 | return LongLo(); |
| 86 | case 'F': |
| 87 | return Float(); |
| 88 | case 'D': |
| 89 | return DoubleLo(); |
| 90 | case 'V': // For void types, conflict types. |
| 91 | default: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 92 | return Conflict(); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 93 | } |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 94 | } else if (descriptor[0] == 'L' || descriptor[0] == '[') { |
| 95 | return From(loader, descriptor, precise); |
| 96 | } else { |
| 97 | return Conflict(); |
| 98 | } |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 99 | } |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 100 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 101 | const RegType& RegTypeCache::RegTypeFromPrimitiveType(Primitive::Type prim_type) const { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 102 | DCHECK(RegTypeCache::primitive_initialized_); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 103 | switch (prim_type) { |
| 104 | case Primitive::kPrimBoolean: |
| 105 | return *BooleanType::GetInstance(); |
| 106 | case Primitive::kPrimByte: |
| 107 | return *ByteType::GetInstance(); |
| 108 | case Primitive::kPrimShort: |
| 109 | return *ShortType::GetInstance(); |
| 110 | case Primitive::kPrimChar: |
| 111 | return *CharType::GetInstance(); |
| 112 | case Primitive::kPrimInt: |
| 113 | return *IntegerType::GetInstance(); |
| 114 | case Primitive::kPrimLong: |
| 115 | return *LongLoType::GetInstance(); |
| 116 | case Primitive::kPrimFloat: |
| 117 | return *FloatType::GetInstance(); |
| 118 | case Primitive::kPrimDouble: |
| 119 | return *DoubleLoType::GetInstance(); |
| 120 | case Primitive::kPrimVoid: |
| 121 | default: |
| 122 | return *ConflictType::GetInstance(); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 126 | bool RegTypeCache::MatchDescriptor(size_t idx, const StringPiece& descriptor, bool precise) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 127 | const RegType* entry = entries_[idx]; |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 128 | if (descriptor != entry->descriptor_) { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 129 | return false; |
| 130 | } |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 131 | if (entry->HasClass()) { |
| 132 | return MatchingPrecisionForClass(entry, precise); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 133 | } |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 134 | // There is no notion of precise unresolved references, the precise information is just dropped |
| 135 | // on the floor. |
| 136 | DCHECK(entry->IsUnresolvedReference()); |
| 137 | return true; |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 140 | mirror::Class* RegTypeCache::ResolveClass(const char* descriptor, mirror::ClassLoader* loader) { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 141 | // Class was not found, must create new type. |
| 142 | // Try resolving class |
| 143 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 144 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 145 | StackHandleScope<1> hs(self); |
| 146 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(loader)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 147 | mirror::Class* klass = nullptr; |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 148 | if (can_load_classes_) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 149 | klass = class_linker->FindClass(self, descriptor, class_loader); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 150 | } else { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 151 | klass = class_linker->LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), |
| 152 | loader); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 153 | if (klass != nullptr && !klass->IsLoaded()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 154 | // We found the class but without it being loaded its not safe for use. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 155 | klass = nullptr; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 156 | } |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 157 | } |
| 158 | return klass; |
| 159 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 160 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 161 | const RegType& RegTypeCache::From(mirror::ClassLoader* loader, const char* descriptor, |
| 162 | bool precise) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 163 | // Try looking up the class in the cache first. We use a StringPiece to avoid continual strlen |
| 164 | // operations on the descriptor. |
| 165 | StringPiece descriptor_sp(descriptor); |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 166 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 167 | if (MatchDescriptor(i, descriptor_sp, precise)) { |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 168 | return *(entries_[i]); |
| 169 | } |
| 170 | } |
| 171 | // Class not found in the cache, will create a new type for that. |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 172 | // Try resolving class. |
| 173 | mirror::Class* klass = ResolveClass(descriptor, loader); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 174 | if (klass != nullptr) { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 175 | // Class resolved, first look for the class in the list of entries |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 176 | // Class was not found, must create new type. |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 177 | // To pass the verification, the type should be imprecise, |
Sameer Abu Asal | 9f57a86 | 2013-04-19 10:08:00 -0700 | [diff] [blame] | 178 | // instantiable or an interface with the precise type set to false. |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 179 | DCHECK(!precise || klass->IsInstantiable()); |
Sameer Abu Asal | 9f57a86 | 2013-04-19 10:08:00 -0700 | [diff] [blame] | 180 | // Create a precise type if: |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 181 | // 1- Class is final and NOT an interface. a precise interface is meaningless !! |
Sameer Abu Asal | 9f57a86 | 2013-04-19 10:08:00 -0700 | [diff] [blame] | 182 | // 2- Precise Flag passed as true. |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 183 | RegType* entry; |
Sameer Abu Asal | 9f57a86 | 2013-04-19 10:08:00 -0700 | [diff] [blame] | 184 | // Create an imprecise type if we can't tell for a fact that it is precise. |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 185 | if (klass->CannotBeAssignedFromOtherTypes() || precise) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 186 | DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass()); |
| 187 | DCHECK(!klass->IsInterface()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 188 | entry = new PreciseReferenceType(klass, descriptor_sp.as_string(), entries_.size()); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 189 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 190 | entry = new ReferenceType(klass, descriptor_sp.as_string(), entries_.size()); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 191 | } |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 192 | AddEntry(entry); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 193 | return *entry; |
| 194 | } else { // Class not resolved. |
| 195 | // We tried loading the class and failed, this might get an exception raised |
| 196 | // so we want to clear it before we go on. |
Andreas Gampe | 6398156 | 2014-04-17 12:28:43 -0700 | [diff] [blame] | 197 | if (can_load_classes_) { |
| 198 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 199 | Thread::Current()->ClearException(); |
| 200 | } else { |
| 201 | DCHECK(!Thread::Current()->IsExceptionPending()); |
| 202 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 203 | if (IsValidDescriptor(descriptor)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 204 | RegType* entry = new UnresolvedReferenceType(descriptor_sp.as_string(), entries_.size()); |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 205 | AddEntry(entry); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 206 | return *entry; |
| 207 | } else { |
| 208 | // The descriptor is broken return the unknown type as there's nothing sensible that |
| 209 | // could be done at runtime |
| 210 | return Conflict(); |
| 211 | } |
| 212 | } |
| 213 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 214 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 215 | const RegType& RegTypeCache::FromClass(const char* descriptor, mirror::Class* klass, bool precise) { |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 216 | DCHECK(klass != nullptr); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 217 | if (klass->IsPrimitive()) { |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 218 | // Note: precise isn't used for primitive classes. A char is assignable to an int. All |
| 219 | // primitive classes are final. |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 220 | return RegTypeFromPrimitiveType(klass->GetPrimitiveType()); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 221 | } else { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 222 | // Look for the reference in the list of entries to have. |
| 223 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 224 | const RegType* cur_entry = entries_[i]; |
Ian Rogers | 5441091 | 2014-09-10 15:33:05 -0700 | [diff] [blame] | 225 | if (cur_entry->klass_.Read() == klass && MatchingPrecisionForClass(cur_entry, precise)) { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 226 | return *cur_entry; |
| 227 | } |
| 228 | } |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 229 | // No reference to the class was found, create new reference. |
| 230 | RegType* entry; |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 231 | if (precise) { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 232 | entry = new PreciseReferenceType(klass, descriptor, entries_.size()); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 233 | } else { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 234 | entry = new ReferenceType(klass, descriptor, entries_.size()); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 235 | } |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 236 | AddEntry(entry); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 237 | return *entry; |
| 238 | } |
| 239 | } |
| 240 | |
Mathieu Chartier | 12d625f | 2015-03-13 11:33:37 -0700 | [diff] [blame] | 241 | RegTypeCache::RegTypeCache(bool can_load_classes) : can_load_classes_(can_load_classes) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 242 | if (kIsDebugBuild) { |
| 243 | Thread::Current()->AssertThreadSuspensionIsAllowable(gAborting == 0); |
Andreas Gampe | 6398156 | 2014-04-17 12:28:43 -0700 | [diff] [blame] | 244 | } |
| 245 | entries_.reserve(64); |
| 246 | FillPrimitiveAndSmallConstantTypes(); |
| 247 | } |
| 248 | |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 249 | RegTypeCache::~RegTypeCache() { |
| 250 | CHECK_LE(primitive_count_, entries_.size()); |
| 251 | // Delete only the non primitive types. |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 252 | if (entries_.size() == kNumPrimitivesAndSmallConstants) { |
| 253 | // All entries are from the global pool, nothing to delete. |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 254 | return; |
| 255 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 256 | std::vector<const RegType*>::iterator non_primitive_begin = entries_.begin(); |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 257 | std::advance(non_primitive_begin, kNumPrimitivesAndSmallConstants); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 258 | STLDeleteContainerPointers(non_primitive_begin, entries_.end()); |
| 259 | } |
| 260 | |
| 261 | void RegTypeCache::ShutDown() { |
| 262 | if (RegTypeCache::primitive_initialized_) { |
| 263 | UndefinedType::Destroy(); |
| 264 | ConflictType::Destroy(); |
| 265 | BooleanType::Destroy(); |
| 266 | ByteType::Destroy(); |
| 267 | ShortType::Destroy(); |
| 268 | CharType::Destroy(); |
| 269 | IntegerType::Destroy(); |
| 270 | LongLoType::Destroy(); |
| 271 | LongHiType::Destroy(); |
Ian Rogers | 25ae7eb | 2013-04-18 16:06:05 -0700 | [diff] [blame] | 272 | FloatType::Destroy(); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 273 | DoubleLoType::Destroy(); |
| 274 | DoubleHiType::Destroy(); |
Ian Rogers | dfe78a6 | 2013-11-14 09:20:55 -0800 | [diff] [blame] | 275 | for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 276 | const PreciseConstType* type = small_precise_constants_[value - kMinSmallConstant]; |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 277 | delete type; |
Ian Rogers | dfe78a6 | 2013-11-14 09:20:55 -0800 | [diff] [blame] | 278 | small_precise_constants_[value - kMinSmallConstant] = nullptr; |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 279 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 280 | RegTypeCache::primitive_initialized_ = false; |
| 281 | RegTypeCache::primitive_count_ = 0; |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 285 | template <class Type> |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 286 | const Type* RegTypeCache::CreatePrimitiveTypeInstance(const std::string& descriptor) { |
| 287 | mirror::Class* klass = nullptr; |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 288 | // Try loading the class from linker. |
| 289 | if (!descriptor.empty()) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 290 | klass = art::Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(), |
| 291 | descriptor.c_str()); |
Logan Chien | 10f0ca2 | 2014-09-23 23:01:47 +0800 | [diff] [blame] | 292 | DCHECK(klass != nullptr); |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 293 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 294 | const Type* entry = Type::CreateInstance(klass, descriptor, RegTypeCache::primitive_count_); |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 295 | RegTypeCache::primitive_count_++; |
| 296 | return entry; |
| 297 | } |
| 298 | |
| 299 | void RegTypeCache::CreatePrimitiveAndSmallConstantTypes() { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 300 | CreatePrimitiveTypeInstance<UndefinedType>(""); |
| 301 | CreatePrimitiveTypeInstance<ConflictType>(""); |
| 302 | CreatePrimitiveTypeInstance<BooleanType>("Z"); |
| 303 | CreatePrimitiveTypeInstance<ByteType>("B"); |
| 304 | CreatePrimitiveTypeInstance<ShortType>("S"); |
| 305 | CreatePrimitiveTypeInstance<CharType>("C"); |
| 306 | CreatePrimitiveTypeInstance<IntegerType>("I"); |
| 307 | CreatePrimitiveTypeInstance<LongLoType>("J"); |
| 308 | CreatePrimitiveTypeInstance<LongHiType>("J"); |
| 309 | CreatePrimitiveTypeInstance<FloatType>("F"); |
| 310 | CreatePrimitiveTypeInstance<DoubleLoType>("D"); |
| 311 | CreatePrimitiveTypeInstance<DoubleHiType>("D"); |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 312 | for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) { |
| 313 | PreciseConstType* type = new PreciseConstType(value, primitive_count_); |
| 314 | small_precise_constants_[value - kMinSmallConstant] = type; |
| 315 | primitive_count_++; |
| 316 | } |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 319 | const RegType& RegTypeCache::FromUnresolvedMerge(const RegType& left, const RegType& right) { |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 320 | std::set<uint16_t> types; |
| 321 | if (left.IsUnresolvedMergedReference()) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 322 | RegType& non_const(const_cast<RegType&>(left)); |
| 323 | types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes(); |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 324 | } else { |
| 325 | types.insert(left.GetId()); |
| 326 | } |
| 327 | if (right.IsUnresolvedMergedReference()) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 328 | RegType& non_const(const_cast<RegType&>(right)); |
| 329 | std::set<uint16_t> right_types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes(); |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 330 | types.insert(right_types.begin(), right_types.end()); |
| 331 | } else { |
| 332 | types.insert(right.GetId()); |
| 333 | } |
| 334 | // Check if entry already exists. |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 335 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 336 | const RegType* cur_entry = entries_[i]; |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 337 | if (cur_entry->IsUnresolvedMergedReference()) { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 338 | std::set<uint16_t> cur_entry_types = |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 339 | (down_cast<const UnresolvedMergedType*>(cur_entry))->GetMergedTypes(); |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 340 | if (cur_entry_types == types) { |
| 341 | return *cur_entry; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | // Create entry. |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 346 | RegType* entry = new UnresolvedMergedType(left.GetId(), right.GetId(), this, entries_.size()); |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 347 | AddEntry(entry); |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 348 | if (kIsDebugBuild) { |
| 349 | UnresolvedMergedType* tmp_entry = down_cast<UnresolvedMergedType*>(entry); |
| 350 | std::set<uint16_t> check_types = tmp_entry->GetMergedTypes(); |
| 351 | CHECK(check_types == types); |
| 352 | } |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 353 | return *entry; |
| 354 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 355 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 356 | const RegType& RegTypeCache::FromUnresolvedSuperClass(const RegType& child) { |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 357 | // Check if entry already exists. |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 358 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 359 | const RegType* cur_entry = entries_[i]; |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 360 | if (cur_entry->IsUnresolvedSuperClass()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 361 | const UnresolvedSuperClass* tmp_entry = |
| 362 | down_cast<const UnresolvedSuperClass*>(cur_entry); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 363 | uint16_t unresolved_super_child_id = |
| 364 | tmp_entry->GetUnresolvedSuperClassChildId(); |
| 365 | if (unresolved_super_child_id == child.GetId()) { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 366 | return *cur_entry; |
| 367 | } |
| 368 | } |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 369 | } |
| 370 | RegType* entry = new UnresolvedSuperClass(child.GetId(), this, entries_.size()); |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 371 | AddEntry(entry); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 372 | return *entry; |
| 373 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 374 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 375 | const UninitializedType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 376 | UninitializedType* entry = nullptr; |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 377 | const std::string& descriptor(type.GetDescriptor()); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 378 | if (type.IsUnresolvedTypes()) { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 379 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 380 | const RegType* cur_entry = entries_[i]; |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 381 | if (cur_entry->IsUnresolvedAndUninitializedReference() && |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 382 | down_cast<const UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc() |
| 383 | == allocation_pc && |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 384 | (cur_entry->GetDescriptor() == descriptor)) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 385 | return *down_cast<const UnresolvedUninitializedRefType*>(cur_entry); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 386 | } |
| 387 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 388 | entry = new UnresolvedUninitializedRefType(descriptor, allocation_pc, entries_.size()); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 389 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 390 | mirror::Class* klass = type.GetClass(); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 391 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 392 | const RegType* cur_entry = entries_[i]; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 393 | if (cur_entry->IsUninitializedReference() && |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 394 | down_cast<const UninitializedReferenceType*>(cur_entry) |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 395 | ->GetAllocationPc() == allocation_pc && |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 396 | cur_entry->GetClass() == klass) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 397 | return *down_cast<const UninitializedReferenceType*>(cur_entry); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 400 | entry = new UninitializedReferenceType(klass, descriptor, allocation_pc, entries_.size()); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 401 | } |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 402 | AddEntry(entry); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 403 | return *entry; |
| 404 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 405 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 406 | const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 407 | RegType* entry; |
Sameer Abu Asal | 9f57a86 | 2013-04-19 10:08:00 -0700 | [diff] [blame] | 408 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 409 | if (uninit_type.IsUnresolvedTypes()) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 410 | const std::string& descriptor(uninit_type.GetDescriptor()); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 411 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 412 | const RegType* cur_entry = entries_[i]; |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 413 | if (cur_entry->IsUnresolvedReference() && |
| 414 | cur_entry->GetDescriptor() == descriptor) { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 415 | return *cur_entry; |
| 416 | } |
| 417 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 418 | entry = new UnresolvedReferenceType(descriptor, entries_.size()); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 419 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 420 | mirror::Class* klass = uninit_type.GetClass(); |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 421 | if (uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) { |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 422 | // For uninitialized "this reference" look for reference types that are not precise. |
Sameer Abu Asal | 9f57a86 | 2013-04-19 10:08:00 -0700 | [diff] [blame] | 423 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 424 | const RegType* cur_entry = entries_[i]; |
Sameer Abu Asal | 9f57a86 | 2013-04-19 10:08:00 -0700 | [diff] [blame] | 425 | if (cur_entry->IsReference() && cur_entry->GetClass() == klass) { |
| 426 | return *cur_entry; |
| 427 | } |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 428 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 429 | entry = new ReferenceType(klass, "", entries_.size()); |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 430 | } else if (klass->IsInstantiable()) { |
| 431 | // We're uninitialized because of allocation, look or create a precise type as allocations |
| 432 | // may only create objects of that type. |
| 433 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 434 | const RegType* cur_entry = entries_[i]; |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 435 | if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) { |
| 436 | return *cur_entry; |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 437 | } |
Sameer Abu Asal | 9f57a86 | 2013-04-19 10:08:00 -0700 | [diff] [blame] | 438 | } |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 439 | entry = new PreciseReferenceType(klass, uninit_type.GetDescriptor(), entries_.size()); |
| 440 | } else { |
| 441 | return Conflict(); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 442 | } |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 443 | } |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 444 | AddEntry(entry); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 445 | return *entry; |
| 446 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 447 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 448 | const UninitializedType& RegTypeCache::UninitializedThisArgument(const RegType& type) { |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 449 | UninitializedType* entry; |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 450 | const std::string& descriptor(type.GetDescriptor()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 451 | if (type.IsUnresolvedTypes()) { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 452 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 453 | const RegType* cur_entry = entries_[i]; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 454 | if (cur_entry->IsUnresolvedAndUninitializedThisReference() && |
| 455 | cur_entry->GetDescriptor() == descriptor) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 456 | return *down_cast<const UninitializedType*>(cur_entry); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 457 | } |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 458 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 459 | entry = new UnresolvedUninitializedThisRefType(descriptor, entries_.size()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 460 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 461 | mirror::Class* klass = type.GetClass(); |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 462 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 463 | const RegType* cur_entry = entries_[i]; |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 464 | if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 465 | return *down_cast<const UninitializedType*>(cur_entry); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 466 | } |
| 467 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 468 | entry = new UninitializedThisReferenceType(klass, descriptor, entries_.size()); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 469 | } |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 470 | AddEntry(entry); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 471 | return *entry; |
| 472 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 473 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 474 | const ConstantType& RegTypeCache::FromCat1NonSmallConstant(int32_t value, bool precise) { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 475 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 476 | const RegType* cur_entry = entries_[i]; |
Ian Rogers | 5441091 | 2014-09-10 15:33:05 -0700 | [diff] [blame] | 477 | if (cur_entry->klass_.IsNull() && cur_entry->IsConstant() && |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 478 | cur_entry->IsPreciseConstant() == precise && |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 479 | (down_cast<const ConstantType*>(cur_entry))->ConstantValue() == value) { |
| 480 | return *down_cast<const ConstantType*>(cur_entry); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 483 | ConstantType* entry; |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 484 | if (precise) { |
| 485 | entry = new PreciseConstType(value, entries_.size()); |
| 486 | } else { |
| 487 | entry = new ImpreciseConstType(value, entries_.size()); |
| 488 | } |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 489 | AddEntry(entry); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 490 | return *entry; |
| 491 | } |
| 492 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 493 | const ConstantType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 494 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 495 | const RegType* cur_entry = entries_[i]; |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 496 | if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) && |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 497 | (down_cast<const ConstantType*>(cur_entry))->ConstantValueLo() == value) { |
| 498 | return *down_cast<const ConstantType*>(cur_entry); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 499 | } |
| 500 | } |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 501 | ConstantType* entry; |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 502 | if (precise) { |
| 503 | entry = new PreciseConstLoType(value, entries_.size()); |
| 504 | } else { |
| 505 | entry = new ImpreciseConstLoType(value, entries_.size()); |
| 506 | } |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 507 | AddEntry(entry); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 508 | return *entry; |
| 509 | } |
| 510 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 511 | const ConstantType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 512 | for (size_t i = primitive_count_; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 513 | const RegType* cur_entry = entries_[i]; |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 514 | if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) && |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 515 | (down_cast<const ConstantType*>(cur_entry))->ConstantValueHi() == value) { |
| 516 | return *down_cast<const ConstantType*>(cur_entry); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
Ian Rogers | 41c65c1 | 2013-09-05 16:57:17 -0700 | [diff] [blame] | 519 | ConstantType* entry; |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 520 | if (precise) { |
| 521 | entry = new PreciseConstHiType(value, entries_.size()); |
| 522 | } else { |
| 523 | entry = new ImpreciseConstHiType(value, entries_.size()); |
| 524 | } |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 525 | AddEntry(entry); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 526 | return *entry; |
| 527 | } |
| 528 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 529 | const RegType& RegTypeCache::GetComponentType(const RegType& array, mirror::ClassLoader* loader) { |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 530 | if (!array.IsArrayTypes()) { |
| 531 | return Conflict(); |
| 532 | } else if (array.IsUnresolvedTypes()) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 533 | const std::string& descriptor(array.GetDescriptor()); |
| 534 | const std::string component(descriptor.substr(1, descriptor.size() - 1)); |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 535 | return FromDescriptor(loader, component.c_str(), false); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 536 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 537 | mirror::Class* klass = array.GetClass()->GetComponentType(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 538 | std::string temp; |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 539 | if (klass->IsErroneous()) { |
| 540 | // Arrays may have erroneous component types, use unresolved in that case. |
| 541 | // We assume that the primitive classes are not erroneous, so we know it is a |
| 542 | // reference type. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 543 | return FromDescriptor(loader, klass->GetDescriptor(&temp), false); |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 544 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 545 | return FromClass(klass->GetDescriptor(&temp), klass, |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 546 | klass->CannotBeAssignedFromOtherTypes()); |
| 547 | } |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
| 551 | void RegTypeCache::Dump(std::ostream& os) { |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 552 | for (size_t i = 0; i < entries_.size(); i++) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 553 | const RegType* cur_entry = entries_[i]; |
| 554 | if (cur_entry != nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 555 | os << i << ": " << cur_entry->Dump() << "\n"; |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 556 | } |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
Mathieu Chartier | 7c438b1 | 2014-09-12 17:01:24 -0700 | [diff] [blame] | 560 | void RegTypeCache::VisitStaticRoots(RootCallback* callback, void* arg) { |
| 561 | // Visit the primitive types, this is required since if there are no active verifiers they wont |
| 562 | // be in the entries array, and therefore not visited as roots. |
| 563 | if (primitive_initialized_) { |
Mathieu Chartier | 12d625f | 2015-03-13 11:33:37 -0700 | [diff] [blame] | 564 | RootInfo ri(kRootUnknown); |
| 565 | UndefinedType::GetInstance()->VisitRoots(callback, arg, ri); |
| 566 | ConflictType::GetInstance()->VisitRoots(callback, arg, ri); |
| 567 | BooleanType::GetInstance()->VisitRoots(callback, arg, ri); |
| 568 | ByteType::GetInstance()->VisitRoots(callback, arg, ri); |
| 569 | ShortType::GetInstance()->VisitRoots(callback, arg, ri); |
| 570 | CharType::GetInstance()->VisitRoots(callback, arg, ri); |
| 571 | IntegerType::GetInstance()->VisitRoots(callback, arg, ri); |
| 572 | LongLoType::GetInstance()->VisitRoots(callback, arg, ri); |
| 573 | LongHiType::GetInstance()->VisitRoots(callback, arg, ri); |
| 574 | FloatType::GetInstance()->VisitRoots(callback, arg, ri); |
| 575 | DoubleLoType::GetInstance()->VisitRoots(callback, arg, ri); |
| 576 | DoubleHiType::GetInstance()->VisitRoots(callback, arg, ri); |
Mathieu Chartier | 7c438b1 | 2014-09-12 17:01:24 -0700 | [diff] [blame] | 577 | for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) { |
Mathieu Chartier | 12d625f | 2015-03-13 11:33:37 -0700 | [diff] [blame] | 578 | small_precise_constants_[value - kMinSmallConstant]->VisitRoots(callback, arg, ri); |
Mathieu Chartier | 7c438b1 | 2014-09-12 17:01:24 -0700 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
Mathieu Chartier | 12d625f | 2015-03-13 11:33:37 -0700 | [diff] [blame] | 583 | void RegTypeCache::VisitRoots(RootCallback* callback, void* arg, const RootInfo& root_info) { |
Hiroshi Yamauchi | 3e78162 | 2015-02-25 12:52:34 -0800 | [diff] [blame] | 584 | // Exclude the static roots that are visited by VisitStaticRoots(). |
| 585 | for (size_t i = primitive_count_; i < entries_.size(); ++i) { |
Mathieu Chartier | 12d625f | 2015-03-13 11:33:37 -0700 | [diff] [blame] | 586 | entries_[i]->VisitRoots(callback, arg, root_info); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
Andreas Gampe | aa910d5 | 2014-07-30 18:59:05 -0700 | [diff] [blame] | 590 | void RegTypeCache::AddEntry(RegType* new_entry) { |
| 591 | entries_.push_back(new_entry); |
| 592 | } |
| 593 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 594 | } // namespace verifier |
| 595 | } // namespace art |