blob: 3d244144938151ba9cb516d0951d8d4a8f77e7da [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 Rogers4f6ad8a2013-03-18 15:27:28 -070020#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/class-inl.h"
22#include "mirror/object-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070023#include "object_utils.h"
24
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 Rogers1bf8d4d2013-05-30 00:18:49 -070068const RegType& RegTypeCache::FromDescriptor(mirror::ClassLoader* loader, const char* descriptor,
69 bool precise) {
70 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 Rogers1bf8d4d2013-05-30 00:18:49 -0700100const 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 Rogers1bf8d4d2013-05-30 00:18:49 -0700125bool RegTypeCache::MatchDescriptor(size_t idx, const char* descriptor, bool precise) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700126 RegType* entry = entries_[idx];
127 if (entry->descriptor_ != descriptor) {
128 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();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700143 SirtRef<mirror::ClassLoader> class_loader(Thread::Current(), loader);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800144 mirror::Class* klass = NULL;
145 if (can_load_classes_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700146 klass = class_linker->FindClass(descriptor, class_loader);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800147 } else {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700148 klass = class_linker->LookupClass(descriptor, loader);
Ian Rogers62d6c772013-02-27 08:32:07 -0800149 if (klass != NULL && !klass->IsLoaded()) {
150 // We found the class but without it being loaded its not safe for use.
151 klass = NULL;
152 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800153 }
154 return klass;
155}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700156
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800157void RegTypeCache::ClearException() {
158 if (can_load_classes_) {
159 DCHECK(Thread::Current()->IsExceptionPending());
160 Thread::Current()->ClearException();
161 } else {
162 DCHECK(!Thread::Current()->IsExceptionPending());
163 }
164}
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700165
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700166const RegType& RegTypeCache::From(mirror::ClassLoader* loader, const char* descriptor,
167 bool precise) {
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700168 // Try looking up the class in the cache first.
169 for (size_t i = primitive_count_; i < entries_.size(); i++) {
170 if (MatchDescriptor(i, descriptor, precise)) {
171 return *(entries_[i]);
172 }
173 }
174 // Class not found in the cache, will create a new type for that.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800175 // Try resolving class.
176 mirror::Class* klass = ResolveClass(descriptor, loader);
177 if (klass != NULL) {
178 // Class resolved, first look for the class in the list of entries
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800179 // Class was not found, must create new type.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700180 // To pass the verification, the type should be imprecise,
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700181 // instantiable or an interface with the precise type set to false.
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700182 DCHECK(!precise || klass->IsInstantiable());
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700183 // Create a precise type if:
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700184 // 1- Class is final and NOT an interface. a precise interface is meaningless !!
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700185 // 2- Precise Flag passed as true.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800186 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700187 // Create an imprecise type if we can't tell for a fact that it is precise.
Ian Rogers04f94f42013-06-10 15:09:26 -0700188 if (klass->CannotBeAssignedFromOtherTypes() || precise) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700189 DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass());
190 DCHECK(!klass->IsInterface());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800191 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
192 } else {
193 entry = new ReferenceType(klass, descriptor, entries_.size());
194 }
195 entries_.push_back(entry);
196 return *entry;
197 } else { // Class not resolved.
198 // We tried loading the class and failed, this might get an exception raised
199 // so we want to clear it before we go on.
200 ClearException();
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700201 if (IsValidDescriptor(descriptor)) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800202 RegType* entry = new UnresolvedReferenceType(descriptor, entries_.size());
203 entries_.push_back(entry);
204 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 Rogers637c65b2013-05-31 11:46:00 -0700213const RegType& RegTypeCache::FromClass(const char* descriptor, mirror::Class* klass, bool precise) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -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 Rogers637c65b2013-05-31 11:46:00 -0700223 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 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700234 entries_.push_back(entry);
235 return *entry;
236 }
237}
238
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800239RegTypeCache::~RegTypeCache() {
240 CHECK_LE(primitive_count_, entries_.size());
241 // Delete only the non primitive types.
Ian Rogers41c65c12013-09-05 16:57:17 -0700242 if (entries_.size() == kNumPrimitivesAndSmallConstants) {
243 // All entries are from the global pool, nothing to delete.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800244 return;
245 }
246 std::vector<RegType*>::iterator non_primitive_begin = entries_.begin();
Ian Rogers41c65c12013-09-05 16:57:17 -0700247 std::advance(non_primitive_begin, kNumPrimitivesAndSmallConstants);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800248 STLDeleteContainerPointers(non_primitive_begin, entries_.end());
249}
250
251void RegTypeCache::ShutDown() {
252 if (RegTypeCache::primitive_initialized_) {
253 UndefinedType::Destroy();
254 ConflictType::Destroy();
255 BooleanType::Destroy();
256 ByteType::Destroy();
257 ShortType::Destroy();
258 CharType::Destroy();
259 IntegerType::Destroy();
260 LongLoType::Destroy();
261 LongHiType::Destroy();
Ian Rogers25ae7eb2013-04-18 16:06:05 -0700262 FloatType::Destroy();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800263 DoubleLoType::Destroy();
264 DoubleHiType::Destroy();
Ian Rogersdfe78a62013-11-14 09:20:55 -0800265 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700266 PreciseConstType* type = small_precise_constants_[value - kMinSmallConstant];
267 delete type;
Ian Rogersdfe78a62013-11-14 09:20:55 -0800268 small_precise_constants_[value - kMinSmallConstant] = nullptr;
Ian Rogers41c65c12013-09-05 16:57:17 -0700269 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800270 RegTypeCache::primitive_initialized_ = false;
271 RegTypeCache::primitive_count_ = 0;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800272 }
273}
274
Ian Rogers41c65c12013-09-05 16:57:17 -0700275template <class Type>
276Type* RegTypeCache::CreatePrimitiveTypeInstance(const std::string& descriptor) {
277 mirror::Class* klass = NULL;
278 // Try loading the class from linker.
279 if (!descriptor.empty()) {
280 klass = art::Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor.c_str());
281 }
282 Type* entry = Type::CreateInstance(klass, descriptor, RegTypeCache::primitive_count_);
283 RegTypeCache::primitive_count_++;
284 return entry;
285}
286
287void RegTypeCache::CreatePrimitiveAndSmallConstantTypes() {
Ian Rogers33e95662013-05-20 20:29:14 -0700288 CreatePrimitiveTypeInstance<UndefinedType>("");
289 CreatePrimitiveTypeInstance<ConflictType>("");
290 CreatePrimitiveTypeInstance<BooleanType>("Z");
291 CreatePrimitiveTypeInstance<ByteType>("B");
292 CreatePrimitiveTypeInstance<ShortType>("S");
293 CreatePrimitiveTypeInstance<CharType>("C");
294 CreatePrimitiveTypeInstance<IntegerType>("I");
295 CreatePrimitiveTypeInstance<LongLoType>("J");
296 CreatePrimitiveTypeInstance<LongHiType>("J");
297 CreatePrimitiveTypeInstance<FloatType>("F");
298 CreatePrimitiveTypeInstance<DoubleLoType>("D");
299 CreatePrimitiveTypeInstance<DoubleHiType>("D");
Ian Rogers41c65c12013-09-05 16:57:17 -0700300 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
301 PreciseConstType* type = new PreciseConstType(value, primitive_count_);
302 small_precise_constants_[value - kMinSmallConstant] = type;
303 primitive_count_++;
304 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800305}
306
Ian Rogers529781d2012-07-23 17:24:29 -0700307const RegType& RegTypeCache::FromUnresolvedMerge(const RegType& left, const RegType& right) {
308 std::set<uint16_t> types;
309 if (left.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800310 RegType& non_const(const_cast<RegType&>(left));
311 types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700312 } else {
313 types.insert(left.GetId());
314 }
315 if (right.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800316 RegType& non_const(const_cast<RegType&>(right));
317 std::set<uint16_t> right_types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700318 types.insert(right_types.begin(), right_types.end());
319 } else {
320 types.insert(right.GetId());
321 }
322 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800323 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers529781d2012-07-23 17:24:29 -0700324 RegType* cur_entry = entries_[i];
325 if (cur_entry->IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800326 std::set<uint16_t> cur_entry_types =
327 (down_cast<UnresolvedMergedType*>(cur_entry))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700328 if (cur_entry_types == types) {
329 return *cur_entry;
330 }
331 }
332 }
333 // Create entry.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800334 RegType* entry = new UnresolvedMergedType(left.GetId(), right.GetId(), this, entries_.size());
Ian Rogers529781d2012-07-23 17:24:29 -0700335 entries_.push_back(entry);
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700336 if (kIsDebugBuild) {
337 UnresolvedMergedType* tmp_entry = down_cast<UnresolvedMergedType*>(entry);
338 std::set<uint16_t> check_types = tmp_entry->GetMergedTypes();
339 CHECK(check_types == types);
340 }
Ian Rogers529781d2012-07-23 17:24:29 -0700341 return *entry;
342}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700343
Ian Rogers529781d2012-07-23 17:24:29 -0700344const RegType& RegTypeCache::FromUnresolvedSuperClass(const RegType& child) {
345 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800346 for (size_t i = primitive_count_; i < entries_.size(); i++) {
347 RegType* cur_entry = entries_[i];
348 if (cur_entry->IsUnresolvedSuperClass()) {
349 UnresolvedSuperClass* tmp_entry =
350 down_cast<UnresolvedSuperClass*>(cur_entry);
351 uint16_t unresolved_super_child_id =
352 tmp_entry->GetUnresolvedSuperClassChildId();
353 if (unresolved_super_child_id == child.GetId()) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700354 return *cur_entry;
355 }
356 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800357 }
358 RegType* entry = new UnresolvedSuperClass(child.GetId(), this, entries_.size());
359 entries_.push_back(entry);
360 return *entry;
361}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700362
Ian Rogers41c65c12013-09-05 16:57:17 -0700363const UninitializedType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
364 UninitializedType* entry = NULL;
Ian Rogers637c65b2013-05-31 11:46:00 -0700365 const std::string& descriptor(type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800366 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800367 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700368 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800369 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
370 down_cast<UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc() == allocation_pc &&
371 (cur_entry->GetDescriptor() == descriptor)) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700372 return *down_cast<UnresolvedUninitializedRefType*>(cur_entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800373 }
374 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700375 entry = new UnresolvedUninitializedRefType(descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700376 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800378 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700379 RegType* cur_entry = entries_[i];
Ian Rogers776ac1f2012-04-13 23:36:36 -0700380 if (cur_entry->IsUninitializedReference() &&
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700381 down_cast<UninitializedReferenceType*>(cur_entry)
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800382 ->GetAllocationPc() == allocation_pc &&
Ian Rogers776ac1f2012-04-13 23:36:36 -0700383 cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700384 return *down_cast<UninitializedReferenceType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700385 }
386 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700387 entry = new UninitializedReferenceType(klass, descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700388 }
389 entries_.push_back(entry);
390 return *entry;
391}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700392
Ian Rogers776ac1f2012-04-13 23:36:36 -0700393const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
394 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700395
Ian Rogers776ac1f2012-04-13 23:36:36 -0700396 if (uninit_type.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700397 const std::string& descriptor(uninit_type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800398 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700399 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800400 if (cur_entry->IsUnresolvedReference() &&
401 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700402 return *cur_entry;
403 }
404 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700405 entry = new UnresolvedReferenceType(descriptor.c_str(), entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700406 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407 mirror::Class* klass = uninit_type.GetClass();
Brian Carlstromdf629502013-07-17 22:39:56 -0700408 if (uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700409 // For uninitialized "this reference" look for reference types that are not precise.
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700410 for (size_t i = primitive_count_; i < entries_.size(); i++) {
411 RegType* cur_entry = entries_[i];
412 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
413 return *cur_entry;
414 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700415 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700416 entry = new ReferenceType(klass, "", entries_.size());
Ian Rogers62342ec2013-06-11 10:26:37 -0700417 } else if (klass->IsInstantiable()) {
418 // We're uninitialized because of allocation, look or create a precise type as allocations
419 // may only create objects of that type.
420 for (size_t i = primitive_count_; i < entries_.size(); i++) {
421 RegType* cur_entry = entries_[i];
422 if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) {
423 return *cur_entry;
Ian Rogers04f94f42013-06-10 15:09:26 -0700424 }
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700425 }
Ian Rogers62342ec2013-06-11 10:26:37 -0700426 entry = new PreciseReferenceType(klass, uninit_type.GetDescriptor(), entries_.size());
427 } else {
428 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700429 }
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700430 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700431 entries_.push_back(entry);
432 return *entry;
433}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700434
Ian Rogers41c65c12013-09-05 16:57:17 -0700435const ImpreciseConstType& RegTypeCache::ByteConstant() {
436 const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::min(), false);
437 DCHECK(result.IsImpreciseConstant());
438 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800439}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700440
Ian Rogers41c65c12013-09-05 16:57:17 -0700441const ImpreciseConstType& RegTypeCache::ShortConstant() {
442 const ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::min(), false);
443 DCHECK(result.IsImpreciseConstant());
444 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800445}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700446
Ian Rogers41c65c12013-09-05 16:57:17 -0700447const ImpreciseConstType& RegTypeCache::IntConstant() {
448 const ConstantType& result = FromCat1Const(std::numeric_limits<jint>::max(), false);
449 DCHECK(result.IsImpreciseConstant());
450 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800451}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700452
Ian Rogers41c65c12013-09-05 16:57:17 -0700453const UninitializedType& RegTypeCache::UninitializedThisArgument(const RegType& type) {
454 UninitializedType* entry;
Ian Rogers637c65b2013-05-31 11:46:00 -0700455 const std::string& descriptor(type.GetDescriptor());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700456 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800457 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700458 RegType* cur_entry = entries_[i];
459 if (cur_entry->IsUnresolvedAndUninitializedThisReference() &&
460 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700461 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700462 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700463 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700464 entry = new UnresolvedUninitializedThisRefType(descriptor, entries_.size());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700465 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800466 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800467 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700468 RegType* cur_entry = entries_[i];
Ian Rogers62342ec2013-06-11 10:26:37 -0700469 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700470 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700471 }
472 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700473 entry = new UninitializedThisReferenceType(klass, descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700474 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700475 entries_.push_back(entry);
476 return *entry;
477}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700478
Ian Rogers41c65c12013-09-05 16:57:17 -0700479const ConstantType& RegTypeCache::FromCat1NonSmallConstant(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800480 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700481 RegType* cur_entry = entries_[i];
Ian Rogers637c65b2013-05-31 11:46:00 -0700482 if (cur_entry->klass_ == NULL && cur_entry->IsConstant() &&
483 cur_entry->IsPreciseConstant() == precise &&
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800484 (down_cast<ConstantType*>(cur_entry))->ConstantValue() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700485 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700486 }
487 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700488 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800489 if (precise) {
490 entry = new PreciseConstType(value, entries_.size());
491 } else {
492 entry = new ImpreciseConstType(value, entries_.size());
493 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800494 entries_.push_back(entry);
495 return *entry;
496}
497
Ian Rogers41c65c12013-09-05 16:57:17 -0700498const ConstantType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800499 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800500 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800501 if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) &&
502 (down_cast<ConstantType*>(cur_entry))->ConstantValueLo() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700503 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800504 }
505 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700506 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800507 if (precise) {
508 entry = new PreciseConstLoType(value, entries_.size());
509 } else {
510 entry = new ImpreciseConstLoType(value, entries_.size());
511 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800512 entries_.push_back(entry);
513 return *entry;
514}
515
Ian Rogers41c65c12013-09-05 16:57:17 -0700516const ConstantType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800517 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800518 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800519 if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) &&
520 (down_cast<ConstantType*>(cur_entry))->ConstantValueHi() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700521 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800522 }
523 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700524 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800525 if (precise) {
526 entry = new PreciseConstHiType(value, entries_.size());
527 } else {
528 entry = new ImpreciseConstHiType(value, entries_.size());
529 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700530 entries_.push_back(entry);
531 return *entry;
532}
533
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800534const RegType& RegTypeCache::GetComponentType(const RegType& array, mirror::ClassLoader* loader) {
Ian Rogersa9a82542013-10-04 11:17:26 -0700535 if (!array.IsArrayTypes()) {
536 return Conflict();
537 } else if (array.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700538 const std::string& descriptor(array.GetDescriptor());
539 const std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogersb4903572012-10-11 11:52:56 -0700540 return FromDescriptor(loader, component.c_str(), false);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700541 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800542 mirror::Class* klass = array.GetClass()->GetComponentType();
Ian Rogers04f94f42013-06-10 15:09:26 -0700543 return FromClass(ClassHelper(klass).GetDescriptor(), klass,
544 klass->CannotBeAssignedFromOtherTypes());
Ian Rogersb4903572012-10-11 11:52:56 -0700545 }
546}
547
548void RegTypeCache::Dump(std::ostream& os) {
Ian Rogersb4903572012-10-11 11:52:56 -0700549 for (size_t i = 0; i < entries_.size(); i++) {
550 RegType* cur_entry = entries_[i];
551 if (cur_entry != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800552 os << i << ": " << cur_entry->Dump() << "\n";
Ian Rogersb4903572012-10-11 11:52:56 -0700553 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700554 }
555}
556
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800557void RegTypeCache::VisitRoots(RootVisitor* visitor, void* arg) {
558 for (RegType* entry : entries_) {
559 entry->VisitRoots(visitor, arg);
560 }
561}
562
Elliott Hughesa21039c2012-06-21 12:09:25 -0700563} // namespace verifier
564} // namespace art