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