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