blob: 121fccb994102f71778695529b772e9956493066 [file] [log] [blame]
Ian Rogers776ac1f2012-04-13 23:36:36 -07001/*
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 Asal51a5fb72013-02-19 14:25:01 -080017#include "reg_type_cache-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070018
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080019#include "base/casts.h"
Ian Rogers98379392014-02-24 16:53:16 -080020#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/class-inl.h"
23#include "mirror/object-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070024#include "reg_type-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070025
26namespace art {
27namespace verifier {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070028
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080029bool RegTypeCache::primitive_initialized_ = false;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080030uint16_t RegTypeCache::primitive_count_ = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -070031const PreciseConstType* RegTypeCache::small_precise_constants_[kMaxSmallConstant - kMinSmallConstant + 1];
Ian Rogers776ac1f2012-04-13 23:36:36 -070032
Ian Rogers7b078e82014-09-10 14:44:24 -070033static bool MatchingPrecisionForClass(const RegType* entry, bool precise)
Ian Rogersb4903572012-10-11 11:52:56 -070034 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers04f94f42013-06-10 15:09:26 -070035 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 Rogersb4903572012-10-11 11:52:56 -070046}
47
Ian Rogers41c65c12013-09-05 16:57:17 -070048void RegTypeCache::FillPrimitiveAndSmallConstantTypes() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080049 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 Rogers41c65c12013-09-05 16:57:17 -070061 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 Asal51a5fb72013-02-19 14:25:01 -080066 DCHECK_EQ(entries_.size(), primitive_count_);
67}
68
Ian Rogersd8f69b02014-09-10 21:43:52 +000069const RegType& RegTypeCache::FromDescriptor(mirror::ClassLoader* loader, const char* descriptor,
70 bool precise) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070071 DCHECK(RegTypeCache::primitive_initialized_);
72 if (descriptor[1] == '\0') {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080073 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 Rogersad0b3a32012-04-16 14:50:24 -070092 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -070093 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080094 } else if (descriptor[0] == 'L' || descriptor[0] == '[') {
95 return From(loader, descriptor, precise);
96 } else {
97 return Conflict();
98 }
99};
100
Ian Rogersd8f69b02014-09-10 21:43:52 +0000101const RegType& RegTypeCache::RegTypeFromPrimitiveType(Primitive::Type prim_type) const {
Ian Rogers7b078e82014-09-10 14:44:24 -0700102 DCHECK(RegTypeCache::primitive_initialized_);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800103 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 Rogers776ac1f2012-04-13 23:36:36 -0700123 }
124}
125
Ian Rogers1ff3c982014-08-12 02:30:58 -0700126bool RegTypeCache::MatchDescriptor(size_t idx, const StringPiece& descriptor, bool precise) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700127 const RegType* entry = entries_[idx];
Ian Rogers1ff3c982014-08-12 02:30:58 -0700128 if (descriptor != entry->descriptor_) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700129 return false;
130 }
Ian Rogers04f94f42013-06-10 15:09:26 -0700131 if (entry->HasClass()) {
132 return MatchingPrecisionForClass(entry, precise);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800133 }
Ian Rogers04f94f42013-06-10 15:09:26 -0700134 // 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 Asal51a5fb72013-02-19 14:25:01 -0800138}
139
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700140mirror::Class* RegTypeCache::ResolveClass(const char* descriptor, mirror::ClassLoader* loader) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800141 // Class was not found, must create new type.
142 // Try resolving class
143 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -0800144 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700145 StackHandleScope<1> hs(self);
146 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(loader));
Ian Rogers7b078e82014-09-10 14:44:24 -0700147 mirror::Class* klass = nullptr;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800148 if (can_load_classes_) {
Ian Rogers98379392014-02-24 16:53:16 -0800149 klass = class_linker->FindClass(self, descriptor, class_loader);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800150 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -0700151 klass = class_linker->LookupClass(self, descriptor, loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700152 if (klass != nullptr && !klass->IsLoaded()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800153 // We found the class but without it being loaded its not safe for use.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700154 klass = nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800155 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800156 }
157 return klass;
158}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700159
Ian Rogersd8f69b02014-09-10 21:43:52 +0000160const RegType& RegTypeCache::From(mirror::ClassLoader* loader, const char* descriptor,
161 bool precise) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700162 // Try looking up the class in the cache first. We use a StringPiece to avoid continual strlen
163 // operations on the descriptor.
164 StringPiece descriptor_sp(descriptor);
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700165 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700166 if (MatchDescriptor(i, descriptor_sp, precise)) {
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700167 return *(entries_[i]);
168 }
169 }
170 // Class not found in the cache, will create a new type for that.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800171 // Try resolving class.
172 mirror::Class* klass = ResolveClass(descriptor, loader);
Ian Rogers7b078e82014-09-10 14:44:24 -0700173 if (klass != nullptr) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800174 // Class resolved, first look for the class in the list of entries
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800175 // Class was not found, must create new type.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700176 // To pass the verification, the type should be imprecise,
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700177 // instantiable or an interface with the precise type set to false.
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700178 DCHECK(!precise || klass->IsInstantiable());
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700179 // Create a precise type if:
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700180 // 1- Class is final and NOT an interface. a precise interface is meaningless !!
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700181 // 2- Precise Flag passed as true.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800182 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700183 // Create an imprecise type if we can't tell for a fact that it is precise.
Ian Rogers04f94f42013-06-10 15:09:26 -0700184 if (klass->CannotBeAssignedFromOtherTypes() || precise) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700185 DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass());
186 DCHECK(!klass->IsInterface());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700187 entry = new PreciseReferenceType(klass, descriptor_sp.as_string(), entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800188 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700189 entry = new ReferenceType(klass, descriptor_sp.as_string(), entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800190 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700191 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800192 return *entry;
193 } else { // Class not resolved.
194 // We tried loading the class and failed, this might get an exception raised
195 // so we want to clear it before we go on.
Andreas Gampe63981562014-04-17 12:28:43 -0700196 if (can_load_classes_) {
197 DCHECK(Thread::Current()->IsExceptionPending());
198 Thread::Current()->ClearException();
199 } else {
200 DCHECK(!Thread::Current()->IsExceptionPending());
201 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700202 if (IsValidDescriptor(descriptor)) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700203 RegType* entry = new UnresolvedReferenceType(descriptor_sp.as_string(), entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700204 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800205 return *entry;
206 } else {
207 // The descriptor is broken return the unknown type as there's nothing sensible that
208 // could be done at runtime
209 return Conflict();
210 }
211 }
212}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700213
Ian Rogersd8f69b02014-09-10 21:43:52 +0000214const RegType& RegTypeCache::FromClass(const char* descriptor, mirror::Class* klass, bool precise) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700215 DCHECK(klass != nullptr);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700216 if (klass->IsPrimitive()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700217 // Note: precise isn't used for primitive classes. A char is assignable to an int. All
218 // primitive classes are final.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800219 return RegTypeFromPrimitiveType(klass->GetPrimitiveType());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700220 } else {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800221 // Look for the reference in the list of entries to have.
222 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700223 const RegType* cur_entry = entries_[i];
Ian Rogers54410912014-09-10 15:33:05 -0700224 if (cur_entry->klass_.Read() == klass && MatchingPrecisionForClass(cur_entry, precise)) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700225 return *cur_entry;
226 }
227 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800228 // No reference to the class was found, create new reference.
229 RegType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800230 if (precise) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700231 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800232 } else {
Ian Rogers637c65b2013-05-31 11:46:00 -0700233 entry = new ReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800234 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700235 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700236 return *entry;
237 }
238}
239
Andreas Gampe63981562014-04-17 12:28:43 -0700240RegTypeCache::RegTypeCache(bool can_load_classes) : can_load_classes_(can_load_classes) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700241 if (kIsDebugBuild) {
242 Thread::Current()->AssertThreadSuspensionIsAllowable(gAborting == 0);
Andreas Gampe63981562014-04-17 12:28:43 -0700243 }
244 entries_.reserve(64);
245 FillPrimitiveAndSmallConstantTypes();
246}
247
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800248RegTypeCache::~RegTypeCache() {
249 CHECK_LE(primitive_count_, entries_.size());
250 // Delete only the non primitive types.
Ian Rogers41c65c12013-09-05 16:57:17 -0700251 if (entries_.size() == kNumPrimitivesAndSmallConstants) {
252 // All entries are from the global pool, nothing to delete.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800253 return;
254 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700255 std::vector<const RegType*>::iterator non_primitive_begin = entries_.begin();
Ian Rogers41c65c12013-09-05 16:57:17 -0700256 std::advance(non_primitive_begin, kNumPrimitivesAndSmallConstants);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800257 STLDeleteContainerPointers(non_primitive_begin, entries_.end());
258}
259
260void RegTypeCache::ShutDown() {
261 if (RegTypeCache::primitive_initialized_) {
262 UndefinedType::Destroy();
263 ConflictType::Destroy();
264 BooleanType::Destroy();
265 ByteType::Destroy();
266 ShortType::Destroy();
267 CharType::Destroy();
268 IntegerType::Destroy();
269 LongLoType::Destroy();
270 LongHiType::Destroy();
Ian Rogers25ae7eb2013-04-18 16:06:05 -0700271 FloatType::Destroy();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800272 DoubleLoType::Destroy();
273 DoubleHiType::Destroy();
Ian Rogersdfe78a62013-11-14 09:20:55 -0800274 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700275 const PreciseConstType* type = small_precise_constants_[value - kMinSmallConstant];
Ian Rogers41c65c12013-09-05 16:57:17 -0700276 delete type;
Ian Rogersdfe78a62013-11-14 09:20:55 -0800277 small_precise_constants_[value - kMinSmallConstant] = nullptr;
Ian Rogers41c65c12013-09-05 16:57:17 -0700278 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800279 RegTypeCache::primitive_initialized_ = false;
280 RegTypeCache::primitive_count_ = 0;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800281 }
282}
283
Ian Rogers41c65c12013-09-05 16:57:17 -0700284template <class Type>
Ian Rogers7b078e82014-09-10 14:44:24 -0700285const Type* RegTypeCache::CreatePrimitiveTypeInstance(const std::string& descriptor) {
286 mirror::Class* klass = nullptr;
Ian Rogers41c65c12013-09-05 16:57:17 -0700287 // Try loading the class from linker.
288 if (!descriptor.empty()) {
Ian Rogers98379392014-02-24 16:53:16 -0800289 klass = art::Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(),
290 descriptor.c_str());
Ian Rogers41c65c12013-09-05 16:57:17 -0700291 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700292 const Type* entry = Type::CreateInstance(klass, descriptor, RegTypeCache::primitive_count_);
Ian Rogers41c65c12013-09-05 16:57:17 -0700293 RegTypeCache::primitive_count_++;
294 return entry;
295}
296
297void RegTypeCache::CreatePrimitiveAndSmallConstantTypes() {
Ian Rogers33e95662013-05-20 20:29:14 -0700298 CreatePrimitiveTypeInstance<UndefinedType>("");
299 CreatePrimitiveTypeInstance<ConflictType>("");
300 CreatePrimitiveTypeInstance<BooleanType>("Z");
301 CreatePrimitiveTypeInstance<ByteType>("B");
302 CreatePrimitiveTypeInstance<ShortType>("S");
303 CreatePrimitiveTypeInstance<CharType>("C");
304 CreatePrimitiveTypeInstance<IntegerType>("I");
305 CreatePrimitiveTypeInstance<LongLoType>("J");
306 CreatePrimitiveTypeInstance<LongHiType>("J");
307 CreatePrimitiveTypeInstance<FloatType>("F");
308 CreatePrimitiveTypeInstance<DoubleLoType>("D");
309 CreatePrimitiveTypeInstance<DoubleHiType>("D");
Ian Rogers41c65c12013-09-05 16:57:17 -0700310 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
311 PreciseConstType* type = new PreciseConstType(value, primitive_count_);
312 small_precise_constants_[value - kMinSmallConstant] = type;
313 primitive_count_++;
314 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800315}
316
Ian Rogersd8f69b02014-09-10 21:43:52 +0000317const RegType& RegTypeCache::FromUnresolvedMerge(const RegType& left, const RegType& right) {
Ian Rogers529781d2012-07-23 17:24:29 -0700318 std::set<uint16_t> types;
319 if (left.IsUnresolvedMergedReference()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +0000320 RegType& non_const(const_cast<RegType&>(left));
321 types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700322 } else {
323 types.insert(left.GetId());
324 }
325 if (right.IsUnresolvedMergedReference()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +0000326 RegType& non_const(const_cast<RegType&>(right));
327 std::set<uint16_t> right_types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700328 types.insert(right_types.begin(), right_types.end());
329 } else {
330 types.insert(right.GetId());
331 }
332 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800333 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700334 const RegType* cur_entry = entries_[i];
Ian Rogers529781d2012-07-23 17:24:29 -0700335 if (cur_entry->IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800336 std::set<uint16_t> cur_entry_types =
Ian Rogers7b078e82014-09-10 14:44:24 -0700337 (down_cast<const UnresolvedMergedType*>(cur_entry))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700338 if (cur_entry_types == types) {
339 return *cur_entry;
340 }
341 }
342 }
343 // Create entry.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800344 RegType* entry = new UnresolvedMergedType(left.GetId(), right.GetId(), this, entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700345 AddEntry(entry);
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700346 if (kIsDebugBuild) {
347 UnresolvedMergedType* tmp_entry = down_cast<UnresolvedMergedType*>(entry);
348 std::set<uint16_t> check_types = tmp_entry->GetMergedTypes();
349 CHECK(check_types == types);
350 }
Ian Rogers529781d2012-07-23 17:24:29 -0700351 return *entry;
352}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700353
Ian Rogersd8f69b02014-09-10 21:43:52 +0000354const RegType& RegTypeCache::FromUnresolvedSuperClass(const RegType& child) {
Ian Rogers529781d2012-07-23 17:24:29 -0700355 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800356 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700357 const RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800358 if (cur_entry->IsUnresolvedSuperClass()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700359 const UnresolvedSuperClass* tmp_entry =
360 down_cast<const UnresolvedSuperClass*>(cur_entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800361 uint16_t unresolved_super_child_id =
362 tmp_entry->GetUnresolvedSuperClassChildId();
363 if (unresolved_super_child_id == child.GetId()) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700364 return *cur_entry;
365 }
366 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800367 }
368 RegType* entry = new UnresolvedSuperClass(child.GetId(), this, entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700369 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800370 return *entry;
371}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700372
Ian Rogersd8f69b02014-09-10 21:43:52 +0000373const UninitializedType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700374 UninitializedType* entry = nullptr;
Ian Rogers637c65b2013-05-31 11:46:00 -0700375 const std::string& descriptor(type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800376 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800377 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700378 const RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800379 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
Ian Rogers7b078e82014-09-10 14:44:24 -0700380 down_cast<const UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc()
381 == allocation_pc &&
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800382 (cur_entry->GetDescriptor() == descriptor)) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700383 return *down_cast<const UnresolvedUninitializedRefType*>(cur_entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800384 }
385 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700386 entry = new UnresolvedUninitializedRefType(descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700387 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800389 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700390 const RegType* cur_entry = entries_[i];
Ian Rogers776ac1f2012-04-13 23:36:36 -0700391 if (cur_entry->IsUninitializedReference() &&
Ian Rogers7b078e82014-09-10 14:44:24 -0700392 down_cast<const UninitializedReferenceType*>(cur_entry)
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800393 ->GetAllocationPc() == allocation_pc &&
Ian Rogers776ac1f2012-04-13 23:36:36 -0700394 cur_entry->GetClass() == klass) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700395 return *down_cast<const UninitializedReferenceType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700396 }
397 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700398 entry = new UninitializedReferenceType(klass, descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700399 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700400 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700401 return *entry;
402}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700403
Ian Rogersd8f69b02014-09-10 21:43:52 +0000404const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700405 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700406
Ian Rogers776ac1f2012-04-13 23:36:36 -0700407 if (uninit_type.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700408 const std::string& descriptor(uninit_type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800409 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700410 const RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800411 if (cur_entry->IsUnresolvedReference() &&
412 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700413 return *cur_entry;
414 }
415 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700416 entry = new UnresolvedReferenceType(descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700417 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800418 mirror::Class* klass = uninit_type.GetClass();
Brian Carlstromdf629502013-07-17 22:39:56 -0700419 if (uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700420 // For uninitialized "this reference" look for reference types that are not precise.
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700421 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700422 const RegType* cur_entry = entries_[i];
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700423 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
424 return *cur_entry;
425 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700426 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700427 entry = new ReferenceType(klass, "", entries_.size());
Ian Rogers62342ec2013-06-11 10:26:37 -0700428 } else if (klass->IsInstantiable()) {
429 // We're uninitialized because of allocation, look or create a precise type as allocations
430 // may only create objects of that type.
431 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700432 const RegType* cur_entry = entries_[i];
Ian Rogers62342ec2013-06-11 10:26:37 -0700433 if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) {
434 return *cur_entry;
Ian Rogers04f94f42013-06-10 15:09:26 -0700435 }
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700436 }
Ian Rogers62342ec2013-06-11 10:26:37 -0700437 entry = new PreciseReferenceType(klass, uninit_type.GetDescriptor(), entries_.size());
438 } else {
439 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700440 }
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700441 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700442 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700443 return *entry;
444}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700445
Ian Rogersd8f69b02014-09-10 21:43:52 +0000446const UninitializedType& RegTypeCache::UninitializedThisArgument(const RegType& type) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700447 UninitializedType* entry;
Ian Rogers637c65b2013-05-31 11:46:00 -0700448 const std::string& descriptor(type.GetDescriptor());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700449 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800450 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700451 const RegType* cur_entry = entries_[i];
Ian Rogersad0b3a32012-04-16 14:50:24 -0700452 if (cur_entry->IsUnresolvedAndUninitializedThisReference() &&
453 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700454 return *down_cast<const UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700455 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700456 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700457 entry = new UnresolvedUninitializedThisRefType(descriptor, entries_.size());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700458 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800459 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800460 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700461 const RegType* cur_entry = entries_[i];
Ian Rogers62342ec2013-06-11 10:26:37 -0700462 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700463 return *down_cast<const UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700464 }
465 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700466 entry = new UninitializedThisReferenceType(klass, descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700467 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700468 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700469 return *entry;
470}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700471
Ian Rogersd8f69b02014-09-10 21:43:52 +0000472const ConstantType& RegTypeCache::FromCat1NonSmallConstant(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800473 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700474 const RegType* cur_entry = entries_[i];
Ian Rogers54410912014-09-10 15:33:05 -0700475 if (cur_entry->klass_.IsNull() && cur_entry->IsConstant() &&
Ian Rogers637c65b2013-05-31 11:46:00 -0700476 cur_entry->IsPreciseConstant() == precise &&
Ian Rogers7b078e82014-09-10 14:44:24 -0700477 (down_cast<const ConstantType*>(cur_entry))->ConstantValue() == value) {
478 return *down_cast<const ConstantType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700479 }
480 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700481 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800482 if (precise) {
483 entry = new PreciseConstType(value, entries_.size());
484 } else {
485 entry = new ImpreciseConstType(value, entries_.size());
486 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700487 AddEntry(entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800488 return *entry;
489}
490
Ian Rogersd8f69b02014-09-10 21:43:52 +0000491const ConstantType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800492 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700493 const RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800494 if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) &&
Ian Rogers7b078e82014-09-10 14:44:24 -0700495 (down_cast<const ConstantType*>(cur_entry))->ConstantValueLo() == value) {
496 return *down_cast<const ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800497 }
498 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700499 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800500 if (precise) {
501 entry = new PreciseConstLoType(value, entries_.size());
502 } else {
503 entry = new ImpreciseConstLoType(value, entries_.size());
504 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700505 AddEntry(entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800506 return *entry;
507}
508
Ian Rogersd8f69b02014-09-10 21:43:52 +0000509const ConstantType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800510 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700511 const RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800512 if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) &&
Ian Rogers7b078e82014-09-10 14:44:24 -0700513 (down_cast<const ConstantType*>(cur_entry))->ConstantValueHi() == value) {
514 return *down_cast<const ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800515 }
516 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700517 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800518 if (precise) {
519 entry = new PreciseConstHiType(value, entries_.size());
520 } else {
521 entry = new ImpreciseConstHiType(value, entries_.size());
522 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700523 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700524 return *entry;
525}
526
Ian Rogersd8f69b02014-09-10 21:43:52 +0000527const RegType& RegTypeCache::GetComponentType(const RegType& array, mirror::ClassLoader* loader) {
Ian Rogersa9a82542013-10-04 11:17:26 -0700528 if (!array.IsArrayTypes()) {
529 return Conflict();
530 } else if (array.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700531 const std::string& descriptor(array.GetDescriptor());
532 const std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogersb4903572012-10-11 11:52:56 -0700533 return FromDescriptor(loader, component.c_str(), false);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700534 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800535 mirror::Class* klass = array.GetClass()->GetComponentType();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700536 std::string temp;
Andreas Gampeaa910d52014-07-30 18:59:05 -0700537 if (klass->IsErroneous()) {
538 // Arrays may have erroneous component types, use unresolved in that case.
539 // We assume that the primitive classes are not erroneous, so we know it is a
540 // reference type.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700541 return FromDescriptor(loader, klass->GetDescriptor(&temp), false);
Andreas Gampeaa910d52014-07-30 18:59:05 -0700542 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700543 return FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampeaa910d52014-07-30 18:59:05 -0700544 klass->CannotBeAssignedFromOtherTypes());
545 }
Ian Rogersb4903572012-10-11 11:52:56 -0700546 }
547}
548
549void RegTypeCache::Dump(std::ostream& os) {
Ian Rogersb4903572012-10-11 11:52:56 -0700550 for (size_t i = 0; i < entries_.size(); i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700551 const RegType* cur_entry = entries_[i];
552 if (cur_entry != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800553 os << i << ": " << cur_entry->Dump() << "\n";
Ian Rogersb4903572012-10-11 11:52:56 -0700554 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700555 }
556}
557
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800558void RegTypeCache::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700559 for (const RegType* entry : entries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800560 entry->VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800561 }
562}
563
Andreas Gampeaa910d52014-07-30 18:59:05 -0700564void RegTypeCache::AddEntry(RegType* new_entry) {
565 entries_.push_back(new_entry);
566}
567
Elliott Hughesa21039c2012-06-21 12:09:25 -0700568} // namespace verifier
569} // namespace art