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