blob: 111e8679c014487643879ea44fba847ec25dfefe [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
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700159const RegType& 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 }
188 entries_.push_back(entry);
189 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());
201 entries_.push_back(entry);
202 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
Ian Rogers637c65b2013-05-31 11:46:00 -0700211const RegType& RegTypeCache::FromClass(const char* descriptor, mirror::Class* klass, bool precise) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -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];
Ian Rogers637c65b2013-05-31 11:46:00 -0700221 if (cur_entry->klass_ == 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 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700232 entries_.push_back(entry);
233 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
Ian Rogers529781d2012-07-23 17:24:29 -0700314const RegType& RegTypeCache::FromUnresolvedMerge(const RegType& left, const RegType& right) {
315 std::set<uint16_t> types;
316 if (left.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800317 RegType& non_const(const_cast<RegType&>(left));
318 types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700319 } else {
320 types.insert(left.GetId());
321 }
322 if (right.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800323 RegType& non_const(const_cast<RegType&>(right));
324 std::set<uint16_t> right_types = (down_cast<UnresolvedMergedType*>(&non_const))->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());
Ian Rogers529781d2012-07-23 17:24:29 -0700342 entries_.push_back(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
Ian Rogers529781d2012-07-23 17:24:29 -0700351const RegType& RegTypeCache::FromUnresolvedSuperClass(const RegType& child) {
352 // 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());
366 entries_.push_back(entry);
367 return *entry;
368}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700369
Ian Rogers41c65c12013-09-05 16:57:17 -0700370const UninitializedType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
371 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 }
396 entries_.push_back(entry);
397 return *entry;
398}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700399
Ian Rogers776ac1f2012-04-13 23:36:36 -0700400const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
401 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 Rogers1bf8d4d2013-05-30 00:18:49 -0700412 entry = new UnresolvedReferenceType(descriptor.c_str(), 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 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700438 entries_.push_back(entry);
439 return *entry;
440}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700441
Ian Rogers41c65c12013-09-05 16:57:17 -0700442const ImpreciseConstType& RegTypeCache::ByteConstant() {
443 const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::min(), false);
444 DCHECK(result.IsImpreciseConstant());
445 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800446}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700447
Sebastien Hertz849600b2013-12-20 10:28:08 +0100448const ImpreciseConstType& RegTypeCache::CharConstant() {
449 int32_t jchar_max = static_cast<int32_t>(std::numeric_limits<jchar>::max());
450 const ConstantType& result = FromCat1Const(jchar_max, false);
451 DCHECK(result.IsImpreciseConstant());
452 return *down_cast<const ImpreciseConstType*>(&result);
453}
454
Ian Rogers41c65c12013-09-05 16:57:17 -0700455const ImpreciseConstType& RegTypeCache::ShortConstant() {
456 const ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::min(), false);
457 DCHECK(result.IsImpreciseConstant());
458 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800459}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700460
Ian Rogers41c65c12013-09-05 16:57:17 -0700461const ImpreciseConstType& RegTypeCache::IntConstant() {
462 const ConstantType& result = FromCat1Const(std::numeric_limits<jint>::max(), false);
463 DCHECK(result.IsImpreciseConstant());
464 return *down_cast<const ImpreciseConstType*>(&result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700466
Sebastien Hertz849600b2013-12-20 10:28:08 +0100467const ImpreciseConstType& RegTypeCache::PosByteConstant() {
468 const ConstantType& result = FromCat1Const(std::numeric_limits<jbyte>::max(), false);
469 DCHECK(result.IsImpreciseConstant());
470 return *down_cast<const ImpreciseConstType*>(&result);
471}
472
473const ImpreciseConstType& RegTypeCache::PosShortConstant() {
474 const ConstantType& result = FromCat1Const(std::numeric_limits<jshort>::max(), false);
475 DCHECK(result.IsImpreciseConstant());
476 return *down_cast<const ImpreciseConstType*>(&result);
477}
478
Ian Rogers41c65c12013-09-05 16:57:17 -0700479const UninitializedType& RegTypeCache::UninitializedThisArgument(const RegType& type) {
480 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 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700501 entries_.push_back(entry);
502 return *entry;
503}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700504
Ian Rogers41c65c12013-09-05 16:57:17 -0700505const ConstantType& 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];
Ian Rogers637c65b2013-05-31 11:46:00 -0700508 if (cur_entry->klass_ == NULL && cur_entry->IsConstant() &&
509 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 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800520 entries_.push_back(entry);
521 return *entry;
522}
523
Ian Rogers41c65c12013-09-05 16:57:17 -0700524const ConstantType& 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 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800538 entries_.push_back(entry);
539 return *entry;
540}
541
Ian Rogers41c65c12013-09-05 16:57:17 -0700542const ConstantType& 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 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700556 entries_.push_back(entry);
557 return *entry;
558}
559
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800560const RegType& RegTypeCache::GetComponentType(const 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 Rogers04f94f42013-06-10 15:09:26 -0700569 return FromClass(ClassHelper(klass).GetDescriptor(), klass,
570 klass->CannotBeAssignedFromOtherTypes());
Ian Rogersb4903572012-10-11 11:52:56 -0700571 }
572}
573
574void RegTypeCache::Dump(std::ostream& os) {
Ian Rogersb4903572012-10-11 11:52:56 -0700575 for (size_t i = 0; i < entries_.size(); i++) {
576 RegType* cur_entry = entries_[i];
577 if (cur_entry != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800578 os << i << ": " << cur_entry->Dump() << "\n";
Ian Rogersb4903572012-10-11 11:52:56 -0700579 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700580 }
581}
582
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800583void RegTypeCache::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800584 for (RegType* entry : entries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800585 entry->VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800586 }
587}
588
Elliott Hughesa21039c2012-06-21 12:09:25 -0700589} // namespace verifier
590} // namespace art