1 #ifdef USE_PRAGMA_IDENT_SRC 2 #pragma ident "@(#)ciInstanceKlass.cpp 1.45 07/09/28 10:23:23 JVM" 3 #endif 4 /* 5 * Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved. 6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7 * 8 * This code is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License version 2 only, as 10 * published by the Free Software Foundation. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 23 * CA 95054 USA or visit www.sun.com if you need additional information or 24 * have any questions. 25 * 26 */ 27 28 #include "incls/_precompiled.incl" 29 #include "incls/_ciInstanceKlass.cpp.incl" 30 31 // ciInstanceKlass 32 // 33 // This class represents a klassOop in the HotSpot virtual machine 34 // whose Klass part in an instanceKlass. 35 36 // ------------------------------------------------------------------ 37 // ciInstanceKlass::ciInstanceKlass 38 // 39 // Loaded instance klass. 40 ciInstanceKlass::ciInstanceKlass(KlassHandle h_k) : 41 ciKlass(h_k), _non_static_fields(NULL) 42 { 43 assert(get_Klass()->oop_is_instance(), "wrong type"); 44 instanceKlass* ik = get_instanceKlass(); 45 46 AccessFlags access_flags = ik->access_flags(); 47 _flags = ciFlags(access_flags); 48 _has_finalizer = access_flags.has_finalizer(); 49 _has_subklass = ik->subklass() != NULL; 50 _is_initialized = ik->is_initialized(); 51 // Next line must follow and use the result of the previous line: 52 _is_linked = _is_initialized || ik->is_linked(); 53 _nonstatic_field_size = ik->nonstatic_field_size(); 54 _has_nonstatic_fields = ik->has_nonstatic_fields(); 55 _nonstatic_fields = NULL; // initialized lazily by compute_nonstatic_fields: 56 57 _nof_implementors = ik->nof_implementors(); 58 for (int i = 0; i < implementors_limit; i++) { 59 _implementors[i] = NULL; // we will fill these lazily 60 } 61 62 Thread *thread = Thread::current(); 63 if (ciObjectFactory::is_initialized()) { 64 _loader = JNIHandles::make_local(thread, ik->class_loader()); 65 _protection_domain = JNIHandles::make_local(thread, 66 ik->protection_domain()); 67 _is_shared = false; 68 } else { 69 Handle h_loader(thread, ik->class_loader()); 70 Handle h_protection_domain(thread, ik->protection_domain()); 71 _loader = JNIHandles::make_global(h_loader); 72 _protection_domain = JNIHandles::make_global(h_protection_domain); 73 _is_shared = true; 74 } 75 76 // Lazy fields get filled in only upon request. 77 _super = NULL; 78 _java_mirror = NULL; 79 80 if (is_shared()) { 81 if (h_k() != SystemDictionary::object_klass()) { 82 super(); 83 } 84 java_mirror(); 85 //compute_nonstatic_fields(); // done outside of constructor 86 } 87 88 _field_cache = NULL; 89 } 90 91 // Version for unloaded classes: 92 ciInstanceKlass::ciInstanceKlass(ciSymbol* name, 93 jobject loader, jobject protection_domain) 94 : ciKlass(name, ciInstanceKlassKlass::make()) 95 { 96 assert(name->byte_at(0) != '[', "not an instance klass"); 97 _is_initialized = false; 98 _is_linked = false; 99 _nonstatic_field_size = -1; 100 _has_nonstatic_fields = false; 101 _nonstatic_fields = NULL; 102 _nof_implementors = -1; 103 _loader = loader; 104 _protection_domain = protection_domain; 105 _is_shared = false; 106 _super = NULL; 107 _java_mirror = NULL; 108 _field_cache = NULL; 109 } 110 111 112 113 // ------------------------------------------------------------------ 114 // ciInstanceKlass::compute_shared_is_initialized 115 bool ciInstanceKlass::compute_shared_is_initialized() { 116 GUARDED_VM_ENTRY( 117 instanceKlass* ik = get_instanceKlass(); 118 _is_initialized = ik->is_initialized(); 119 return _is_initialized; 120 ) 121 } 122 123 // ------------------------------------------------------------------ 124 // ciInstanceKlass::compute_shared_is_linked 125 bool ciInstanceKlass::compute_shared_is_linked() { 126 GUARDED_VM_ENTRY( 127 instanceKlass* ik = get_instanceKlass(); 128 _is_linked = ik->is_linked(); 129 return _is_linked; 130 ) 131 } 132 133 // ------------------------------------------------------------------ 134 // ciInstanceKlass::compute_shared_has_subklass 135 bool ciInstanceKlass::compute_shared_has_subklass() { 136 GUARDED_VM_ENTRY( 137 instanceKlass* ik = get_instanceKlass(); 138 _has_subklass = ik->subklass() != NULL; 139 return _has_subklass; 140 ) 141 } 142 143 // ------------------------------------------------------------------ 144 // ciInstanceKlass::compute_shared_nof_implementors 145 int ciInstanceKlass::compute_shared_nof_implementors() { 146 // We requery this property, since it is a very old ciObject. 147 GUARDED_VM_ENTRY( 148 instanceKlass* ik = get_instanceKlass(); 149 _nof_implementors = ik->nof_implementors(); 150 return _nof_implementors; 151 ) 152 } 153 154 // ------------------------------------------------------------------ 155 // ciInstanceKlass::loader 156 oop ciInstanceKlass::loader() { 157 ASSERT_IN_VM; 158 return JNIHandles::resolve(_loader); 159 } 160 161 // ------------------------------------------------------------------ 162 // ciInstanceKlass::loader_handle 163 jobject ciInstanceKlass::loader_handle() { 164 return _loader; 165 } 166 167 // ------------------------------------------------------------------ 168 // ciInstanceKlass::protection_domain 169 oop ciInstanceKlass::protection_domain() { 170 ASSERT_IN_VM; 171 return JNIHandles::resolve(_protection_domain); 172 } 173 174 // ------------------------------------------------------------------ 175 // ciInstanceKlass::protection_domain_handle 176 jobject ciInstanceKlass::protection_domain_handle() { 177 return _protection_domain; 178 } 179 180 // ------------------------------------------------------------------ 181 // ciInstanceKlass::field_cache 182 // 183 // Get the field cache associated with this klass. 184 ciConstantPoolCache* ciInstanceKlass::field_cache() { 185 if (is_shared()) { 186 return NULL; 187 } 188 if (_field_cache == NULL) { 189 assert(!is_java_lang_Object(), "Object has no fields"); 190 Arena* arena = CURRENT_ENV->arena(); 191 _field_cache = new (arena) ciConstantPoolCache(arena, 5); 192 } 193 return _field_cache; 194 } 195 196 // ------------------------------------------------------------------ 197 // ciInstanceKlass::get_canonical_holder 198 // 199 ciInstanceKlass* ciInstanceKlass::get_canonical_holder(int offset) { 200 #ifdef ASSERT 201 if (!(offset >= 0 && offset < layout_helper())) { 202 tty->print("*** get_canonical_holder(%d) on ", offset); 203 this->print(); 204 tty->print_cr(" ***"); 205 }; 206 assert(offset >= 0 && offset < layout_helper(), "offset must be tame"); 207 #endif 208 209 if (offset < instanceOopDesc::base_offset_in_bytes()) { 210 // All header offsets belong properly to java/lang/Object. 211 return CURRENT_ENV->Object_klass(); 212 } 213 214 ciInstanceKlass* self = this; 215 for (;;) { 216 assert(self->is_loaded(), "must be loaded to have size"); 217 ciInstanceKlass* super = self->super(); 218 if (super == NULL || super->nof_nonstatic_fields() == 0 || 219 !super->contains_field_offset(offset)) { 220 return self; 221 } else { 222 self = super; // return super->get_canonical_holder(offset) 223 } 224 } 225 } 226 227 // ------------------------------------------------------------------ 228 // ciInstanceKlass::is_java_lang_Object 229 // 230 // Is this klass java.lang.Object? 231 bool ciInstanceKlass::is_java_lang_Object() { 232 return equals(CURRENT_ENV->Object_klass()); 233 } 234 235 // ------------------------------------------------------------------ 236 // ciInstanceKlass::uses_default_loader 237 bool ciInstanceKlass::uses_default_loader() { 238 VM_ENTRY_MARK; 239 return loader() == NULL; 240 } 241 242 // ------------------------------------------------------------------ 243 // ciInstanceKlass::print_impl 244 // 245 // Implementation of the print method. 246 void ciInstanceKlass::print_impl(outputStream* st) { 247 ciKlass::print_impl(st); 248 GUARDED_VM_ENTRY(st->print(" loader=0x%x", (address)loader());) 249 if (is_loaded()) { 250 st->print(" loaded=true initialized=%s finalized=%s subklass=%s size=%d flags=", 251 bool_to_str(is_initialized()), 252 bool_to_str(has_finalizer()), 253 bool_to_str(has_subklass()), 254 layout_helper()); 255 256 _flags.print_klass_flags(); 257 258 if (_super) { 259 st->print(" super="); 260 _super->print_name(); 261 } 262 if (_java_mirror) { 263 st->print(" mirror=PRESENT"); 264 } 265 } else { 266 st->print(" loaded=false"); 267 } 268 } 269 270 // ------------------------------------------------------------------ 271 // ciInstanceKlass::super 272 // 273 // Get the superklass of this klass. 274 ciInstanceKlass* ciInstanceKlass::super() { 275 assert(is_loaded(), "must be loaded"); 276 if (_super == NULL && !is_java_lang_Object()) { 277 GUARDED_VM_ENTRY( 278 klassOop super_klass = get_instanceKlass()->super(); 279 _super = CURRENT_ENV->get_object(super_klass)->as_instance_klass(); 280 ) 281 } 282 return _super; 283 } 284 285 // ------------------------------------------------------------------ 286 // ciInstanceKlass::java_mirror 287 // 288 // Get the instance of java.lang.Class corresponding to this klass. 289 ciInstance* ciInstanceKlass::java_mirror() { 290 assert(is_loaded(), "must be loaded"); 291 if (_java_mirror == NULL) { 292 _java_mirror = ciKlass::java_mirror(); 293 } 294 return _java_mirror; 295 } 296 297 // ------------------------------------------------------------------ 298 // ciInstanceKlass::unique_concrete_subklass 299 ciInstanceKlass* ciInstanceKlass::unique_concrete_subklass() { 300 if (!is_loaded()) return NULL; // No change if class is not loaded 301 if (!is_abstract()) return NULL; // Only applies to abstract classes. 302 if (!has_subklass()) return NULL; // Must have at least one subklass. 303 VM_ENTRY_MARK; 304 instanceKlass* ik = get_instanceKlass(); 305 Klass* up = ik->up_cast_abstract(); 306 assert(up->oop_is_instance(), "must be instanceKlass"); 307 if (ik == up) { 308 return NULL; 309 } 310 return CURRENT_THREAD_ENV->get_object(up->as_klassOop())->as_instance_klass(); 311 } 312 313 // ------------------------------------------------------------------ 314 // ciInstanceKlass::has_finalizable_subclass 315 bool ciInstanceKlass::has_finalizable_subclass() { 316 if (!is_loaded()) return true; 317 VM_ENTRY_MARK; 318 return Dependencies::find_finalizable_subclass(get_instanceKlass()) != NULL; 319 } 320 321 // ------------------------------------------------------------------ 322 // ciInstanceKlass::get_field_by_offset 323 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) { 324 if (!is_static) { 325 for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) { 326 ciField* field = _nonstatic_fields->at(i); 327 int field_off = field->offset_in_bytes(); 328 if (field_off == field_offset) 329 return field; 330 if (field_off > field_offset) 331 break; 332 // could do binary search or check bins, but probably not worth it 333 } 334 return NULL; 335 } 336 VM_ENTRY_MARK; 337 instanceKlass* k = get_instanceKlass(); 338 fieldDescriptor fd; 339 if (!k->find_field_from_offset(field_offset, is_static, &fd)) { 340 return NULL; 341 } 342 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd); 343 return field; 344 } 345 346 // ------------------------------------------------------------------ 347 // ciInstanceKlass::non_static_fields. 348 349 class NonStaticFieldFiller: public FieldClosure { 350 GrowableArray<ciField*>* _arr; 351 ciEnv* _curEnv; 352 public: 353 NonStaticFieldFiller(ciEnv* curEnv, GrowableArray<ciField*>* arr) : 354 _curEnv(curEnv), _arr(arr) 355 {} 356 void do_field(fieldDescriptor* fd) { 357 ciField* field = new (_curEnv->arena()) ciField(fd); 358 _arr->append(field); 359 } 360 }; 361 362 GrowableArray<ciField*>* ciInstanceKlass::non_static_fields() { 363 if (_non_static_fields == NULL) { 364 VM_ENTRY_MARK; 365 ciEnv* curEnv = ciEnv::current(); 366 instanceKlass* ik = get_instanceKlass(); 367 int max_n_fields = ik->fields()->length()/instanceKlass::next_offset; 368 369 _non_static_fields = 370 new (curEnv->arena()) GrowableArray<ciField*>(max_n_fields); 371 NonStaticFieldFiller filler(curEnv, _non_static_fields); 372 ik->do_nonstatic_fields(&filler); 373 } 374 return _non_static_fields; 375 } 376 377 static int sort_field_by_offset(ciField** a, ciField** b) { 378 return (*a)->offset_in_bytes() - (*b)->offset_in_bytes(); 379 // (no worries about 32-bit overflow...) 380 } 381 382 // ------------------------------------------------------------------ 383 // ciInstanceKlass::compute_nonstatic_fields 384 int ciInstanceKlass::compute_nonstatic_fields() { 385 assert(is_loaded(), "must be loaded"); 386 387 if (_nonstatic_fields != NULL) 388 return _nonstatic_fields->length(); 389 390 if (!has_nonstatic_fields()) { 391 Arena* arena = CURRENT_ENV->arena(); 392 _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL); 393 return 0; 394 } 395 assert(!is_java_lang_Object(), "bootstrap OK"); 396 397 // Size in bytes of my fields, including inherited fields. 398 int fsize = nonstatic_field_size() * heapOopSize; 399 400 ciInstanceKlass* super = this->super(); 401 GrowableArray<ciField*>* super_fields = NULL; 402 if (super != NULL && super->has_nonstatic_fields()) { 403 int super_fsize = super->nonstatic_field_size() * heapOopSize; 404 int super_flen = super->nof_nonstatic_fields(); 405 super_fields = super->_nonstatic_fields; 406 assert(super_flen == 0 || super_fields != NULL, "first get nof_fields"); 407 // See if I am no larger than my super; if so, I can use his fields. 408 if (fsize == super_fsize) { 409 _nonstatic_fields = super_fields; 410 return super_fields->length(); 411 } 412 } 413 414 GrowableArray<ciField*>* fields = NULL; 415 GUARDED_VM_ENTRY({ 416 fields = compute_nonstatic_fields_impl(super_fields); 417 }); 418 419 if (fields == NULL) { 420 // This can happen if this class (java.lang.Class) has invisible fields. 421 _nonstatic_fields = super_fields; 422 return super_fields->length(); 423 } 424 425 int flen = fields->length(); 426 427 // Now sort them by offset, ascending. 428 // (In principle, they could mix with superclass fields.) 429 fields->sort(sort_field_by_offset); 430 #ifdef ASSERT 431 int last_offset = instanceOopDesc::base_offset_in_bytes(); 432 for (int i = 0; i < fields->length(); i++) { 433 ciField* field = fields->at(i); 434 int offset = field->offset_in_bytes(); 435 int size = (field->_type == NULL) ? heapOopSize : field->size_in_bytes(); 436 assert(last_offset <= offset, "no field overlap"); 437 if (last_offset > (int)sizeof(oopDesc)) 438 assert((offset - last_offset) < BytesPerLong, "no big holes"); 439 // Note: Two consecutive T_BYTE fields will be separated by wordSize-1 440 // padding bytes if one of them is declared by a superclass. 441 // This is a minor inefficiency classFileParser.cpp. 442 last_offset = offset + size; 443 } 444 assert(last_offset <= (int)instanceOopDesc::base_offset_in_bytes() + fsize, "no overflow"); 445 #endif 446 447 _nonstatic_fields = fields; 448 return flen; 449 } 450 451 GrowableArray<ciField*>* 452 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>* 453 super_fields) { 454 ASSERT_IN_VM; 455 Arena* arena = CURRENT_ENV->arena(); 456 int flen = 0; 457 GrowableArray<ciField*>* fields = NULL; 458 instanceKlass* k = get_instanceKlass(); 459 typeArrayOop fields_array = k->fields(); 460 for (int pass = 0; pass <= 1; pass++) { 461 for (int i = 0, alen = fields_array->length(); i < alen; i += instanceKlass::next_offset) { 462 fieldDescriptor fd; 463 fd.initialize(k->as_klassOop(), i); 464 if (fd.is_static()) continue; 465 if (pass == 0) { 466 flen += 1; 467 } else { 468 ciField* field = new (arena) ciField(&fd); 469 fields->append(field); 470 } 471 } 472 473 // Between passes, allocate the array: 474 if (pass == 0) { 475 if (flen == 0) { 476 return NULL; // return nothing if none are locally declared 477 } 478 if (super_fields != NULL) { 479 flen += super_fields->length(); 480 } 481 fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, NULL); 482 if (super_fields != NULL) { 483 fields->appendAll(super_fields); 484 } 485 } 486 } 487 assert(fields->length() == flen, "sanity"); 488 return fields; 489 } 490 491 // ------------------------------------------------------------------ 492 // ciInstanceKlass::find_method 493 // 494 // Find a method in this klass. 495 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) { 496 VM_ENTRY_MARK; 497 instanceKlass* k = get_instanceKlass(); 498 symbolOop name_sym = name->get_symbolOop(); 499 symbolOop sig_sym= signature->get_symbolOop(); 500 501 methodOop m = k->find_method(name_sym, sig_sym); 502 if (m == NULL) return NULL; 503 504 return CURRENT_THREAD_ENV->get_object(m)->as_method(); 505 } 506 507 // ------------------------------------------------------------------ 508 // ciInstanceKlass::is_leaf_type 509 bool ciInstanceKlass::is_leaf_type() { 510 assert(is_loaded(), "must be loaded"); 511 if (is_shared()) { 512 return is_final(); // approximately correct 513 } else { 514 return !_has_subklass && (_nof_implementors == 0); 515 } 516 } 517 518 // ------------------------------------------------------------------ 519 // ciInstanceKlass::implementor 520 // 521 // Report an implementor of this interface. 522 // Returns NULL if exact information is not available. 523 // Note that there are various races here, since my copy 524 // of _nof_implementors might be out of date with respect 525 // to results returned by instanceKlass::implementor. 526 // This is OK, since any dependencies we decide to assert 527 // will be checked later under the Compile_lock. 528 ciInstanceKlass* ciInstanceKlass::implementor(int n) { 529 if (n > implementors_limit) { 530 return NULL; 531 } 532 ciInstanceKlass* impl = _implementors[n]; 533 if (impl == NULL) { 534 if (_nof_implementors > implementors_limit) { 535 return NULL; 536 } 537 // Go into the VM to fetch the implementor. 538 { 539 VM_ENTRY_MARK; 540 klassOop k = get_instanceKlass()->implementor(n); 541 if (k != NULL) { 542 impl = CURRENT_THREAD_ENV->get_object(k)->as_instance_klass(); 543 } 544 } 545 // Memoize this result. 546 if (!is_shared()) { 547 _implementors[n] = (impl == NULL)? this: impl; 548 } 549 } else if (impl == this) { 550 impl = NULL; // memoized null result from a VM query 551 } 552 return impl; 553 }