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