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