src/share/vm/ci/ciObjectFactory.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6198
55fb97c4c58d
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

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

mercurial