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