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 "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)                              \
 165   if (SystemDictionary::name##_is_loaded()) \
 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::cached_metadata
 274 //
 275 // Get the ciMetadata corresponding to some Metadata. If the ciMetadata has
 276 // already been created, it is returned. Otherwise, null is returned.
 277 ciMetadata* ciObjectFactory::cached_metadata(Metadata* key) {
 278   ASSERT_IN_VM;
 279 
 280   bool found = false;
 281   int index = _ci_metadata->find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found);
 282 
 283   if (!found) {
 284     return NULL;
 285   }
 286   return _ci_metadata->at(index)->as_metadata();
 287 }
 288 
 289 
 290 // ------------------------------------------------------------------
 291 // ciObjectFactory::get_metadata
 292 //
 293 // Get the ciMetadata corresponding to some Metadata. If the ciMetadata has
 294 // already been created, it is returned. Otherwise, a new ciMetadata
 295 // is created.
 296 ciMetadata* ciObjectFactory::get_metadata(Metadata* key) {
 297   ASSERT_IN_VM;
 298 
 299 #ifdef ASSERT
 300   if (CIObjectFactoryVerify) {
 301     Metadata* last = NULL;
 302     for (int j = 0; j< _ci_metadata->length(); j++) {
 303       Metadata* o = _ci_metadata->at(j)->constant_encoding();
 304       assert(last < o, "out of order");
 305       last = o;
 306     }
 307   }
 308 #endif // ASSERT
 309   int len = _ci_metadata->length();
 310   bool found = false;
 311   int index = _ci_metadata->find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found);
 312 #ifdef ASSERT
 313   if (CIObjectFactoryVerify) {
 314     for (int i=0; i<_ci_metadata->length(); i++) {
 315       if (_ci_metadata->at(i)->constant_encoding() == key) {
 316         assert(index == i, " bad lookup");
 317       }
 318     }
 319   }
 320 #endif
 321 
 322   if (!found) {
 323     // The ciMetadata does not yet exist. Create it and insert it
 324     // into the cache.
 325     ciMetadata* new_object = create_new_metadata(key);
 326     init_ident_of(new_object);
 327     assert(new_object->is_metadata(), "must be");
 328 
 329     if (len != _ci_metadata->length()) {
 330       // creating the new object has recursively entered new objects
 331       // into the table.  We need to recompute our index.
 332       index = _ci_metadata->find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found);
 333     }
 334     assert(!found, "no double insert");
 335     _ci_metadata->insert_before(index, new_object);
 336     return new_object;
 337   }
 338   return _ci_metadata->at(index)->as_metadata();
 339 }
 340 
 341 // ------------------------------------------------------------------
 342 // ciObjectFactory::create_new_object
 343 //
 344 // Create a new ciObject from an oop.
 345 //
 346 // Implementation note: this functionality could be virtual behavior
 347 // of the oop itself.  For now, we explicitly marshal the object.
 348 ciObject* ciObjectFactory::create_new_object(oop o) {
 349   EXCEPTION_CONTEXT;
 350 
 351   if (o->is_instance()) {
 352     instanceHandle h_i(THREAD, (instanceOop)o);
 353     if (java_lang_invoke_CallSite::is_instance(o))
 354       return new (arena()) ciCallSite(h_i);
 355     else if (java_lang_invoke_MemberName::is_instance(o))
 356       return new (arena()) ciMemberName(h_i);
 357     else if (java_lang_invoke_MethodHandle::is_instance(o))
 358       return new (arena()) ciMethodHandle(h_i);
 359     else if (java_lang_invoke_MethodType::is_instance(o))
 360       return new (arena()) ciMethodType(h_i);
 361     else
 362       return new (arena()) ciInstance(h_i);
 363   } else if (o->is_objArray()) {
 364     objArrayHandle h_oa(THREAD, (objArrayOop)o);
 365     return new (arena()) ciObjArray(h_oa);
 366   } else if (o->is_typeArray()) {
 367     typeArrayHandle h_ta(THREAD, (typeArrayOop)o);
 368     return new (arena()) ciTypeArray(h_ta);
 369   } else if (o->is_valueArray()) {
 370     valueArrayHandle h_ta(THREAD, (valueArrayOop)o);
 371     return new (arena()) ciValueArray(h_ta);
 372   }
 373 
 374   // The oop is of some type not supported by the compiler interface.
 375   ShouldNotReachHere();
 376   return NULL;
 377 }
 378 
 379 // ------------------------------------------------------------------
 380 // ciObjectFactory::create_new_metadata
 381 //
 382 // Create a new ciMetadata from a Metadata*.
 383 //
 384 // Implementation note: in order to keep Metadata live, an auxiliary ciObject
 385 // is used, which points to it's holder.
 386 ciMetadata* ciObjectFactory::create_new_metadata(Metadata* o) {
 387   EXCEPTION_CONTEXT;
 388 
 389   if (o->is_klass()) {
 390     Klass* k = (Klass*)o;
 391     if (k->is_value()) {
 392       return new (arena()) ciValueKlass(k);
 393     } else if (k->is_instance_klass()) {
 394       return new (arena()) ciInstanceKlass(k);
 395     } else if (k->is_valueArray_klass()) {
 396       return new (arena()) ciValueArrayKlass(k);
 397     } else if (k->is_objArray_klass()) {
 398       return new (arena()) ciObjArrayKlass(k);
 399     } else if (k->is_typeArray_klass()) {
 400       return new (arena()) ciTypeArrayKlass(k);
 401     }
 402   } else if (o->is_method()) {
 403     methodHandle h_m(THREAD, (Method*)o);
 404     ciEnv *env = CURRENT_THREAD_ENV;
 405     ciInstanceKlass* holder = env->get_instance_klass(h_m()->method_holder());
 406     return new (arena()) ciMethod(h_m, holder);
 407   } else if (o->is_methodData()) {
 408     // Hold methodHandle alive - might not be necessary ???
 409     methodHandle h_m(THREAD, ((MethodData*)o)->method());
 410     return new (arena()) ciMethodData((MethodData*)o);
 411   }
 412 
 413   // The Metadata* is of some type not supported by the compiler interface.
 414   ShouldNotReachHere();
 415   return NULL;
 416 }
 417 
 418 //------------------------------------------------------------------
 419 // ciObjectFactory::get_unloaded_method
 420 //
 421 // Get the ciMethod representing an unloaded/unfound method.
 422 //
 423 // Implementation note: unloaded methods are currently stored in
 424 // an unordered array, requiring a linear-time lookup for each
 425 // unloaded method.  This may need to change.
 426 ciMethod* ciObjectFactory::get_unloaded_method(ciInstanceKlass* holder,
 427                                                ciSymbol*        name,
 428                                                ciSymbol*        signature,
 429                                                ciInstanceKlass* accessor) {
 430   ciSignature* that = NULL;
 431   for (int i = 0; i < _unloaded_methods->length(); i++) {
 432     ciMethod* entry = _unloaded_methods->at(i);
 433     if (entry->holder()->equals(holder) &&
 434         entry->name()->equals(name) &&
 435         entry->signature()->as_symbol()->equals(signature)) {
 436       // Short-circuit slow resolve.
 437       if (entry->signature()->accessing_klass() == accessor) {
 438         // We've found a match.
 439         return entry;
 440       } else {
 441         // Lazily create ciSignature
 442         if (that == NULL)  that = new (arena()) ciSignature(accessor, constantPoolHandle(), signature);
 443         if (entry->signature()->equals(that)) {
 444           // We've found a match.
 445           return entry;
 446         }
 447       }
 448     }
 449   }
 450 
 451   // This is a new unloaded method.  Create it and stick it in
 452   // the cache.
 453   ciMethod* new_method = new (arena()) ciMethod(holder, name, signature, accessor);
 454 
 455   init_ident_of(new_method);
 456   _unloaded_methods->append(new_method);
 457 
 458   return new_method;
 459 }
 460 
 461 //------------------------------------------------------------------
 462 // ciObjectFactory::get_unloaded_klass
 463 //
 464 // Get a ciKlass representing an unloaded klass.
 465 //
 466 // Implementation note: unloaded klasses are currently stored in
 467 // an unordered array, requiring a linear-time lookup for each
 468 // unloaded klass.  This may need to change.
 469 ciKlass* ciObjectFactory::get_unloaded_klass(ciKlass* accessing_klass,
 470                                              ciSymbol* name,
 471                                              bool create_if_not_found) {
 472   EXCEPTION_CONTEXT;
 473   oop loader = NULL;
 474   oop domain = NULL;
 475   if (accessing_klass != NULL) {
 476     loader = accessing_klass->loader();
 477     domain = accessing_klass->protection_domain();
 478   }
 479   for (int i=0; i<_unloaded_klasses->length(); i++) {
 480     ciKlass* entry = _unloaded_klasses->at(i);
 481     if (entry->name()->equals(name) &&
 482         oopDesc::equals(entry->loader(), loader) &&
 483         oopDesc::equals(entry->protection_domain(), domain)) {
 484       // We've found a match.
 485       return entry;
 486     }
 487   }
 488 
 489   if (!create_if_not_found)
 490     return NULL;
 491 
 492   // This is a new unloaded klass.  Create it and stick it in
 493   // the cache.
 494   ciKlass* new_klass = NULL;
 495 
 496   // Two cases: this is an unloaded ObjArrayKlass or an
 497   // unloaded InstanceKlass.  Deal with both.
 498   if (name->char_at(0) == '[') {
 499     // Decompose the name.'
 500     FieldArrayInfo fd;
 501     BasicType element_type = FieldType::get_array_info(name->get_symbol(),
 502                                                        fd, THREAD);
 503     if (HAS_PENDING_EXCEPTION) {
 504       CLEAR_PENDING_EXCEPTION;
 505       CURRENT_THREAD_ENV->record_out_of_memory_failure();
 506       return ciEnv::_unloaded_ciobjarrayklass;
 507     }
 508     int dimension = fd.dimension();
 509     assert(element_type != T_ARRAY, "unsuccessful decomposition");
 510     ciKlass* element_klass = NULL;
 511     if (element_type == T_OBJECT || element_type == T_VALUETYPE) {
 512       ciEnv *env = CURRENT_THREAD_ENV;
 513       ciSymbol* ci_name = env->get_symbol(fd.object_key());
 514       element_klass =
 515         env->get_klass_by_name(accessing_klass, ci_name, false)->as_instance_klass();
 516     } else {
 517       assert(dimension > 1, "one dimensional type arrays are always loaded.");
 518 
 519       // The type array itself takes care of one of the dimensions.
 520       dimension--;
 521 
 522       // The element klass is a TypeArrayKlass.
 523       element_klass = ciTypeArrayKlass::make(element_type);
 524     }
 525     new_klass = new (arena()) ciObjArrayKlass(name, element_klass, dimension);
 526   } else {
 527     jobject loader_handle = NULL;
 528     jobject domain_handle = NULL;
 529     if (accessing_klass != NULL) {
 530       loader_handle = accessing_klass->loader_handle();
 531       domain_handle = accessing_klass->protection_domain_handle();
 532     }
 533     new_klass = new (arena()) ciInstanceKlass(name, loader_handle, domain_handle);
 534   }
 535   init_ident_of(new_klass);
 536   _unloaded_klasses->append(new_klass);
 537 
 538   return new_klass;
 539 }
 540 
 541 
 542 //------------------------------------------------------------------
 543 // ciObjectFactory::get_unloaded_instance
 544 //
 545 // Get a ciInstance representing an as-yet undetermined instance of a given class.
 546 //
 547 ciInstance* ciObjectFactory::get_unloaded_instance(ciInstanceKlass* instance_klass) {
 548   for (int i=0; i<_unloaded_instances->length(); i++) {
 549     ciInstance* entry = _unloaded_instances->at(i);
 550     if (entry->klass()->equals(instance_klass)) {
 551       // We've found a match.
 552       return entry;
 553     }
 554   }
 555 
 556   // This is a new unloaded instance.  Create it and stick it in
 557   // the cache.
 558   ciInstance* new_instance = new (arena()) ciInstance(instance_klass);
 559 
 560   init_ident_of(new_instance);
 561   _unloaded_instances->append(new_instance);
 562 
 563   // make sure it looks the way we want:
 564   assert(!new_instance->is_loaded(), "");
 565   assert(new_instance->klass() == instance_klass, "");
 566 
 567   return new_instance;
 568 }
 569 
 570 
 571 //------------------------------------------------------------------
 572 // ciObjectFactory::get_unloaded_klass_mirror
 573 //
 574 // Get a ciInstance representing an unresolved klass mirror.
 575 //
 576 // Currently, this ignores the parameters and returns a unique unloaded instance.
 577 ciInstance* ciObjectFactory::get_unloaded_klass_mirror(ciKlass*  type) {
 578   assert(ciEnv::_Class_klass != NULL, "");
 579   return get_unloaded_instance(ciEnv::_Class_klass->as_instance_klass());
 580 }
 581 
 582 //------------------------------------------------------------------
 583 // ciObjectFactory::get_unloaded_method_handle_constant
 584 //
 585 // Get a ciInstance representing an unresolved method handle constant.
 586 //
 587 // Currently, this ignores the parameters and returns a unique unloaded instance.
 588 ciInstance* ciObjectFactory::get_unloaded_method_handle_constant(ciKlass*  holder,
 589                                                                  ciSymbol* name,
 590                                                                  ciSymbol* signature,
 591                                                                  int       ref_kind) {
 592   if (ciEnv::_MethodHandle_klass == NULL)  return NULL;
 593   return get_unloaded_instance(ciEnv::_MethodHandle_klass->as_instance_klass());
 594 }
 595 
 596 //------------------------------------------------------------------
 597 // ciObjectFactory::get_unloaded_method_type_constant
 598 //
 599 // Get a ciInstance representing an unresolved method type constant.
 600 //
 601 // Currently, this ignores the parameters and returns a unique unloaded instance.
 602 ciInstance* ciObjectFactory::get_unloaded_method_type_constant(ciSymbol* signature) {
 603   if (ciEnv::_MethodType_klass == NULL)  return NULL;
 604   return get_unloaded_instance(ciEnv::_MethodType_klass->as_instance_klass());
 605 }
 606 
 607 ciInstance* ciObjectFactory::get_unloaded_object_constant() {
 608   if (ciEnv::_Object_klass == NULL)  return NULL;
 609   return get_unloaded_instance(ciEnv::_Object_klass->as_instance_klass());
 610 }
 611 
 612 //------------------------------------------------------------------
 613 // ciObjectFactory::get_empty_methodData
 614 //
 615 // Get the ciMethodData representing the methodData for a method with
 616 // none.
 617 ciMethodData* ciObjectFactory::get_empty_methodData() {
 618   ciMethodData* new_methodData = new (arena()) ciMethodData();
 619   init_ident_of(new_methodData);
 620   return new_methodData;
 621 }
 622 
 623 //------------------------------------------------------------------
 624 // ciObjectFactory::get_return_address
 625 //
 626 // Get a ciReturnAddress for a specified bci.
 627 ciReturnAddress* ciObjectFactory::get_return_address(int bci) {
 628   for (int i=0; i<_return_addresses->length(); i++) {
 629     ciReturnAddress* entry = _return_addresses->at(i);
 630     if (entry->bci() == bci) {
 631       // We've found a match.
 632       return entry;
 633     }
 634   }
 635 
 636   ciReturnAddress* new_ret_addr = new (arena()) ciReturnAddress(bci);
 637   init_ident_of(new_ret_addr);
 638   _return_addresses->append(new_ret_addr);
 639   return new_ret_addr;
 640 }
 641 
 642 ciWrapper* ciObjectFactory::make_never_null_wrapper(ciType* type) {
 643   ciWrapper* wrapper = new (arena()) ciWrapper(type, /* never_null */ true);
 644   init_ident_of(wrapper);
 645   return wrapper;
 646 }
 647 
 648 // ------------------------------------------------------------------
 649 // ciObjectFactory::init_ident_of
 650 void ciObjectFactory::init_ident_of(ciBaseObject* obj) {
 651   obj->set_ident(_next_ident++);
 652 }
 653 
 654 static ciObjectFactory::NonPermObject* emptyBucket = NULL;
 655 
 656 // ------------------------------------------------------------------
 657 // ciObjectFactory::find_non_perm
 658 //
 659 // Use a small hash table, hashed on the klass of the key.
 660 // If there is no entry in the cache corresponding to this oop, return
 661 // the null tail of the bucket into which the oop should be inserted.
 662 ciObjectFactory::NonPermObject* &ciObjectFactory::find_non_perm(oop key) {
 663   assert(Universe::heap()->is_in_reserved(key), "must be");
 664   ciMetadata* klass = get_metadata(key->klass());
 665   NonPermObject* *bp = &_non_perm_bucket[(unsigned) klass->hash() % NON_PERM_BUCKETS];
 666   for (NonPermObject* p; (p = (*bp)) != NULL; bp = &p->next()) {
 667     if (is_equal(p, key))  break;
 668   }
 669   return (*bp);
 670 }
 671 
 672 
 673 
 674 // ------------------------------------------------------------------
 675 // Code for for NonPermObject
 676 //
 677 inline ciObjectFactory::NonPermObject::NonPermObject(ciObjectFactory::NonPermObject* &bucket, oop key, ciObject* object) {
 678   assert(ciObjectFactory::is_initialized(), "");
 679   _object = object;
 680   _next = bucket;
 681   bucket = this;
 682 }
 683 
 684 
 685 
 686 // ------------------------------------------------------------------
 687 // ciObjectFactory::insert_non_perm
 688 //
 689 // Insert a ciObject into the non-perm table.
 690 void ciObjectFactory::insert_non_perm(ciObjectFactory::NonPermObject* &where, oop key, ciObject* obj) {
 691   assert(Universe::heap()->is_in_reserved_or_null(key), "must be");
 692   assert(&where != &emptyBucket, "must not try to fill empty bucket");
 693   NonPermObject* p = new (arena()) NonPermObject(where, key, obj);
 694   assert(where == p && is_equal(p, key) && p->object() == obj, "entry must match");
 695   assert(find_non_perm(key) == p, "must find the same spot");
 696   ++_non_perm_count;
 697 }
 698 
 699 // ------------------------------------------------------------------
 700 // ciObjectFactory::vm_symbol_at
 701 // Get the ciSymbol corresponding to some index in vmSymbols.
 702 ciSymbol* ciObjectFactory::vm_symbol_at(int index) {
 703   assert(index >= vmSymbols::FIRST_SID && index < vmSymbols::SID_LIMIT, "oob");
 704   return _shared_ci_symbols[index];
 705 }
 706 
 707 // ------------------------------------------------------------------
 708 // ciObjectFactory::metadata_do
 709 void ciObjectFactory::metadata_do(void f(Metadata*)) {
 710   if (_ci_metadata == NULL) return;
 711   for (int j = 0; j< _ci_metadata->length(); j++) {
 712     Metadata* o = _ci_metadata->at(j)->constant_encoding();
 713     f(o);
 714   }
 715 }
 716 
 717 // ------------------------------------------------------------------
 718 // ciObjectFactory::print_contents_impl
 719 void ciObjectFactory::print_contents_impl() {
 720   int len = _ci_metadata->length();
 721   tty->print_cr("ciObjectFactory (%d) meta data contents:", len);
 722   for (int i=0; i<len; i++) {
 723     _ci_metadata->at(i)->print();
 724     tty->cr();
 725   }
 726 }
 727 
 728 // ------------------------------------------------------------------
 729 // ciObjectFactory::print_contents
 730 void ciObjectFactory::print_contents() {
 731   print();
 732   tty->cr();
 733   GUARDED_VM_ENTRY(print_contents_impl();)
 734 }
 735 
 736 // ------------------------------------------------------------------
 737 // ciObjectFactory::print
 738 //
 739 // Print debugging information about the object factory
 740 void ciObjectFactory::print() {
 741   tty->print("<ciObjectFactory oops=%d metadata=%d unloaded_methods=%d unloaded_instances=%d unloaded_klasses=%d>",
 742              _non_perm_count, _ci_metadata->length(), _unloaded_methods->length(),
 743              _unloaded_instances->length(),
 744              _unloaded_klasses->length());
 745 }