blob: 9dd57b880bf45ab59250d284a1d651b7149c102a [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();
145 SirtRef<mirror::ClassLoader> class_loader(self, loader);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800146 mirror::Class* klass = NULL;
147 if (can_load_classes_) {
Ian Rogers98379392014-02-24 16:53:16 -0800148 klass = class_linker->FindClass(self, descriptor, class_loader);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800149 } else {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700150 klass = class_linker->LookupClass(descriptor, loader);
Ian Rogers62d6c772013-02-27 08:32:07 -0800151 if (klass != NULL && !klass->IsLoaded()) {
152 // We found the class but without it being loaded its not safe for use.
153 klass = NULL;
154 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800155 }
156 return klass;
157}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700158
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800159void RegTypeCache::ClearException() {
160 if (can_load_classes_) {
161 DCHECK(Thread::Current()->IsExceptionPending());
162 Thread::Current()->ClearException();
163 } else {
164 DCHECK(!Thread::Current()->IsExceptionPending());
165 }
166}
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700167
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700168const RegType& RegTypeCache::From(mirror::ClassLoader* loader, const char* descriptor,
169 bool precise) {
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700170 // Try looking up the class in the cache first.
171 for (size_t i = primitive_count_; i < entries_.size(); i++) {
172 if (MatchDescriptor(i, descriptor, precise)) {
173 return *(entries_[i]);
174 }
175 }
176 // Class not found in the cache, will create a new type for that.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800177 // Try resolving class.
178 mirror::Class* klass = ResolveClass(descriptor, loader);
179 if (klass != NULL) {
180 // Class resolved, first look for the class in the list of entries
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800181 // Class was not found, must create new type.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700182 // To pass the verification, the type should be imprecise,
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700183 // instantiable or an interface with the precise type set to false.
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700184 DCHECK(!precise || klass->IsInstantiable());
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700185 // Create a precise type if:
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700186 // 1- Class is final and NOT an interface. a precise interface is meaningless !!
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700187 // 2- Precise Flag passed as true.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800188 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700189 // Create an imprecise type if we can't tell for a fact that it is precise.
Ian Rogers04f94f42013-06-10 15:09:26 -0700190 if (klass->CannotBeAssignedFromOtherTypes() || precise) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700191 DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass());
192 DCHECK(!klass->IsInterface());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800193 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
194 } else {
195 entry = new ReferenceType(klass, descriptor, entries_.size());
196 }
197 entries_.push_back(entry);
198 return *entry;
199 } else { // Class not resolved.
200 // We tried loading the class and failed, this might get an exception raised
201 // so we want to clear it before we go on.
202 ClearException();
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700203 if (IsValidDescriptor(descriptor)) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800204 RegType* entry = new UnresolvedReferenceType(descriptor, entries_.size());
205 entries_.push_back(entry);
206 return *entry;
207 } else {
208 // The descriptor is broken return the unknown type as there's nothing sensible that
209 // could be done at runtime
210 return Conflict();
211 }
212 }
213}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700214
Ian Rogers637c65b2013-05-31 11:46:00 -0700215const RegType& RegTypeCache::FromClass(const char* descriptor, mirror::Class* klass, bool precise) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700216 DCHECK(klass != nullptr);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700217 if (klass->IsPrimitive()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700218 // Note: precise isn't used for primitive classes. A char is assignable to an int. All
219 // primitive classes are final.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800220 return RegTypeFromPrimitiveType(klass->GetPrimitiveType());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700221 } else {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800222 // Look for the reference in the list of entries to have.
223 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700224 RegType* cur_entry = entries_[i];
Ian Rogers637c65b2013-05-31 11:46:00 -0700225 if (cur_entry->klass_ == klass && MatchingPrecisionForClass(cur_entry, precise)) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700226 return *cur_entry;
227 }
228 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800229 // No reference to the class was found, create new reference.
230 RegType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800231 if (precise) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700232 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800233 } else {
Ian Rogers637c65b2013-05-31 11:46:00 -0700234 entry = new ReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800235 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700236 entries_.push_back(entry);
237 return *entry;
238 }
239}
240
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800241RegTypeCache::~RegTypeCache() {
242 CHECK_LE(primitive_count_, entries_.size());
243 // Delete only the non primitive types.
Ian Rogers41c65c12013-09-05 16:57:17 -0700244 if (entries_.size() == kNumPrimitivesAndSmallConstants) {
245 // All entries are from the global pool, nothing to delete.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800246 return;
247 }
248 std::vector<RegType*>::iterator non_primitive_begin = entries_.begin();
Ian Rogers41c65c12013-09-05 16:57:17 -0700249 std::advance(non_primitive_begin, kNumPrimitivesAndSmallConstants);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800250 STLDeleteContainerPointers(non_primitive_begin, entries_.end());
251}
252
253void RegTypeCache::ShutDown() {
254 if (RegTypeCache::primitive_initialized_) {
255 UndefinedType::Destroy();
256 ConflictType::Destroy();
257 BooleanType::Destroy();
258 ByteType::Destroy();
259 ShortType::Destroy();
260 CharType::Destroy();
261 IntegerType::Destroy();
262 LongLoType::Destroy();
263 LongHiType::Destroy();
Ian Rogers25ae7eb2013-04-18 16:06:05 -0700264 FloatType::Destroy();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800265 DoubleLoType::Destroy();
266 DoubleHiType::Destroy();
Ian Rogersdfe78a62013-11-14 09:20:55 -0800267 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700268 PreciseConstType* type = small_precise_constants_[value - kMinSmallConstant];
269 delete type;
Ian Rogersdfe78a62013-11-14 09:20:55 -0800270 small_precise_constants_[value - kMinSmallConstant] = nullptr;
Ian Rogers41c65c12013-09-05 16:57:17 -0700271 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800272 RegTypeCache::primitive_initialized_ = false;
273 RegTypeCache::primitive_count_ = 0;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800274 }
275}
276
Ian Rogers41c65c12013-09-05 16:57:17 -0700277template <class Type>
278Type* RegTypeCache::CreatePrimitiveTypeInstance(const std::string& descriptor) {
279 mirror::Class* klass = NULL;
280 // Try loading the class from linker.
281 if (!descriptor.empty()) {
Ian Rogers98379392014-02-24 16:53:16 -0800282 klass = art::Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(),
283 descriptor.c_str());
Ian Rogers41c65c12013-09-05 16:57:17 -0700284 }
285 Type* entry = Type::CreateInstance(klass, descriptor, RegTypeCache::primitive_count_);
286 RegTypeCache::primitive_count_++;
287 return entry;
288}
289
290void RegTypeCache::CreatePrimitiveAndSmallConstantTypes() {
Ian Rogers33e95662013-05-20 20:29:14 -0700291 CreatePrimitiveTypeInstance<UndefinedType>("");
292 CreatePrimitiveTypeInstance<ConflictType>("");
293 CreatePrimitiveTypeInstance<BooleanType>("Z");
294 CreatePrimitiveTypeInstance<ByteType>("B");
295 CreatePrimitiveTypeInstance<ShortType>("S");
296 CreatePrimitiveTypeInstance<CharType>("C");
297 CreatePrimitiveTypeInstance<IntegerType>("I");
298 CreatePrimitiveTypeInstance<LongLoType>("J");
299 CreatePrimitiveTypeInstance<LongHiType>("J");
300 CreatePrimitiveTypeInstance<FloatType>("F");
301 CreatePrimitiveTypeInstance<DoubleLoType>("D");
302 CreatePrimitiveTypeInstance<DoubleHiType>("D");
Ian Rogers41c65c12013-09-05 16:57:17 -0700303 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
304 PreciseConstType* type = new PreciseConstType(value, primitive_count_);
305 small_precise_constants_[value - kMinSmallConstant] = type;
306 primitive_count_++;
307 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800308}
309
Ian Rogers529781d2012-07-23 17:24:29 -0700310const RegType& RegTypeCache::FromUnresolvedMerge(const RegType& left, const RegType& right) {
311 std::set<uint16_t> types;
312 if (left.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800313 RegType& non_const(const_cast<RegType&>(left));
314 types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700315 } else {
316 types.insert(left.GetId());
317 }
318 if (right.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800319 RegType& non_const(const_cast<RegType&>(right));
320 std::set<uint16_t> right_types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700321 types.insert(right_types.begin(), right_types.end());
322 } else {
323 types.insert(right.GetId());
324 }
325 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800326 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers529781d2012-07-23 17:24:29 -0700327 RegType* cur_entry = entries_[i];
328 if (cur_entry->IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800329 std::set<uint16_t> cur_entry_types =
330 (down_cast<UnresolvedMergedType*>(cur_entry))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700331 if (cur_entry_types == types) {
332 return *cur_entry;
333 }
334 }
335 }
336 // Create entry.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800337 RegType* entry = new UnresolvedMergedType(left.GetId(), right.GetId(), this, entries_.size());
Ian Rogers529781d2012-07-23 17:24:29 -0700338 entries_.push_back(entry);
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700339 if (kIsDebugBuild) {
340 UnresolvedMergedType* tmp_entry = down_cast<UnresolvedMergedType*>(entry);
341 std::set<uint16_t> check_types = tmp_entry->GetMergedTypes();
342 CHECK(check_types == types);
343 }
Ian Rogers529781d2012-07-23 17:24:29 -0700344 return *entry;
345}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700346
Ian Rogers529781d2012-07-23 17:24:29 -0700347const RegType& RegTypeCache::FromUnresolvedSuperClass(const RegType& child) {
348 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800349 for (size_t i = primitive_count_; i < entries_.size(); i++) {
350 RegType* cur_entry = entries_[i];
351 if (cur_entry->IsUnresolvedSuperClass()) {
352 UnresolvedSuperClass* tmp_entry =
353 down_cast<UnresolvedSuperClass*>(cur_entry);
354 uint16_t unresolved_super_child_id =
355 tmp_entry->GetUnresolvedSuperClassChildId();
356 if (unresolved_super_child_id == child.GetId()) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700357 return *cur_entry;
358 }
359 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800360 }
361 RegType* entry = new UnresolvedSuperClass(child.GetId(), this, entries_.size());
362 entries_.push_back(entry);
363 return *entry;
364}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700365
Ian Rogers41c65c12013-09-05 16:57:17 -0700366const UninitializedType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
367 UninitializedType* entry = NULL;
Ian Rogers637c65b2013-05-31 11:46:00 -0700368 const std::string& descriptor(type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800369 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800370 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700371 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800372 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
373 down_cast<UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc() == allocation_pc &&
374 (cur_entry->GetDescriptor() == descriptor)) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700375 return *down_cast<UnresolvedUninitializedRefType*>(cur_entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800376 }
377 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700378 entry = new UnresolvedUninitializedRefType(descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700379 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800381 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700382 RegType* cur_entry = entries_[i];
Ian Rogers776ac1f2012-04-13 23:36:36 -0700383 if (cur_entry->IsUninitializedReference() &&
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700384 down_cast<UninitializedReferenceType*>(cur_entry)
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800385 ->GetAllocationPc() == allocation_pc &&
Ian Rogers776ac1f2012-04-13 23:36:36 -0700386 cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700387 return *down_cast<UninitializedReferenceType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700388 }
389 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700390 entry = new UninitializedReferenceType(klass, descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700391 }
392 entries_.push_back(entry);
393 return *entry;
394}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700395
Ian Rogers776ac1f2012-04-13 23:36:36 -0700396const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
397 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700398
Ian Rogers776ac1f2012-04-13 23:36:36 -0700399 if (uninit_type.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700400 const std::string& descriptor(uninit_type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800401 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700402 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800403 if (cur_entry->IsUnresolvedReference() &&
404 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700405 return *cur_entry;
406 }
407 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700408 entry = new UnresolvedReferenceType(descriptor.c_str(), entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700409 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 mirror::Class* klass = uninit_type.GetClass();
Brian Carlstromdf629502013-07-17 22:39:56 -0700411 if (uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700412 // For uninitialized "this reference" look for reference types that are not precise.
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700413 for (size_t i = primitive_count_; i < entries_.size(); i++) {
414 RegType* cur_entry = entries_[i];
415 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
416 return *cur_entry;
417 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700418 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700419 entry = new ReferenceType(klass, "", entries_.size());
Ian Rogers62342ec2013-06-11 10:26:37 -0700420 } else if (klass->IsInstantiable()) {
421 // We're uninitialized because of allocation, look or create a precise type as allocations
422 // may only create objects of that type.
423 for (size_t i = primitive_count_; i < entries_.size(); i++) {
424 RegType* cur_entry = entries_[i];
425 if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) {
426 return *cur_entry;
Ian Rogers04f94f42013-06-10 15:09:26 -0700427 }
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700428 }
Ian Rogers62342ec2013-06-11 10:26:37 -0700429 entry = new PreciseReferenceType(klass, uninit_type.GetDescriptor(), entries_.size());
430 } else {
431 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700432 }
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700433 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700434 entries_.push_back(entry);
435 return *entry;
436}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700437
Ian Rogers41c65c12013-09-05 16:57:17 -0700438const ImpreciseConstType& RegTypeCache::ByteConstant() {
439 const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::min(), false);
440 DCHECK(result.IsImpreciseConstant());
441 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800442}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700443
Sebastien Hertz849600b2013-12-20 10:28:08 +0100444const ImpreciseConstType& RegTypeCache::CharConstant() {
445 int32_t jchar_max = static_cast<int32_t>(std::numeric_limits<jchar>::max());
446 const ConstantType& result = FromCat1Const(jchar_max, false);
447 DCHECK(result.IsImpreciseConstant());
448 return *down_cast<const ImpreciseConstType*>(&result);
449}
450
Ian Rogers41c65c12013-09-05 16:57:17 -0700451const ImpreciseConstType& RegTypeCache::ShortConstant() {
452 const ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::min(), false);
453 DCHECK(result.IsImpreciseConstant());
454 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800455}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700456
Ian Rogers41c65c12013-09-05 16:57:17 -0700457const ImpreciseConstType& RegTypeCache::IntConstant() {
458 const ConstantType& result = FromCat1Const(std::numeric_limits<jint>::max(), false);
459 DCHECK(result.IsImpreciseConstant());
460 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800461}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700462
Sebastien Hertz849600b2013-12-20 10:28:08 +0100463const ImpreciseConstType& RegTypeCache::PosByteConstant() {
464 const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::max(), false);
465 DCHECK(result.IsImpreciseConstant());
466 return *down_cast<const ImpreciseConstType*>(&result);
467}
468
469const ImpreciseConstType& RegTypeCache::PosShortConstant() {
470 const ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::max(), false);
471 DCHECK(result.IsImpreciseConstant());
472 return *down_cast<const ImpreciseConstType*>(&result);
473}
474
Ian Rogers41c65c12013-09-05 16:57:17 -0700475const UninitializedType& RegTypeCache::UninitializedThisArgument(const RegType& type) {
476 UninitializedType* entry;
Ian Rogers637c65b2013-05-31 11:46:00 -0700477 const std::string& descriptor(type.GetDescriptor());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700478 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800479 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700480 RegType* cur_entry = entries_[i];
481 if (cur_entry->IsUnresolvedAndUninitializedThisReference() &&
482 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700483 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700484 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700485 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700486 entry = new UnresolvedUninitializedThisRefType(descriptor, entries_.size());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700487 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800488 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800489 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700490 RegType* cur_entry = entries_[i];
Ian Rogers62342ec2013-06-11 10:26:37 -0700491 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700492 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700493 }
494 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700495 entry = new UninitializedThisReferenceType(klass, descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700496 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700497 entries_.push_back(entry);
498 return *entry;
499}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700500
Ian Rogers41c65c12013-09-05 16:57:17 -0700501const ConstantType& RegTypeCache::FromCat1NonSmallConstant(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800502 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700503 RegType* cur_entry = entries_[i];
Ian Rogers637c65b2013-05-31 11:46:00 -0700504 if (cur_entry->klass_ == NULL && cur_entry->IsConstant() &&
505 cur_entry->IsPreciseConstant() == precise &&
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800506 (down_cast<ConstantType*>(cur_entry))->ConstantValue() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700507 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700508 }
509 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700510 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800511 if (precise) {
512 entry = new PreciseConstType(value, entries_.size());
513 } else {
514 entry = new ImpreciseConstType(value, entries_.size());
515 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800516 entries_.push_back(entry);
517 return *entry;
518}
519
Ian Rogers41c65c12013-09-05 16:57:17 -0700520const ConstantType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800521 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800522 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800523 if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) &&
524 (down_cast<ConstantType*>(cur_entry))->ConstantValueLo() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700525 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800526 }
527 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700528 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800529 if (precise) {
530 entry = new PreciseConstLoType(value, entries_.size());
531 } else {
532 entry = new ImpreciseConstLoType(value, entries_.size());
533 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800534 entries_.push_back(entry);
535 return *entry;
536}
537
Ian Rogers41c65c12013-09-05 16:57:17 -0700538const ConstantType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800539 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800540 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800541 if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) &&
542 (down_cast<ConstantType*>(cur_entry))->ConstantValueHi() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700543 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800544 }
545 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700546 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800547 if (precise) {
548 entry = new PreciseConstHiType(value, entries_.size());
549 } else {
550 entry = new ImpreciseConstHiType(value, entries_.size());
551 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700552 entries_.push_back(entry);
553 return *entry;
554}
555
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800556const RegType& RegTypeCache::GetComponentType(const RegType& array, mirror::ClassLoader* loader) {
Ian Rogersa9a82542013-10-04 11:17:26 -0700557 if (!array.IsArrayTypes()) {
558 return Conflict();
559 } else if (array.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700560 const std::string& descriptor(array.GetDescriptor());
561 const std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogersb4903572012-10-11 11:52:56 -0700562 return FromDescriptor(loader, component.c_str(), false);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700563 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800564 mirror::Class* klass = array.GetClass()->GetComponentType();
Ian Rogers04f94f42013-06-10 15:09:26 -0700565 return FromClass(ClassHelper(klass).GetDescriptor(), klass,
566 klass->CannotBeAssignedFromOtherTypes());
Ian Rogersb4903572012-10-11 11:52:56 -0700567 }
568}
569
570void RegTypeCache::Dump(std::ostream& os) {
Ian Rogersb4903572012-10-11 11:52:56 -0700571 for (size_t i = 0; i < entries_.size(); i++) {
572 RegType* cur_entry = entries_[i];
573 if (cur_entry != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800574 os << i << ": " << cur_entry->Dump() << "\n";
Ian Rogersb4903572012-10-11 11:52:56 -0700575 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700576 }
577}
578
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800579void RegTypeCache::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800580 for (RegType* entry : entries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800581 entry->VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800582 }
583}
584
Elliott Hughesa21039c2012-06-21 12:09:25 -0700585} // namespace verifier
586} // namespace art