blob: c0e4351c154b44a53f3336d631497aad15c9d314 [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 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();
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) {
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700161 // Try looking up the class in the cache first.
162 for (size_t i = primitive_count_; i < entries_.size(); i++) {
163 if (MatchDescriptor(i, descriptor, precise)) {
164 return *(entries_[i]);
165 }
166 }
167 // Class not found in the cache, will create a new type for that.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800168 // Try resolving class.
169 mirror::Class* klass = ResolveClass(descriptor, loader);
170 if (klass != NULL) {
171 // Class resolved, first look for the class in the list of entries
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800172 // Class was not found, must create new type.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700173 // To pass the verification, the type should be imprecise,
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700174 // instantiable or an interface with the precise type set to false.
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700175 DCHECK(!precise || klass->IsInstantiable());
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700176 // Create a precise type if:
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700177 // 1- Class is final and NOT an interface. a precise interface is meaningless !!
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700178 // 2- Precise Flag passed as true.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800179 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700180 // Create an imprecise type if we can't tell for a fact that it is precise.
Ian Rogers04f94f42013-06-10 15:09:26 -0700181 if (klass->CannotBeAssignedFromOtherTypes() || precise) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700182 DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass());
183 DCHECK(!klass->IsInterface());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800184 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
185 } else {
186 entry = new ReferenceType(klass, descriptor, entries_.size());
187 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700188 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800189 return *entry;
190 } else { // Class not resolved.
191 // We tried loading the class and failed, this might get an exception raised
192 // so we want to clear it before we go on.
Andreas Gampe63981562014-04-17 12:28:43 -0700193 if (can_load_classes_) {
194 DCHECK(Thread::Current()->IsExceptionPending());
195 Thread::Current()->ClearException();
196 } else {
197 DCHECK(!Thread::Current()->IsExceptionPending());
198 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700199 if (IsValidDescriptor(descriptor)) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800200 RegType* entry = new UnresolvedReferenceType(descriptor, entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700201 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800202 return *entry;
203 } else {
204 // The descriptor is broken return the unknown type as there's nothing sensible that
205 // could be done at runtime
206 return Conflict();
207 }
208 }
209}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700210
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700211RegType& RegTypeCache::FromClass(const char* descriptor, mirror::Class* klass, bool precise) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700212 DCHECK(klass != nullptr);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700213 if (klass->IsPrimitive()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700214 // Note: precise isn't used for primitive classes. A char is assignable to an int. All
215 // primitive classes are final.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800216 return RegTypeFromPrimitiveType(klass->GetPrimitiveType());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700217 } else {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800218 // Look for the reference in the list of entries to have.
219 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700220 RegType* cur_entry = entries_[i];
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700221 if (cur_entry->klass_.Read() == klass && MatchingPrecisionForClass(cur_entry, precise)) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700222 return *cur_entry;
223 }
224 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800225 // No reference to the class was found, create new reference.
226 RegType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800227 if (precise) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700228 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800229 } else {
Ian Rogers637c65b2013-05-31 11:46:00 -0700230 entry = new ReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800231 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700232 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700233 return *entry;
234 }
235}
236
Andreas Gampe63981562014-04-17 12:28:43 -0700237RegTypeCache::RegTypeCache(bool can_load_classes) : can_load_classes_(can_load_classes) {
238 if (kIsDebugBuild && can_load_classes) {
239 Thread::Current()->AssertThreadSuspensionIsAllowable();
240 }
241 entries_.reserve(64);
242 FillPrimitiveAndSmallConstantTypes();
243}
244
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800245RegTypeCache::~RegTypeCache() {
246 CHECK_LE(primitive_count_, entries_.size());
247 // Delete only the non primitive types.
Ian Rogers41c65c12013-09-05 16:57:17 -0700248 if (entries_.size() == kNumPrimitivesAndSmallConstants) {
249 // All entries are from the global pool, nothing to delete.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800250 return;
251 }
252 std::vector<RegType*>::iterator non_primitive_begin = entries_.begin();
Ian Rogers41c65c12013-09-05 16:57:17 -0700253 std::advance(non_primitive_begin, kNumPrimitivesAndSmallConstants);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800254 STLDeleteContainerPointers(non_primitive_begin, entries_.end());
255}
256
257void RegTypeCache::ShutDown() {
258 if (RegTypeCache::primitive_initialized_) {
259 UndefinedType::Destroy();
260 ConflictType::Destroy();
261 BooleanType::Destroy();
262 ByteType::Destroy();
263 ShortType::Destroy();
264 CharType::Destroy();
265 IntegerType::Destroy();
266 LongLoType::Destroy();
267 LongHiType::Destroy();
Ian Rogers25ae7eb2013-04-18 16:06:05 -0700268 FloatType::Destroy();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800269 DoubleLoType::Destroy();
270 DoubleHiType::Destroy();
Ian Rogersdfe78a62013-11-14 09:20:55 -0800271 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700272 PreciseConstType* type = small_precise_constants_[value - kMinSmallConstant];
273 delete type;
Ian Rogersdfe78a62013-11-14 09:20:55 -0800274 small_precise_constants_[value - kMinSmallConstant] = nullptr;
Ian Rogers41c65c12013-09-05 16:57:17 -0700275 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800276 RegTypeCache::primitive_initialized_ = false;
277 RegTypeCache::primitive_count_ = 0;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800278 }
279}
280
Ian Rogers41c65c12013-09-05 16:57:17 -0700281template <class Type>
282Type* RegTypeCache::CreatePrimitiveTypeInstance(const std::string& descriptor) {
283 mirror::Class* klass = NULL;
284 // Try loading the class from linker.
285 if (!descriptor.empty()) {
Ian Rogers98379392014-02-24 16:53:16 -0800286 klass = art::Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(),
287 descriptor.c_str());
Ian Rogers41c65c12013-09-05 16:57:17 -0700288 }
289 Type* entry = Type::CreateInstance(klass, descriptor, RegTypeCache::primitive_count_);
290 RegTypeCache::primitive_count_++;
291 return entry;
292}
293
294void RegTypeCache::CreatePrimitiveAndSmallConstantTypes() {
Ian Rogers33e95662013-05-20 20:29:14 -0700295 CreatePrimitiveTypeInstance<UndefinedType>("");
296 CreatePrimitiveTypeInstance<ConflictType>("");
297 CreatePrimitiveTypeInstance<BooleanType>("Z");
298 CreatePrimitiveTypeInstance<ByteType>("B");
299 CreatePrimitiveTypeInstance<ShortType>("S");
300 CreatePrimitiveTypeInstance<CharType>("C");
301 CreatePrimitiveTypeInstance<IntegerType>("I");
302 CreatePrimitiveTypeInstance<LongLoType>("J");
303 CreatePrimitiveTypeInstance<LongHiType>("J");
304 CreatePrimitiveTypeInstance<FloatType>("F");
305 CreatePrimitiveTypeInstance<DoubleLoType>("D");
306 CreatePrimitiveTypeInstance<DoubleHiType>("D");
Ian Rogers41c65c12013-09-05 16:57:17 -0700307 for (int32_t value = kMinSmallConstant; value <= kMaxSmallConstant; ++value) {
308 PreciseConstType* type = new PreciseConstType(value, primitive_count_);
309 small_precise_constants_[value - kMinSmallConstant] = type;
310 primitive_count_++;
311 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800312}
313
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700314RegType& RegTypeCache::FromUnresolvedMerge(RegType& left, RegType& right) {
Ian Rogers529781d2012-07-23 17:24:29 -0700315 std::set<uint16_t> types;
316 if (left.IsUnresolvedMergedReference()) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700317 types = (down_cast<UnresolvedMergedType*>(&left))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700318 } else {
319 types.insert(left.GetId());
320 }
321 if (right.IsUnresolvedMergedReference()) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700322 std::set<uint16_t> right_types = (down_cast<UnresolvedMergedType*>(&right))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700323 types.insert(right_types.begin(), right_types.end());
324 } else {
325 types.insert(right.GetId());
326 }
327 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800328 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers529781d2012-07-23 17:24:29 -0700329 RegType* cur_entry = entries_[i];
330 if (cur_entry->IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800331 std::set<uint16_t> cur_entry_types =
332 (down_cast<UnresolvedMergedType*>(cur_entry))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700333 if (cur_entry_types == types) {
334 return *cur_entry;
335 }
336 }
337 }
338 // Create entry.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800339 RegType* entry = new UnresolvedMergedType(left.GetId(), right.GetId(), this, entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700340 AddEntry(entry);
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700341 if (kIsDebugBuild) {
342 UnresolvedMergedType* tmp_entry = down_cast<UnresolvedMergedType*>(entry);
343 std::set<uint16_t> check_types = tmp_entry->GetMergedTypes();
344 CHECK(check_types == types);
345 }
Ian Rogers529781d2012-07-23 17:24:29 -0700346 return *entry;
347}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700348
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700349RegType& RegTypeCache::FromUnresolvedSuperClass(RegType& child) {
Ian Rogers529781d2012-07-23 17:24:29 -0700350 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800351 for (size_t i = primitive_count_; i < entries_.size(); i++) {
352 RegType* cur_entry = entries_[i];
353 if (cur_entry->IsUnresolvedSuperClass()) {
354 UnresolvedSuperClass* tmp_entry =
355 down_cast<UnresolvedSuperClass*>(cur_entry);
356 uint16_t unresolved_super_child_id =
357 tmp_entry->GetUnresolvedSuperClassChildId();
358 if (unresolved_super_child_id == child.GetId()) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700359 return *cur_entry;
360 }
361 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800362 }
363 RegType* entry = new UnresolvedSuperClass(child.GetId(), this, entries_.size());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700364 AddEntry(entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800365 return *entry;
366}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700367
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700368UninitializedType& RegTypeCache::Uninitialized(RegType& type, uint32_t allocation_pc) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700369 UninitializedType* entry = NULL;
Ian Rogers637c65b2013-05-31 11:46:00 -0700370 const std::string& descriptor(type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800371 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800372 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700373 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800374 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
375 down_cast<UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc() == allocation_pc &&
376 (cur_entry->GetDescriptor() == descriptor)) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700377 return *down_cast<UnresolvedUninitializedRefType*>(cur_entry);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800378 }
379 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700380 entry = new UnresolvedUninitializedRefType(descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700381 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800382 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800383 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700384 RegType* cur_entry = entries_[i];
Ian Rogers776ac1f2012-04-13 23:36:36 -0700385 if (cur_entry->IsUninitializedReference() &&
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700386 down_cast<UninitializedReferenceType*>(cur_entry)
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800387 ->GetAllocationPc() == allocation_pc &&
Ian Rogers776ac1f2012-04-13 23:36:36 -0700388 cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700389 return *down_cast<UninitializedReferenceType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700390 }
391 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700392 entry = new UninitializedReferenceType(klass, descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700393 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700394 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700395 return *entry;
396}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700397
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700398RegType& RegTypeCache::FromUninitialized(RegType& uninit_type) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700399 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700400
Ian Rogers776ac1f2012-04-13 23:36:36 -0700401 if (uninit_type.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700402 const std::string& descriptor(uninit_type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800403 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700404 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800405 if (cur_entry->IsUnresolvedReference() &&
406 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700407 return *cur_entry;
408 }
409 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700410 entry = new UnresolvedReferenceType(descriptor.c_str(), entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700411 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800412 mirror::Class* klass = uninit_type.GetClass();
Brian Carlstromdf629502013-07-17 22:39:56 -0700413 if (uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700414 // For uninitialized "this reference" look for reference types that are not precise.
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700415 for (size_t i = primitive_count_; i < entries_.size(); i++) {
416 RegType* cur_entry = entries_[i];
417 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
418 return *cur_entry;
419 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700420 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700421 entry = new ReferenceType(klass, "", entries_.size());
Ian Rogers62342ec2013-06-11 10:26:37 -0700422 } else if (klass->IsInstantiable()) {
423 // We're uninitialized because of allocation, look or create a precise type as allocations
424 // may only create objects of that type.
425 for (size_t i = primitive_count_; i < entries_.size(); i++) {
426 RegType* cur_entry = entries_[i];
427 if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) {
428 return *cur_entry;
Ian Rogers04f94f42013-06-10 15:09:26 -0700429 }
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700430 }
Ian Rogers62342ec2013-06-11 10:26:37 -0700431 entry = new PreciseReferenceType(klass, uninit_type.GetDescriptor(), entries_.size());
432 } else {
433 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700434 }
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700435 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700436 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700437 return *entry;
438}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700439
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700440ImpreciseConstType& RegTypeCache::ByteConstant() {
441 ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::min(), false);
Ian Rogers41c65c12013-09-05 16:57:17 -0700442 DCHECK(result.IsImpreciseConstant());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700443 return *down_cast<ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800444}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700445
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700446ImpreciseConstType& RegTypeCache::CharConstant() {
Sebastien Hertz849600b2013-12-20 10:28:08 +0100447 int32_t jchar_max = static_cast<int32_t>(std::numeric_limits<jchar>::max());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700448 ConstantType& result = FromCat1Const(jchar_max, false);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100449 DCHECK(result.IsImpreciseConstant());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700450 return *down_cast<ImpreciseConstType*>(&result);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100451}
452
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700453ImpreciseConstType& RegTypeCache::ShortConstant() {
454 ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::min(), false);
Ian Rogers41c65c12013-09-05 16:57:17 -0700455 DCHECK(result.IsImpreciseConstant());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700456 return *down_cast<ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800457}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700458
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700459ImpreciseConstType& RegTypeCache::IntConstant() {
460 ConstantType& result = FromCat1Const(std::numeric_limits<jint>::max(), false);
Ian Rogers41c65c12013-09-05 16:57:17 -0700461 DCHECK(result.IsImpreciseConstant());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700462 return *down_cast<ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800463}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700464
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700465ImpreciseConstType& RegTypeCache::PosByteConstant() {
466 ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::max(), false);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100467 DCHECK(result.IsImpreciseConstant());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700468 return *down_cast<ImpreciseConstType*>(&result);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100469}
470
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700471ImpreciseConstType& RegTypeCache::PosShortConstant() {
472 ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::max(), false);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100473 DCHECK(result.IsImpreciseConstant());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700474 return *down_cast<ImpreciseConstType*>(&result);
Sebastien Hertz849600b2013-12-20 10:28:08 +0100475}
476
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700477UninitializedType& RegTypeCache::UninitializedThisArgument(RegType& type) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700478 UninitializedType* entry;
Ian Rogers637c65b2013-05-31 11:46:00 -0700479 const std::string& descriptor(type.GetDescriptor());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700480 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800481 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700482 RegType* cur_entry = entries_[i];
483 if (cur_entry->IsUnresolvedAndUninitializedThisReference() &&
484 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700485 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700486 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700487 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700488 entry = new UnresolvedUninitializedThisRefType(descriptor, entries_.size());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700489 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800490 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800491 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700492 RegType* cur_entry = entries_[i];
Ian Rogers62342ec2013-06-11 10:26:37 -0700493 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700494 return *down_cast<UninitializedType*>(cur_entry);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700495 }
496 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700497 entry = new UninitializedThisReferenceType(klass, descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700498 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700499 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700500 return *entry;
501}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700502
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700503ConstantType& RegTypeCache::FromCat1NonSmallConstant(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800504 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700505 RegType* cur_entry = entries_[i];
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700506 if (cur_entry->klass_.IsNull() && cur_entry->IsConstant() &&
Ian Rogers637c65b2013-05-31 11:46:00 -0700507 cur_entry->IsPreciseConstant() == precise &&
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800508 (down_cast<ConstantType*>(cur_entry))->ConstantValue() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700509 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700510 }
511 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700512 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800513 if (precise) {
514 entry = new PreciseConstType(value, entries_.size());
515 } else {
516 entry = new ImpreciseConstType(value, entries_.size());
517 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700518 AddEntry(entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800519 return *entry;
520}
521
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700522ConstantType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800523 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800524 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800525 if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) &&
526 (down_cast<ConstantType*>(cur_entry))->ConstantValueLo() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700527 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800528 }
529 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700530 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800531 if (precise) {
532 entry = new PreciseConstLoType(value, entries_.size());
533 } else {
534 entry = new ImpreciseConstLoType(value, entries_.size());
535 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700536 AddEntry(entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800537 return *entry;
538}
539
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700540ConstantType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800541 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800542 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800543 if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) &&
544 (down_cast<ConstantType*>(cur_entry))->ConstantValueHi() == value) {
Ian Rogers41c65c12013-09-05 16:57:17 -0700545 return *down_cast<ConstantType*>(cur_entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800546 }
547 }
Ian Rogers41c65c12013-09-05 16:57:17 -0700548 ConstantType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800549 if (precise) {
550 entry = new PreciseConstHiType(value, entries_.size());
551 } else {
552 entry = new ImpreciseConstHiType(value, entries_.size());
553 }
Andreas Gampeaa910d52014-07-30 18:59:05 -0700554 AddEntry(entry);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700555 return *entry;
556}
557
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -0700558RegType& RegTypeCache::GetComponentType(RegType& array, mirror::ClassLoader* loader) {
Ian Rogersa9a82542013-10-04 11:17:26 -0700559 if (!array.IsArrayTypes()) {
560 return Conflict();
561 } else if (array.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700562 const std::string& descriptor(array.GetDescriptor());
563 const std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogersb4903572012-10-11 11:52:56 -0700564 return FromDescriptor(loader, component.c_str(), false);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700565 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800566 mirror::Class* klass = array.GetClass()->GetComponentType();
Andreas Gampeaa910d52014-07-30 18:59:05 -0700567 if (klass->IsErroneous()) {
568 // Arrays may have erroneous component types, use unresolved in that case.
569 // We assume that the primitive classes are not erroneous, so we know it is a
570 // reference type.
571 return FromDescriptor(loader, klass->GetDescriptor().c_str(), false);
572 } else {
573 return FromClass(klass->GetDescriptor().c_str(), klass,
574 klass->CannotBeAssignedFromOtherTypes());
575 }
Ian Rogersb4903572012-10-11 11:52:56 -0700576 }
577}
578
579void RegTypeCache::Dump(std::ostream& os) {
Ian Rogersb4903572012-10-11 11:52:56 -0700580 for (size_t i = 0; i < entries_.size(); i++) {
581 RegType* cur_entry = entries_[i];
582 if (cur_entry != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800583 os << i << ": " << cur_entry->Dump() << "\n";
Ian Rogersb4903572012-10-11 11:52:56 -0700584 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700585 }
586}
587
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800588void RegTypeCache::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800589 for (RegType* entry : entries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800590 entry->VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800591 }
592}
593
Andreas Gampeaa910d52014-07-30 18:59:05 -0700594void RegTypeCache::AddEntry(RegType* new_entry) {
595 entries_.push_back(new_entry);
596}
597
Elliott Hughesa21039c2012-06-21 12:09:25 -0700598} // namespace verifier
599} // namespace art