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