blob: 689a33e2586423e44848f9fef072a2613e00eb0e [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#include "object_utils.h"
25
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 Rogers41c65c12013-09-05 16:57:17 -070031PreciseConstType* RegTypeCache::small_precise_constants_[kMaxSmallConstant - kMinSmallConstant + 1];
Ian Rogers776ac1f2012-04-13 23:36:36 -070032
Ian Rogersb4903572012-10-11 11:52:56 -070033static bool MatchingPrecisionForClass(RegType* entry, bool precise)
34 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 Rogers1bf8d4d2013-05-30 00:18:49 -070069const RegType& RegTypeCache::FromDescriptor(mirror::ClassLoader* loader, const char* descriptor,
70 bool precise) {
71 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 Rogers1bf8d4d2013-05-30 00:18:49 -0700101const RegType& RegTypeCache::RegTypeFromPrimitiveType(Primitive::Type prim_type) const {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800102 CHECK(RegTypeCache::primitive_initialized_);
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 Rogers776ac1f2012-04-13 23:36:36 -0700123 }
124}
125
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700126bool RegTypeCache::MatchDescriptor(size_t idx, const char* descriptor, bool precise) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700127 RegType* entry = entries_[idx];
128 if (entry->descriptor_ != descriptor) {
129 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));
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800147 mirror::Class* klass = NULL;
148 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 Rogers1bf8d4d2013-05-30 00:18:49 -0700151 klass = class_linker->LookupClass(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 Rogers1bf8d4d2013-05-30 00:18:49 -0700160const RegType& RegTypeCache::From(mirror::ClassLoader* loader, const char* descriptor,
161 bool precise) {
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700162 // Try looking up the class in the cache first.
163 for (size_t i = primitive_count_; i < entries_.size(); i++) {
164 if (MatchDescriptor(i, descriptor, precise)) {
165 return *(entries_[i]);
166 }
167 }
168 // Class not found in the cache, will create a new type for that.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800169 // Try resolving class.
170 mirror::Class* klass = ResolveClass(descriptor, loader);
171 if (klass != NULL) {
172 // Class resolved, first look for the class in the list of entries
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800173 // Class was not found, must create new type.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700174 // To pass the verification, the type should be imprecise,
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700175 // instantiable or an interface with the precise type set to false.
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700176 DCHECK(!precise || klass->IsInstantiable());
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700177 // Create a precise type if:
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700178 // 1- Class is final and NOT an interface. a precise interface is meaningless !!
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700179 // 2- Precise Flag passed as true.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800180 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700181 // Create an imprecise type if we can't tell for a fact that it is precise.
Ian Rogers04f94f42013-06-10 15:09:26 -0700182 if (klass->CannotBeAssignedFromOtherTypes() || precise) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700183 DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass());
184 DCHECK(!klass->IsInterface());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800185 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
186 } else {
187 entry = new ReferenceType(klass, descriptor, entries_.size());
188 }
189 entries_.push_back(entry);
190 return *entry;
191 } else { // Class not resolved.
192 // We tried loading the class and failed, this might get an exception raised
193 // so we want to clear it before we go on.
Andreas Gampe63981562014-04-17 12:28:43 -0700194 if (can_load_classes_) {
195 DCHECK(Thread::Current()->IsExceptionPending());
196 Thread::Current()->ClearException();
197 } else {
198 DCHECK(!Thread::Current()->IsExceptionPending());
199 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700200 if (IsValidDescriptor(descriptor)) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800201 RegType* entry = new UnresolvedReferenceType(descriptor, entries_.size());
202 entries_.push_back(entry);
203 return *entry;
204 } else {
205 // The descriptor is broken return the unknown type as there's nothing sensible that
206 // could be done at runtime
207 return Conflict();
208 }
209 }
210}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700211
Ian Rogers637c65b2013-05-31 11:46:00 -0700212const RegType& RegTypeCache::FromClass(const char* descriptor, mirror::Class* klass, bool precise) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700213 DCHECK(klass != nullptr);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700214 if (klass->IsPrimitive()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700215 // Note: precise isn't used for primitive classes. A char is assignable to an int. All
216 // primitive classes are final.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800217 return RegTypeFromPrimitiveType(klass->GetPrimitiveType());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700218 } else {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800219 // Look for the reference in the list of entries to have.
220 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700221 RegType* cur_entry = entries_[i];
Ian Rogers637c65b2013-05-31 11:46:00 -0700222 if (cur_entry->klass_ == klass && MatchingPrecisionForClass(cur_entry, precise)) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700223 return *cur_entry;
224 }
225 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800226 // No reference to the class was found, create new reference.
227 RegType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800228 if (precise) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700229 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800230 } else {
Ian Rogers637c65b2013-05-31 11:46:00 -0700231 entry = new ReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800232 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700233 entries_.push_back(entry);
234 return *entry;
235 }
236}
237
Andreas Gampe63981562014-04-17 12:28:43 -0700238RegTypeCache::RegTypeCache(bool can_load_classes) : can_load_classes_(can_load_classes) {
239 if (kIsDebugBuild && can_load_classes) {
240 Thread::Current()->AssertThreadSuspensionIsAllowable();
241 }
242 entries_.reserve(64);
243 FillPrimitiveAndSmallConstantTypes();
244}
245
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800246RegTypeCache::~RegTypeCache() {
247 CHECK_LE(primitive_count_, entries_.size());
248 // Delete only the non primitive types.
Ian Rogers41c65c12013-09-05 16:57:17 -0700249 if (entries_.size() == kNumPrimitivesAndSmallConstants) {
250 // All entries are from the global pool, nothing to delete.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800251 return;
252 }
253 std::vector<RegType*>::iterator non_primitive_begin = entries_.begin();
Ian Rogers41c65c12013-09-05 16:57:17 -0700254 std::advance(non_primitive_begin, kNumPrimitivesAndSmallConstants);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800255 STLDeleteContainerPointers(non_primitive_begin, entries_.end());
256}
257
258void RegTypeCache::ShutDown() {
259 if (RegTypeCache::primitive_initialized_) {
260 UndefinedType::Destroy();
261 ConflictType::Destroy();
262 BooleanType::Destroy();
263 ByteType::Destroy();
264 ShortType::Destroy();
265 CharType::Destroy();
266 IntegerType::Destroy();
267 LongLoType::Destroy();
268 LongHiType::Destroy();
Ian Rogers25ae7eb2013-04-18 16:06:05 -0700269 FloatType::Destroy();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800270 DoubleLoType::Destroy();
271 DoubleHiType::Destroy();
Ian Rogersdfe78a62013-11-14 09:20:55 -0800272 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700273 PreciseConstType* type = small_precise_constants_[value - kMinSmallConstant];
274 delete type;
Ian Rogersdfe78a62013-11-14 09:20:55 -0800275 small_precise_constants_[value - kMinSmallConstant] = nullptr;
Ian Rogers41c65c12013-09-05 16:57:17 -0700276 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800277 RegTypeCache::primitive_initialized_ = false;
278 RegTypeCache::primitive_count_ = 0;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800279 }
280}
281
Ian Rogers41c65c12013-09-05 16:57:17 -0700282template <class Type>
283Type* RegTypeCache::CreatePrimitiveTypeInstance(const std::string& descriptor) {
284 mirror::Class* klass = NULL;
285 // Try loading the class from linker.
286 if (!descriptor.empty()) {
Ian Rogers98379392014-02-24 16:53:16 -0800287 klass = art::Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(),
288 descriptor.c_str());
Ian Rogers41c65c12013-09-05 16:57:17 -0700289 }
290 Type* entry = Type::CreateInstance(klass, descriptor, RegTypeCache::primitive_count_);
291 RegTypeCache::primitive_count_++;
292 return entry;
293}
294
295void RegTypeCache::CreatePrimitiveAndSmallConstantTypes() {
Ian Rogers33e95662013-05-20 20:29:14 -0700296 CreatePrimitiveTypeInstance<UndefinedType>("");
297 CreatePrimitiveTypeInstance<ConflictType>("");
298 CreatePrimitiveTypeInstance<BooleanType>("Z");
299 CreatePrimitiveTypeInstance<ByteType>("B");
300 CreatePrimitiveTypeInstance<ShortType>("S");
301 CreatePrimitiveTypeInstance<CharType>("C");
302 CreatePrimitiveTypeInstance<IntegerType>("I");
303 CreatePrimitiveTypeInstance<LongLoType>("J");
304 CreatePrimitiveTypeInstance<LongHiType>("J");
305 CreatePrimitiveTypeInstance<FloatType>("F");
306 CreatePrimitiveTypeInstance<DoubleLoType>("D");
307 CreatePrimitiveTypeInstance<DoubleHiType>("D");
Ian Rogers41c65c12013-09-05 16:57:17 -0700308 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
309 PreciseConstType* type = new PreciseConstType(value, primitive_count_);
310 small_precise_constants_[value - kMinSmallConstant] = type;
311 primitive_count_++;
312 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800313}
314
Ian Rogers529781d2012-07-23 17:24:29 -0700315const RegType& RegTypeCache::FromUnresolvedMerge(const RegType& left, const RegType& right) {
316 std::set<uint16_t> types;
317 if (left.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800318 RegType& non_const(const_cast<RegType&>(left));
319 types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700320 } else {
321 types.insert(left.GetId());
322 }
323 if (right.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800324 RegType& non_const(const_cast<RegType&>(right));
325 std::set<uint16_t> right_types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700326 types.insert(right_types.begin(), right_types.end());
327 } else {
328 types.insert(right.GetId());
329 }
330 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800331 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers529781d2012-07-23 17:24:29 -0700332 RegType* cur_entry = entries_[i];
333 if (cur_entry->IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800334 std::set<uint16_t> cur_entry_types =
335 (down_cast<UnresolvedMergedType*>(cur_entry))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700336 if (cur_entry_types == types) {
337 return *cur_entry;
338 }
339 }
340 }
341 // Create entry.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800342 RegType* entry = new UnresolvedMergedType(left.GetId(), right.GetId(), this, entries_.size());
Ian Rogers529781d2012-07-23 17:24:29 -0700343 entries_.push_back(entry);
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700344 if (kIsDebugBuild) {
345 UnresolvedMergedType* tmp_entry = down_cast<UnresolvedMergedType*>(entry);
346 std::set<uint16_t> check_types = tmp_entry->GetMergedTypes();
347 CHECK(check_types == types);
348 }
Ian Rogers529781d2012-07-23 17:24:29 -0700349 return *entry;
350}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700351
Ian Rogers529781d2012-07-23 17:24:29 -0700352const RegType& RegTypeCache::FromUnresolvedSuperClass(const RegType& child) {
353 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800354 for (size_t i = primitive_count_; i < entries_.size(); i++) {
355 RegType* cur_entry = entries_[i];
356 if (cur_entry->IsUnresolvedSuperClass()) {
357 UnresolvedSuperClass* tmp_entry =
358 down_cast<UnresolvedSuperClass*>(cur_entry);
359 uint16_t unresolved_super_child_id =
360 tmp_entry->GetUnresolvedSuperClassChildId();
361 if (unresolved_super_child_id == child.GetId()) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700362 return *cur_entry;
363 }
364 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800365 }
366 RegType* entry = new UnresolvedSuperClass(child.GetId(), this, entries_.size());
367 entries_.push_back(entry);
368 return *entry;
369}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700370
Ian Rogers41c65c12013-09-05 16:57:17 -0700371const UninitializedType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
372 UninitializedType* entry = NULL;
Ian Rogers637c65b2013-05-31 11:46:00 -0700373 const std::string& descriptor(type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800374 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800375 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700376 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800377 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
378 down_cast<UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc() == allocation_pc &&
379 (cur_entry->GetDescriptor() == descriptor)) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700380 return *down_cast<UnresolvedUninitializedRefType*>(cur_entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800381 }
382 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700383 entry = new UnresolvedUninitializedRefType(descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700384 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800386 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700387 RegType* cur_entry = entries_[i];
Ian Rogers776ac1f2012-04-13 23:36:36 -0700388 if (cur_entry->IsUninitializedReference() &&
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700389 down_cast<UninitializedReferenceType*>(cur_entry)
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800390 ->GetAllocationPc() == allocation_pc &&
Ian Rogers776ac1f2012-04-13 23:36:36 -0700391 cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700392 return *down_cast<UninitializedReferenceType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700393 }
394 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700395 entry = new UninitializedReferenceType(klass, descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700396 }
397 entries_.push_back(entry);
398 return *entry;
399}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700400
Ian Rogers776ac1f2012-04-13 23:36:36 -0700401const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
402 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700403
Ian Rogers776ac1f2012-04-13 23:36:36 -0700404 if (uninit_type.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700405 const std::string& descriptor(uninit_type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800406 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700407 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800408 if (cur_entry->IsUnresolvedReference() &&
409 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700410 return *cur_entry;
411 }
412 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700413 entry = new UnresolvedReferenceType(descriptor.c_str(), entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700414 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800415 mirror::Class* klass = uninit_type.GetClass();
Brian Carlstromdf629502013-07-17 22:39:56 -0700416 if (uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700417 // For uninitialized "this reference" look for reference types that are not precise.
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700418 for (size_t i = primitive_count_; i < entries_.size(); i++) {
419 RegType* cur_entry = entries_[i];
420 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
421 return *cur_entry;
422 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700423 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700424 entry = new ReferenceType(klass, "", entries_.size());
Ian Rogers62342ec2013-06-11 10:26:37 -0700425 } else if (klass->IsInstantiable()) {
426 // We're uninitialized because of allocation, look or create a precise type as allocations
427 // may only create objects of that type.
428 for (size_t i = primitive_count_; i < entries_.size(); i++) {
429 RegType* cur_entry = entries_[i];
430 if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) {
431 return *cur_entry;
Ian Rogers04f94f42013-06-10 15:09:26 -0700432 }
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700433 }
Ian Rogers62342ec2013-06-11 10:26:37 -0700434 entry = new PreciseReferenceType(klass, uninit_type.GetDescriptor(), entries_.size());
435 } else {
436 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700437 }
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700438 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700439 entries_.push_back(entry);
440 return *entry;
441}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700442
Ian Rogers41c65c12013-09-05 16:57:17 -0700443const ImpreciseConstType& RegTypeCache::ByteConstant() {
444 const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::min(), false);
445 DCHECK(result.IsImpreciseConstant());
446 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800447}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700448
Sebastien Hertz849600b2013-12-20 10:28:08 +0100449const ImpreciseConstType& RegTypeCache::CharConstant() {
450 int32_t jchar_max = static_cast<int32_t>(std::numeric_limits<jchar>::max());
451 const ConstantType& result = FromCat1Const(jchar_max, false);
452 DCHECK(result.IsImpreciseConstant());
453 return *down_cast<const ImpreciseConstType*>(&result);
454}
455
Ian Rogers41c65c12013-09-05 16:57:17 -0700456const ImpreciseConstType& RegTypeCache::ShortConstant() {
457 const ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::min(), false);
458 DCHECK(result.IsImpreciseConstant());
459 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800460}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700461
Ian Rogers41c65c12013-09-05 16:57:17 -0700462const ImpreciseConstType& RegTypeCache::IntConstant() {
463 const ConstantType& result = FromCat1Const(std::numeric_limits<jint>::max(), false);
464 DCHECK(result.IsImpreciseConstant());
465 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800466}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700467
Sebastien Hertz849600b2013-12-20 10:28:08 +0100468const ImpreciseConstType& RegTypeCache::PosByteConstant() {
469 const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::max(), false);
470 DCHECK(result.IsImpreciseConstant());
471 return *down_cast<const ImpreciseConstType*>(&result);
472}
473
474const ImpreciseConstType& RegTypeCache::PosShortConstant() {
475 const ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::max(), false);
476 DCHECK(result.IsImpreciseConstant());
477 return *down_cast<const ImpreciseConstType*>(&result);
478}
479
Ian Rogers41c65c12013-09-05 16:57:17 -0700480const UninitializedType& RegTypeCache::UninitializedThisArgument(const RegType& type) {
481 UninitializedType* entry;
Ian Rogers637c65b2013-05-31 11:46:00 -0700482 const std::string& descriptor(type.GetDescriptor());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700483 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800484 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700485 RegType* cur_entry = entries_[i];
486 if (cur_entry->IsUnresolvedAndUninitializedThisReference() &&
487 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700488 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700489 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700490 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700491 entry = new UnresolvedUninitializedThisRefType(descriptor, entries_.size());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700492 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800493 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800494 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700495 RegType* cur_entry = entries_[i];
Ian Rogers62342ec2013-06-11 10:26:37 -0700496 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700497 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700498 }
499 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700500 entry = new UninitializedThisReferenceType(klass, descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700501 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700502 entries_.push_back(entry);
503 return *entry;
504}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700505
Ian Rogers41c65c12013-09-05 16:57:17 -0700506const ConstantType& RegTypeCache::FromCat1NonSmallConstant(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800507 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700508 RegType* cur_entry = entries_[i];
Ian Rogers637c65b2013-05-31 11:46:00 -0700509 if (cur_entry->klass_ == NULL && cur_entry->IsConstant() &&
510 cur_entry->IsPreciseConstant() == precise &&
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800511 (down_cast<ConstantType*>(cur_entry))->ConstantValue() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700512 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700513 }
514 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700515 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800516 if (precise) {
517 entry = new PreciseConstType(value, entries_.size());
518 } else {
519 entry = new ImpreciseConstType(value, entries_.size());
520 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800521 entries_.push_back(entry);
522 return *entry;
523}
524
Ian Rogers41c65c12013-09-05 16:57:17 -0700525const ConstantType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800526 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800527 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800528 if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) &&
529 (down_cast<ConstantType*>(cur_entry))->ConstantValueLo() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700530 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800531 }
532 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700533 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800534 if (precise) {
535 entry = new PreciseConstLoType(value, entries_.size());
536 } else {
537 entry = new ImpreciseConstLoType(value, entries_.size());
538 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800539 entries_.push_back(entry);
540 return *entry;
541}
542
Ian Rogers41c65c12013-09-05 16:57:17 -0700543const ConstantType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800544 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800545 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800546 if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) &&
547 (down_cast<ConstantType*>(cur_entry))->ConstantValueHi() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700548 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800549 }
550 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700551 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800552 if (precise) {
553 entry = new PreciseConstHiType(value, entries_.size());
554 } else {
555 entry = new ImpreciseConstHiType(value, entries_.size());
556 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700557 entries_.push_back(entry);
558 return *entry;
559}
560
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800561const RegType& RegTypeCache::GetComponentType(const RegType& array, mirror::ClassLoader* loader) {
Ian Rogersa9a82542013-10-04 11:17:26 -0700562 if (!array.IsArrayTypes()) {
563 return Conflict();
564 } else if (array.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700565 const std::string& descriptor(array.GetDescriptor());
566 const std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogersb4903572012-10-11 11:52:56 -0700567 return FromDescriptor(loader, component.c_str(), false);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700568 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800569 mirror::Class* klass = array.GetClass()->GetComponentType();
Ian Rogers04f94f42013-06-10 15:09:26 -0700570 return FromClass(ClassHelper(klass).GetDescriptor(), klass,
571 klass->CannotBeAssignedFromOtherTypes());
Ian Rogersb4903572012-10-11 11:52:56 -0700572 }
573}
574
575void RegTypeCache::Dump(std::ostream& os) {
Ian Rogersb4903572012-10-11 11:52:56 -0700576 for (size_t i = 0; i < entries_.size(); i++) {
577 RegType* cur_entry = entries_[i];
578 if (cur_entry != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800579 os << i << ": " << cur_entry->Dump() << "\n";
Ian Rogersb4903572012-10-11 11:52:56 -0700580 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700581 }
582}
583
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800584void RegTypeCache::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800585 for (RegType* entry : entries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800586 entry->VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800587 }
588}
589
Elliott Hughesa21039c2012-06-21 12:09:25 -0700590} // namespace verifier
591} // namespace art