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