blob: 446dd0080c5604baa7694f1d1583d1588b9ac392 [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();
143 mirror::Class* klass = NULL;
144 if (can_load_classes_) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700145 klass = class_linker->FindClass(descriptor, loader);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800146 } else {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700147 klass = class_linker->LookupClass(descriptor, loader);
Ian Rogers62d6c772013-02-27 08:32:07 -0800148 if (klass != NULL && !klass->IsLoaded()) {
149 // We found the class but without it being loaded its not safe for use.
150 klass = NULL;
151 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800152 }
153 return klass;
154}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700155
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800156void RegTypeCache::ClearException() {
157 if (can_load_classes_) {
158 DCHECK(Thread::Current()->IsExceptionPending());
159 Thread::Current()->ClearException();
160 } else {
161 DCHECK(!Thread::Current()->IsExceptionPending());
162 }
163}
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700164
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700165const RegType& RegTypeCache::From(mirror::ClassLoader* loader, const char* descriptor,
166 bool precise) {
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700167 // Try looking up the class in the cache first.
168 for (size_t i = primitive_count_; i < entries_.size(); i++) {
169 if (MatchDescriptor(i, descriptor, precise)) {
170 return *(entries_[i]);
171 }
172 }
173 // Class not found in the cache, will create a new type for that.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800174 // Try resolving class.
175 mirror::Class* klass = ResolveClass(descriptor, loader);
176 if (klass != NULL) {
177 // Class resolved, first look for the class in the list of entries
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800178 // Class was not found, must create new type.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700179 // To pass the verification, the type should be imprecise,
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700180 // instantiable or an interface with the precise type set to false.
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700181 DCHECK(!precise || klass->IsInstantiable());
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700182 // Create a precise type if:
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700183 // 1- Class is final and NOT an interface. a precise interface is meaningless !!
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700184 // 2- Precise Flag passed as true.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800185 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700186 // Create an imprecise type if we can't tell for a fact that it is precise.
Ian Rogers04f94f42013-06-10 15:09:26 -0700187 if (klass->CannotBeAssignedFromOtherTypes() || precise) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700188 DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass());
189 DCHECK(!klass->IsInterface());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800190 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
191 } else {
192 entry = new ReferenceType(klass, descriptor, entries_.size());
193 }
194 entries_.push_back(entry);
195 return *entry;
196 } else { // Class not resolved.
197 // We tried loading the class and failed, this might get an exception raised
198 // so we want to clear it before we go on.
199 ClearException();
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
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800238RegTypeCache::~RegTypeCache() {
239 CHECK_LE(primitive_count_, entries_.size());
240 // Delete only the non primitive types.
Ian Rogers41c65c12013-09-05 16:57:17 -0700241 if (entries_.size() == kNumPrimitivesAndSmallConstants) {
242 // All entries are from the global pool, nothing to delete.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800243 return;
244 }
245 std::vector<RegType*>::iterator non_primitive_begin = entries_.begin();
Ian Rogers41c65c12013-09-05 16:57:17 -0700246 std::advance(non_primitive_begin, kNumPrimitivesAndSmallConstants);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800247 STLDeleteContainerPointers(non_primitive_begin, entries_.end());
248}
249
250void RegTypeCache::ShutDown() {
251 if (RegTypeCache::primitive_initialized_) {
252 UndefinedType::Destroy();
253 ConflictType::Destroy();
254 BooleanType::Destroy();
255 ByteType::Destroy();
256 ShortType::Destroy();
257 CharType::Destroy();
258 IntegerType::Destroy();
259 LongLoType::Destroy();
260 LongHiType::Destroy();
Ian Rogers25ae7eb2013-04-18 16:06:05 -0700261 FloatType::Destroy();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800262 DoubleLoType::Destroy();
263 DoubleHiType::Destroy();
Ian Rogers41c65c12013-09-05 16:57:17 -0700264 for (uint16_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
265 PreciseConstType* type = small_precise_constants_[value - kMinSmallConstant];
266 delete type;
267 }
268
Ian Rogers62d6c772013-02-27 08:32:07 -0800269 RegTypeCache::primitive_initialized_ = false;
270 RegTypeCache::primitive_count_ = 0;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800271 }
272}
273
Ian Rogers41c65c12013-09-05 16:57:17 -0700274template <class Type>
275Type* RegTypeCache::CreatePrimitiveTypeInstance(const std::string& descriptor) {
276 mirror::Class* klass = NULL;
277 // Try loading the class from linker.
278 if (!descriptor.empty()) {
279 klass = art::Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor.c_str());
280 }
281 Type* entry = Type::CreateInstance(klass, descriptor, RegTypeCache::primitive_count_);
282 RegTypeCache::primitive_count_++;
283 return entry;
284}
285
286void RegTypeCache::CreatePrimitiveAndSmallConstantTypes() {
Ian Rogers33e95662013-05-20 20:29:14 -0700287 CreatePrimitiveTypeInstance<UndefinedType>("");
288 CreatePrimitiveTypeInstance<ConflictType>("");
289 CreatePrimitiveTypeInstance<BooleanType>("Z");
290 CreatePrimitiveTypeInstance<ByteType>("B");
291 CreatePrimitiveTypeInstance<ShortType>("S");
292 CreatePrimitiveTypeInstance<CharType>("C");
293 CreatePrimitiveTypeInstance<IntegerType>("I");
294 CreatePrimitiveTypeInstance<LongLoType>("J");
295 CreatePrimitiveTypeInstance<LongHiType>("J");
296 CreatePrimitiveTypeInstance<FloatType>("F");
297 CreatePrimitiveTypeInstance<DoubleLoType>("D");
298 CreatePrimitiveTypeInstance<DoubleHiType>("D");
Ian Rogers41c65c12013-09-05 16:57:17 -0700299 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
300 PreciseConstType* type = new PreciseConstType(value, primitive_count_);
301 small_precise_constants_[value - kMinSmallConstant] = type;
302 primitive_count_++;
303 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800304}
305
Ian Rogers529781d2012-07-23 17:24:29 -0700306const RegType& RegTypeCache::FromUnresolvedMerge(const RegType& left, const RegType& right) {
307 std::set<uint16_t> types;
308 if (left.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800309 RegType& non_const(const_cast<RegType&>(left));
310 types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700311 } else {
312 types.insert(left.GetId());
313 }
314 if (right.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800315 RegType& non_const(const_cast<RegType&>(right));
316 std::set<uint16_t> right_types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700317 types.insert(right_types.begin(), right_types.end());
318 } else {
319 types.insert(right.GetId());
320 }
321 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800322 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers529781d2012-07-23 17:24:29 -0700323 RegType* cur_entry = entries_[i];
324 if (cur_entry->IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800325 std::set<uint16_t> cur_entry_types =
326 (down_cast<UnresolvedMergedType*>(cur_entry))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700327 if (cur_entry_types == types) {
328 return *cur_entry;
329 }
330 }
331 }
332 // Create entry.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800333 RegType* entry = new UnresolvedMergedType(left.GetId(), right.GetId(), this, entries_.size());
Ian Rogers529781d2012-07-23 17:24:29 -0700334 entries_.push_back(entry);
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700335 if (kIsDebugBuild) {
336 UnresolvedMergedType* tmp_entry = down_cast<UnresolvedMergedType*>(entry);
337 std::set<uint16_t> check_types = tmp_entry->GetMergedTypes();
338 CHECK(check_types == types);
339 }
Ian Rogers529781d2012-07-23 17:24:29 -0700340 return *entry;
341}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700342
Ian Rogers529781d2012-07-23 17:24:29 -0700343const RegType& RegTypeCache::FromUnresolvedSuperClass(const RegType& child) {
344 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800345 for (size_t i = primitive_count_; i < entries_.size(); i++) {
346 RegType* cur_entry = entries_[i];
347 if (cur_entry->IsUnresolvedSuperClass()) {
348 UnresolvedSuperClass* tmp_entry =
349 down_cast<UnresolvedSuperClass*>(cur_entry);
350 uint16_t unresolved_super_child_id =
351 tmp_entry->GetUnresolvedSuperClassChildId();
352 if (unresolved_super_child_id == child.GetId()) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700353 return *cur_entry;
354 }
355 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800356 }
357 RegType* entry = new UnresolvedSuperClass(child.GetId(), this, entries_.size());
358 entries_.push_back(entry);
359 return *entry;
360}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700361
Ian Rogers41c65c12013-09-05 16:57:17 -0700362const UninitializedType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
363 UninitializedType* entry = NULL;
Ian Rogers637c65b2013-05-31 11:46:00 -0700364 const std::string& descriptor(type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800365 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800366 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700367 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800368 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
369 down_cast<UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc() == allocation_pc &&
370 (cur_entry->GetDescriptor() == descriptor)) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700371 return *down_cast<UnresolvedUninitializedRefType*>(cur_entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800372 }
373 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700374 entry = new UnresolvedUninitializedRefType(descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700375 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800376 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800377 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700378 RegType* cur_entry = entries_[i];
Ian Rogers776ac1f2012-04-13 23:36:36 -0700379 if (cur_entry->IsUninitializedReference() &&
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700380 down_cast<UninitializedReferenceType*>(cur_entry)
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800381 ->GetAllocationPc() == allocation_pc &&
Ian Rogers776ac1f2012-04-13 23:36:36 -0700382 cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700383 return *down_cast<UninitializedReferenceType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700384 }
385 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700386 entry = new UninitializedReferenceType(klass, descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700387 }
388 entries_.push_back(entry);
389 return *entry;
390}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700391
Ian Rogers776ac1f2012-04-13 23:36:36 -0700392const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
393 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700394
Ian Rogers776ac1f2012-04-13 23:36:36 -0700395 if (uninit_type.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700396 const std::string& descriptor(uninit_type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800397 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700398 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800399 if (cur_entry->IsUnresolvedReference() &&
400 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700401 return *cur_entry;
402 }
403 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700404 entry = new UnresolvedReferenceType(descriptor.c_str(), entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700405 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800406 mirror::Class* klass = uninit_type.GetClass();
Brian Carlstromdf629502013-07-17 22:39:56 -0700407 if (uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700408 // For uninitialized "this reference" look for reference types that are not precise.
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700409 for (size_t i = primitive_count_; i < entries_.size(); i++) {
410 RegType* cur_entry = entries_[i];
411 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
412 return *cur_entry;
413 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700414 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700415 entry = new ReferenceType(klass, "", entries_.size());
Ian Rogers62342ec2013-06-11 10:26:37 -0700416 } else if (klass->IsInstantiable()) {
417 // We're uninitialized because of allocation, look or create a precise type as allocations
418 // may only create objects of that type.
419 for (size_t i = primitive_count_; i < entries_.size(); i++) {
420 RegType* cur_entry = entries_[i];
421 if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) {
422 return *cur_entry;
Ian Rogers04f94f42013-06-10 15:09:26 -0700423 }
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700424 }
Ian Rogers62342ec2013-06-11 10:26:37 -0700425 entry = new PreciseReferenceType(klass, uninit_type.GetDescriptor(), entries_.size());
426 } else {
427 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700428 }
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700429 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700430 entries_.push_back(entry);
431 return *entry;
432}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700433
Ian Rogers41c65c12013-09-05 16:57:17 -0700434const ImpreciseConstType& RegTypeCache::ByteConstant() {
435 const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::min(), false);
436 DCHECK(result.IsImpreciseConstant());
437 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800438}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700439
Ian Rogers41c65c12013-09-05 16:57:17 -0700440const ImpreciseConstType& RegTypeCache::ShortConstant() {
441 const ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::min(), false);
442 DCHECK(result.IsImpreciseConstant());
443 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800444}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700445
Ian Rogers41c65c12013-09-05 16:57:17 -0700446const ImpreciseConstType& RegTypeCache::IntConstant() {
447 const ConstantType& result = FromCat1Const(std::numeric_limits<jint>::max(), false);
448 DCHECK(result.IsImpreciseConstant());
449 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800450}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700451
Ian Rogers41c65c12013-09-05 16:57:17 -0700452const UninitializedType& RegTypeCache::UninitializedThisArgument(const RegType& type) {
453 UninitializedType* entry;
Ian Rogers637c65b2013-05-31 11:46:00 -0700454 const std::string& descriptor(type.GetDescriptor());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700455 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800456 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700457 RegType* cur_entry = entries_[i];
458 if (cur_entry->IsUnresolvedAndUninitializedThisReference() &&
459 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700460 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700461 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700462 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700463 entry = new UnresolvedUninitializedThisRefType(descriptor, entries_.size());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700464 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800466 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700467 RegType* cur_entry = entries_[i];
Ian Rogers62342ec2013-06-11 10:26:37 -0700468 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700469 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700470 }
471 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700472 entry = new UninitializedThisReferenceType(klass, descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700473 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700474 entries_.push_back(entry);
475 return *entry;
476}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700477
Ian Rogers41c65c12013-09-05 16:57:17 -0700478const ConstantType& RegTypeCache::FromCat1NonSmallConstant(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800479 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700480 RegType* cur_entry = entries_[i];
Ian Rogers637c65b2013-05-31 11:46:00 -0700481 if (cur_entry->klass_ == NULL && cur_entry->IsConstant() &&
482 cur_entry->IsPreciseConstant() == precise &&
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800483 (down_cast<ConstantType*>(cur_entry))->ConstantValue() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700484 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700485 }
486 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700487 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800488 if (precise) {
489 entry = new PreciseConstType(value, entries_.size());
490 } else {
491 entry = new ImpreciseConstType(value, entries_.size());
492 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800493 entries_.push_back(entry);
494 return *entry;
495}
496
Ian Rogers41c65c12013-09-05 16:57:17 -0700497const ConstantType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800498 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800499 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800500 if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) &&
501 (down_cast<ConstantType*>(cur_entry))->ConstantValueLo() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700502 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800503 }
504 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700505 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800506 if (precise) {
507 entry = new PreciseConstLoType(value, entries_.size());
508 } else {
509 entry = new ImpreciseConstLoType(value, entries_.size());
510 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800511 entries_.push_back(entry);
512 return *entry;
513}
514
Ian Rogers41c65c12013-09-05 16:57:17 -0700515const ConstantType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800516 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800517 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800518 if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) &&
519 (down_cast<ConstantType*>(cur_entry))->ConstantValueHi() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700520 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800521 }
522 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700523 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800524 if (precise) {
525 entry = new PreciseConstHiType(value, entries_.size());
526 } else {
527 entry = new ImpreciseConstHiType(value, entries_.size());
528 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700529 entries_.push_back(entry);
530 return *entry;
531}
532
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800533const RegType& RegTypeCache::GetComponentType(const RegType& array, mirror::ClassLoader* loader) {
Ian Rogersa9a82542013-10-04 11:17:26 -0700534 if (!array.IsArrayTypes()) {
535 return Conflict();
536 } else if (array.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700537 const std::string& descriptor(array.GetDescriptor());
538 const std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogersb4903572012-10-11 11:52:56 -0700539 return FromDescriptor(loader, component.c_str(), false);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700540 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800541 mirror::Class* klass = array.GetClass()->GetComponentType();
Ian Rogers04f94f42013-06-10 15:09:26 -0700542 return FromClass(ClassHelper(klass).GetDescriptor(), klass,
543 klass->CannotBeAssignedFromOtherTypes());
Ian Rogersb4903572012-10-11 11:52:56 -0700544 }
545}
546
547void RegTypeCache::Dump(std::ostream& os) {
Ian Rogersb4903572012-10-11 11:52:56 -0700548 for (size_t i = 0; i < entries_.size(); i++) {
549 RegType* cur_entry = entries_[i];
550 if (cur_entry != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800551 os << i << ": " << cur_entry->Dump() << "\n";
Ian Rogersb4903572012-10-11 11:52:56 -0700552 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700553 }
554}
555
Elliott Hughesa21039c2012-06-21 12:09:25 -0700556} // namespace verifier
557} // namespace art