blob: 601325083552124cc6bccc008623e8b20a2b24a6 [file] [log] [blame]
Ian Rogers776ac1f2012-04-13 23:36:36 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080017#include "reg_type_cache-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070018
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080019#include "base/casts.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070020#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/class-inl.h"
22#include "mirror/object-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070023#include "object_utils.h"
24
25namespace art {
26namespace verifier {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070027
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080028bool RegTypeCache::primitive_initialized_ = false;
29uint16_t RegTypeCache::primitive_start_ = 0;
30uint16_t RegTypeCache::primitive_count_ = 0;
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
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080047void RegTypeCache::FillPrimitiveTypes() {
48 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());
60 DCHECK_EQ(entries_.size(), primitive_count_);
61}
62
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070063const RegType& RegTypeCache::FromDescriptor(mirror::ClassLoader* loader, const char* descriptor,
64 bool precise) {
65 DCHECK(RegTypeCache::primitive_initialized_);
66 if (descriptor[1] == '\0') {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080067 switch (descriptor[0]) {
68 case 'Z':
69 return Boolean();
70 case 'B':
71 return Byte();
72 case 'S':
73 return Short();
74 case 'C':
75 return Char();
76 case 'I':
77 return Integer();
78 case 'J':
79 return LongLo();
80 case 'F':
81 return Float();
82 case 'D':
83 return DoubleLo();
84 case 'V': // For void types, conflict types.
85 default:
Ian Rogersad0b3a32012-04-16 14:50:24 -070086 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -070087 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080088 } else if (descriptor[0] == 'L' || descriptor[0] == '[') {
89 return From(loader, descriptor, precise);
90 } else {
91 return Conflict();
92 }
93};
94
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070095const RegType& RegTypeCache::RegTypeFromPrimitiveType(Primitive::Type prim_type) const {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080096 CHECK(RegTypeCache::primitive_initialized_);
97 switch (prim_type) {
98 case Primitive::kPrimBoolean:
99 return *BooleanType::GetInstance();
100 case Primitive::kPrimByte:
101 return *ByteType::GetInstance();
102 case Primitive::kPrimShort:
103 return *ShortType::GetInstance();
104 case Primitive::kPrimChar:
105 return *CharType::GetInstance();
106 case Primitive::kPrimInt:
107 return *IntegerType::GetInstance();
108 case Primitive::kPrimLong:
109 return *LongLoType::GetInstance();
110 case Primitive::kPrimFloat:
111 return *FloatType::GetInstance();
112 case Primitive::kPrimDouble:
113 return *DoubleLoType::GetInstance();
114 case Primitive::kPrimVoid:
115 default:
116 return *ConflictType::GetInstance();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700117 }
118}
119
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700120bool RegTypeCache::MatchDescriptor(size_t idx, const char* descriptor, bool precise) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700121 RegType* entry = entries_[idx];
122 if (entry->descriptor_ != descriptor) {
123 return false;
124 }
Ian Rogers04f94f42013-06-10 15:09:26 -0700125 if (entry->HasClass()) {
126 return MatchingPrecisionForClass(entry, precise);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800127 }
Ian Rogers04f94f42013-06-10 15:09:26 -0700128 // There is no notion of precise unresolved references, the precise information is just dropped
129 // on the floor.
130 DCHECK(entry->IsUnresolvedReference());
131 return true;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800132}
133
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700134mirror::Class* RegTypeCache::ResolveClass(const char* descriptor, mirror::ClassLoader* loader) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800135 // Class was not found, must create new type.
136 // Try resolving class
137 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
138 mirror::Class* klass = NULL;
139 if (can_load_classes_) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700140 klass = class_linker->FindClass(descriptor, loader);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800141 } else {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700142 klass = class_linker->LookupClass(descriptor, loader);
Ian Rogers62d6c772013-02-27 08:32:07 -0800143 if (klass != NULL && !klass->IsLoaded()) {
144 // We found the class but without it being loaded its not safe for use.
145 klass = NULL;
146 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800147 }
148 return klass;
149}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700150
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800151void RegTypeCache::ClearException() {
152 if (can_load_classes_) {
153 DCHECK(Thread::Current()->IsExceptionPending());
154 Thread::Current()->ClearException();
155 } else {
156 DCHECK(!Thread::Current()->IsExceptionPending());
157 }
158}
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700159
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700160const RegType& RegTypeCache::From(mirror::ClassLoader* loader, const char* descriptor,
161 bool precise) {
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700162 // Try looking up the class in the cache first.
163 for (size_t i = primitive_count_; i < entries_.size(); i++) {
164 if (MatchDescriptor(i, descriptor, precise)) {
165 return *(entries_[i]);
166 }
167 }
168 // Class not found in the cache, will create a new type for that.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800169 // Try resolving class.
170 mirror::Class* klass = ResolveClass(descriptor, loader);
171 if (klass != NULL) {
172 // Class resolved, first look for the class in the list of entries
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800173 // Class was not found, must create new type.
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700174 //To pass the verification, the type should be imprecise,
175 // instantiable or an interface with the precise type set to false.
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700176 DCHECK(!precise || klass->IsInstantiable());
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700177 // Create a precise type if:
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700178 // 1- Class is final and NOT an interface. a precise interface is meaningless !!
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700179 // 2- Precise Flag passed as true.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800180 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700181 // Create an imprecise type if we can't tell for a fact that it is precise.
Ian Rogers04f94f42013-06-10 15:09:26 -0700182 if (klass->CannotBeAssignedFromOtherTypes() || precise) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700183 DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass());
184 DCHECK(!klass->IsInterface());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800185 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
186 } else {
187 entry = new ReferenceType(klass, descriptor, entries_.size());
188 }
189 entries_.push_back(entry);
190 return *entry;
191 } else { // Class not resolved.
192 // We tried loading the class and failed, this might get an exception raised
193 // so we want to clear it before we go on.
194 ClearException();
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700195 if (IsValidDescriptor(descriptor)) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800196 RegType* entry = new UnresolvedReferenceType(descriptor, entries_.size());
197 entries_.push_back(entry);
198 return *entry;
199 } else {
200 // The descriptor is broken return the unknown type as there's nothing sensible that
201 // could be done at runtime
202 return Conflict();
203 }
204 }
205}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700206
Ian Rogers637c65b2013-05-31 11:46:00 -0700207const RegType& RegTypeCache::FromClass(const char* descriptor, mirror::Class* klass, bool precise) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700208 if (klass->IsPrimitive()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700209 // Note: precise isn't used for primitive classes. A char is assignable to an int. All
210 // primitive classes are final.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800211 return RegTypeFromPrimitiveType(klass->GetPrimitiveType());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700212 } else {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800213 // Look for the reference in the list of entries to have.
214 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700215 RegType* cur_entry = entries_[i];
Ian Rogers637c65b2013-05-31 11:46:00 -0700216 if (cur_entry->klass_ == klass && MatchingPrecisionForClass(cur_entry, precise)) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700217 return *cur_entry;
218 }
219 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800220 // No reference to the class was found, create new reference.
221 RegType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800222 if (precise) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700223 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800224 } else {
Ian Rogers637c65b2013-05-31 11:46:00 -0700225 entry = new ReferenceType(klass, descriptor, entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800226 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700227 entries_.push_back(entry);
228 return *entry;
229 }
230}
231
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800232RegTypeCache::~RegTypeCache() {
233 CHECK_LE(primitive_count_, entries_.size());
234 // Delete only the non primitive types.
Ian Rogers62d6c772013-02-27 08:32:07 -0800235 if (entries_.size() == kNumPrimitives) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800236 // All entries are primitive, nothing to delete.
237 return;
238 }
239 std::vector<RegType*>::iterator non_primitive_begin = entries_.begin();
Ian Rogers62d6c772013-02-27 08:32:07 -0800240 std::advance(non_primitive_begin, kNumPrimitives);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800241 STLDeleteContainerPointers(non_primitive_begin, entries_.end());
242}
243
244void RegTypeCache::ShutDown() {
245 if (RegTypeCache::primitive_initialized_) {
246 UndefinedType::Destroy();
247 ConflictType::Destroy();
248 BooleanType::Destroy();
249 ByteType::Destroy();
250 ShortType::Destroy();
251 CharType::Destroy();
252 IntegerType::Destroy();
253 LongLoType::Destroy();
254 LongHiType::Destroy();
Ian Rogers25ae7eb2013-04-18 16:06:05 -0700255 FloatType::Destroy();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800256 DoubleLoType::Destroy();
257 DoubleHiType::Destroy();
Ian Rogers62d6c772013-02-27 08:32:07 -0800258 RegTypeCache::primitive_initialized_ = false;
259 RegTypeCache::primitive_count_ = 0;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800260 }
261}
262
263void RegTypeCache::CreatePrimitiveTypes() {
Ian Rogers33e95662013-05-20 20:29:14 -0700264 CreatePrimitiveTypeInstance<UndefinedType>("");
265 CreatePrimitiveTypeInstance<ConflictType>("");
266 CreatePrimitiveTypeInstance<BooleanType>("Z");
267 CreatePrimitiveTypeInstance<ByteType>("B");
268 CreatePrimitiveTypeInstance<ShortType>("S");
269 CreatePrimitiveTypeInstance<CharType>("C");
270 CreatePrimitiveTypeInstance<IntegerType>("I");
271 CreatePrimitiveTypeInstance<LongLoType>("J");
272 CreatePrimitiveTypeInstance<LongHiType>("J");
273 CreatePrimitiveTypeInstance<FloatType>("F");
274 CreatePrimitiveTypeInstance<DoubleLoType>("D");
275 CreatePrimitiveTypeInstance<DoubleHiType>("D");
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800276}
277
Ian Rogers529781d2012-07-23 17:24:29 -0700278const RegType& RegTypeCache::FromUnresolvedMerge(const RegType& left, const RegType& right) {
279 std::set<uint16_t> types;
280 if (left.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800281 RegType& non_const(const_cast<RegType&>(left));
282 types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700283 } else {
284 types.insert(left.GetId());
285 }
286 if (right.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800287 RegType& non_const(const_cast<RegType&>(right));
288 std::set<uint16_t> right_types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700289 types.insert(right_types.begin(), right_types.end());
290 } else {
291 types.insert(right.GetId());
292 }
293 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800294 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers529781d2012-07-23 17:24:29 -0700295 RegType* cur_entry = entries_[i];
296 if (cur_entry->IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800297 std::set<uint16_t> cur_entry_types =
298 (down_cast<UnresolvedMergedType*>(cur_entry))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700299 if (cur_entry_types == types) {
300 return *cur_entry;
301 }
302 }
303 }
304 // Create entry.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800305 RegType* entry = new UnresolvedMergedType(left.GetId(), right.GetId(), this, entries_.size());
Ian Rogers529781d2012-07-23 17:24:29 -0700306 entries_.push_back(entry);
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700307 if (kIsDebugBuild) {
308 UnresolvedMergedType* tmp_entry = down_cast<UnresolvedMergedType*>(entry);
309 std::set<uint16_t> check_types = tmp_entry->GetMergedTypes();
310 CHECK(check_types == types);
311 }
Ian Rogers529781d2012-07-23 17:24:29 -0700312 return *entry;
313}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700314
Ian Rogers529781d2012-07-23 17:24:29 -0700315const RegType& RegTypeCache::FromUnresolvedSuperClass(const RegType& child) {
316 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800317 for (size_t i = primitive_count_; i < entries_.size(); i++) {
318 RegType* cur_entry = entries_[i];
319 if (cur_entry->IsUnresolvedSuperClass()) {
320 UnresolvedSuperClass* tmp_entry =
321 down_cast<UnresolvedSuperClass*>(cur_entry);
322 uint16_t unresolved_super_child_id =
323 tmp_entry->GetUnresolvedSuperClassChildId();
324 if (unresolved_super_child_id == child.GetId()) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700325 return *cur_entry;
326 }
327 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800328 }
329 RegType* entry = new UnresolvedSuperClass(child.GetId(), this, entries_.size());
330 entries_.push_back(entry);
331 return *entry;
332}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700333
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800334const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
335 RegType* entry = NULL;
336 RegType* cur_entry = NULL;
Ian Rogers637c65b2013-05-31 11:46:00 -0700337 const std::string& descriptor(type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800338 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800339 for (size_t i = primitive_count_; i < entries_.size(); i++) {
340 cur_entry = entries_[i];
341 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
342 down_cast<UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc() == allocation_pc &&
343 (cur_entry->GetDescriptor() == descriptor)) {
344 return *cur_entry;
345 }
346 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700347 entry = new UnresolvedUninitializedRefType(descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700348 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800350 for (size_t i = primitive_count_; i < entries_.size(); i++) {
351 cur_entry = entries_[i];
Ian Rogers776ac1f2012-04-13 23:36:36 -0700352 if (cur_entry->IsUninitializedReference() &&
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700353 down_cast<UninitializedReferenceType*>(cur_entry)
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800354 ->GetAllocationPc() == allocation_pc &&
Ian Rogers776ac1f2012-04-13 23:36:36 -0700355 cur_entry->GetClass() == klass) {
356 return *cur_entry;
357 }
358 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700359 entry = new UninitializedReferenceType(klass, descriptor, allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700360 }
361 entries_.push_back(entry);
362 return *entry;
363}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700364
Ian Rogers776ac1f2012-04-13 23:36:36 -0700365const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
366 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700367
Ian Rogers776ac1f2012-04-13 23:36:36 -0700368 if (uninit_type.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700369 const std::string& descriptor(uninit_type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800370 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700371 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800372 if (cur_entry->IsUnresolvedReference() &&
373 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700374 return *cur_entry;
375 }
376 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700377 entry = new UnresolvedReferenceType(descriptor.c_str(), entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700378 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379 mirror::Class* klass = uninit_type.GetClass();
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700380 if(uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) {
Ian Rogers04f94f42013-06-10 15:09:26 -0700381 // For uninitialized "this reference" look for reference types that are not precise.
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700382 for (size_t i = primitive_count_; i < entries_.size(); i++) {
383 RegType* cur_entry = entries_[i];
384 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
385 return *cur_entry;
386 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700387 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700388 entry = new ReferenceType(klass, "", entries_.size());
Ian Rogers62342ec2013-06-11 10:26:37 -0700389 } else if (klass->IsInstantiable()) {
390 // We're uninitialized because of allocation, look or create a precise type as allocations
391 // may only create objects of that type.
392 for (size_t i = primitive_count_; i < entries_.size(); i++) {
393 RegType* cur_entry = entries_[i];
394 if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) {
395 return *cur_entry;
Ian Rogers04f94f42013-06-10 15:09:26 -0700396 }
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700397 }
Ian Rogers62342ec2013-06-11 10:26:37 -0700398 entry = new PreciseReferenceType(klass, uninit_type.GetDescriptor(), entries_.size());
399 } else {
400 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700401 }
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700402 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700403 entries_.push_back(entry);
404 return *entry;
405}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700406
407const RegType& RegTypeCache::ByteConstant() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800408 return FromCat1Const(std::numeric_limits<jbyte>::min(), false);
409}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700410
411const RegType& RegTypeCache::ShortConstant() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800412 return FromCat1Const(std::numeric_limits<jshort>::min(), false);
413}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700414
415const RegType& RegTypeCache::IntConstant() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800416 return FromCat1Const(std::numeric_limits<jint>::max(), false);
417}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700418
Ian Rogersad0b3a32012-04-16 14:50:24 -0700419const RegType& RegTypeCache::UninitializedThisArgument(const RegType& type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420 RegType* entry;
Ian Rogers637c65b2013-05-31 11:46:00 -0700421 const std::string& descriptor(type.GetDescriptor());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700422 if (type.IsUnresolvedTypes()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800423 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700424 RegType* cur_entry = entries_[i];
425 if (cur_entry->IsUnresolvedAndUninitializedThisReference() &&
426 cur_entry->GetDescriptor() == descriptor) {
427 return *cur_entry;
428 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700429 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700430 entry = new UnresolvedUninitializedThisRefType(descriptor, entries_.size());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700431 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800432 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800433 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700434 RegType* cur_entry = entries_[i];
Ian Rogers62342ec2013-06-11 10:26:37 -0700435 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700436 return *cur_entry;
437 }
438 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700439 entry = new UninitializedThisReferenceType(klass, descriptor, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700440 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700441 entries_.push_back(entry);
442 return *entry;
443}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700444
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800445const RegType& RegTypeCache::FromCat1Const(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800446 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700447 RegType* cur_entry = entries_[i];
Ian Rogers637c65b2013-05-31 11:46:00 -0700448 if (cur_entry->klass_ == NULL && cur_entry->IsConstant() &&
449 cur_entry->IsPreciseConstant() == precise &&
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800450 (down_cast<ConstantType*>(cur_entry))->ConstantValue() == value) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700451 return *cur_entry;
452 }
453 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800454 RegType* entry;
455 if (precise) {
456 entry = new PreciseConstType(value, entries_.size());
457 } else {
458 entry = new ImpreciseConstType(value, entries_.size());
459 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800460 entries_.push_back(entry);
461 return *entry;
462}
463
464const RegType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800465 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800466 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800467 if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) &&
468 (down_cast<ConstantType*>(cur_entry))->ConstantValueLo() == value) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800469 return *cur_entry;
470 }
471 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800472 RegType* entry;
473 if (precise) {
474 entry = new PreciseConstLoType(value, entries_.size());
475 } else {
476 entry = new ImpreciseConstLoType(value, entries_.size());
477 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800478 entries_.push_back(entry);
479 return *entry;
480}
481
482const RegType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800483 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800484 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800485 if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) &&
486 (down_cast<ConstantType*>(cur_entry))->ConstantValueHi() == value) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800487 return *cur_entry;
488 }
489 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800490 RegType* entry;
491 if (precise) {
492 entry = new PreciseConstHiType(value, entries_.size());
493 } else {
494 entry = new ImpreciseConstHiType(value, entries_.size());
495 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700496 entries_.push_back(entry);
497 return *entry;
498}
499
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800500const RegType& RegTypeCache::GetComponentType(const RegType& array, mirror::ClassLoader* loader) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700501 CHECK(array.IsArrayTypes());
502 if (array.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700503 const std::string& descriptor(array.GetDescriptor());
504 const std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogersb4903572012-10-11 11:52:56 -0700505 return FromDescriptor(loader, component.c_str(), false);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700506 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800507 mirror::Class* klass = array.GetClass()->GetComponentType();
Ian Rogers04f94f42013-06-10 15:09:26 -0700508 return FromClass(ClassHelper(klass).GetDescriptor(), klass,
509 klass->CannotBeAssignedFromOtherTypes());
Ian Rogersb4903572012-10-11 11:52:56 -0700510 }
511}
512
513void RegTypeCache::Dump(std::ostream& os) {
Ian Rogersb4903572012-10-11 11:52:56 -0700514 for (size_t i = 0; i < entries_.size(); i++) {
515 RegType* cur_entry = entries_[i];
516 if (cur_entry != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800517 os << i << ": " << cur_entry->Dump() << "\n";
Ian Rogersb4903572012-10-11 11:52:56 -0700518 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700519 }
520}
521
Elliott Hughesa21039c2012-06-21 12:09:25 -0700522} // namespace verifier
523} // namespace art