30 #include "ci/ciMethod.hpp" 31 #include "ci/ciMethodData.hpp" 32 #include "ci/ciMethodHandle.hpp" 33 #include "ci/ciMethodType.hpp" 34 #include "ci/ciNullObject.hpp" 35 #include "ci/ciObjArray.hpp" 36 #include "ci/ciObjArrayKlass.hpp" 37 #include "ci/ciObject.hpp" 38 #include "ci/ciObjectFactory.hpp" 39 #include "ci/ciSymbol.hpp" 40 #include "ci/ciTypeArray.hpp" 41 #include "ci/ciTypeArrayKlass.hpp" 42 #include "ci/ciUtilities.hpp" 43 #include "classfile/javaClasses.inline.hpp" 44 #include "classfile/systemDictionary.hpp" 45 #include "gc/shared/collectedHeap.inline.hpp" 46 #include "memory/allocation.inline.hpp" 47 #include "oops/oop.inline.hpp" 48 #include "runtime/fieldType.hpp" 49 #include "utilities/macros.hpp" 50 #if INCLUDE_ALL_GCS 51 # include "gc/g1/g1SATBCardTableModRefBS.hpp" 52 #endif 53 54 // ciObjectFactory 55 // 56 // This class handles requests for the creation of new instances 57 // of ciObject and its subclasses. It contains a caching mechanism 58 // which ensures that for each oop, at most one ciObject is created. 59 // This invariant allows more efficient implementation of ciObject. 60 // 61 // Implementation note: the oop->ciObject mapping is represented as 62 // a table stored in an array. Even though objects are moved 63 // by the garbage collector, the compactor preserves their relative 64 // order; address comparison of oops (in perm space) is safe so long 65 // as we prohibit GC during our comparisons. We currently use binary 66 // search to find the oop in the table, and inserting a new oop 67 // into the table may be costly. If this cost ends up being 68 // problematic the underlying data structure can be switched to some 69 // sort of balanced binary tree. 70 71 GrowableArray<ciMetadata*>* ciObjectFactory::_shared_ci_metadata = NULL; 72 ciSymbol* ciObjectFactory::_shared_ci_symbols[vmSymbols::SID_LIMIT]; 346 } else if (o->is_typeArray()) { 347 typeArrayHandle h_ta(THREAD, (typeArrayOop)o); 348 return new (arena()) ciTypeArray(h_ta); 349 } 350 351 // The oop is of some type not supported by the compiler interface. 352 ShouldNotReachHere(); 353 return NULL; 354 } 355 356 // ------------------------------------------------------------------ 357 // ciObjectFactory::create_new_metadata 358 // 359 // Create a new ciMetadata from a Metadata*. 360 // 361 // Implementation note: in order to keep Metadata live, an auxiliary ciObject 362 // is used, which points to it's holder. 363 ciMetadata* ciObjectFactory::create_new_metadata(Metadata* o) { 364 EXCEPTION_CONTEXT; 365 366 // Hold metadata from unloading by keeping it's holder alive. 367 if (_initialized && o->is_klass()) { 368 Klass* holder = ((Klass*)o); 369 if (holder->is_instance_klass() && InstanceKlass::cast(holder)->is_anonymous()) { 370 // Though ciInstanceKlass records class loader oop, it's not enough to keep 371 // VM anonymous classes alive (loader == NULL). Klass holder should be used instead. 372 // It is enough to record a ciObject, since cached elements are never removed 373 // during ciObjectFactory lifetime. ciObjectFactory itself is created for 374 // every compilation and lives for the whole duration of the compilation. 375 ciObject* h = get(holder->klass_holder()); 376 } 377 } 378 379 if (o->is_klass()) { 380 Klass* k = (Klass*)o; 381 if (k->is_instance_klass()) { 382 return new (arena()) ciInstanceKlass(k); 383 } else if (k->is_objArray_klass()) { 384 return new (arena()) ciObjArrayKlass(k); 385 } else if (k->is_typeArray_klass()) { 386 return new (arena()) ciTypeArrayKlass(k); 387 } 388 } else if (o->is_method()) { 389 methodHandle h_m(THREAD, (Method*)o); 390 ciEnv *env = CURRENT_THREAD_ENV; 391 ciInstanceKlass* holder = env->get_instance_klass(h_m()->method_holder()); 392 return new (arena()) ciMethod(h_m, holder); 393 } else if (o->is_methodData()) { 394 // Hold methodHandle alive - might not be necessary ??? 395 methodHandle h_m(THREAD, ((MethodData*)o)->method()); 396 return new (arena()) ciMethodData((MethodData*)o); 397 } 398 399 // The Metadata* is of some type not supported by the compiler interface. 400 ShouldNotReachHere(); 401 return NULL; 402 } 403 404 // ------------------------------------------------------------------ 405 // ciObjectFactory::ensure_metadata_alive 406 // 407 // Ensure that the metadata wrapped by the ciMetadata is kept alive by GC. 408 // This is primarily useful for metadata which is considered as weak roots 409 // by the GC but need to be strong roots if reachable from a current compilation. 410 // 411 void ciObjectFactory::ensure_metadata_alive(ciMetadata* m) { 412 ASSERT_IN_VM; // We're handling raw oops here. 413 414 #if INCLUDE_ALL_GCS 415 if (!UseG1GC) { 416 return; 417 } 418 Klass* metadata_owner_klass; 419 if (m->is_klass()) { 420 metadata_owner_klass = m->as_klass()->get_Klass(); 421 } else if (m->is_method()) { 422 metadata_owner_klass = m->as_method()->get_Method()->constants()->pool_holder(); 423 } else { 424 fatal("Not implemented for other types of metadata"); 425 return; 426 } 427 428 oop metadata_holder = metadata_owner_klass->klass_holder(); 429 if (metadata_holder != NULL) { 430 G1SATBCardTableModRefBS::enqueue(metadata_holder); 431 } 432 433 #endif 434 } 435 436 //------------------------------------------------------------------ 437 // ciObjectFactory::get_unloaded_method 438 // 439 // Get the ciMethod representing an unloaded/unfound method. 440 // 441 // Implementation note: unloaded methods are currently stored in 442 // an unordered array, requiring a linear-time lookup for each 443 // unloaded method. This may need to change. 444 ciMethod* ciObjectFactory::get_unloaded_method(ciInstanceKlass* holder, 445 ciSymbol* name, 446 ciSymbol* signature, 447 ciInstanceKlass* accessor) { 448 ciSignature* that = NULL; 449 for (int i = 0; i < _unloaded_methods->length(); i++) { 450 ciMethod* entry = _unloaded_methods->at(i); 451 if (entry->holder()->equals(holder) && 452 entry->name()->equals(name) && 453 entry->signature()->as_symbol()->equals(signature)) { | 30 #include "ci/ciMethod.hpp" 31 #include "ci/ciMethodData.hpp" 32 #include "ci/ciMethodHandle.hpp" 33 #include "ci/ciMethodType.hpp" 34 #include "ci/ciNullObject.hpp" 35 #include "ci/ciObjArray.hpp" 36 #include "ci/ciObjArrayKlass.hpp" 37 #include "ci/ciObject.hpp" 38 #include "ci/ciObjectFactory.hpp" 39 #include "ci/ciSymbol.hpp" 40 #include "ci/ciTypeArray.hpp" 41 #include "ci/ciTypeArrayKlass.hpp" 42 #include "ci/ciUtilities.hpp" 43 #include "classfile/javaClasses.inline.hpp" 44 #include "classfile/systemDictionary.hpp" 45 #include "gc/shared/collectedHeap.inline.hpp" 46 #include "memory/allocation.inline.hpp" 47 #include "oops/oop.inline.hpp" 48 #include "runtime/fieldType.hpp" 49 #include "utilities/macros.hpp" 50 51 // ciObjectFactory 52 // 53 // This class handles requests for the creation of new instances 54 // of ciObject and its subclasses. It contains a caching mechanism 55 // which ensures that for each oop, at most one ciObject is created. 56 // This invariant allows more efficient implementation of ciObject. 57 // 58 // Implementation note: the oop->ciObject mapping is represented as 59 // a table stored in an array. Even though objects are moved 60 // by the garbage collector, the compactor preserves their relative 61 // order; address comparison of oops (in perm space) is safe so long 62 // as we prohibit GC during our comparisons. We currently use binary 63 // search to find the oop in the table, and inserting a new oop 64 // into the table may be costly. If this cost ends up being 65 // problematic the underlying data structure can be switched to some 66 // sort of balanced binary tree. 67 68 GrowableArray<ciMetadata*>* ciObjectFactory::_shared_ci_metadata = NULL; 69 ciSymbol* ciObjectFactory::_shared_ci_symbols[vmSymbols::SID_LIMIT]; 343 } else if (o->is_typeArray()) { 344 typeArrayHandle h_ta(THREAD, (typeArrayOop)o); 345 return new (arena()) ciTypeArray(h_ta); 346 } 347 348 // The oop is of some type not supported by the compiler interface. 349 ShouldNotReachHere(); 350 return NULL; 351 } 352 353 // ------------------------------------------------------------------ 354 // ciObjectFactory::create_new_metadata 355 // 356 // Create a new ciMetadata from a Metadata*. 357 // 358 // Implementation note: in order to keep Metadata live, an auxiliary ciObject 359 // is used, which points to it's holder. 360 ciMetadata* ciObjectFactory::create_new_metadata(Metadata* o) { 361 EXCEPTION_CONTEXT; 362 363 if (o->is_klass()) { 364 Klass* k = (Klass*)o; 365 if (k->is_instance_klass()) { 366 return new (arena()) ciInstanceKlass(k); 367 } else if (k->is_objArray_klass()) { 368 return new (arena()) ciObjArrayKlass(k); 369 } else if (k->is_typeArray_klass()) { 370 return new (arena()) ciTypeArrayKlass(k); 371 } 372 } else if (o->is_method()) { 373 methodHandle h_m(THREAD, (Method*)o); 374 ciEnv *env = CURRENT_THREAD_ENV; 375 ciInstanceKlass* holder = env->get_instance_klass(h_m()->method_holder()); 376 return new (arena()) ciMethod(h_m, holder); 377 } else if (o->is_methodData()) { 378 // Hold methodHandle alive - might not be necessary ??? 379 methodHandle h_m(THREAD, ((MethodData*)o)->method()); 380 return new (arena()) ciMethodData((MethodData*)o); 381 } 382 383 // The Metadata* is of some type not supported by the compiler interface. 384 ShouldNotReachHere(); 385 return NULL; 386 } 387 388 //------------------------------------------------------------------ 389 // ciObjectFactory::get_unloaded_method 390 // 391 // Get the ciMethod representing an unloaded/unfound method. 392 // 393 // Implementation note: unloaded methods are currently stored in 394 // an unordered array, requiring a linear-time lookup for each 395 // unloaded method. This may need to change. 396 ciMethod* ciObjectFactory::get_unloaded_method(ciInstanceKlass* holder, 397 ciSymbol* name, 398 ciSymbol* signature, 399 ciInstanceKlass* accessor) { 400 ciSignature* that = NULL; 401 for (int i = 0; i < _unloaded_methods->length(); i++) { 402 ciMethod* entry = _unloaded_methods->at(i); 403 if (entry->holder()->equals(holder) && 404 entry->name()->equals(name) && 405 entry->signature()->as_symbol()->equals(signature)) { |