1 /* 2 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "ci/ciCallSite.hpp" 27 #include "ci/ciInstance.hpp" 28 #include "ci/ciInstanceKlass.hpp" 29 #include "ci/ciValueKlass.hpp" 30 #include "ci/ciMemberName.hpp" 31 #include "ci/ciMethod.hpp" 32 #include "ci/ciMethodData.hpp" 33 #include "ci/ciMethodHandle.hpp" 34 #include "ci/ciMethodType.hpp" 35 #include "ci/ciNullObject.hpp" 36 #include "ci/ciObjArray.hpp" 37 #include "ci/ciObjArrayKlass.hpp" 38 #include "ci/ciObject.hpp" 39 #include "ci/ciObjectFactory.hpp" 40 #include "ci/ciSymbol.hpp" 41 #include "ci/ciTypeArray.hpp" 42 #include "ci/ciTypeArrayKlass.hpp" 43 #include "ci/ciUtilities.hpp" 44 #include "classfile/javaClasses.inline.hpp" 45 #include "classfile/systemDictionary.hpp" 46 #include "gc/shared/collectedHeap.inline.hpp" 47 #include "memory/allocation.inline.hpp" 48 #include "oops/oop.inline.hpp" 49 #include "runtime/fieldType.hpp" 50 #include "utilities/macros.hpp" 51 #if INCLUDE_ALL_GCS 52 # include "gc/g1/g1SATBCardTableModRefBS.hpp" 53 #endif 54 55 // ciObjectFactory 56 // 57 // This class handles requests for the creation of new instances 58 // of ciObject and its subclasses. It contains a caching mechanism 59 // which ensures that for each oop, at most one ciObject is created. 60 // This invariant allows more efficient implementation of ciObject. 61 // 62 // Implementation note: the oop->ciObject mapping is represented as 63 // a table stored in an array. Even though objects are moved 64 // by the garbage collector, the compactor preserves their relative 65 // order; address comparison of oops (in perm space) is safe so long 66 // as we prohibit GC during our comparisons. We currently use binary 67 // search to find the oop in the table, and inserting a new oop 68 // into the table may be costly. If this cost ends up being 69 // problematic the underlying data structure can be switched to some 70 // sort of balanced binary tree. 71 72 GrowableArray<ciMetadata*>* ciObjectFactory::_shared_ci_metadata = NULL; 73 ciSymbol* ciObjectFactory::_shared_ci_symbols[vmSymbols::SID_LIMIT]; 74 int ciObjectFactory::_shared_ident_limit = 0; 75 volatile bool ciObjectFactory::_initialized = false; 76 77 78 // ------------------------------------------------------------------ 79 // ciObjectFactory::ciObjectFactory 80 ciObjectFactory::ciObjectFactory(Arena* arena, 81 int expected_size) { 82 83 for (int i = 0; i < NON_PERM_BUCKETS; i++) { 84 _non_perm_bucket[i] = NULL; 85 } 86 _non_perm_count = 0; 87 88 _next_ident = _shared_ident_limit; 89 _arena = arena; 90 _ci_metadata = new (arena) GrowableArray<ciMetadata*>(arena, expected_size, 0, NULL); 91 92 // If the shared ci objects exist append them to this factory's objects 93 94 if (_shared_ci_metadata != NULL) { 95 _ci_metadata->appendAll(_shared_ci_metadata); 96 } 97 98 _unloaded_methods = new (arena) GrowableArray<ciMethod*>(arena, 4, 0, NULL); 99 _unloaded_klasses = new (arena) GrowableArray<ciKlass*>(arena, 8, 0, NULL); 100 _unloaded_instances = new (arena) GrowableArray<ciInstance*>(arena, 4, 0, NULL); 101 _return_addresses = 102 new (arena) GrowableArray<ciReturnAddress*>(arena, 8, 0, NULL); 103 104 _symbols = new (arena) GrowableArray<ciSymbol*>(arena, 100, 0, NULL); 105 } 106 107 // ------------------------------------------------------------------ 108 // ciObjectFactory::ciObjectFactory 109 void ciObjectFactory::initialize() { 110 ASSERT_IN_VM; 111 JavaThread* thread = JavaThread::current(); 112 HandleMark handle_mark(thread); 113 114 // This Arena is long lived and exists in the resource mark of the 115 // compiler thread that initializes the initial ciObjectFactory which 116 // creates the shared ciObjects that all later ciObjectFactories use. 117 Arena* arena = new (mtCompiler) Arena(mtCompiler); 118 ciEnv initial(arena); 119 ciEnv* env = ciEnv::current(); 120 env->_factory->init_shared_objects(); 121 122 _initialized = true; 123 124 } 125 126 void ciObjectFactory::init_shared_objects() { 127 128 _next_ident = 1; // start numbering CI objects at 1 129 130 { 131 // Create the shared symbols, but not in _shared_ci_metadata. 132 int i; 133 for (i = vmSymbols::FIRST_SID; i < vmSymbols::SID_LIMIT; i++) { 134 Symbol* vmsym = vmSymbols::symbol_at((vmSymbols::SID) i); 135 assert(vmSymbols::find_sid(vmsym) == i, "1-1 mapping"); 136 ciSymbol* sym = new (_arena) ciSymbol(vmsym, (vmSymbols::SID) i); 137 init_ident_of(sym); 138 _shared_ci_symbols[i] = sym; 139 } 140 #ifdef ASSERT 141 for (i = vmSymbols::FIRST_SID; i < vmSymbols::SID_LIMIT; i++) { 142 Symbol* vmsym = vmSymbols::symbol_at((vmSymbols::SID) i); 143 ciSymbol* sym = vm_symbol_at((vmSymbols::SID) i); 144 assert(sym->get_symbol() == vmsym, "oop must match"); 145 } 146 assert(ciSymbol::void_class_signature()->get_symbol() == vmSymbols::void_class_signature(), "spot check"); 147 #endif 148 } 149 150 _ci_metadata = new (_arena) GrowableArray<ciMetadata*>(_arena, 64, 0, NULL); 151 152 for (int i = T_BOOLEAN; i <= T_CONFLICT; i++) { 153 BasicType t = (BasicType)i; 154 if (type2name(t) != NULL && t != T_OBJECT && t != T_ARRAY && 155 t != T_VALUETYPE && t != T_NARROWOOP && t != T_NARROWKLASS) { 156 ciType::_basic_types[t] = new (_arena) ciType(t); 157 init_ident_of(ciType::_basic_types[t]); 158 } 159 } 160 161 ciEnv::_null_object_instance = new (_arena) ciNullObject(); 162 init_ident_of(ciEnv::_null_object_instance); 163 164 #define WK_KLASS_DEFN(name, ignore_s, opt) \ 165 if (SystemDictionary::name() != NULL) \ 166 ciEnv::_##name = get_metadata(SystemDictionary::name())->as_instance_klass(); 167 168 WK_KLASSES_DO(WK_KLASS_DEFN) 169 #undef WK_KLASS_DEFN 170 171 for (int len = -1; len != _ci_metadata->length(); ) { 172 len = _ci_metadata->length(); 173 for (int i2 = 0; i2 < len; i2++) { 174 ciMetadata* obj = _ci_metadata->at(i2); 175 assert (obj->is_metadata(), "what else would it be?"); 176 if (obj->is_loaded() && obj->is_instance_klass()) { 177 obj->as_instance_klass()->compute_nonstatic_fields(); 178 } 179 } 180 } 181 182 ciEnv::_unloaded_cisymbol = ciObjectFactory::get_symbol(vmSymbols::dummy_symbol()); 183 // Create dummy InstanceKlass and ObjArrayKlass object and assign them idents 184 ciEnv::_unloaded_ciinstance_klass = new (_arena) ciInstanceKlass(ciEnv::_unloaded_cisymbol, NULL, NULL); 185 init_ident_of(ciEnv::_unloaded_ciinstance_klass); 186 ciEnv::_unloaded_ciobjarrayklass = new (_arena) ciObjArrayKlass(ciEnv::_unloaded_cisymbol, ciEnv::_unloaded_ciinstance_klass, 1); 187 init_ident_of(ciEnv::_unloaded_ciobjarrayklass); 188 assert(ciEnv::_unloaded_ciobjarrayklass->is_obj_array_klass(), "just checking"); 189 190 get_metadata(Universe::boolArrayKlassObj()); 191 get_metadata(Universe::charArrayKlassObj()); 192 get_metadata(Universe::singleArrayKlassObj()); 193 get_metadata(Universe::doubleArrayKlassObj()); 194 get_metadata(Universe::byteArrayKlassObj()); 195 get_metadata(Universe::shortArrayKlassObj()); 196 get_metadata(Universe::intArrayKlassObj()); 197 get_metadata(Universe::longArrayKlassObj()); 198 199 200 201 assert(_non_perm_count == 0, "no shared non-perm objects"); 202 203 // The shared_ident_limit is the first ident number that will 204 // be used for non-shared objects. That is, numbers less than 205 // this limit are permanently assigned to shared CI objects, 206 // while the higher numbers are recycled afresh by each new ciEnv. 207 208 _shared_ident_limit = _next_ident; 209 _shared_ci_metadata = _ci_metadata; 210 } 211 212 213 ciSymbol* ciObjectFactory::get_symbol(Symbol* key) { 214 vmSymbols::SID sid = vmSymbols::find_sid(key); 215 if (sid != vmSymbols::NO_SID) { 216 // do not pollute the main cache with it 217 return vm_symbol_at(sid); 218 } 219 220 assert(vmSymbols::find_sid(key) == vmSymbols::NO_SID, ""); 221 ciSymbol* s = new (arena()) ciSymbol(key, vmSymbols::NO_SID); 222 _symbols->push(s); 223 return s; 224 } 225 226 // Decrement the refcount when done on symbols referenced by this compilation. 227 void ciObjectFactory::remove_symbols() { 228 for (int i = 0; i < _symbols->length(); i++) { 229 ciSymbol* s = _symbols->at(i); 230 s->get_symbol()->decrement_refcount(); 231 } 232 // Since _symbols is resource allocated we're not allowed to delete it 233 // but it'll go away just the same. 234 } 235 236 // ------------------------------------------------------------------ 237 // ciObjectFactory::get 238 // 239 // Get the ciObject corresponding to some oop. If the ciObject has 240 // already been created, it is returned. Otherwise, a new ciObject 241 // is created. 242 ciObject* ciObjectFactory::get(oop key) { 243 ASSERT_IN_VM; 244 245 assert(Universe::heap()->is_in_reserved(key), "must be"); 246 247 NonPermObject* &bucket = find_non_perm(key); 248 if (bucket != NULL) { 249 return bucket->object(); 250 } 251 252 // The ciObject does not yet exist. Create it and insert it 253 // into the cache. 254 Handle keyHandle(key); 255 ciObject* new_object = create_new_object(keyHandle()); 256 assert(keyHandle() == new_object->get_oop(), "must be properly recorded"); 257 init_ident_of(new_object); 258 assert(Universe::heap()->is_in_reserved(new_object->get_oop()), "must be"); 259 260 // Not a perm-space object. 261 insert_non_perm(bucket, keyHandle(), new_object); 262 return new_object; 263 } 264 265 int ciObjectFactory::metadata_compare(Metadata* const& key, ciMetadata* const& elt) { 266 Metadata* value = elt->constant_encoding(); 267 if (key < value) return -1; 268 else if (key > value) return 1; 269 else return 0; 270 } 271 272 // ------------------------------------------------------------------ 273 // ciObjectFactory::get_metadata 274 // 275 // Get the ciMetadata corresponding to some Metadata. If the ciMetadata has 276 // already been created, it is returned. Otherwise, a new ciMetadata 277 // is created. 278 ciMetadata* ciObjectFactory::get_metadata(Metadata* key) { 279 ASSERT_IN_VM; 280 281 #ifdef ASSERT 282 if (CIObjectFactoryVerify) { 283 Metadata* last = NULL; 284 for (int j = 0; j< _ci_metadata->length(); j++) { 285 Metadata* o = _ci_metadata->at(j)->constant_encoding(); 286 assert(last < o, "out of order"); 287 last = o; 288 } 289 } 290 #endif // ASSERT 291 int len = _ci_metadata->length(); 292 bool found = false; 293 int index = _ci_metadata->find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found); 294 #ifdef ASSERT 295 if (CIObjectFactoryVerify) { 296 for (int i=0; i<_ci_metadata->length(); i++) { 297 if (_ci_metadata->at(i)->constant_encoding() == key) { 298 assert(index == i, " bad lookup"); 299 } 300 } 301 } 302 #endif 303 304 if (!found) { 305 // The ciMetadata does not yet exist. Create it and insert it 306 // into the cache. 307 ciMetadata* new_object = create_new_metadata(key); 308 init_ident_of(new_object); 309 assert(new_object->is_metadata(), "must be"); 310 311 if (len != _ci_metadata->length()) { 312 // creating the new object has recursively entered new objects 313 // into the table. We need to recompute our index. 314 index = _ci_metadata->find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found); 315 } 316 assert(!found, "no double insert"); 317 _ci_metadata->insert_before(index, new_object); 318 return new_object; 319 } 320 return _ci_metadata->at(index)->as_metadata(); 321 } 322 323 // ------------------------------------------------------------------ 324 // ciObjectFactory::create_new_object 325 // 326 // Create a new ciObject from an oop. 327 // 328 // Implementation note: this functionality could be virtual behavior 329 // of the oop itself. For now, we explicitly marshal the object. 330 ciObject* ciObjectFactory::create_new_object(oop o) { 331 EXCEPTION_CONTEXT; 332 333 if (o->is_instance()) { 334 instanceHandle h_i(THREAD, (instanceOop)o); 335 if (java_lang_invoke_CallSite::is_instance(o)) 336 return new (arena()) ciCallSite(h_i); 337 else if (java_lang_invoke_MemberName::is_instance(o)) 338 return new (arena()) ciMemberName(h_i); 339 else if (java_lang_invoke_MethodHandle::is_instance(o)) 340 return new (arena()) ciMethodHandle(h_i); 341 else if (java_lang_invoke_MethodType::is_instance(o)) 342 return new (arena()) ciMethodType(h_i); 343 else 344 return new (arena()) ciInstance(h_i); 345 } else if (o->is_objArray()) { 346 objArrayHandle h_oa(THREAD, (objArrayOop)o); 347 return new (arena()) ciObjArray(h_oa); 348 } else if (o->is_typeArray()) { 349 typeArrayHandle h_ta(THREAD, (typeArrayOop)o); 350 return new (arena()) ciTypeArray(h_ta); 351 } 352 353 // The oop is of some type not supported by the compiler interface. 354 ShouldNotReachHere(); 355 return NULL; 356 } 357 358 // ------------------------------------------------------------------ 359 // ciObjectFactory::create_new_metadata 360 // 361 // Create a new ciMetadata from a Metadata*. 362 // 363 // Implementation note: in order to keep Metadata live, an auxiliary ciObject 364 // is used, which points to it's holder. 365 ciMetadata* ciObjectFactory::create_new_metadata(Metadata* o) { 366 EXCEPTION_CONTEXT; 367 368 // Hold metadata from unloading by keeping it's holder alive. 369 if (_initialized && o->is_klass()) { 370 Klass* holder = ((Klass*)o); 371 if (holder->is_instance_klass() && InstanceKlass::cast(holder)->is_anonymous()) { 372 // Though ciInstanceKlass records class loader oop, it's not enough to keep 373 // VM anonymous classes alive (loader == NULL). Klass holder should be used instead. 374 // It is enough to record a ciObject, since cached elements are never removed 375 // during ciObjectFactory lifetime. ciObjectFactory itself is created for 376 // every compilation and lives for the whole duration of the compilation. 377 ciObject* h = get(holder->klass_holder()); 378 } 379 } 380 381 if (o->is_klass()) { 382 KlassHandle h_k(THREAD, (Klass*)o); 383 Klass* k = (Klass*)o; 384 if (k->is_value()) { 385 return new (arena()) ciValueKlass(h_k); 386 } else if (k->is_instance_klass()) { 387 return new (arena()) ciInstanceKlass(h_k); 388 } else if (k->is_valueArray_klass()) { 389 return new (arena()) ciValueArrayKlass(h_k); 390 } else if (k->is_objArray_klass()) { 391 return new (arena()) ciObjArrayKlass(h_k); 392 } else if (k->is_typeArray_klass()) { 393 return new (arena()) ciTypeArrayKlass(h_k); 394 } 395 } else if (o->is_method()) { 396 methodHandle h_m(THREAD, (Method*)o); 397 ciEnv *env = CURRENT_THREAD_ENV; 398 ciInstanceKlass* holder = env->get_instance_klass(h_m()->method_holder()); 399 return new (arena()) ciMethod(h_m, holder); 400 } else if (o->is_methodData()) { 401 // Hold methodHandle alive - might not be necessary ??? 402 methodHandle h_m(THREAD, ((MethodData*)o)->method()); 403 return new (arena()) ciMethodData((MethodData*)o); 404 } 405 406 // The Metadata* is of some type not supported by the compiler interface. 407 ShouldNotReachHere(); 408 return NULL; 409 } 410 411 // ------------------------------------------------------------------ 412 // ciObjectFactory::ensure_metadata_alive 413 // 414 // Ensure that the metadata wrapped by the ciMetadata is kept alive by GC. 415 // This is primarily useful for metadata which is considered as weak roots 416 // by the GC but need to be strong roots if reachable from a current compilation. 417 // 418 void ciObjectFactory::ensure_metadata_alive(ciMetadata* m) { 419 ASSERT_IN_VM; // We're handling raw oops here. 420 421 #if INCLUDE_ALL_GCS 422 if (!UseG1GC) { 423 return; 424 } 425 Klass* metadata_owner_klass; 426 if (m->is_klass()) { 427 metadata_owner_klass = m->as_klass()->get_Klass(); 428 } else if (m->is_method()) { 429 metadata_owner_klass = m->as_method()->get_Method()->constants()->pool_holder(); 430 } else { 431 fatal("Not implemented for other types of metadata"); 432 return; 433 } 434 435 oop metadata_holder = metadata_owner_klass->klass_holder(); 436 if (metadata_holder != NULL) { 437 G1SATBCardTableModRefBS::enqueue(metadata_holder); 438 } 439 440 #endif 441 } 442 443 //------------------------------------------------------------------ 444 // ciObjectFactory::get_unloaded_method 445 // 446 // Get the ciMethod representing an unloaded/unfound method. 447 // 448 // Implementation note: unloaded methods are currently stored in 449 // an unordered array, requiring a linear-time lookup for each 450 // unloaded method. This may need to change. 451 ciMethod* ciObjectFactory::get_unloaded_method(ciInstanceKlass* holder, 452 ciSymbol* name, 453 ciSymbol* signature, 454 ciInstanceKlass* accessor) { 455 ciSignature* that = NULL; 456 for (int i = 0; i < _unloaded_methods->length(); i++) { 457 ciMethod* entry = _unloaded_methods->at(i); 458 if (entry->holder()->equals(holder) && 459 entry->name()->equals(name) && 460 entry->signature()->as_symbol()->equals(signature)) { 461 // Short-circuit slow resolve. 462 if (entry->signature()->accessing_klass() == accessor) { 463 // We've found a match. 464 return entry; 465 } else { 466 // Lazily create ciSignature 467 if (that == NULL) that = new (arena()) ciSignature(accessor, constantPoolHandle(), signature); 468 if (entry->signature()->equals(that)) { 469 // We've found a match. 470 return entry; 471 } 472 } 473 } 474 } 475 476 // This is a new unloaded method. Create it and stick it in 477 // the cache. 478 ciMethod* new_method = new (arena()) ciMethod(holder, name, signature, accessor); 479 480 init_ident_of(new_method); 481 _unloaded_methods->append(new_method); 482 483 return new_method; 484 } 485 486 //------------------------------------------------------------------ 487 // ciObjectFactory::get_unloaded_klass 488 // 489 // Get a ciKlass representing an unloaded klass. 490 // 491 // Implementation note: unloaded klasses are currently stored in 492 // an unordered array, requiring a linear-time lookup for each 493 // unloaded klass. This may need to change. 494 ciKlass* ciObjectFactory::get_unloaded_klass(ciKlass* accessing_klass, 495 ciSymbol* name, 496 bool create_if_not_found) { 497 EXCEPTION_CONTEXT; 498 oop loader = NULL; 499 oop domain = NULL; 500 if (accessing_klass != NULL) { 501 loader = accessing_klass->loader(); 502 domain = accessing_klass->protection_domain(); 503 } 504 for (int i=0; i<_unloaded_klasses->length(); i++) { 505 ciKlass* entry = _unloaded_klasses->at(i); 506 if (entry->name()->equals(name) && 507 entry->loader() == loader && 508 entry->protection_domain() == domain) { 509 // We've found a match. 510 return entry; 511 } 512 } 513 514 if (!create_if_not_found) 515 return NULL; 516 517 // This is a new unloaded klass. Create it and stick it in 518 // the cache. 519 ciKlass* new_klass = NULL; 520 521 // Two cases: this is an unloaded ObjArrayKlass or an 522 // unloaded InstanceKlass. Deal with both. 523 if (name->byte_at(0) == '[') { 524 // Decompose the name.' 525 FieldArrayInfo fd; 526 BasicType element_type = FieldType::get_array_info(name->get_symbol(), 527 fd, THREAD); 528 if (HAS_PENDING_EXCEPTION) { 529 CLEAR_PENDING_EXCEPTION; 530 CURRENT_THREAD_ENV->record_out_of_memory_failure(); 531 return ciEnv::_unloaded_ciobjarrayklass; 532 } 533 int dimension = fd.dimension(); 534 assert(element_type != T_ARRAY, "unsuccessful decomposition"); 535 ciKlass* element_klass = NULL; 536 if (element_type == T_OBJECT || element_type == T_VALUETYPE) { 537 ciEnv *env = CURRENT_THREAD_ENV; 538 ciSymbol* ci_name = env->get_symbol(fd.object_key()); 539 element_klass = 540 env->get_klass_by_name(accessing_klass, ci_name, false)->as_instance_klass(); 541 } else { 542 assert(dimension > 1, "one dimensional type arrays are always loaded."); 543 544 // The type array itself takes care of one of the dimensions. 545 dimension--; 546 547 // The element klass is a TypeArrayKlass. 548 element_klass = ciTypeArrayKlass::make(element_type); 549 } 550 new_klass = new (arena()) ciObjArrayKlass(name, element_klass, dimension); 551 } else { 552 jobject loader_handle = NULL; 553 jobject domain_handle = NULL; 554 if (accessing_klass != NULL) { 555 loader_handle = accessing_klass->loader_handle(); 556 domain_handle = accessing_klass->protection_domain_handle(); 557 } 558 new_klass = new (arena()) ciInstanceKlass(name, loader_handle, domain_handle); 559 } 560 init_ident_of(new_klass); 561 _unloaded_klasses->append(new_klass); 562 563 return new_klass; 564 } 565 566 567 //------------------------------------------------------------------ 568 // ciObjectFactory::get_unloaded_instance 569 // 570 // Get a ciInstance representing an as-yet undetermined instance of a given class. 571 // 572 ciInstance* ciObjectFactory::get_unloaded_instance(ciInstanceKlass* instance_klass) { 573 for (int i=0; i<_unloaded_instances->length(); i++) { 574 ciInstance* entry = _unloaded_instances->at(i); 575 if (entry->klass()->equals(instance_klass)) { 576 // We've found a match. 577 return entry; 578 } 579 } 580 581 // This is a new unloaded instance. Create it and stick it in 582 // the cache. 583 ciInstance* new_instance = new (arena()) ciInstance(instance_klass); 584 585 init_ident_of(new_instance); 586 _unloaded_instances->append(new_instance); 587 588 // make sure it looks the way we want: 589 assert(!new_instance->is_loaded(), ""); 590 assert(new_instance->klass() == instance_klass, ""); 591 592 return new_instance; 593 } 594 595 596 //------------------------------------------------------------------ 597 // ciObjectFactory::get_unloaded_klass_mirror 598 // 599 // Get a ciInstance representing an unresolved klass mirror. 600 // 601 // Currently, this ignores the parameters and returns a unique unloaded instance. 602 ciInstance* ciObjectFactory::get_unloaded_klass_mirror(ciKlass* type) { 603 assert(ciEnv::_Class_klass != NULL, ""); 604 return get_unloaded_instance(ciEnv::_Class_klass->as_instance_klass()); 605 } 606 607 //------------------------------------------------------------------ 608 // ciObjectFactory::get_unloaded_method_handle_constant 609 // 610 // Get a ciInstance representing an unresolved method handle constant. 611 // 612 // Currently, this ignores the parameters and returns a unique unloaded instance. 613 ciInstance* ciObjectFactory::get_unloaded_method_handle_constant(ciKlass* holder, 614 ciSymbol* name, 615 ciSymbol* signature, 616 int ref_kind) { 617 if (ciEnv::_MethodHandle_klass == NULL) return NULL; 618 return get_unloaded_instance(ciEnv::_MethodHandle_klass->as_instance_klass()); 619 } 620 621 //------------------------------------------------------------------ 622 // ciObjectFactory::get_unloaded_method_type_constant 623 // 624 // Get a ciInstance representing an unresolved method type constant. 625 // 626 // Currently, this ignores the parameters and returns a unique unloaded instance. 627 ciInstance* ciObjectFactory::get_unloaded_method_type_constant(ciSymbol* signature) { 628 if (ciEnv::_MethodType_klass == NULL) return NULL; 629 return get_unloaded_instance(ciEnv::_MethodType_klass->as_instance_klass()); 630 } 631 632 ciInstance* ciObjectFactory::get_unloaded_object_constant() { 633 if (ciEnv::_Object_klass == NULL) return NULL; 634 return get_unloaded_instance(ciEnv::_Object_klass->as_instance_klass()); 635 } 636 637 //------------------------------------------------------------------ 638 // ciObjectFactory::get_empty_methodData 639 // 640 // Get the ciMethodData representing the methodData for a method with 641 // none. 642 ciMethodData* ciObjectFactory::get_empty_methodData() { 643 ciMethodData* new_methodData = new (arena()) ciMethodData(); 644 init_ident_of(new_methodData); 645 return new_methodData; 646 } 647 648 //------------------------------------------------------------------ 649 // ciObjectFactory::get_return_address 650 // 651 // Get a ciReturnAddress for a specified bci. 652 ciReturnAddress* ciObjectFactory::get_return_address(int bci) { 653 for (int i=0; i<_return_addresses->length(); i++) { 654 ciReturnAddress* entry = _return_addresses->at(i); 655 if (entry->bci() == bci) { 656 // We've found a match. 657 return entry; 658 } 659 } 660 661 ciReturnAddress* new_ret_addr = new (arena()) ciReturnAddress(bci); 662 init_ident_of(new_ret_addr); 663 _return_addresses->append(new_ret_addr); 664 return new_ret_addr; 665 } 666 667 // ------------------------------------------------------------------ 668 // ciObjectFactory::init_ident_of 669 void ciObjectFactory::init_ident_of(ciBaseObject* obj) { 670 obj->set_ident(_next_ident++); 671 } 672 673 static ciObjectFactory::NonPermObject* emptyBucket = NULL; 674 675 // ------------------------------------------------------------------ 676 // ciObjectFactory::find_non_perm 677 // 678 // Use a small hash table, hashed on the klass of the key. 679 // If there is no entry in the cache corresponding to this oop, return 680 // the null tail of the bucket into which the oop should be inserted. 681 ciObjectFactory::NonPermObject* &ciObjectFactory::find_non_perm(oop key) { 682 assert(Universe::heap()->is_in_reserved(key), "must be"); 683 ciMetadata* klass = get_metadata(key->klass()); 684 NonPermObject* *bp = &_non_perm_bucket[(unsigned) klass->hash() % NON_PERM_BUCKETS]; 685 for (NonPermObject* p; (p = (*bp)) != NULL; bp = &p->next()) { 686 if (is_equal(p, key)) break; 687 } 688 return (*bp); 689 } 690 691 692 693 // ------------------------------------------------------------------ 694 // Code for for NonPermObject 695 // 696 inline ciObjectFactory::NonPermObject::NonPermObject(ciObjectFactory::NonPermObject* &bucket, oop key, ciObject* object) { 697 assert(ciObjectFactory::is_initialized(), ""); 698 _object = object; 699 _next = bucket; 700 bucket = this; 701 } 702 703 704 705 // ------------------------------------------------------------------ 706 // ciObjectFactory::insert_non_perm 707 // 708 // Insert a ciObject into the non-perm table. 709 void ciObjectFactory::insert_non_perm(ciObjectFactory::NonPermObject* &where, oop key, ciObject* obj) { 710 assert(Universe::heap()->is_in_reserved_or_null(key), "must be"); 711 assert(&where != &emptyBucket, "must not try to fill empty bucket"); 712 NonPermObject* p = new (arena()) NonPermObject(where, key, obj); 713 assert(where == p && is_equal(p, key) && p->object() == obj, "entry must match"); 714 assert(find_non_perm(key) == p, "must find the same spot"); 715 ++_non_perm_count; 716 } 717 718 // ------------------------------------------------------------------ 719 // ciObjectFactory::vm_symbol_at 720 // Get the ciSymbol corresponding to some index in vmSymbols. 721 ciSymbol* ciObjectFactory::vm_symbol_at(int index) { 722 assert(index >= vmSymbols::FIRST_SID && index < vmSymbols::SID_LIMIT, "oob"); 723 return _shared_ci_symbols[index]; 724 } 725 726 // ------------------------------------------------------------------ 727 // ciObjectFactory::metadata_do 728 void ciObjectFactory::metadata_do(void f(Metadata*)) { 729 if (_ci_metadata == NULL) return; 730 for (int j = 0; j< _ci_metadata->length(); j++) { 731 Metadata* o = _ci_metadata->at(j)->constant_encoding(); 732 f(o); 733 } 734 } 735 736 // ------------------------------------------------------------------ 737 // ciObjectFactory::print_contents_impl 738 void ciObjectFactory::print_contents_impl() { 739 int len = _ci_metadata->length(); 740 tty->print_cr("ciObjectFactory (%d) meta data contents:", len); 741 for (int i=0; i<len; i++) { 742 _ci_metadata->at(i)->print(); 743 tty->cr(); 744 } 745 } 746 747 // ------------------------------------------------------------------ 748 // ciObjectFactory::print_contents 749 void ciObjectFactory::print_contents() { 750 print(); 751 tty->cr(); 752 GUARDED_VM_ENTRY(print_contents_impl();) 753 } 754 755 // ------------------------------------------------------------------ 756 // ciObjectFactory::print 757 // 758 // Print debugging information about the object factory 759 void ciObjectFactory::print() { 760 tty->print("<ciObjectFactory oops=%d metadata=%d unloaded_methods=%d unloaded_instances=%d unloaded_klasses=%d>", 761 _non_perm_count, _ci_metadata->length(), _unloaded_methods->length(), 762 _unloaded_instances->length(), 763 _unloaded_klasses->length()); 764 }