duke@435: /* coleenp@4037: * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "ci/ciCallSite.hpp" stefank@2314: #include "ci/ciInstance.hpp" stefank@2314: #include "ci/ciInstanceKlass.hpp" twisti@3969: #include "ci/ciMemberName.hpp" stefank@2314: #include "ci/ciMethod.hpp" stefank@2314: #include "ci/ciMethodData.hpp" stefank@2314: #include "ci/ciMethodHandle.hpp" stefank@2314: #include "ci/ciNullObject.hpp" stefank@2314: #include "ci/ciObjArray.hpp" stefank@2314: #include "ci/ciObjArrayKlass.hpp" coleenp@4037: #include "ci/ciObject.hpp" stefank@2314: #include "ci/ciObjectFactory.hpp" stefank@2314: #include "ci/ciSymbol.hpp" stefank@2314: #include "ci/ciTypeArray.hpp" stefank@2314: #include "ci/ciTypeArrayKlass.hpp" stefank@2314: #include "ci/ciUtilities.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "gc_interface/collectedHeap.inline.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "oops/oop.inline2.hpp" stefank@2314: #include "runtime/fieldType.hpp" duke@435: duke@435: // ciObjectFactory duke@435: // duke@435: // This class handles requests for the creation of new instances duke@435: // of ciObject and its subclasses. It contains a caching mechanism duke@435: // which ensures that for each oop, at most one ciObject is created. duke@435: // This invariant allows more efficient implementation of ciObject. duke@435: // duke@435: // Implementation note: the oop->ciObject mapping is represented as duke@435: // a table stored in an array. Even though objects are moved duke@435: // by the garbage collector, the compactor preserves their relative duke@435: // order; address comparison of oops (in perm space) is safe so long duke@435: // as we prohibit GC during our comparisons. We currently use binary duke@435: // search to find the oop in the table, and inserting a new oop duke@435: // into the table may be costly. If this cost ends up being duke@435: // problematic the underlying data structure can be switched to some duke@435: // sort of balanced binary tree. duke@435: coleenp@4037: GrowableArray* ciObjectFactory::_shared_ci_metadata = NULL; duke@435: ciSymbol* ciObjectFactory::_shared_ci_symbols[vmSymbols::SID_LIMIT]; duke@435: int ciObjectFactory::_shared_ident_limit = 0; duke@435: volatile bool ciObjectFactory::_initialized = false; duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::ciObjectFactory duke@435: ciObjectFactory::ciObjectFactory(Arena* arena, duke@435: int expected_size) { duke@435: duke@435: for (int i = 0; i < NON_PERM_BUCKETS; i++) { duke@435: _non_perm_bucket[i] = NULL; duke@435: } duke@435: _non_perm_count = 0; duke@435: duke@435: _next_ident = _shared_ident_limit; duke@435: _arena = arena; coleenp@4037: _ci_metadata = new (arena) GrowableArray(arena, expected_size, 0, NULL); duke@435: duke@435: // If the shared ci objects exist append them to this factory's objects duke@435: coleenp@4037: if (_shared_ci_metadata != NULL) { coleenp@4037: _ci_metadata->appendAll(_shared_ci_metadata); duke@435: } duke@435: duke@435: _unloaded_methods = new (arena) GrowableArray(arena, 4, 0, NULL); duke@435: _unloaded_klasses = new (arena) GrowableArray(arena, 8, 0, NULL); jrose@1957: _unloaded_instances = new (arena) GrowableArray(arena, 4, 0, NULL); duke@435: _return_addresses = duke@435: new (arena) GrowableArray(arena, 8, 0, NULL); coleenp@2497: coleenp@2497: _symbols = new (arena) GrowableArray(arena, 100, 0, NULL); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::ciObjectFactory duke@435: void ciObjectFactory::initialize() { duke@435: ASSERT_IN_VM; duke@435: JavaThread* thread = JavaThread::current(); duke@435: HandleMark handle_mark(thread); duke@435: duke@435: // This Arena is long lived and exists in the resource mark of the duke@435: // compiler thread that initializes the initial ciObjectFactory which duke@435: // creates the shared ciObjects that all later ciObjectFactories use. zgu@3900: Arena* arena = new (mtCompiler) Arena(); duke@435: ciEnv initial(arena); duke@435: ciEnv* env = ciEnv::current(); duke@435: env->_factory->init_shared_objects(); duke@435: duke@435: _initialized = true; duke@435: duke@435: } duke@435: duke@435: void ciObjectFactory::init_shared_objects() { duke@435: duke@435: _next_ident = 1; // start numbering CI objects at 1 duke@435: duke@435: { coleenp@4037: // Create the shared symbols, but not in _shared_ci_metadata. duke@435: int i; duke@435: for (i = vmSymbols::FIRST_SID; i < vmSymbols::SID_LIMIT; i++) { coleenp@2497: Symbol* vmsym = vmSymbols::symbol_at((vmSymbols::SID) i); coleenp@2497: assert(vmSymbols::find_sid(vmsym) == i, "1-1 mapping"); coleenp@2497: ciSymbol* sym = new (_arena) ciSymbol(vmsym, (vmSymbols::SID) i); duke@435: init_ident_of(sym); duke@435: _shared_ci_symbols[i] = sym; duke@435: } duke@435: #ifdef ASSERT duke@435: for (i = vmSymbols::FIRST_SID; i < vmSymbols::SID_LIMIT; i++) { coleenp@2497: Symbol* vmsym = vmSymbols::symbol_at((vmSymbols::SID) i); duke@435: ciSymbol* sym = vm_symbol_at((vmSymbols::SID) i); coleenp@2497: assert(sym->get_symbol() == vmsym, "oop must match"); duke@435: } coleenp@2497: assert(ciSymbol::void_class_signature()->get_symbol() == vmSymbols::void_class_signature(), "spot check"); duke@435: #endif duke@435: } duke@435: coleenp@4037: _ci_metadata = new (_arena) GrowableArray(_arena, 64, 0, NULL); duke@435: duke@435: for (int i = T_BOOLEAN; i <= T_CONFLICT; i++) { duke@435: BasicType t = (BasicType)i; coleenp@548: if (type2name(t) != NULL && t != T_OBJECT && t != T_ARRAY && t != T_NARROWOOP) { duke@435: ciType::_basic_types[t] = new (_arena) ciType(t); duke@435: init_ident_of(ciType::_basic_types[t]); duke@435: } duke@435: } duke@435: duke@435: ciEnv::_null_object_instance = new (_arena) ciNullObject(); duke@435: init_ident_of(ciEnv::_null_object_instance); never@1577: never@1577: #define WK_KLASS_DEFN(name, ignore_s, opt) \ never@1577: if (SystemDictionary::name() != NULL) \ coleenp@4037: ciEnv::_##name = get_metadata(SystemDictionary::name())->as_instance_klass(); never@1577: never@1577: WK_KLASSES_DO(WK_KLASS_DEFN) never@1577: #undef WK_KLASS_DEFN duke@435: coleenp@4037: for (int len = -1; len != _ci_metadata->length(); ) { coleenp@4037: len = _ci_metadata->length(); duke@435: for (int i2 = 0; i2 < len; i2++) { coleenp@4037: ciMetadata* obj = _ci_metadata->at(i2); coleenp@4037: assert (obj->is_metadata(), "what else would it be?"); duke@435: if (obj->is_loaded() && obj->is_instance_klass()) { duke@435: obj->as_instance_klass()->compute_nonstatic_fields(); duke@435: } duke@435: } duke@435: } duke@435: coleenp@2497: ciEnv::_unloaded_cisymbol = ciObjectFactory::get_symbol(vmSymbols::dummy_symbol()); coleenp@4037: // Create dummy InstanceKlass and objArrayKlass object and assign them idents duke@435: ciEnv::_unloaded_ciinstance_klass = new (_arena) ciInstanceKlass(ciEnv::_unloaded_cisymbol, NULL, NULL); duke@435: init_ident_of(ciEnv::_unloaded_ciinstance_klass); duke@435: ciEnv::_unloaded_ciobjarrayklass = new (_arena) ciObjArrayKlass(ciEnv::_unloaded_cisymbol, ciEnv::_unloaded_ciinstance_klass, 1); duke@435: init_ident_of(ciEnv::_unloaded_ciobjarrayklass); duke@435: assert(ciEnv::_unloaded_ciobjarrayklass->is_obj_array_klass(), "just checking"); duke@435: coleenp@4037: get_metadata(Universe::boolArrayKlassObj()); coleenp@4037: get_metadata(Universe::charArrayKlassObj()); coleenp@4037: get_metadata(Universe::singleArrayKlassObj()); coleenp@4037: get_metadata(Universe::doubleArrayKlassObj()); coleenp@4037: get_metadata(Universe::byteArrayKlassObj()); coleenp@4037: get_metadata(Universe::shortArrayKlassObj()); coleenp@4037: get_metadata(Universe::intArrayKlassObj()); coleenp@4037: get_metadata(Universe::longArrayKlassObj()); duke@435: duke@435: duke@435: duke@435: assert(_non_perm_count == 0, "no shared non-perm objects"); duke@435: duke@435: // The shared_ident_limit is the first ident number that will duke@435: // be used for non-shared objects. That is, numbers less than duke@435: // this limit are permanently assigned to shared CI objects, duke@435: // while the higher numbers are recycled afresh by each new ciEnv. duke@435: duke@435: _shared_ident_limit = _next_ident; coleenp@4037: _shared_ci_metadata = _ci_metadata; duke@435: } duke@435: coleenp@2497: coleenp@2497: ciSymbol* ciObjectFactory::get_symbol(Symbol* key) { coleenp@2497: vmSymbols::SID sid = vmSymbols::find_sid(key); coleenp@2497: if (sid != vmSymbols::NO_SID) { coleenp@2497: // do not pollute the main cache with it coleenp@2497: return vm_symbol_at(sid); coleenp@2497: } coleenp@2497: coleenp@2497: assert(vmSymbols::find_sid(key) == vmSymbols::NO_SID, ""); coleenp@2497: ciSymbol* s = new (arena()) ciSymbol(key, vmSymbols::NO_SID); coleenp@2497: _symbols->push(s); coleenp@2497: return s; coleenp@2497: } coleenp@2497: coleenp@2497: // Decrement the refcount when done on symbols referenced by this compilation. coleenp@2497: void ciObjectFactory::remove_symbols() { coleenp@2497: for (int i = 0; i < _symbols->length(); i++) { coleenp@2497: ciSymbol* s = _symbols->at(i); coleenp@2497: s->get_symbol()->decrement_refcount(); coleenp@2497: } coleenp@2497: // Since _symbols is resource allocated we're not allowed to delete it coleenp@2497: // but it'll go away just the same. coleenp@2497: } coleenp@2497: duke@435: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::get duke@435: // duke@435: // Get the ciObject corresponding to some oop. If the ciObject has duke@435: // already been created, it is returned. Otherwise, a new ciObject duke@435: // is created. duke@435: ciObject* ciObjectFactory::get(oop key) { duke@435: ASSERT_IN_VM; duke@435: coleenp@4037: assert(key == NULL || Universe::heap()->is_in_reserved(key), "must be"); coleenp@4037: duke@435: NonPermObject* &bucket = find_non_perm(key); duke@435: if (bucket != NULL) { duke@435: return bucket->object(); duke@435: } duke@435: duke@435: // The ciObject does not yet exist. Create it and insert it duke@435: // into the cache. duke@435: Handle keyHandle(key); duke@435: ciObject* new_object = create_new_object(keyHandle()); duke@435: assert(keyHandle() == new_object->get_oop(), "must be properly recorded"); duke@435: init_ident_of(new_object); coleenp@4037: assert(Universe::heap()->is_in_reserved(new_object->get_oop()), "must be"); coleenp@4037: duke@435: // Not a perm-space object. duke@435: insert_non_perm(bucket, keyHandle(), new_object); duke@435: return new_object; duke@435: } coleenp@4037: coleenp@4037: // ------------------------------------------------------------------ coleenp@4037: // ciObjectFactory::get coleenp@4037: // coleenp@4037: // Get the ciObject corresponding to some oop. If the ciObject has coleenp@4037: // already been created, it is returned. Otherwise, a new ciObject coleenp@4037: // is created. coleenp@4037: ciMetadata* ciObjectFactory::get_metadata(Metadata* key) { coleenp@4037: ASSERT_IN_VM; coleenp@4037: coleenp@4037: assert(key == NULL || key->is_metadata(), "must be"); coleenp@4037: coleenp@4037: #ifdef ASSERT coleenp@4037: if (CIObjectFactoryVerify) { coleenp@4037: Metadata* last = NULL; coleenp@4037: for (int j = 0; j< _ci_metadata->length(); j++) { coleenp@4037: Metadata* o = _ci_metadata->at(j)->constant_encoding(); coleenp@4037: assert(last < o, "out of order"); coleenp@4037: last = o; coleenp@4037: } coleenp@4037: } coleenp@4037: #endif // ASSERT coleenp@4037: int len = _ci_metadata->length(); coleenp@4037: int index = find(key, _ci_metadata); coleenp@4037: #ifdef ASSERT coleenp@4037: if (CIObjectFactoryVerify) { coleenp@4037: for (int i=0; i<_ci_metadata->length(); i++) { coleenp@4037: if (_ci_metadata->at(i)->constant_encoding() == key) { coleenp@4037: assert(index == i, " bad lookup"); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: #endif coleenp@4037: if (!is_found_at(index, key, _ci_metadata)) { coleenp@4037: // The ciObject does not yet exist. Create it and insert it coleenp@4037: // into the cache. coleenp@4037: ciMetadata* new_object = create_new_object(key); coleenp@4037: init_ident_of(new_object); coleenp@4037: assert(new_object->is_metadata(), "must be"); coleenp@4037: coleenp@4037: if (len != _ci_metadata->length()) { duke@435: // creating the new object has recursively entered new objects duke@435: // into the table. We need to recompute our index. coleenp@4037: index = find(key, _ci_metadata); duke@435: } coleenp@4037: assert(!is_found_at(index, key, _ci_metadata), "no double insert"); coleenp@4037: insert(index, new_object, _ci_metadata); duke@435: return new_object; duke@435: } coleenp@4037: return _ci_metadata->at(index)->as_metadata(); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::create_new_object duke@435: // duke@435: // Create a new ciObject from an oop. duke@435: // duke@435: // Implementation note: this functionality could be virtual behavior duke@435: // of the oop itself. For now, we explicitly marshal the object. duke@435: ciObject* ciObjectFactory::create_new_object(oop o) { duke@435: EXCEPTION_CONTEXT; duke@435: coleenp@4037: if (o->is_instance()) { duke@435: instanceHandle h_i(THREAD, (instanceOop)o); jrose@2639: if (java_lang_invoke_CallSite::is_instance(o)) twisti@1573: return new (arena()) ciCallSite(h_i); twisti@3969: else if (java_lang_invoke_MemberName::is_instance(o)) twisti@3969: return new (arena()) ciMemberName(h_i); jrose@2639: else if (java_lang_invoke_MethodHandle::is_instance(o)) twisti@1573: return new (arena()) ciMethodHandle(h_i); twisti@1573: else twisti@1573: return new (arena()) ciInstance(h_i); duke@435: } else if (o->is_objArray()) { duke@435: objArrayHandle h_oa(THREAD, (objArrayOop)o); duke@435: return new (arena()) ciObjArray(h_oa); duke@435: } else if (o->is_typeArray()) { duke@435: typeArrayHandle h_ta(THREAD, (typeArrayOop)o); duke@435: return new (arena()) ciTypeArray(h_ta); coleenp@4037: } coleenp@4037: coleenp@4037: // The oop is of some type not supported by the compiler interface. coleenp@4037: ShouldNotReachHere(); coleenp@4037: return NULL; coleenp@4037: } coleenp@4037: coleenp@4037: // ------------------------------------------------------------------ coleenp@4037: // ciObjectFactory::create_new_object coleenp@4037: // coleenp@4037: // Create a new ciObject from a Metadata*. coleenp@4037: // coleenp@4037: // Implementation note: this functionality could be virtual behavior coleenp@4037: // of the oop itself. For now, we explicitly marshal the object. coleenp@4037: ciMetadata* ciObjectFactory::create_new_object(Metadata* o) { coleenp@4037: EXCEPTION_CONTEXT; coleenp@4037: coleenp@4037: if (o->is_klass()) { coleenp@4037: KlassHandle h_k(THREAD, (Klass*)o); coleenp@4037: Klass* k = (Klass*)o; coleenp@4037: if (k->oop_is_instance()) { coleenp@4037: return new (arena()) ciInstanceKlass(h_k); coleenp@4037: } else if (k->oop_is_objArray()) { coleenp@4037: return new (arena()) ciObjArrayKlass(h_k); coleenp@4037: } else if (k->oop_is_typeArray()) { coleenp@4037: return new (arena()) ciTypeArrayKlass(h_k); coleenp@4037: } coleenp@4037: } else if (o->is_method()) { coleenp@4037: methodHandle h_m(THREAD, (Method*)o); coleenp@4037: return new (arena()) ciMethod(h_m); coleenp@4037: } else if (o->is_methodData()) { coleenp@4037: // Hold methodHandle alive - might not be necessary ??? coleenp@4037: methodHandle h_m(THREAD, ((MethodData*)o)->method()); coleenp@4037: return new (arena()) ciMethodData((MethodData*)o); duke@435: } duke@435: duke@435: // The oop is of some type not supported by the compiler interface. duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------------------------------------------ duke@435: // ciObjectFactory::get_unloaded_method duke@435: // duke@435: // Get the ciMethod representing an unloaded/unfound method. duke@435: // duke@435: // Implementation note: unloaded methods are currently stored in duke@435: // an unordered array, requiring a linear-time lookup for each duke@435: // unloaded method. This may need to change. duke@435: ciMethod* ciObjectFactory::get_unloaded_method(ciInstanceKlass* holder, duke@435: ciSymbol* name, twisti@3197: ciSymbol* signature, twisti@3197: ciInstanceKlass* accessor) { twisti@3197: ciSignature* that = NULL; twisti@3197: for (int i = 0; i < _unloaded_methods->length(); i++) { duke@435: ciMethod* entry = _unloaded_methods->at(i); duke@435: if (entry->holder()->equals(holder) && duke@435: entry->name()->equals(name) && duke@435: entry->signature()->as_symbol()->equals(signature)) { twisti@3197: // Short-circuit slow resolve. twisti@3197: if (entry->signature()->accessing_klass() == accessor) { twisti@3197: // We've found a match. twisti@3197: return entry; twisti@3197: } else { twisti@3197: // Lazily create ciSignature twisti@3197: if (that == NULL) that = new (arena()) ciSignature(accessor, constantPoolHandle(), signature); twisti@3197: if (entry->signature()->equals(that)) { twisti@3197: // We've found a match. twisti@3197: return entry; twisti@3197: } twisti@3197: } duke@435: } duke@435: } duke@435: duke@435: // This is a new unloaded method. Create it and stick it in duke@435: // the cache. twisti@3197: ciMethod* new_method = new (arena()) ciMethod(holder, name, signature, accessor); duke@435: duke@435: init_ident_of(new_method); duke@435: _unloaded_methods->append(new_method); duke@435: duke@435: return new_method; duke@435: } duke@435: duke@435: //------------------------------------------------------------------ duke@435: // ciObjectFactory::get_unloaded_klass duke@435: // duke@435: // Get a ciKlass representing an unloaded klass. duke@435: // duke@435: // Implementation note: unloaded klasses are currently stored in duke@435: // an unordered array, requiring a linear-time lookup for each duke@435: // unloaded klass. This may need to change. duke@435: ciKlass* ciObjectFactory::get_unloaded_klass(ciKlass* accessing_klass, duke@435: ciSymbol* name, duke@435: bool create_if_not_found) { duke@435: EXCEPTION_CONTEXT; duke@435: oop loader = NULL; duke@435: oop domain = NULL; duke@435: if (accessing_klass != NULL) { duke@435: loader = accessing_klass->loader(); duke@435: domain = accessing_klass->protection_domain(); duke@435: } duke@435: for (int i=0; i<_unloaded_klasses->length(); i++) { duke@435: ciKlass* entry = _unloaded_klasses->at(i); duke@435: if (entry->name()->equals(name) && duke@435: entry->loader() == loader && duke@435: entry->protection_domain() == domain) { duke@435: // We've found a match. duke@435: return entry; duke@435: } duke@435: } duke@435: duke@435: if (!create_if_not_found) duke@435: return NULL; duke@435: duke@435: // This is a new unloaded klass. Create it and stick it in duke@435: // the cache. duke@435: ciKlass* new_klass = NULL; duke@435: duke@435: // Two cases: this is an unloaded objArrayKlass or an coleenp@4037: // unloaded InstanceKlass. Deal with both. duke@435: if (name->byte_at(0) == '[') { duke@435: // Decompose the name.' coleenp@2497: FieldArrayInfo fd; coleenp@2497: BasicType element_type = FieldType::get_array_info(name->get_symbol(), coleenp@2497: fd, THREAD); duke@435: if (HAS_PENDING_EXCEPTION) { duke@435: CLEAR_PENDING_EXCEPTION; duke@435: CURRENT_THREAD_ENV->record_out_of_memory_failure(); duke@435: return ciEnv::_unloaded_ciobjarrayklass; duke@435: } coleenp@2497: int dimension = fd.dimension(); duke@435: assert(element_type != T_ARRAY, "unsuccessful decomposition"); duke@435: ciKlass* element_klass = NULL; duke@435: if (element_type == T_OBJECT) { duke@435: ciEnv *env = CURRENT_THREAD_ENV; coleenp@2497: ciSymbol* ci_name = env->get_symbol(fd.object_key()); duke@435: element_klass = duke@435: env->get_klass_by_name(accessing_klass, ci_name, false)->as_instance_klass(); duke@435: } else { duke@435: assert(dimension > 1, "one dimensional type arrays are always loaded."); duke@435: duke@435: // The type array itself takes care of one of the dimensions. duke@435: dimension--; duke@435: duke@435: // The element klass is a typeArrayKlass. duke@435: element_klass = ciTypeArrayKlass::make(element_type); duke@435: } duke@435: new_klass = new (arena()) ciObjArrayKlass(name, element_klass, dimension); duke@435: } else { duke@435: jobject loader_handle = NULL; duke@435: jobject domain_handle = NULL; duke@435: if (accessing_klass != NULL) { duke@435: loader_handle = accessing_klass->loader_handle(); duke@435: domain_handle = accessing_klass->protection_domain_handle(); duke@435: } duke@435: new_klass = new (arena()) ciInstanceKlass(name, loader_handle, domain_handle); duke@435: } duke@435: init_ident_of(new_klass); duke@435: _unloaded_klasses->append(new_klass); duke@435: duke@435: return new_klass; duke@435: } duke@435: jrose@1957: jrose@1957: //------------------------------------------------------------------ jrose@1957: // ciObjectFactory::get_unloaded_instance jrose@1957: // jrose@1957: // Get a ciInstance representing an as-yet undetermined instance of a given class. jrose@1957: // jrose@1957: ciInstance* ciObjectFactory::get_unloaded_instance(ciInstanceKlass* instance_klass) { jrose@1957: for (int i=0; i<_unloaded_instances->length(); i++) { jrose@1957: ciInstance* entry = _unloaded_instances->at(i); jrose@1957: if (entry->klass()->equals(instance_klass)) { jrose@1957: // We've found a match. jrose@1957: return entry; jrose@1957: } jrose@1957: } jrose@1957: jrose@1957: // This is a new unloaded instance. Create it and stick it in jrose@1957: // the cache. jrose@1957: ciInstance* new_instance = new (arena()) ciInstance(instance_klass); jrose@1957: jrose@1957: init_ident_of(new_instance); jrose@1957: _unloaded_instances->append(new_instance); jrose@1957: jrose@1957: // make sure it looks the way we want: jrose@1957: assert(!new_instance->is_loaded(), ""); jrose@1957: assert(new_instance->klass() == instance_klass, ""); jrose@1957: jrose@1957: return new_instance; jrose@1957: } jrose@1957: jrose@1957: jrose@1957: //------------------------------------------------------------------ jrose@1957: // ciObjectFactory::get_unloaded_klass_mirror jrose@1957: // jrose@1957: // Get a ciInstance representing an unresolved klass mirror. jrose@1957: // jrose@1957: // Currently, this ignores the parameters and returns a unique unloaded instance. jrose@1957: ciInstance* ciObjectFactory::get_unloaded_klass_mirror(ciKlass* type) { jrose@1957: assert(ciEnv::_Class_klass != NULL, ""); jrose@1957: return get_unloaded_instance(ciEnv::_Class_klass->as_instance_klass()); jrose@1957: } jrose@1957: jrose@1957: //------------------------------------------------------------------ jrose@1957: // ciObjectFactory::get_unloaded_method_handle_constant jrose@1957: // jrose@1957: // Get a ciInstance representing an unresolved method handle constant. jrose@1957: // jrose@1957: // Currently, this ignores the parameters and returns a unique unloaded instance. jrose@1957: ciInstance* ciObjectFactory::get_unloaded_method_handle_constant(ciKlass* holder, jrose@1957: ciSymbol* name, jrose@1957: ciSymbol* signature, jrose@1957: int ref_kind) { jrose@1957: if (ciEnv::_MethodHandle_klass == NULL) return NULL; jrose@1957: return get_unloaded_instance(ciEnv::_MethodHandle_klass->as_instance_klass()); jrose@1957: } jrose@1957: jrose@1957: //------------------------------------------------------------------ jrose@1957: // ciObjectFactory::get_unloaded_method_type_constant jrose@1957: // jrose@1957: // Get a ciInstance representing an unresolved method type constant. jrose@1957: // jrose@1957: // Currently, this ignores the parameters and returns a unique unloaded instance. jrose@1957: ciInstance* ciObjectFactory::get_unloaded_method_type_constant(ciSymbol* signature) { jrose@1957: if (ciEnv::_MethodType_klass == NULL) return NULL; jrose@1957: return get_unloaded_instance(ciEnv::_MethodType_klass->as_instance_klass()); jrose@1957: } jrose@1957: jrose@1957: jrose@1957: duke@435: //------------------------------------------------------------------ duke@435: // ciObjectFactory::get_empty_methodData duke@435: // duke@435: // Get the ciMethodData representing the methodData for a method with duke@435: // none. duke@435: ciMethodData* ciObjectFactory::get_empty_methodData() { duke@435: ciMethodData* new_methodData = new (arena()) ciMethodData(); duke@435: init_ident_of(new_methodData); duke@435: return new_methodData; duke@435: } duke@435: duke@435: //------------------------------------------------------------------ duke@435: // ciObjectFactory::get_return_address duke@435: // duke@435: // Get a ciReturnAddress for a specified bci. duke@435: ciReturnAddress* ciObjectFactory::get_return_address(int bci) { duke@435: for (int i=0; i<_return_addresses->length(); i++) { duke@435: ciReturnAddress* entry = _return_addresses->at(i); duke@435: if (entry->bci() == bci) { duke@435: // We've found a match. duke@435: return entry; duke@435: } duke@435: } duke@435: duke@435: ciReturnAddress* new_ret_addr = new (arena()) ciReturnAddress(bci); duke@435: init_ident_of(new_ret_addr); duke@435: _return_addresses->append(new_ret_addr); duke@435: return new_ret_addr; duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::init_ident_of coleenp@4037: void ciObjectFactory::init_ident_of(ciBaseObject* obj) { duke@435: obj->set_ident(_next_ident++); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::find duke@435: // duke@435: // Use binary search to find the position of this oop in the cache. duke@435: // If there is no entry in the cache corresponding to this oop, return duke@435: // the position at which the oop should be inserted. coleenp@4037: int ciObjectFactory::find(Metadata* key, GrowableArray* objects) { duke@435: int min = 0; duke@435: int max = objects->length()-1; duke@435: duke@435: // print_contents(); duke@435: duke@435: while (max >= min) { duke@435: int mid = (max + min) / 2; coleenp@4037: Metadata* value = objects->at(mid)->constant_encoding(); duke@435: if (value < key) { duke@435: min = mid + 1; duke@435: } else if (value > key) { duke@435: max = mid - 1; duke@435: } else { duke@435: return mid; duke@435: } duke@435: } duke@435: return min; duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::is_found_at duke@435: // duke@435: // Verify that the binary seach found the given key. coleenp@4037: bool ciObjectFactory::is_found_at(int index, Metadata* key, GrowableArray* objects) { duke@435: return (index < objects->length() && coleenp@4037: objects->at(index)->constant_encoding() == key); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::insert duke@435: // duke@435: // Insert a ciObject into the table at some index. coleenp@4037: void ciObjectFactory::insert(int index, ciMetadata* obj, GrowableArray* objects) { duke@435: int len = objects->length(); duke@435: if (len == index) { duke@435: objects->append(obj); duke@435: } else { duke@435: objects->append(objects->at(len-1)); duke@435: int pos; duke@435: for (pos = len-2; pos >= index; pos--) { duke@435: objects->at_put(pos+1,objects->at(pos)); duke@435: } duke@435: objects->at_put(index, obj); duke@435: } duke@435: } duke@435: duke@435: static ciObjectFactory::NonPermObject* emptyBucket = NULL; duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::find_non_perm duke@435: // duke@435: // Use a small hash table, hashed on the klass of the key. duke@435: // If there is no entry in the cache corresponding to this oop, return duke@435: // the null tail of the bucket into which the oop should be inserted. duke@435: ciObjectFactory::NonPermObject* &ciObjectFactory::find_non_perm(oop key) { coleenp@4037: assert(Universe::heap()->is_in_reserved_or_null(key), "must be"); coleenp@4037: ciMetadata* klass = get_metadata(key->klass()); duke@435: NonPermObject* *bp = &_non_perm_bucket[(unsigned) klass->hash() % NON_PERM_BUCKETS]; duke@435: for (NonPermObject* p; (p = (*bp)) != NULL; bp = &p->next()) { duke@435: if (is_equal(p, key)) break; duke@435: } duke@435: return (*bp); duke@435: } duke@435: duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // Code for for NonPermObject duke@435: // duke@435: inline ciObjectFactory::NonPermObject::NonPermObject(ciObjectFactory::NonPermObject* &bucket, oop key, ciObject* object) { duke@435: assert(ciObjectFactory::is_initialized(), ""); duke@435: _object = object; duke@435: _next = bucket; duke@435: bucket = this; duke@435: } duke@435: duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::insert_non_perm duke@435: // duke@435: // Insert a ciObject into the non-perm table. duke@435: void ciObjectFactory::insert_non_perm(ciObjectFactory::NonPermObject* &where, oop key, ciObject* obj) { coleenp@4037: assert(Universe::heap()->is_in_reserved_or_null(key), "must be"); duke@435: assert(&where != &emptyBucket, "must not try to fill empty bucket"); duke@435: NonPermObject* p = new (arena()) NonPermObject(where, key, obj); duke@435: assert(where == p && is_equal(p, key) && p->object() == obj, "entry must match"); duke@435: assert(find_non_perm(key) == p, "must find the same spot"); duke@435: ++_non_perm_count; duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::vm_symbol_at duke@435: // Get the ciSymbol corresponding to some index in vmSymbols. duke@435: ciSymbol* ciObjectFactory::vm_symbol_at(int index) { duke@435: assert(index >= vmSymbols::FIRST_SID && index < vmSymbols::SID_LIMIT, "oob"); duke@435: return _shared_ci_symbols[index]; duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ coleenp@4037: // ciObjectFactory::metadata_do coleenp@4037: void ciObjectFactory::metadata_do(void f(Metadata*)) { coleenp@4037: if (_ci_metadata == NULL) return; coleenp@4037: for (int j = 0; j< _ci_metadata->length(); j++) { coleenp@4037: Metadata* o = _ci_metadata->at(j)->constant_encoding(); coleenp@4037: f(o); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::print_contents_impl duke@435: void ciObjectFactory::print_contents_impl() { coleenp@4037: int len = _ci_metadata->length(); coleenp@4037: tty->print_cr("ciObjectFactory (%d) meta data contents:", len); duke@435: for (int i=0; iat(i)->print(); duke@435: tty->cr(); duke@435: } duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::print_contents duke@435: void ciObjectFactory::print_contents() { duke@435: print(); duke@435: tty->cr(); duke@435: GUARDED_VM_ENTRY(print_contents_impl();) duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // ciObjectFactory::print duke@435: // duke@435: // Print debugging information about the object factory duke@435: void ciObjectFactory::print() { coleenp@4037: tty->print("", coleenp@4037: _non_perm_count, _ci_metadata->length(), _unloaded_methods->length(), jrose@1957: _unloaded_instances->length(), duke@435: _unloaded_klasses->length()); duke@435: }