blob: 641f327a1212c442d8bef24b8b5834541135f4d4 [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 Rogers776ac1f2012-04-13 23:36:36 -070024
25namespace art {
26namespace verifier {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070027
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080028bool RegTypeCache::primitive_initialized_ = false;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080029uint16_t RegTypeCache::primitive_count_ = 0;
Ian Rogers41c65c12013-09-05 16:57:17 -070030PreciseConstType* RegTypeCache::small_precise_constants_[kMaxSmallConstant - kMinSmallConstant + 1];
Ian Rogers776ac1f2012-04-13 23:36:36 -070031
Ian Rogersb4903572012-10-11 11:52:56 -070032static bool MatchingPrecisionForClass(RegType* entry, bool precise)
33 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers04f94f42013-06-10 15:09:26 -070034 if (entry->IsPreciseReference() == precise) {
35 // We were or weren't looking for a precise reference and we found what we need.
36 return true;
37 } else {
38 if (!precise && entry->GetClass()->CannotBeAssignedFromOtherTypes()) {
39 // We weren't looking for a precise reference, as we're looking up based on a descriptor, but
40 // we found a matching entry based on the descriptor. Return the precise entry in that case.
41 return true;
42 }
43 return false;
44 }
Ian Rogersb4903572012-10-11 11:52:56 -070045}
46
Ian Rogers41c65c12013-09-05 16:57:17 -070047void RegTypeCache::FillPrimitiveAndSmallConstantTypes() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080048 entries_.push_back(UndefinedType::GetInstance());
49 entries_.push_back(ConflictType::GetInstance());
50 entries_.push_back(BooleanType::GetInstance());
51 entries_.push_back(ByteType::GetInstance());
52 entries_.push_back(ShortType::GetInstance());
53 entries_.push_back(CharType::GetInstance());
54 entries_.push_back(IntegerType::GetInstance());
55 entries_.push_back(LongLoType::GetInstance());
56 entries_.push_back(LongHiType::GetInstance());
57 entries_.push_back(FloatType::GetInstance());
58 entries_.push_back(DoubleLoType::GetInstance());
59 entries_.push_back(DoubleHiType::GetInstance());
Ian Rogers41c65c12013-09-05 16:57:17 -070060 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
61 int32_t i = value - kMinSmallConstant;
62 DCHECK_EQ(entries_.size(), small_precise_constants_[i]->GetId());
63 entries_.push_back(small_precise_constants_[i]);
64 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080065 DCHECK_EQ(entries_.size(), primitive_count_);
66}
67
Ian Rogersd8f69b02014-09-10 21:43:52 +000068const RegType& RegTypeCache::FromDescriptor(mirror::ClassLoader* loader, const char* descriptor,
69 bool precise) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070070 DCHECK(RegTypeCache::primitive_initialized_);
71 if (descriptor[1] == '\0') {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080072 switch (descriptor[0]) {
73 case 'Z':
74 return Boolean();
75 case 'B':
76 return Byte();
77 case 'S':
78 return Short();
79 case 'C':
80 return Char();
81 case 'I':
82 return Integer();
83 case 'J':
84 return LongLo();
85 case 'F':
86 return Float();
87 case 'D':
88 return DoubleLo();
89 case 'V': // For void types, conflict types.
90 default:
Ian Rogersad0b3a32012-04-16 14:50:24 -070091 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -070092 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080093 } else if (descriptor[0] == 'L' || descriptor[0] == '[') {
94 return From(loader, descriptor, precise);
95 } else {
96 return Conflict();
97 }
98};
99
Ian Rogersd8f69b02014-09-10 21:43:52 +0000100const RegType& RegTypeCache::RegTypeFromPrimitiveType(Primitive::Type prim_type) const {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800101 CHECK(RegTypeCache::primitive_initialized_);
102 switch (prim_type) {
103 case Primitive::kPrimBoolean:
104 return *BooleanType::GetInstance();
105 case Primitive::kPrimByte:
106 return *ByteType::GetInstance();
107 case Primitive::kPrimShort:
108 return *ShortType::GetInstance();
109 case Primitive::kPrimChar:
110 return *CharType::GetInstance();
111 case Primitive::kPrimInt:
112 return *IntegerType::GetInstance();
113 case Primitive::kPrimLong:
114 return *LongLoType::GetInstance();
115 case Primitive::kPrimFloat:
116 return *FloatType::GetInstance();
117 case Primitive::kPrimDouble:
118 return *DoubleLoType::GetInstance();
119 case Primitive::kPrimVoid:
120 default:
121 return *ConflictType::GetInstance();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700122 }
123}
124
Ian Rogers1ff3c982014-08-12 02:30:58 -0700125bool RegTypeCache::MatchDescriptor(size_t idx, const StringPiece& descriptor, bool precise) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700126 RegType* entry = entries_[idx];
Ian Rogers1ff3c982014-08-12 02:30:58 -0700127 if (descriptor != entry->descriptor_) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700128 return false;
129 }
Ian Rogers04f94f42013-06-10 15:09:26 -0700130 if (entry->HasClass()) {
131 return MatchingPrecisionForClass(entry, precise);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800132 }
Ian Rogers04f94f42013-06-10 15:09:26 -0700133 // There is no notion of precise unresolved references, the precise information is just dropped
134 // on the floor.
135 DCHECK(entry->IsUnresolvedReference());
136 return true;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800137}
138
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700139mirror::Class* RegTypeCache::ResolveClass(const char* descriptor, mirror::ClassLoader* loader) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800140 // Class was not found, must create new type.
141 // Try resolving class
142 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -0800143 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700144 StackHandleScope<1> hs(self);
145 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(loader));
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800146 mirror::Class* klass = NULL;
147 if (can_load_classes_) {
Ian Rogers98379392014-02-24 16:53:16 -0800148 klass = class_linker->FindClass(self, descriptor, class_loader);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800149 } else {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700150 klass = class_linker->LookupClass(descriptor, loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700151 if (klass != nullptr && !klass->IsLoaded()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800152 // We found the class but without it being loaded its not safe for use.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700153 klass = nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800154 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800155 }
156 return klass;
157}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700158
Ian Rogersd8f69b02014-09-10 21:43:52 +0000159const RegType& RegTypeCache::From(mirror::ClassLoader* loader, const char* descriptor,
160 bool precise) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700161 // Try looking up the class in the cache first. We use a StringPiece to avoid continual strlen
162 // operations on the descriptor.
163 StringPiece descriptor_sp(descriptor);
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700164 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700165 if (MatchDescriptor(i, descriptor_sp, precise)) {
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700166 return *(entries_[i]);
167 }
168 }
169 // Class not found in the cache, will create a new type for that.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800170 // Try resolving class.
171 mirror::Class* klass = ResolveClass(descriptor, loader);
172 if (klass != NULL) {
173 // Class resolved, first look for the class in the list of entries
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800174 // Class was not found, must create new type.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700175 // To pass the verification, the type should be imprecise,
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700176 // instantiable or an interface with the precise type set to false.
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700177 DCHECK(!precise || klass->IsInstantiable());
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700178 // Create a precise type if:
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700179 // 1- Class is final and NOT an interface. a precise interface is meaningless !!
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700180 // 2- Precise Flag passed as true.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800181 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700182 // Create an imprecise type if we can't tell for a fact that it is precise.
Ian Rogers04f94f42013-06-10 15:09:26 -0700183 if (klass->CannotBeAssignedFromOtherTypes() || precise) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700184 DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass());
185 DCHECK(!klass->IsInterface());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700186 entry = new PreciseReferenceType(klass, descriptor_sp.as_string(), entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800187 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700188 entry = new ReferenceType(klass, descriptor_sp.as_string(), entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800189 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700190 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800191 return *entry;
192 } else { // Class not resolved.
193 // We tried loading the class and failed, this might get an exception raised
194 // so we want to clear it before we go on.
Andreas Gampe63981562014-04-17 12:28:43 -0700195 if (can_load_classes_) {
196 DCHECK(Thread::Current()->IsExceptionPending());
197 Thread::Current()->ClearException();
198 } else {
199 DCHECK(!Thread::Current()->IsExceptionPending());
200 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700201 if (IsValidDescriptor(descriptor)) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700202 RegType* entry = new UnresolvedReferenceType(descriptor_sp.as_string(), entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700203 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800204 return *entry;
205 } else {
206 // The descriptor is broken return the unknown type as there's nothing sensible that
207 // could be done at runtime
208 return Conflict();
209 }
210 }
211}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700212
Ian Rogersd8f69b02014-09-10 21:43:52 +0000213const RegType& RegTypeCache::FromClass(const char* descriptor, mirror::Class* klass, bool precise) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700214 DCHECK(klass != nullptr);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700215 if (klass->IsPrimitive()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700216 // Note: precise isn't used for primitive classes. A char is assignable to an int. All
217 // primitive classes are final.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800218 return RegTypeFromPrimitiveType(klass->GetPrimitiveType());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700219 } else {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800220 // Look for the reference in the list of entries to have.
221 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700222 RegType* cur_entry = entries_[i];
Ian Rogersd8f69b02014-09-10 21:43:52 +0000223 if (cur_entry->klass_ == klass && MatchingPrecisionForClass(cur_entry, precise)) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700224 return *cur_entry;
225 }
226 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800227 // No reference to the class was found, create new reference.
228 RegType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800229 if (precise) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700230 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800231 } else {
Ian Rogers637c65b2013-05-31 11:46:00 -0700232 entry = new ReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800233 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700234 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700235 return *entry;
236 }
237}
238
Andreas Gampe63981562014-04-17 12:28:43 -0700239RegTypeCache::RegTypeCache(bool can_load_classes) : can_load_classes_(can_load_classes) {
240 if (kIsDebugBuild && can_load_classes) {
241 Thread::Current()->AssertThreadSuspensionIsAllowable();
242 }
243 entries_.reserve(64);
244 FillPrimitiveAndSmallConstantTypes();
245}
246
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800247RegTypeCache::~RegTypeCache() {
248 CHECK_LE(primitive_count_, entries_.size());
249 // Delete only the non primitive types.
Ian Rogers41c65c12013-09-05 16:57:17 -0700250 if (entries_.size() == kNumPrimitivesAndSmallConstants) {
251 // All entries are from the global pool, nothing to delete.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800252 return;
253 }
254 std::vector<RegType*>::iterator non_primitive_begin = entries_.begin();
Ian Rogers41c65c12013-09-05 16:57:17 -0700255 std::advance(non_primitive_begin, kNumPrimitivesAndSmallConstants);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800256 STLDeleteContainerPointers(non_primitive_begin, entries_.end());
257}
258
259void RegTypeCache::ShutDown() {
260 if (RegTypeCache::primitive_initialized_) {
261 UndefinedType::Destroy();
262 ConflictType::Destroy();
263 BooleanType::Destroy();
264 ByteType::Destroy();
265 ShortType::Destroy();
266 CharType::Destroy();
267 IntegerType::Destroy();
268 LongLoType::Destroy();
269 LongHiType::Destroy();
Ian Rogers25ae7eb2013-04-18 16:06:05 -0700270 FloatType::Destroy();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800271 DoubleLoType::Destroy();
272 DoubleHiType::Destroy();
Ian Rogersdfe78a62013-11-14 09:20:55 -0800273 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700274 PreciseConstType* type = small_precise_constants_[value - kMinSmallConstant];
275 delete type;
Ian Rogersdfe78a62013-11-14 09:20:55 -0800276 small_precise_constants_[value - kMinSmallConstant] = nullptr;
Ian Rogers41c65c12013-09-05 16:57:17 -0700277 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800278 RegTypeCache::primitive_initialized_ = false;
279 RegTypeCache::primitive_count_ = 0;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800280 }
281}
282
Ian Rogers41c65c12013-09-05 16:57:17 -0700283template <class Type>
284Type* RegTypeCache::CreatePrimitiveTypeInstance(const std::string& descriptor) {
285 mirror::Class* klass = NULL;
286 // Try loading the class from linker.
287 if (!descriptor.empty()) {
Ian Rogers98379392014-02-24 16:53:16 -0800288 klass = art::Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(),
289 descriptor.c_str());
Ian Rogers41c65c12013-09-05 16:57:17 -0700290 }
291 Type* entry = Type::CreateInstance(klass, descriptor, RegTypeCache::primitive_count_);
292 RegTypeCache::primitive_count_++;
293 return entry;
294}
295
296void RegTypeCache::CreatePrimitiveAndSmallConstantTypes() {
Ian Rogers33e95662013-05-20 20:29:14 -0700297 CreatePrimitiveTypeInstance<UndefinedType>("");
298 CreatePrimitiveTypeInstance<ConflictType>("");
299 CreatePrimitiveTypeInstance<BooleanType>("Z");
300 CreatePrimitiveTypeInstance<ByteType>("B");
301 CreatePrimitiveTypeInstance<ShortType>("S");
302 CreatePrimitiveTypeInstance<CharType>("C");
303 CreatePrimitiveTypeInstance<IntegerType>("I");
304 CreatePrimitiveTypeInstance<LongLoType>("J");
305 CreatePrimitiveTypeInstance<LongHiType>("J");
306 CreatePrimitiveTypeInstance<FloatType>("F");
307 CreatePrimitiveTypeInstance<DoubleLoType>("D");
308 CreatePrimitiveTypeInstance<DoubleHiType>("D");
Ian Rogers41c65c12013-09-05 16:57:17 -0700309 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
310 PreciseConstType* type = new PreciseConstType(value, primitive_count_);
311 small_precise_constants_[value - kMinSmallConstant] = type;
312 primitive_count_++;
313 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800314}
315
Ian Rogersd8f69b02014-09-10 21:43:52 +0000316const RegType& RegTypeCache::FromUnresolvedMerge(const RegType& left, const RegType& right) {
Ian Rogers529781d2012-07-23 17:24:29 -0700317 std::set<uint16_t> types;
318 if (left.IsUnresolvedMergedReference()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +0000319 RegType& non_const(const_cast<RegType&>(left));
320 types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700321 } else {
322 types.insert(left.GetId());
323 }
324 if (right.IsUnresolvedMergedReference()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +0000325 RegType& non_const(const_cast<RegType&>(right));
326 std::set<uint16_t> right_types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700327 types.insert(right_types.begin(), right_types.end());
328 } else {
329 types.insert(right.GetId());
330 }
331 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800332 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers529781d2012-07-23 17:24:29 -0700333 RegType* cur_entry = entries_[i];
334 if (cur_entry->IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800335 std::set<uint16_t> cur_entry_types =
336 (down_cast<UnresolvedMergedType*>(cur_entry))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700337 if (cur_entry_types == types) {
338 return *cur_entry;
339 }
340 }
341 }
342 // Create entry.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800343 RegType* entry = new UnresolvedMergedType(left.GetId(), right.GetId(), this, entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700344 AddEntry(entry);
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700345 if (kIsDebugBuild) {
346 UnresolvedMergedType* tmp_entry = down_cast<UnresolvedMergedType*>(entry);
347 std::set<uint16_t> check_types = tmp_entry->GetMergedTypes();
348 CHECK(check_types == types);
349 }
Ian Rogers529781d2012-07-23 17:24:29 -0700350 return *entry;
351}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700352
Ian Rogersd8f69b02014-09-10 21:43:52 +0000353const RegType& RegTypeCache::FromUnresolvedSuperClass(const RegType& child) {
Ian Rogers529781d2012-07-23 17:24:29 -0700354 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800355 for (size_t i = primitive_count_; i < entries_.size(); i++) {
356 RegType* cur_entry = entries_[i];
357 if (cur_entry->IsUnresolvedSuperClass()) {
358 UnresolvedSuperClass* tmp_entry =
359 down_cast<UnresolvedSuperClass*>(cur_entry);
360 uint16_t unresolved_super_child_id =
361 tmp_entry->GetUnresolvedSuperClassChildId();
362 if (unresolved_super_child_id == child.GetId()) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700363 return *cur_entry;
364 }
365 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800366 }
367 RegType* entry = new UnresolvedSuperClass(child.GetId(), this, entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700368 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800369 return *entry;
370}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700371
Ian Rogersd8f69b02014-09-10 21:43:52 +0000372const UninitializedType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700373 UninitializedType* entry = NULL;
Ian Rogers637c65b2013-05-31 11:46:00 -0700374 const std::string& descriptor(type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800375 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800376 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700377 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800378 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
379 down_cast<UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc() == allocation_pc &&
380 (cur_entry->GetDescriptor() == descriptor)) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700381 return *down_cast<UnresolvedUninitializedRefType*>(cur_entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800382 }
383 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700384 entry = new UnresolvedUninitializedRefType(descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700385 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800386 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800387 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700388 RegType* cur_entry = entries_[i];
Ian Rogers776ac1f2012-04-13 23:36:36 -0700389 if (cur_entry->IsUninitializedReference() &&
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700390 down_cast<UninitializedReferenceType*>(cur_entry)
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800391 ->GetAllocationPc() == allocation_pc &&
Ian Rogers776ac1f2012-04-13 23:36:36 -0700392 cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700393 return *down_cast<UninitializedReferenceType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700394 }
395 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700396 entry = new UninitializedReferenceType(klass, descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700397 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700398 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700399 return *entry;
400}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700401
Ian Rogersd8f69b02014-09-10 21:43:52 +0000402const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700403 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700404
Ian Rogers776ac1f2012-04-13 23:36:36 -0700405 if (uninit_type.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700406 const std::string& descriptor(uninit_type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800407 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700408 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800409 if (cur_entry->IsUnresolvedReference() &&
410 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700411 return *cur_entry;
412 }
413 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700414 entry = new UnresolvedReferenceType(descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700415 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800416 mirror::Class* klass = uninit_type.GetClass();
Brian Carlstromdf629502013-07-17 22:39:56 -0700417 if (uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700418 // For uninitialized "this reference" look for reference types that are not precise.
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700419 for (size_t i = primitive_count_; i < entries_.size(); i++) {
420 RegType* cur_entry = entries_[i];
421 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
422 return *cur_entry;
423 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700424 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700425 entry = new ReferenceType(klass, "", entries_.size());
Ian Rogers62342ec2013-06-11 10:26:37 -0700426 } else if (klass->IsInstantiable()) {
427 // We're uninitialized because of allocation, look or create a precise type as allocations
428 // may only create objects of that type.
429 for (size_t i = primitive_count_; i < entries_.size(); i++) {
430 RegType* cur_entry = entries_[i];
431 if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) {
432 return *cur_entry;
Ian Rogers04f94f42013-06-10 15:09:26 -0700433 }
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700434 }
Ian Rogers62342ec2013-06-11 10:26:37 -0700435 entry = new PreciseReferenceType(klass, uninit_type.GetDescriptor(), entries_.size());
436 } else {
437 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700438 }
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700439 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700440 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700441 return *entry;
442}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700443
Ian Rogersd8f69b02014-09-10 21:43:52 +0000444const ImpreciseConstType& RegTypeCache::ByteConstant() {
445 const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::min(), false);
Ian Rogers41c65c12013-09-05 16:57:17 -0700446 DCHECK(result.IsImpreciseConstant());
Ian Rogersd8f69b02014-09-10 21:43:52 +0000447 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800448}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700449
Ian Rogersd8f69b02014-09-10 21:43:52 +0000450const ImpreciseConstType& RegTypeCache::CharConstant() {
Sebastien Hertz849600b2013-12-20 10:28:08 +0100451 int32_t jchar_max = static_cast<int32_t>(std::numeric_limits<jchar>::max());
Ian Rogersd8f69b02014-09-10 21:43:52 +0000452 const ConstantType& result = FromCat1Const(jchar_max, false);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100453 DCHECK(result.IsImpreciseConstant());
Ian Rogersd8f69b02014-09-10 21:43:52 +0000454 return *down_cast<const ImpreciseConstType*>(&result);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100455}
456
Ian Rogersd8f69b02014-09-10 21:43:52 +0000457const ImpreciseConstType& RegTypeCache::ShortConstant() {
458 const ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::min(), false);
Ian Rogers41c65c12013-09-05 16:57:17 -0700459 DCHECK(result.IsImpreciseConstant());
Ian Rogersd8f69b02014-09-10 21:43:52 +0000460 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800461}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700462
Ian Rogersd8f69b02014-09-10 21:43:52 +0000463const ImpreciseConstType& RegTypeCache::IntConstant() {
464 const ConstantType& result = FromCat1Const(std::numeric_limits<jint>::max(), false);
Ian Rogers41c65c12013-09-05 16:57:17 -0700465 DCHECK(result.IsImpreciseConstant());
Ian Rogersd8f69b02014-09-10 21:43:52 +0000466 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800467}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700468
Ian Rogersd8f69b02014-09-10 21:43:52 +0000469const ImpreciseConstType& RegTypeCache::PosByteConstant() {
470 const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::max(), false);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100471 DCHECK(result.IsImpreciseConstant());
Ian Rogersd8f69b02014-09-10 21:43:52 +0000472 return *down_cast<const ImpreciseConstType*>(&result);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100473}
474
Ian Rogersd8f69b02014-09-10 21:43:52 +0000475const ImpreciseConstType& RegTypeCache::PosShortConstant() {
476 const ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::max(), false);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100477 DCHECK(result.IsImpreciseConstant());
Ian Rogersd8f69b02014-09-10 21:43:52 +0000478 return *down_cast<const ImpreciseConstType*>(&result);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100479}
480
Ian Rogersd8f69b02014-09-10 21:43:52 +0000481const UninitializedType& RegTypeCache::UninitializedThisArgument(const RegType& type) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700482 UninitializedType* entry;
Ian Rogers637c65b2013-05-31 11:46:00 -0700483 const std::string& descriptor(type.GetDescriptor());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700484 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800485 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700486 RegType* cur_entry = entries_[i];
487 if (cur_entry->IsUnresolvedAndUninitializedThisReference() &&
488 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700489 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700490 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700491 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700492 entry = new UnresolvedUninitializedThisRefType(descriptor, entries_.size());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700493 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800494 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800495 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700496 RegType* cur_entry = entries_[i];
Ian Rogers62342ec2013-06-11 10:26:37 -0700497 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700498 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700499 }
500 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700501 entry = new UninitializedThisReferenceType(klass, descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700502 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700503 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700504 return *entry;
505}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700506
Ian Rogersd8f69b02014-09-10 21:43:52 +0000507const ConstantType& RegTypeCache::FromCat1NonSmallConstant(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800508 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700509 RegType* cur_entry = entries_[i];
Ian Rogersd8f69b02014-09-10 21:43:52 +0000510 if (cur_entry->klass_ == NULL && cur_entry->IsConstant() &&
Ian Rogers637c65b2013-05-31 11:46:00 -0700511 cur_entry->IsPreciseConstant() == precise &&
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800512 (down_cast<ConstantType*>(cur_entry))->ConstantValue() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700513 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700514 }
515 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700516 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800517 if (precise) {
518 entry = new PreciseConstType(value, entries_.size());
519 } else {
520 entry = new ImpreciseConstType(value, entries_.size());
521 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700522 AddEntry(entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800523 return *entry;
524}
525
Ian Rogersd8f69b02014-09-10 21:43:52 +0000526const ConstantType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800527 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800528 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800529 if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) &&
530 (down_cast<ConstantType*>(cur_entry))->ConstantValueLo() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700531 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800532 }
533 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700534 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800535 if (precise) {
536 entry = new PreciseConstLoType(value, entries_.size());
537 } else {
538 entry = new ImpreciseConstLoType(value, entries_.size());
539 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700540 AddEntry(entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800541 return *entry;
542}
543
Ian Rogersd8f69b02014-09-10 21:43:52 +0000544const ConstantType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800545 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800546 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800547 if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) &&
548 (down_cast<ConstantType*>(cur_entry))->ConstantValueHi() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700549 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800550 }
551 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700552 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800553 if (precise) {
554 entry = new PreciseConstHiType(value, entries_.size());
555 } else {
556 entry = new ImpreciseConstHiType(value, entries_.size());
557 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700558 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700559 return *entry;
560}
561
Ian Rogersd8f69b02014-09-10 21:43:52 +0000562const RegType& RegTypeCache::GetComponentType(const RegType& array, mirror::ClassLoader* loader) {
Ian Rogersa9a82542013-10-04 11:17:26 -0700563 if (!array.IsArrayTypes()) {
564 return Conflict();
565 } else if (array.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700566 const std::string& descriptor(array.GetDescriptor());
567 const std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogersb4903572012-10-11 11:52:56 -0700568 return FromDescriptor(loader, component.c_str(), false);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700569 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800570 mirror::Class* klass = array.GetClass()->GetComponentType();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700571 std::string temp;
Andreas Gampeaa910d52014-07-30 18:59:05 -0700572 if (klass->IsErroneous()) {
573 // Arrays may have erroneous component types, use unresolved in that case.
574 // We assume that the primitive classes are not erroneous, so we know it is a
575 // reference type.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700576 return FromDescriptor(loader, klass->GetDescriptor(&temp), false);
Andreas Gampeaa910d52014-07-30 18:59:05 -0700577 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700578 return FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampeaa910d52014-07-30 18:59:05 -0700579 klass->CannotBeAssignedFromOtherTypes());
580 }
Ian Rogersb4903572012-10-11 11:52:56 -0700581 }
582}
583
584void RegTypeCache::Dump(std::ostream& os) {
Ian Rogersb4903572012-10-11 11:52:56 -0700585 for (size_t i = 0; i < entries_.size(); i++) {
586 RegType* cur_entry = entries_[i];
587 if (cur_entry != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800588 os << i << ": " << cur_entry->Dump() << "\n";
Ian Rogersb4903572012-10-11 11:52:56 -0700589 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700590 }
591}
592
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800593void RegTypeCache::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800594 for (RegType* entry : entries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800595 entry->VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800596 }
597}
598
Andreas Gampeaa910d52014-07-30 18:59:05 -0700599void RegTypeCache::AddEntry(RegType* new_entry) {
600 entries_.push_back(new_entry);
601}
602
Elliott Hughesa21039c2012-06-21 12:09:25 -0700603} // namespace verifier
604} // namespace art