1 /*
   2  * Copyright (c) 1999, 2018, 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 "oops/oop.inline.hpp"
  51 #include "runtime/fieldType.hpp"
  52 #include "runtime/handles.inline.hpp"
  53 #include "utilities/macros.hpp"
  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::floatArrayKlassObj());
 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(Thread::current(), key);
 255   ciObject* new_object = create_new_object(keyHandle());
 256   assert(oopDesc::equals(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   } else if (o->is_valueArray()) {
 352     valueArrayHandle h_ta(THREAD, (valueArrayOop)o);
 353     return new (arena()) ciValueArray(h_ta);
 354   }
 355 
 356   // The oop is of some type not supported by the compiler interface.
 357   ShouldNotReachHere();
 358   return NULL;
 359 }
 360 
 361 // ------------------------------------------------------------------
 362 // ciObjectFactory::create_new_metadata
 363 //
 364 // Create a new ciMetadata from a Metadata*.
 365 //
 366 // Implementation note: in order to keep Metadata live, an auxiliary ciObject
 367 // is used, which points to it's holder.
 368 ciMetadata* ciObjectFactory::create_new_metadata(Metadata* o) {
 369   EXCEPTION_CONTEXT;
 370 
 371   if (o->is_klass()) {
 372     Klass* k = (Klass*)o;
 373     if (k->is_value()) {
 374       return new (arena()) ciValueKlass(k);
 375     } else if (k->is_instance_klass()) {
 376       return new (arena()) ciInstanceKlass(k);
 377     } else if (k->is_valueArray_klass()) {
 378       return new (arena()) ciValueArrayKlass(k);
 379     } else if (k->is_objArray_klass()) {
 380       return new (arena()) ciObjArrayKlass(k);
 381     } else if (k->is_typeArray_klass()) {
 382       return new (arena()) ciTypeArrayKlass(k);
 383     }
 384   } else if (o->is_method()) {
 385     methodHandle h_m(THREAD, (Method*)o);
 386     ciEnv *env = CURRENT_THREAD_ENV;
 387     ciInstanceKlass* holder = env->get_instance_klass(h_m()->method_holder());
 388     return new (arena()) ciMethod(h_m, holder);
 389   } else if (o->is_methodData()) {
 390     // Hold methodHandle alive - might not be necessary ???
 391     methodHandle h_m(THREAD, ((MethodData*)o)->method());
 392     return new (arena()) ciMethodData((MethodData*)o);
 393   }
 394 
 395   // The Metadata* is of some type not supported by the compiler interface.
 396   ShouldNotReachHere();
 397   return NULL;
 398 }
 399 
 400 //------------------------------------------------------------------
 401 // ciObjectFactory::get_unloaded_method
 402 //
 403 // Get the ciMethod representing an unloaded/unfound method.
 404 //
 405 // Implementation note: unloaded methods are currently stored in
 406 // an unordered array, requiring a linear-time lookup for each
 407 // unloaded method.  This may need to change.
 408 ciMethod* ciObjectFactory::get_unloaded_method(ciInstanceKlass* holder,
 409                                                ciSymbol*        name,
 410                                                ciSymbol*        signature,
 411                                                ciInstanceKlass* accessor) {
 412   ciSignature* that = NULL;
 413   for (int i = 0; i < _unloaded_methods->length(); i++) {
 414     ciMethod* entry = _unloaded_methods->at(i);
 415     if (entry->holder()->equals(holder) &&
 416         entry->name()->equals(name) &&
 417         entry->signature()->as_symbol()->equals(signature)) {
 418       // Short-circuit slow resolve.
 419       if (entry->signature()->accessing_klass() == accessor) {
 420         // We've found a match.
 421         return entry;
 422       } else {
 423         // Lazily create ciSignature
 424         if (that == NULL)  that = new (arena()) ciSignature(accessor, constantPoolHandle(), signature);
 425         if (entry->signature()->equals(that)) {
 426           // We've found a match.
 427           return entry;
 428         }
 429       }
 430     }
 431   }
 432 
 433   // This is a new unloaded method.  Create it and stick it in
 434   // the cache.
 435   ciMethod* new_method = new (arena()) ciMethod(holder, name, signature, accessor);
 436 
 437   init_ident_of(new_method);
 438   _unloaded_methods->append(new_method);
 439 
 440   return new_method;
 441 }
 442 
 443 //------------------------------------------------------------------
 444 // ciObjectFactory::get_unloaded_klass
 445 //
 446 // Get a ciKlass representing an unloaded klass.
 447 //
 448 // Implementation note: unloaded klasses are currently stored in
 449 // an unordered array, requiring a linear-time lookup for each
 450 // unloaded klass.  This may need to change.
 451 ciKlass* ciObjectFactory::get_unloaded_klass(ciKlass* accessing_klass,
 452                                              ciSymbol* name,
 453                                              bool create_if_not_found,
 454                                              bool is_value_type) {
 455   EXCEPTION_CONTEXT;
 456   oop loader = NULL;
 457   oop domain = NULL;
 458   if (accessing_klass != NULL) {
 459     loader = accessing_klass->loader();
 460     domain = accessing_klass->protection_domain();
 461   }
 462   for (int i=0; i<_unloaded_klasses->length(); i++) {
 463     ciKlass* entry = _unloaded_klasses->at(i);
 464     if (entry->name()->equals(name) &&
 465         oopDesc::equals(entry->loader(), loader) &&
 466         oopDesc::equals(entry->protection_domain(), domain)) {
 467       // We've found a match.
 468       return entry;
 469     }
 470   }
 471 
 472   if (!create_if_not_found)
 473     return NULL;
 474 
 475   // This is a new unloaded klass.  Create it and stick it in
 476   // the cache.
 477   ciKlass* new_klass = NULL;
 478 
 479   // Two cases: this is an unloaded ObjArrayKlass or an
 480   // unloaded InstanceKlass.  Deal with both.
 481   if (name->byte_at(0) == '[') {
 482     // Decompose the name.'
 483     FieldArrayInfo fd;
 484     BasicType element_type = FieldType::get_array_info(name->get_symbol(),
 485                                                        fd, THREAD);
 486     if (HAS_PENDING_EXCEPTION) {
 487       CLEAR_PENDING_EXCEPTION;
 488       CURRENT_THREAD_ENV->record_out_of_memory_failure();
 489       return ciEnv::_unloaded_ciobjarrayklass;
 490     }
 491     int dimension = fd.dimension();
 492     assert(element_type != T_ARRAY, "unsuccessful decomposition");
 493     ciKlass* element_klass = NULL;
 494     if (element_type == T_OBJECT || element_type == T_VALUETYPE) {
 495       ciEnv *env = CURRENT_THREAD_ENV;
 496       ciSymbol* ci_name = env->get_symbol(fd.object_key());
 497       element_klass =
 498         env->get_klass_by_name(accessing_klass, ci_name, false)->as_instance_klass();
 499     } else {
 500       assert(dimension > 1, "one dimensional type arrays are always loaded.");
 501 
 502       // The type array itself takes care of one of the dimensions.
 503       dimension--;
 504 
 505       // The element klass is a TypeArrayKlass.
 506       element_klass = ciTypeArrayKlass::make(element_type);
 507     }
 508     if (element_type == T_VALUETYPE && EnableValhallaC1) { // FIXME C2 can't handle unloaded ciValueArrayKlass
 509       assert(element_klass->is_valuetype(), "must be");
 510       new_klass = new (arena()) ciValueArrayKlass(name, element_klass->as_value_klass(), dimension);
 511     } else {
 512       new_klass = new (arena()) ciObjArrayKlass(name, element_klass, dimension);
 513     }
 514   } else {
 515     jobject loader_handle = NULL;
 516     jobject domain_handle = NULL;
 517     if (accessing_klass != NULL) {
 518       loader_handle = accessing_klass->loader_handle();
 519       domain_handle = accessing_klass->protection_domain_handle();
 520     }
 521     if (is_value_type && EnableValhallaC1) { // FIXME C2 can't handle unloaded ciValueKlass
 522       new_klass = new (arena()) ciValueKlass(name, loader_handle, domain_handle);
 523     } else {
 524       new_klass = new (arena()) ciInstanceKlass(name, loader_handle, domain_handle);
 525     }
 526   }
 527   init_ident_of(new_klass);
 528   _unloaded_klasses->append(new_klass);
 529 
 530   return new_klass;
 531 }
 532 
 533 
 534 //------------------------------------------------------------------
 535 // ciObjectFactory::get_unloaded_instance
 536 //
 537 // Get a ciInstance representing an as-yet undetermined instance of a given class.
 538 //
 539 ciInstance* ciObjectFactory::get_unloaded_instance(ciInstanceKlass* instance_klass) {
 540   for (int i=0; i<_unloaded_instances->length(); i++) {
 541     ciInstance* entry = _unloaded_instances->at(i);
 542     if (entry->klass()->equals(instance_klass)) {
 543       // We've found a match.
 544       return entry;
 545     }
 546   }
 547 
 548   // This is a new unloaded instance.  Create it and stick it in
 549   // the cache.
 550   ciInstance* new_instance = new (arena()) ciInstance(instance_klass);
 551 
 552   init_ident_of(new_instance);
 553   _unloaded_instances->append(new_instance);
 554 
 555   // make sure it looks the way we want:
 556   assert(!new_instance->is_loaded(), "");
 557   assert(new_instance->klass() == instance_klass, "");
 558 
 559   return new_instance;
 560 }
 561 
 562 
 563 //------------------------------------------------------------------
 564 // ciObjectFactory::get_unloaded_klass_mirror
 565 //
 566 // Get a ciInstance representing an unresolved klass mirror.
 567 //
 568 // Currently, this ignores the parameters and returns a unique unloaded instance.
 569 ciInstance* ciObjectFactory::get_unloaded_klass_mirror(ciKlass*  type) {
 570   assert(ciEnv::_Class_klass != NULL, "");
 571   return get_unloaded_instance(ciEnv::_Class_klass->as_instance_klass());
 572 }
 573 
 574 //------------------------------------------------------------------
 575 // ciObjectFactory::get_unloaded_method_handle_constant
 576 //
 577 // Get a ciInstance representing an unresolved method handle constant.
 578 //
 579 // Currently, this ignores the parameters and returns a unique unloaded instance.
 580 ciInstance* ciObjectFactory::get_unloaded_method_handle_constant(ciKlass*  holder,
 581                                                                  ciSymbol* name,
 582                                                                  ciSymbol* signature,
 583                                                                  int       ref_kind) {
 584   if (ciEnv::_MethodHandle_klass == NULL)  return NULL;
 585   return get_unloaded_instance(ciEnv::_MethodHandle_klass->as_instance_klass());
 586 }
 587 
 588 //------------------------------------------------------------------
 589 // ciObjectFactory::get_unloaded_method_type_constant
 590 //
 591 // Get a ciInstance representing an unresolved method type constant.
 592 //
 593 // Currently, this ignores the parameters and returns a unique unloaded instance.
 594 ciInstance* ciObjectFactory::get_unloaded_method_type_constant(ciSymbol* signature) {
 595   if (ciEnv::_MethodType_klass == NULL)  return NULL;
 596   return get_unloaded_instance(ciEnv::_MethodType_klass->as_instance_klass());
 597 }
 598 
 599 ciInstance* ciObjectFactory::get_unloaded_object_constant() {
 600   if (ciEnv::_Object_klass == NULL)  return NULL;
 601   return get_unloaded_instance(ciEnv::_Object_klass->as_instance_klass());
 602 }
 603 
 604 //------------------------------------------------------------------
 605 // ciObjectFactory::get_empty_methodData
 606 //
 607 // Get the ciMethodData representing the methodData for a method with
 608 // none.
 609 ciMethodData* ciObjectFactory::get_empty_methodData() {
 610   ciMethodData* new_methodData = new (arena()) ciMethodData();
 611   init_ident_of(new_methodData);
 612   return new_methodData;
 613 }
 614 
 615 //------------------------------------------------------------------
 616 // ciObjectFactory::get_return_address
 617 //
 618 // Get a ciReturnAddress for a specified bci.
 619 ciReturnAddress* ciObjectFactory::get_return_address(int bci) {
 620   for (int i=0; i<_return_addresses->length(); i++) {
 621     ciReturnAddress* entry = _return_addresses->at(i);
 622     if (entry->bci() == bci) {
 623       // We've found a match.
 624       return entry;
 625     }
 626   }
 627 
 628   ciReturnAddress* new_ret_addr = new (arena()) ciReturnAddress(bci);
 629   init_ident_of(new_ret_addr);
 630   _return_addresses->append(new_ret_addr);
 631   return new_ret_addr;
 632 }
 633 
 634 // ------------------------------------------------------------------
 635 // ciObjectFactory::init_ident_of
 636 void ciObjectFactory::init_ident_of(ciBaseObject* obj) {
 637   obj->set_ident(_next_ident++);
 638 }
 639 
 640 static ciObjectFactory::NonPermObject* emptyBucket = NULL;
 641 
 642 // ------------------------------------------------------------------
 643 // ciObjectFactory::find_non_perm
 644 //
 645 // Use a small hash table, hashed on the klass of the key.
 646 // If there is no entry in the cache corresponding to this oop, return
 647 // the null tail of the bucket into which the oop should be inserted.
 648 ciObjectFactory::NonPermObject* &ciObjectFactory::find_non_perm(oop key) {
 649   assert(Universe::heap()->is_in_reserved(key), "must be");
 650   ciMetadata* klass = get_metadata(key->klass());
 651   NonPermObject* *bp = &_non_perm_bucket[(unsigned) klass->hash() % NON_PERM_BUCKETS];
 652   for (NonPermObject* p; (p = (*bp)) != NULL; bp = &p->next()) {
 653     if (is_equal(p, key))  break;
 654   }
 655   return (*bp);
 656 }
 657 
 658 
 659 
 660 // ------------------------------------------------------------------
 661 // Code for for NonPermObject
 662 //
 663 inline ciObjectFactory::NonPermObject::NonPermObject(ciObjectFactory::NonPermObject* &bucket, oop key, ciObject* object) {
 664   assert(ciObjectFactory::is_initialized(), "");
 665   _object = object;
 666   _next = bucket;
 667   bucket = this;
 668 }
 669 
 670 
 671 
 672 // ------------------------------------------------------------------
 673 // ciObjectFactory::insert_non_perm
 674 //
 675 // Insert a ciObject into the non-perm table.
 676 void ciObjectFactory::insert_non_perm(ciObjectFactory::NonPermObject* &where, oop key, ciObject* obj) {
 677   assert(Universe::heap()->is_in_reserved_or_null(key), "must be");
 678   assert(&where != &emptyBucket, "must not try to fill empty bucket");
 679   NonPermObject* p = new (arena()) NonPermObject(where, key, obj);
 680   assert(where == p && is_equal(p, key) && p->object() == obj, "entry must match");
 681   assert(find_non_perm(key) == p, "must find the same spot");
 682   ++_non_perm_count;
 683 }
 684 
 685 // ------------------------------------------------------------------
 686 // ciObjectFactory::vm_symbol_at
 687 // Get the ciSymbol corresponding to some index in vmSymbols.
 688 ciSymbol* ciObjectFactory::vm_symbol_at(int index) {
 689   assert(index >= vmSymbols::FIRST_SID && index < vmSymbols::SID_LIMIT, "oob");
 690   return _shared_ci_symbols[index];
 691 }
 692 
 693 // ------------------------------------------------------------------
 694 // ciObjectFactory::metadata_do
 695 void ciObjectFactory::metadata_do(void f(Metadata*)) {
 696   if (_ci_metadata == NULL) return;
 697   for (int j = 0; j< _ci_metadata->length(); j++) {
 698     Metadata* o = _ci_metadata->at(j)->constant_encoding();
 699     f(o);
 700   }
 701 }
 702 
 703 // ------------------------------------------------------------------
 704 // ciObjectFactory::print_contents_impl
 705 void ciObjectFactory::print_contents_impl() {
 706   int len = _ci_metadata->length();
 707   tty->print_cr("ciObjectFactory (%d) meta data contents:", len);
 708   for (int i=0; i<len; i++) {
 709     _ci_metadata->at(i)->print();
 710     tty->cr();
 711   }
 712 }
 713 
 714 // ------------------------------------------------------------------
 715 // ciObjectFactory::print_contents
 716 void ciObjectFactory::print_contents() {
 717   print();
 718   tty->cr();
 719   GUARDED_VM_ENTRY(print_contents_impl();)
 720 }
 721 
 722 // ------------------------------------------------------------------
 723 // ciObjectFactory::print
 724 //
 725 // Print debugging information about the object factory
 726 void ciObjectFactory::print() {
 727   tty->print("<ciObjectFactory oops=%d metadata=%d unloaded_methods=%d unloaded_instances=%d unloaded_klasses=%d>",
 728              _non_perm_count, _ci_metadata->length(), _unloaded_methods->length(),
 729              _unloaded_instances->length(),
 730              _unloaded_klasses->length());
 731 }