blob: 482bb4d0eb6f71ad5c88e1f2212047c98859e091 [file] [log] [blame]
Ian Rogers776ac1f2012-04-13 23:36:36 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080017#include "reg_type_cache-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070018
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080019#include "base/casts.h"
Ian Rogers98379392014-02-24 16:53:16 -080020#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/class-inl.h"
23#include "mirror/object-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070024
25namespace art {
26namespace verifier {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070027
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080028bool RegTypeCache::primitive_initialized_ = false;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080029uint16_t RegTypeCache::primitive_count_ = 0;
Ian Rogers41c65c12013-09-05 16:57:17 -070030PreciseConstType* RegTypeCache::small_precise_constants_[kMaxSmallConstant - kMinSmallConstant + 1];
Ian Rogers776ac1f2012-04-13 23:36:36 -070031
Ian Rogersb4903572012-10-11 11:52:56 -070032static bool MatchingPrecisionForClass(RegType* entry, bool precise)
33 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers04f94f42013-06-10 15:09:26 -070034 if (entry->IsPreciseReference() == precise) {
35 // We were or weren't looking for a precise reference and we found what we need.
36 return true;
37 } else {
38 if (!precise && entry->GetClass()->CannotBeAssignedFromOtherTypes()) {
39 // We weren't looking for a precise reference, as we're looking up based on a descriptor, but
40 // we found a matching entry based on the descriptor. Return the precise entry in that case.
41 return true;
42 }
43 return false;
44 }
Ian Rogersb4903572012-10-11 11:52:56 -070045}
46
Ian Rogers41c65c12013-09-05 16:57:17 -070047void RegTypeCache::FillPrimitiveAndSmallConstantTypes() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080048 entries_.push_back(UndefinedType::GetInstance());
49 entries_.push_back(ConflictType::GetInstance());
50 entries_.push_back(BooleanType::GetInstance());
51 entries_.push_back(ByteType::GetInstance());
52 entries_.push_back(ShortType::GetInstance());
53 entries_.push_back(CharType::GetInstance());
54 entries_.push_back(IntegerType::GetInstance());
55 entries_.push_back(LongLoType::GetInstance());
56 entries_.push_back(LongHiType::GetInstance());
57 entries_.push_back(FloatType::GetInstance());
58 entries_.push_back(DoubleLoType::GetInstance());
59 entries_.push_back(DoubleHiType::GetInstance());
Ian Rogers41c65c12013-09-05 16:57:17 -070060 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
61 int32_t i = value - kMinSmallConstant;
62 DCHECK_EQ(entries_.size(), small_precise_constants_[i]->GetId());
63 entries_.push_back(small_precise_constants_[i]);
64 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080065 DCHECK_EQ(entries_.size(), primitive_count_);
66}
67
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -070068RegType& RegTypeCache::FromDescriptor(mirror::ClassLoader* loader, const char* descriptor,
69 bool precise) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070070 DCHECK(RegTypeCache::primitive_initialized_);
71 if (descriptor[1] == '\0') {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080072 switch (descriptor[0]) {
73 case 'Z':
74 return Boolean();
75 case 'B':
76 return Byte();
77 case 'S':
78 return Short();
79 case 'C':
80 return Char();
81 case 'I':
82 return Integer();
83 case 'J':
84 return LongLo();
85 case 'F':
86 return Float();
87 case 'D':
88 return DoubleLo();
89 case 'V': // For void types, conflict types.
90 default:
Ian Rogersad0b3a32012-04-16 14:50:24 -070091 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -070092 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080093 } else if (descriptor[0] == 'L' || descriptor[0] == '[') {
94 return From(loader, descriptor, precise);
95 } else {
96 return Conflict();
97 }
98};
99
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700100RegType& RegTypeCache::RegTypeFromPrimitiveType(Primitive::Type prim_type) const {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800101 CHECK(RegTypeCache::primitive_initialized_);
102 switch (prim_type) {
103 case Primitive::kPrimBoolean:
104 return *BooleanType::GetInstance();
105 case Primitive::kPrimByte:
106 return *ByteType::GetInstance();
107 case Primitive::kPrimShort:
108 return *ShortType::GetInstance();
109 case Primitive::kPrimChar:
110 return *CharType::GetInstance();
111 case Primitive::kPrimInt:
112 return *IntegerType::GetInstance();
113 case Primitive::kPrimLong:
114 return *LongLoType::GetInstance();
115 case Primitive::kPrimFloat:
116 return *FloatType::GetInstance();
117 case Primitive::kPrimDouble:
118 return *DoubleLoType::GetInstance();
119 case Primitive::kPrimVoid:
120 default:
121 return *ConflictType::GetInstance();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700122 }
123}
124
Ian Rogers1ff3c982014-08-12 02:30:58 -0700125bool RegTypeCache::MatchDescriptor(size_t idx, const StringPiece& descriptor, bool precise) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700126 RegType* entry = entries_[idx];
Ian Rogers1ff3c982014-08-12 02:30:58 -0700127 if (descriptor != entry->descriptor_) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700128 return false;
129 }
Ian Rogers04f94f42013-06-10 15:09:26 -0700130 if (entry->HasClass()) {
131 return MatchingPrecisionForClass(entry, precise);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800132 }
Ian Rogers04f94f42013-06-10 15:09:26 -0700133 // There is no notion of precise unresolved references, the precise information is just dropped
134 // on the floor.
135 DCHECK(entry->IsUnresolvedReference());
136 return true;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800137}
138
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700139mirror::Class* RegTypeCache::ResolveClass(const char* descriptor, mirror::ClassLoader* loader) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800140 // Class was not found, must create new type.
141 // Try resolving class
142 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -0800143 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700144 StackHandleScope<1> hs(self);
145 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(loader));
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800146 mirror::Class* klass = NULL;
147 if (can_load_classes_) {
Ian Rogers98379392014-02-24 16:53:16 -0800148 klass = class_linker->FindClass(self, descriptor, class_loader);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800149 } else {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700150 klass = class_linker->LookupClass(descriptor, loader);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700151 if (klass != nullptr && !klass->IsLoaded()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800152 // We found the class but without it being loaded its not safe for use.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700153 klass = nullptr;
Ian Rogers62d6c772013-02-27 08:32:07 -0800154 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800155 }
156 return klass;
157}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700158
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700159RegType& RegTypeCache::From(mirror::ClassLoader* loader, const char* descriptor,
160 bool precise) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700161 // Try looking up the class in the cache first. We use a StringPiece to avoid continual strlen
162 // operations on the descriptor.
163 StringPiece descriptor_sp(descriptor);
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700164 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700165 if (MatchDescriptor(i, descriptor_sp, precise)) {
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700166 return *(entries_[i]);
167 }
168 }
169 // Class not found in the cache, will create a new type for that.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800170 // Try resolving class.
171 mirror::Class* klass = ResolveClass(descriptor, loader);
172 if (klass != NULL) {
173 // Class resolved, first look for the class in the list of entries
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800174 // Class was not found, must create new type.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700175 // To pass the verification, the type should be imprecise,
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700176 // instantiable or an interface with the precise type set to false.
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700177 DCHECK(!precise || klass->IsInstantiable());
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700178 // Create a precise type if:
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700179 // 1- Class is final and NOT an interface. a precise interface is meaningless !!
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700180 // 2- Precise Flag passed as true.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800181 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700182 // Create an imprecise type if we can't tell for a fact that it is precise.
Ian Rogers04f94f42013-06-10 15:09:26 -0700183 if (klass->CannotBeAssignedFromOtherTypes() || precise) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700184 DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass());
185 DCHECK(!klass->IsInterface());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700186 entry = new PreciseReferenceType(klass, descriptor_sp.as_string(), entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800187 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700188 entry = new ReferenceType(klass, descriptor_sp.as_string(), entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800189 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700190 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800191 return *entry;
192 } else { // Class not resolved.
193 // We tried loading the class and failed, this might get an exception raised
194 // so we want to clear it before we go on.
Andreas Gampe63981562014-04-17 12:28:43 -0700195 if (can_load_classes_) {
196 DCHECK(Thread::Current()->IsExceptionPending());
197 Thread::Current()->ClearException();
198 } else {
199 DCHECK(!Thread::Current()->IsExceptionPending());
200 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700201 if (IsValidDescriptor(descriptor)) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700202 RegType* entry = new UnresolvedReferenceType(descriptor_sp.as_string(), entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700203 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800204 return *entry;
205 } else {
206 // The descriptor is broken return the unknown type as there's nothing sensible that
207 // could be done at runtime
208 return Conflict();
209 }
210 }
211}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700212
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700213RegType& RegTypeCache::FromClass(const char* descriptor, mirror::Class* klass, bool precise) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700214 DCHECK(klass != nullptr);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700215 if (klass->IsPrimitive()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700216 // Note: precise isn't used for primitive classes. A char is assignable to an int. All
217 // primitive classes are final.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800218 return RegTypeFromPrimitiveType(klass->GetPrimitiveType());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700219 } else {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800220 // Look for the reference in the list of entries to have.
221 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700222 RegType* cur_entry = entries_[i];
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700223 if (cur_entry->klass_.Read() == klass && MatchingPrecisionForClass(cur_entry, precise)) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700224 return *cur_entry;
225 }
226 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800227 // No reference to the class was found, create new reference.
228 RegType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800229 if (precise) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700230 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800231 } else {
Ian Rogers637c65b2013-05-31 11:46:00 -0700232 entry = new ReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800233 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700234 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700235 return *entry;
236 }
237}
238
Andreas Gampe63981562014-04-17 12:28:43 -0700239RegTypeCache::RegTypeCache(bool can_load_classes) : can_load_classes_(can_load_classes) {
240 if (kIsDebugBuild && can_load_classes) {
241 Thread::Current()->AssertThreadSuspensionIsAllowable();
242 }
243 entries_.reserve(64);
244 FillPrimitiveAndSmallConstantTypes();
245}
246
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800247RegTypeCache::~RegTypeCache() {
248 CHECK_LE(primitive_count_, entries_.size());
249 // Delete only the non primitive types.
Ian Rogers41c65c12013-09-05 16:57:17 -0700250 if (entries_.size() == kNumPrimitivesAndSmallConstants) {
251 // All entries are from the global pool, nothing to delete.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800252 return;
253 }
254 std::vector<RegType*>::iterator non_primitive_begin = entries_.begin();
Ian Rogers41c65c12013-09-05 16:57:17 -0700255 std::advance(non_primitive_begin, kNumPrimitivesAndSmallConstants);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800256 STLDeleteContainerPointers(non_primitive_begin, entries_.end());
257}
258
259void RegTypeCache::ShutDown() {
260 if (RegTypeCache::primitive_initialized_) {
261 UndefinedType::Destroy();
262 ConflictType::Destroy();
263 BooleanType::Destroy();
264 ByteType::Destroy();
265 ShortType::Destroy();
266 CharType::Destroy();
267 IntegerType::Destroy();
268 LongLoType::Destroy();
269 LongHiType::Destroy();
Ian Rogers25ae7eb2013-04-18 16:06:05 -0700270 FloatType::Destroy();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800271 DoubleLoType::Destroy();
272 DoubleHiType::Destroy();
Ian Rogersdfe78a62013-11-14 09:20:55 -0800273 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700274 PreciseConstType* type = small_precise_constants_[value - kMinSmallConstant];
275 delete type;
Ian Rogersdfe78a62013-11-14 09:20:55 -0800276 small_precise_constants_[value - kMinSmallConstant] = nullptr;
Ian Rogers41c65c12013-09-05 16:57:17 -0700277 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800278 RegTypeCache::primitive_initialized_ = false;
279 RegTypeCache::primitive_count_ = 0;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800280 }
281}
282
Ian Rogers41c65c12013-09-05 16:57:17 -0700283template <class Type>
284Type* RegTypeCache::CreatePrimitiveTypeInstance(const std::string& descriptor) {
285 mirror::Class* klass = NULL;
286 // Try loading the class from linker.
287 if (!descriptor.empty()) {
Ian Rogers98379392014-02-24 16:53:16 -0800288 klass = art::Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(),
289 descriptor.c_str());
Ian Rogers41c65c12013-09-05 16:57:17 -0700290 }
291 Type* entry = Type::CreateInstance(klass, descriptor, RegTypeCache::primitive_count_);
292 RegTypeCache::primitive_count_++;
293 return entry;
294}
295
296void RegTypeCache::CreatePrimitiveAndSmallConstantTypes() {
Ian Rogers33e95662013-05-20 20:29:14 -0700297 CreatePrimitiveTypeInstance<UndefinedType>("");
298 CreatePrimitiveTypeInstance<ConflictType>("");
299 CreatePrimitiveTypeInstance<BooleanType>("Z");
300 CreatePrimitiveTypeInstance<ByteType>("B");
301 CreatePrimitiveTypeInstance<ShortType>("S");
302 CreatePrimitiveTypeInstance<CharType>("C");
303 CreatePrimitiveTypeInstance<IntegerType>("I");
304 CreatePrimitiveTypeInstance<LongLoType>("J");
305 CreatePrimitiveTypeInstance<LongHiType>("J");
306 CreatePrimitiveTypeInstance<FloatType>("F");
307 CreatePrimitiveTypeInstance<DoubleLoType>("D");
308 CreatePrimitiveTypeInstance<DoubleHiType>("D");
Ian Rogers41c65c12013-09-05 16:57:17 -0700309 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
310 PreciseConstType* type = new PreciseConstType(value, primitive_count_);
311 small_precise_constants_[value - kMinSmallConstant] = type;
312 primitive_count_++;
313 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800314}
315
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700316RegType& RegTypeCache::FromUnresolvedMerge(RegType& left, RegType& right) {
Ian Rogers529781d2012-07-23 17:24:29 -0700317 std::set<uint16_t> types;
318 if (left.IsUnresolvedMergedReference()) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700319 types = (down_cast<UnresolvedMergedType*>(&left))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700320 } else {
321 types.insert(left.GetId());
322 }
323 if (right.IsUnresolvedMergedReference()) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700324 std::set<uint16_t> right_types = (down_cast<UnresolvedMergedType*>(&right))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700325 types.insert(right_types.begin(), right_types.end());
326 } else {
327 types.insert(right.GetId());
328 }
329 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800330 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers529781d2012-07-23 17:24:29 -0700331 RegType* cur_entry = entries_[i];
332 if (cur_entry->IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800333 std::set<uint16_t> cur_entry_types =
334 (down_cast<UnresolvedMergedType*>(cur_entry))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700335 if (cur_entry_types == types) {
336 return *cur_entry;
337 }
338 }
339 }
340 // Create entry.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800341 RegType* entry = new UnresolvedMergedType(left.GetId(), right.GetId(), this, entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700342 AddEntry(entry);
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700343 if (kIsDebugBuild) {
344 UnresolvedMergedType* tmp_entry = down_cast<UnresolvedMergedType*>(entry);
345 std::set<uint16_t> check_types = tmp_entry->GetMergedTypes();
346 CHECK(check_types == types);
347 }
Ian Rogers529781d2012-07-23 17:24:29 -0700348 return *entry;
349}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700350
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700351RegType& RegTypeCache::FromUnresolvedSuperClass(RegType& child) {
Ian Rogers529781d2012-07-23 17:24:29 -0700352 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800353 for (size_t i = primitive_count_; i < entries_.size(); i++) {
354 RegType* cur_entry = entries_[i];
355 if (cur_entry->IsUnresolvedSuperClass()) {
356 UnresolvedSuperClass* tmp_entry =
357 down_cast<UnresolvedSuperClass*>(cur_entry);
358 uint16_t unresolved_super_child_id =
359 tmp_entry->GetUnresolvedSuperClassChildId();
360 if (unresolved_super_child_id == child.GetId()) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700361 return *cur_entry;
362 }
363 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800364 }
365 RegType* entry = new UnresolvedSuperClass(child.GetId(), this, entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700366 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800367 return *entry;
368}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700369
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700370UninitializedType& RegTypeCache::Uninitialized(RegType& type, uint32_t allocation_pc) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700371 UninitializedType* entry = NULL;
Ian Rogers637c65b2013-05-31 11:46:00 -0700372 const std::string& descriptor(type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800373 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800374 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700375 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800376 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
377 down_cast<UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc() == allocation_pc &&
378 (cur_entry->GetDescriptor() == descriptor)) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700379 return *down_cast<UnresolvedUninitializedRefType*>(cur_entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800380 }
381 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700382 entry = new UnresolvedUninitializedRefType(descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700383 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800384 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800385 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700386 RegType* cur_entry = entries_[i];
Ian Rogers776ac1f2012-04-13 23:36:36 -0700387 if (cur_entry->IsUninitializedReference() &&
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700388 down_cast<UninitializedReferenceType*>(cur_entry)
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800389 ->GetAllocationPc() == allocation_pc &&
Ian Rogers776ac1f2012-04-13 23:36:36 -0700390 cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700391 return *down_cast<UninitializedReferenceType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700392 }
393 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700394 entry = new UninitializedReferenceType(klass, descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700395 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700396 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700397 return *entry;
398}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700399
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700400RegType& RegTypeCache::FromUninitialized(RegType& uninit_type) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700401 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700402
Ian Rogers776ac1f2012-04-13 23:36:36 -0700403 if (uninit_type.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700404 const std::string& descriptor(uninit_type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800405 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700406 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800407 if (cur_entry->IsUnresolvedReference() &&
408 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700409 return *cur_entry;
410 }
411 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700412 entry = new UnresolvedReferenceType(descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700413 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800414 mirror::Class* klass = uninit_type.GetClass();
Brian Carlstromdf629502013-07-17 22:39:56 -0700415 if (uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700416 // For uninitialized "this reference" look for reference types that are not precise.
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700417 for (size_t i = primitive_count_; i < entries_.size(); i++) {
418 RegType* cur_entry = entries_[i];
419 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
420 return *cur_entry;
421 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700422 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700423 entry = new ReferenceType(klass, "", entries_.size());
Ian Rogers62342ec2013-06-11 10:26:37 -0700424 } else if (klass->IsInstantiable()) {
425 // We're uninitialized because of allocation, look or create a precise type as allocations
426 // may only create objects of that type.
427 for (size_t i = primitive_count_; i < entries_.size(); i++) {
428 RegType* cur_entry = entries_[i];
429 if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) {
430 return *cur_entry;
Ian Rogers04f94f42013-06-10 15:09:26 -0700431 }
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700432 }
Ian Rogers62342ec2013-06-11 10:26:37 -0700433 entry = new PreciseReferenceType(klass, uninit_type.GetDescriptor(), entries_.size());
434 } else {
435 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700436 }
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700437 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700438 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700439 return *entry;
440}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700441
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700442ImpreciseConstType& RegTypeCache::ByteConstant() {
443 ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::min(), false);
Ian Rogers41c65c12013-09-05 16:57:17 -0700444 DCHECK(result.IsImpreciseConstant());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700445 return *down_cast<ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800446}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700447
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700448ImpreciseConstType& RegTypeCache::CharConstant() {
Sebastien Hertz849600b2013-12-20 10:28:08 +0100449 int32_t jchar_max = static_cast<int32_t>(std::numeric_limits<jchar>::max());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700450 ConstantType& result = FromCat1Const(jchar_max, false);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100451 DCHECK(result.IsImpreciseConstant());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700452 return *down_cast<ImpreciseConstType*>(&result);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100453}
454
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700455ImpreciseConstType& RegTypeCache::ShortConstant() {
456 ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::min(), false);
Ian Rogers41c65c12013-09-05 16:57:17 -0700457 DCHECK(result.IsImpreciseConstant());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700458 return *down_cast<ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800459}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700460
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700461ImpreciseConstType& RegTypeCache::IntConstant() {
462 ConstantType& result = FromCat1Const(std::numeric_limits<jint>::max(), false);
Ian Rogers41c65c12013-09-05 16:57:17 -0700463 DCHECK(result.IsImpreciseConstant());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700464 return *down_cast<ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700466
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700467ImpreciseConstType& RegTypeCache::PosByteConstant() {
468 ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::max(), false);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100469 DCHECK(result.IsImpreciseConstant());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700470 return *down_cast<ImpreciseConstType*>(&result);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100471}
472
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700473ImpreciseConstType& RegTypeCache::PosShortConstant() {
474 ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::max(), false);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100475 DCHECK(result.IsImpreciseConstant());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700476 return *down_cast<ImpreciseConstType*>(&result);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100477}
478
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700479UninitializedType& RegTypeCache::UninitializedThisArgument(RegType& type) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700480 UninitializedType* entry;
Ian Rogers637c65b2013-05-31 11:46:00 -0700481 const std::string& descriptor(type.GetDescriptor());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700482 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800483 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700484 RegType* cur_entry = entries_[i];
485 if (cur_entry->IsUnresolvedAndUninitializedThisReference() &&
486 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700487 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700488 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700489 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700490 entry = new UnresolvedUninitializedThisRefType(descriptor, entries_.size());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700491 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800492 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800493 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700494 RegType* cur_entry = entries_[i];
Ian Rogers62342ec2013-06-11 10:26:37 -0700495 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700496 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700497 }
498 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700499 entry = new UninitializedThisReferenceType(klass, descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700500 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700501 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700502 return *entry;
503}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700504
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700505ConstantType& RegTypeCache::FromCat1NonSmallConstant(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800506 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700507 RegType* cur_entry = entries_[i];
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700508 if (cur_entry->klass_.IsNull() && cur_entry->IsConstant() &&
Ian Rogers637c65b2013-05-31 11:46:00 -0700509 cur_entry->IsPreciseConstant() == precise &&
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800510 (down_cast<ConstantType*>(cur_entry))->ConstantValue() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700511 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700512 }
513 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700514 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800515 if (precise) {
516 entry = new PreciseConstType(value, entries_.size());
517 } else {
518 entry = new ImpreciseConstType(value, entries_.size());
519 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700520 AddEntry(entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800521 return *entry;
522}
523
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700524ConstantType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800525 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800526 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800527 if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) &&
528 (down_cast<ConstantType*>(cur_entry))->ConstantValueLo() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700529 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800530 }
531 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700532 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800533 if (precise) {
534 entry = new PreciseConstLoType(value, entries_.size());
535 } else {
536 entry = new ImpreciseConstLoType(value, entries_.size());
537 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700538 AddEntry(entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800539 return *entry;
540}
541
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700542ConstantType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800543 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800544 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800545 if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) &&
546 (down_cast<ConstantType*>(cur_entry))->ConstantValueHi() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700547 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800548 }
549 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700550 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800551 if (precise) {
552 entry = new PreciseConstHiType(value, entries_.size());
553 } else {
554 entry = new ImpreciseConstHiType(value, entries_.size());
555 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700556 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700557 return *entry;
558}
559
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700560RegType& RegTypeCache::GetComponentType(RegType& array, mirror::ClassLoader* loader) {
Ian Rogersa9a82542013-10-04 11:17:26 -0700561 if (!array.IsArrayTypes()) {
562 return Conflict();
563 } else if (array.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700564 const std::string& descriptor(array.GetDescriptor());
565 const std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogersb4903572012-10-11 11:52:56 -0700566 return FromDescriptor(loader, component.c_str(), false);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700567 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800568 mirror::Class* klass = array.GetClass()->GetComponentType();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700569 std::string temp;
Andreas Gampeaa910d52014-07-30 18:59:05 -0700570 if (klass->IsErroneous()) {
571 // Arrays may have erroneous component types, use unresolved in that case.
572 // We assume that the primitive classes are not erroneous, so we know it is a
573 // reference type.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700574 return FromDescriptor(loader, klass->GetDescriptor(&temp), false);
Andreas Gampeaa910d52014-07-30 18:59:05 -0700575 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700576 return FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampeaa910d52014-07-30 18:59:05 -0700577 klass->CannotBeAssignedFromOtherTypes());
578 }
Ian Rogersb4903572012-10-11 11:52:56 -0700579 }
580}
581
582void RegTypeCache::Dump(std::ostream& os) {
Ian Rogersb4903572012-10-11 11:52:56 -0700583 for (size_t i = 0; i < entries_.size(); i++) {
584 RegType* cur_entry = entries_[i];
585 if (cur_entry != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800586 os << i << ": " << cur_entry->Dump() << "\n";
Ian Rogersb4903572012-10-11 11:52:56 -0700587 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700588 }
589}
590
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800591void RegTypeCache::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800592 for (RegType* entry : entries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800593 entry->VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800594 }
595}
596
Andreas Gampeaa910d52014-07-30 18:59:05 -0700597void RegTypeCache::AddEntry(RegType* new_entry) {
598 entries_.push_back(new_entry);
599}
600
Elliott Hughesa21039c2012-06-21 12:09:25 -0700601} // namespace verifier
602} // namespace art