blob: 57a825b0bbf08f0ff5605e25568dbbc1efb9bfb0 [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_) {
34 return (entry->IsPreciseReference() == precise) || (entry->GetClass()->IsFinal() && !precise);
35}
36
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080037void RegTypeCache::FillPrimitiveTypes() {
38 entries_.push_back(UndefinedType::GetInstance());
39 entries_.push_back(ConflictType::GetInstance());
40 entries_.push_back(BooleanType::GetInstance());
41 entries_.push_back(ByteType::GetInstance());
42 entries_.push_back(ShortType::GetInstance());
43 entries_.push_back(CharType::GetInstance());
44 entries_.push_back(IntegerType::GetInstance());
45 entries_.push_back(LongLoType::GetInstance());
46 entries_.push_back(LongHiType::GetInstance());
47 entries_.push_back(FloatType::GetInstance());
48 entries_.push_back(DoubleLoType::GetInstance());
49 entries_.push_back(DoubleHiType::GetInstance());
50 DCHECK_EQ(entries_.size(), primitive_count_);
51}
52
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070053const RegType& RegTypeCache::FromDescriptor(mirror::ClassLoader* loader, const char* descriptor,
54 bool precise) {
55 DCHECK(RegTypeCache::primitive_initialized_);
56 if (descriptor[1] == '\0') {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080057 switch (descriptor[0]) {
58 case 'Z':
59 return Boolean();
60 case 'B':
61 return Byte();
62 case 'S':
63 return Short();
64 case 'C':
65 return Char();
66 case 'I':
67 return Integer();
68 case 'J':
69 return LongLo();
70 case 'F':
71 return Float();
72 case 'D':
73 return DoubleLo();
74 case 'V': // For void types, conflict types.
75 default:
Ian Rogersad0b3a32012-04-16 14:50:24 -070076 return Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -070077 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080078 } else if (descriptor[0] == 'L' || descriptor[0] == '[') {
79 return From(loader, descriptor, precise);
80 } else {
81 return Conflict();
82 }
83};
84
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070085const RegType& RegTypeCache::RegTypeFromPrimitiveType(Primitive::Type prim_type) const {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080086 CHECK(RegTypeCache::primitive_initialized_);
87 switch (prim_type) {
88 case Primitive::kPrimBoolean:
89 return *BooleanType::GetInstance();
90 case Primitive::kPrimByte:
91 return *ByteType::GetInstance();
92 case Primitive::kPrimShort:
93 return *ShortType::GetInstance();
94 case Primitive::kPrimChar:
95 return *CharType::GetInstance();
96 case Primitive::kPrimInt:
97 return *IntegerType::GetInstance();
98 case Primitive::kPrimLong:
99 return *LongLoType::GetInstance();
100 case Primitive::kPrimFloat:
101 return *FloatType::GetInstance();
102 case Primitive::kPrimDouble:
103 return *DoubleLoType::GetInstance();
104 case Primitive::kPrimVoid:
105 default:
106 return *ConflictType::GetInstance();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700107 }
108}
109
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700110bool RegTypeCache::MatchDescriptor(size_t idx, const char* descriptor, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800111 RegType* cur_entry = entries_[idx];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800112 if (cur_entry->HasClass()) {
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700113 // Check the descriptor in the reg_type if available.
114 if(!cur_entry->descriptor_.empty()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700115 if (cur_entry->descriptor_ == descriptor && MatchingPrecisionForClass(cur_entry, precise)) {
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700116 return true;
117 }
118 } else {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700119 // TODO: maintain an invariant that when we have a Class the descriptor is computed from that.
120 // Descriptor not found in reg_type, maybe available in Class object.
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700121 // So we might have cases where we have the class but not the descriptor
122 // for that class we need the class helper to get the descriptor
123 // and match it with the one we are given.
124 ClassHelper kh(cur_entry->GetClass());
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700125 if ((strcmp(descriptor, kh.GetDescriptor()) == 0) &&
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700126 MatchingPrecisionForClass(cur_entry, precise)) {
127 return true;
128 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800129 }
130 } else if (cur_entry->IsUnresolvedReference() && cur_entry->GetDescriptor() == descriptor) {
131 return true;
132 }
133 return false;
134}
135
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700136mirror::Class* RegTypeCache::ResolveClass(const char* descriptor, mirror::ClassLoader* loader) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800137 // Class was not found, must create new type.
138 // Try resolving class
139 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
140 mirror::Class* klass = NULL;
141 if (can_load_classes_) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700142 klass = class_linker->FindClass(descriptor, loader);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800143 } else {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700144 klass = class_linker->LookupClass(descriptor, loader);
Ian Rogers62d6c772013-02-27 08:32:07 -0800145 if (klass != NULL && !klass->IsLoaded()) {
146 // We found the class but without it being loaded its not safe for use.
147 klass = NULL;
148 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800149 }
150 return klass;
151}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700152
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800153void RegTypeCache::ClearException() {
154 if (can_load_classes_) {
155 DCHECK(Thread::Current()->IsExceptionPending());
156 Thread::Current()->ClearException();
157 } else {
158 DCHECK(!Thread::Current()->IsExceptionPending());
159 }
160}
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700161
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700162const RegType& RegTypeCache::From(mirror::ClassLoader* loader, const char* descriptor,
163 bool precise) {
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700164 // Try looking up the class in the cache first.
165 for (size_t i = primitive_count_; i < entries_.size(); i++) {
166 if (MatchDescriptor(i, descriptor, precise)) {
167 return *(entries_[i]);
168 }
169 }
170 // Class not found in the cache, will create a new type for that.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800171 // Try resolving class.
172 mirror::Class* klass = ResolveClass(descriptor, loader);
173 if (klass != NULL) {
174 // Class resolved, first look for the class in the list of entries
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800175 // Class was not found, must create new type.
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700176 //To pass the verification, the type should be imprecise,
177 // instantiable or an interface with the precise type set to false.
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700178 DCHECK(!precise || klass->IsInstantiable());
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700179 // Create a precise type if:
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700180 // 1- Class is final and NOT an interface. a precise interface is meaningless !!
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700181 // 2- Precise Flag passed as true.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800182 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700183 // Create an imprecise type if we can't tell for a fact that it is precise.
184 if ((klass->IsFinal()) || precise) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700185 DCHECK(!(klass->IsAbstract()) || klass->IsArrayClass());
186 DCHECK(!klass->IsInterface());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800187 entry = new PreciseReferenceType(klass, descriptor, entries_.size());
188 } else {
189 entry = new ReferenceType(klass, descriptor, entries_.size());
190 }
191 entries_.push_back(entry);
192 return *entry;
193 } else { // Class not resolved.
194 // We tried loading the class and failed, this might get an exception raised
195 // so we want to clear it before we go on.
196 ClearException();
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700197 if (IsValidDescriptor(descriptor)) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800198 RegType* entry = new UnresolvedReferenceType(descriptor, entries_.size());
199 entries_.push_back(entry);
200 return *entry;
201 } else {
202 // The descriptor is broken return the unknown type as there's nothing sensible that
203 // could be done at runtime
204 return Conflict();
205 }
206 }
207}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700208
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209const RegType& RegTypeCache::FromClass(mirror::Class* klass, bool precise) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700210 if (klass->IsPrimitive()) {
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];
Sameer Abu Asal2c6de222013-05-02 17:38:59 -0700216 if ((cur_entry->HasClass()) && cur_entry->GetClass() == klass &&
217 MatchingPrecisionForClass(cur_entry, precise)) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700218 return *cur_entry;
219 }
220 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800221 // No reference to the class was found, create new reference.
222 RegType* entry;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800223 if (precise) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700224 entry = new PreciseReferenceType(klass, "", entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800225 } else {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700226 entry = new ReferenceType(klass, "", entries_.size());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800227 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700228 entries_.push_back(entry);
229 return *entry;
230 }
231}
232
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800233RegTypeCache::~RegTypeCache() {
234 CHECK_LE(primitive_count_, entries_.size());
235 // Delete only the non primitive types.
Ian Rogers62d6c772013-02-27 08:32:07 -0800236 if (entries_.size() == kNumPrimitives) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800237 // All entries are primitive, nothing to delete.
238 return;
239 }
240 std::vector<RegType*>::iterator non_primitive_begin = entries_.begin();
Ian Rogers62d6c772013-02-27 08:32:07 -0800241 std::advance(non_primitive_begin, kNumPrimitives);
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800242 STLDeleteContainerPointers(non_primitive_begin, entries_.end());
243}
244
245void RegTypeCache::ShutDown() {
246 if (RegTypeCache::primitive_initialized_) {
247 UndefinedType::Destroy();
248 ConflictType::Destroy();
249 BooleanType::Destroy();
250 ByteType::Destroy();
251 ShortType::Destroy();
252 CharType::Destroy();
253 IntegerType::Destroy();
254 LongLoType::Destroy();
255 LongHiType::Destroy();
Ian Rogers25ae7eb2013-04-18 16:06:05 -0700256 FloatType::Destroy();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800257 DoubleLoType::Destroy();
258 DoubleHiType::Destroy();
Ian Rogers62d6c772013-02-27 08:32:07 -0800259 RegTypeCache::primitive_initialized_ = false;
260 RegTypeCache::primitive_count_ = 0;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800261 }
262}
263
264void RegTypeCache::CreatePrimitiveTypes() {
Ian Rogers33e95662013-05-20 20:29:14 -0700265 CreatePrimitiveTypeInstance<UndefinedType>("");
266 CreatePrimitiveTypeInstance<ConflictType>("");
267 CreatePrimitiveTypeInstance<BooleanType>("Z");
268 CreatePrimitiveTypeInstance<ByteType>("B");
269 CreatePrimitiveTypeInstance<ShortType>("S");
270 CreatePrimitiveTypeInstance<CharType>("C");
271 CreatePrimitiveTypeInstance<IntegerType>("I");
272 CreatePrimitiveTypeInstance<LongLoType>("J");
273 CreatePrimitiveTypeInstance<LongHiType>("J");
274 CreatePrimitiveTypeInstance<FloatType>("F");
275 CreatePrimitiveTypeInstance<DoubleLoType>("D");
276 CreatePrimitiveTypeInstance<DoubleHiType>("D");
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800277}
278
Ian Rogers529781d2012-07-23 17:24:29 -0700279const RegType& RegTypeCache::FromUnresolvedMerge(const RegType& left, const RegType& right) {
280 std::set<uint16_t> types;
281 if (left.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800282 RegType& non_const(const_cast<RegType&>(left));
283 types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700284 } else {
285 types.insert(left.GetId());
286 }
287 if (right.IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800288 RegType& non_const(const_cast<RegType&>(right));
289 std::set<uint16_t> right_types = (down_cast<UnresolvedMergedType*>(&non_const))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700290 types.insert(right_types.begin(), right_types.end());
291 } else {
292 types.insert(right.GetId());
293 }
294 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800295 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers529781d2012-07-23 17:24:29 -0700296 RegType* cur_entry = entries_[i];
297 if (cur_entry->IsUnresolvedMergedReference()) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800298 std::set<uint16_t> cur_entry_types =
299 (down_cast<UnresolvedMergedType*>(cur_entry))->GetMergedTypes();
Ian Rogers529781d2012-07-23 17:24:29 -0700300 if (cur_entry_types == types) {
301 return *cur_entry;
302 }
303 }
304 }
305 // Create entry.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800306 RegType* entry = new UnresolvedMergedType(left.GetId(), right.GetId(), this, entries_.size());
Ian Rogers529781d2012-07-23 17:24:29 -0700307 entries_.push_back(entry);
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700308 if (kIsDebugBuild) {
309 UnresolvedMergedType* tmp_entry = down_cast<UnresolvedMergedType*>(entry);
310 std::set<uint16_t> check_types = tmp_entry->GetMergedTypes();
311 CHECK(check_types == types);
312 }
Ian Rogers529781d2012-07-23 17:24:29 -0700313 return *entry;
314}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700315
Ian Rogers529781d2012-07-23 17:24:29 -0700316const RegType& RegTypeCache::FromUnresolvedSuperClass(const RegType& child) {
317 // Check if entry already exists.
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800318 for (size_t i = primitive_count_; i < entries_.size(); i++) {
319 RegType* cur_entry = entries_[i];
320 if (cur_entry->IsUnresolvedSuperClass()) {
321 UnresolvedSuperClass* tmp_entry =
322 down_cast<UnresolvedSuperClass*>(cur_entry);
323 uint16_t unresolved_super_child_id =
324 tmp_entry->GetUnresolvedSuperClassChildId();
325 if (unresolved_super_child_id == child.GetId()) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700326 return *cur_entry;
327 }
328 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800329 }
330 RegType* entry = new UnresolvedSuperClass(child.GetId(), this, entries_.size());
331 entries_.push_back(entry);
332 return *entry;
333}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700334
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800335const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
336 RegType* entry = NULL;
337 RegType* cur_entry = NULL;
338 if (type.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700339 const std::string& descriptor(type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800340 for (size_t i = primitive_count_; i < entries_.size(); i++) {
341 cur_entry = entries_[i];
342 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
343 down_cast<UnresolvedUninitializedRefType*>(cur_entry)->GetAllocationPc() == allocation_pc &&
344 (cur_entry->GetDescriptor() == descriptor)) {
345 return *cur_entry;
346 }
347 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700348 entry = new UnresolvedUninitializedRefType(descriptor.c_str(), allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700349 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800350 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800351 for (size_t i = primitive_count_; i < entries_.size(); i++) {
352 cur_entry = entries_[i];
Ian Rogers776ac1f2012-04-13 23:36:36 -0700353 if (cur_entry->IsUninitializedReference() &&
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700354 down_cast<UninitializedReferenceType*>(cur_entry)
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800355 ->GetAllocationPc() == allocation_pc &&
Ian Rogers776ac1f2012-04-13 23:36:36 -0700356 cur_entry->GetClass() == klass) {
357 return *cur_entry;
358 }
359 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700360 entry = new UninitializedReferenceType(klass, "", allocation_pc, entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700361 }
362 entries_.push_back(entry);
363 return *entry;
364}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700365
Ian Rogers776ac1f2012-04-13 23:36:36 -0700366const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
367 RegType* entry;
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700368
Ian Rogers776ac1f2012-04-13 23:36:36 -0700369 if (uninit_type.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700370 const std::string& descriptor(uninit_type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800371 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700372 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800373 if (cur_entry->IsUnresolvedReference() &&
374 cur_entry->GetDescriptor() == descriptor) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700375 return *cur_entry;
376 }
377 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700378 entry = new UnresolvedReferenceType(descriptor.c_str(), entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700379 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380 mirror::Class* klass = uninit_type.GetClass();
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700381 if(uninit_type.IsUninitializedThisReference() && !klass->IsFinal()) {
382 // For uninitialized this reference look for reference types that are not precise.
383 for (size_t i = primitive_count_; i < entries_.size(); i++) {
384 RegType* cur_entry = entries_[i];
385 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
386 return *cur_entry;
387 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700388 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700389 entry = new ReferenceType(klass, "", entries_.size());
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700390 } else {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700391 if (klass->IsFinal()) {
392 if (klass->IsInstantiable()) {
393 for (size_t i = primitive_count_; i < entries_.size(); i++) {
394 RegType* cur_entry = entries_[i];
395 if (cur_entry->IsPreciseReference() && cur_entry->GetClass() == klass) {
396 return *cur_entry;
397 }
398 }
399 // Precise type was not found , create one !
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700400 entry = new PreciseReferenceType(klass, "", entries_.size());
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700401 } else {
402 return Conflict();
403 }
404 } else {
405 // Not a final class, create an imprecise reference. Look up if we have it in the cache first.
406 for (size_t i = primitive_count_; i < entries_.size(); i++) {
407 RegType* cur_entry = entries_[i];
408 if (cur_entry->IsReference() && !(cur_entry->IsPrecise()) &&
409 cur_entry->GetClass() == klass) {
410 return *cur_entry;
411 }
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700412 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700413 entry = new ReferenceType(klass, "", entries_.size());
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700414 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700415 }
Sameer Abu Asal9f57a862013-04-19 10:08:00 -0700416 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700417 entries_.push_back(entry);
418 return *entry;
419}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700420
421const RegType& RegTypeCache::ByteConstant() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800422 return FromCat1Const(std::numeric_limits<jbyte>::min(), false);
423}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700424
425const RegType& RegTypeCache::ShortConstant() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800426 return FromCat1Const(std::numeric_limits<jshort>::min(), false);
427}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700428
429const RegType& RegTypeCache::IntConstant() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800430 return FromCat1Const(std::numeric_limits<jint>::max(), false);
431}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700432
Ian Rogersad0b3a32012-04-16 14:50:24 -0700433const RegType& RegTypeCache::UninitializedThisArgument(const RegType& type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700434 RegType* entry;
435 if (type.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700436 const std::string& descriptor(type.GetDescriptor());
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800437 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700438 RegType* cur_entry = entries_[i];
439 if (cur_entry->IsUnresolvedAndUninitializedThisReference() &&
440 cur_entry->GetDescriptor() == descriptor) {
441 return *cur_entry;
442 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700443 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700444 entry = new UnresolvedUninitializedThisRefType(descriptor, entries_.size());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700445 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800446 mirror::Class* klass = type.GetClass();
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800447 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700448 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800449 if (cur_entry->IsUninitializedThisReference() &&
450 cur_entry->GetClass() == klass) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700451 return *cur_entry;
452 }
453 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700454 entry = new UninitializedThisReferenceType(klass, "", entries_.size());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700455 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700456 entries_.push_back(entry);
457 return *entry;
458}
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700459
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800460const RegType& RegTypeCache::FromCat1Const(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800461 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700462 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800463 if (cur_entry->IsConstant() && cur_entry->IsPreciseConstant() == precise &&
464 (down_cast<ConstantType*>(cur_entry))->ConstantValue() == value) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700465 return *cur_entry;
466 }
467 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800468 RegType* entry;
469 if (precise) {
470 entry = new PreciseConstType(value, entries_.size());
471 } else {
472 entry = new ImpreciseConstType(value, entries_.size());
473 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800474 entries_.push_back(entry);
475 return *entry;
476}
477
478const RegType& RegTypeCache::FromCat2ConstLo(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800479 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800480 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800481 if (cur_entry->IsConstantLo() && (cur_entry->IsPrecise() == precise) &&
482 (down_cast<ConstantType*>(cur_entry))->ConstantValueLo() == value) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800483 return *cur_entry;
484 }
485 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800486 RegType* entry;
487 if (precise) {
488 entry = new PreciseConstLoType(value, entries_.size());
489 } else {
490 entry = new ImpreciseConstLoType(value, entries_.size());
491 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800492 entries_.push_back(entry);
493 return *entry;
494}
495
496const RegType& RegTypeCache::FromCat2ConstHi(int32_t value, bool precise) {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800497 for (size_t i = primitive_count_; i < entries_.size(); i++) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800498 RegType* cur_entry = entries_[i];
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800499 if (cur_entry->IsConstantHi() && (cur_entry->IsPrecise() == precise) &&
500 (down_cast<ConstantType*>(cur_entry))->ConstantValueHi() == value) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800501 return *cur_entry;
502 }
503 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800504 RegType* entry;
505 if (precise) {
506 entry = new PreciseConstHiType(value, entries_.size());
507 } else {
508 entry = new ImpreciseConstHiType(value, entries_.size());
509 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700510 entries_.push_back(entry);
511 return *entry;
512}
513
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800514const RegType& RegTypeCache::GetComponentType(const RegType& array, mirror::ClassLoader* loader) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700515 CHECK(array.IsArrayTypes());
516 if (array.IsUnresolvedTypes()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700517 const std::string& descriptor(array.GetDescriptor());
518 const std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogersb4903572012-10-11 11:52:56 -0700519 return FromDescriptor(loader, component.c_str(), false);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700520 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800521 mirror::Class* klass = array.GetClass()->GetComponentType();
Ian Rogersb4903572012-10-11 11:52:56 -0700522 return FromClass(klass, klass->IsFinal());
523 }
524}
525
526void RegTypeCache::Dump(std::ostream& os) {
Ian Rogersb4903572012-10-11 11:52:56 -0700527 for (size_t i = 0; i < entries_.size(); i++) {
528 RegType* cur_entry = entries_[i];
529 if (cur_entry != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800530 os << i << ": " << cur_entry->Dump() << "\n";
Ian Rogersb4903572012-10-11 11:52:56 -0700531 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700532 }
533}
534
Elliott Hughesa21039c2012-06-21 12:09:25 -0700535} // namespace verifier
536} // namespace art