1 /* 2 * Copyright 1999-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 25 #include "incls/_precompiled.incl" 26 #include "incls/_ciEnv.cpp.incl" 27 28 // ciEnv 29 // 30 // This class is the top level broker for requests from the compiler 31 // to the VM. 32 33 ciObject* ciEnv::_null_object_instance; 34 ciMethodKlass* ciEnv::_method_klass_instance; 35 ciSymbolKlass* ciEnv::_symbol_klass_instance; 36 ciKlassKlass* ciEnv::_klass_klass_instance; 37 ciInstanceKlassKlass* ciEnv::_instance_klass_klass_instance; 38 ciTypeArrayKlassKlass* ciEnv::_type_array_klass_klass_instance; 39 ciObjArrayKlassKlass* ciEnv::_obj_array_klass_klass_instance; 40 41 ciInstanceKlass* ciEnv::_ArrayStoreException; 42 ciInstanceKlass* ciEnv::_Class; 43 ciInstanceKlass* ciEnv::_ClassCastException; 44 ciInstanceKlass* ciEnv::_InvokeDynamic; 45 ciInstanceKlass* ciEnv::_Object; 46 ciInstanceKlass* ciEnv::_Throwable; 47 ciInstanceKlass* ciEnv::_Thread; 48 ciInstanceKlass* ciEnv::_OutOfMemoryError; 49 ciInstanceKlass* ciEnv::_String; 50 51 ciSymbol* ciEnv::_unloaded_cisymbol = NULL; 52 ciInstanceKlass* ciEnv::_unloaded_ciinstance_klass = NULL; 53 ciObjArrayKlass* ciEnv::_unloaded_ciobjarrayklass = NULL; 54 55 jobject ciEnv::_ArrayIndexOutOfBoundsException_handle = NULL; 56 jobject ciEnv::_ArrayStoreException_handle = NULL; 57 jobject ciEnv::_ClassCastException_handle = NULL; 58 59 #ifndef PRODUCT 60 static bool firstEnv = true; 61 #endif /* PRODUCT */ 62 63 // ------------------------------------------------------------------ 64 // ciEnv::ciEnv 65 ciEnv::ciEnv(CompileTask* task, int system_dictionary_modification_counter) { 66 VM_ENTRY_MARK; 67 68 // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc. 69 thread->set_env(this); 70 assert(ciEnv::current() == this, "sanity"); 71 72 _oop_recorder = NULL; 73 _debug_info = NULL; 74 _dependencies = NULL; 75 _failure_reason = NULL; 76 _compilable = MethodCompilable; 77 _break_at_compile = false; 78 _compiler_data = NULL; 79 #ifndef PRODUCT 80 assert(!firstEnv, "not initialized properly"); 81 #endif /* !PRODUCT */ 82 83 _system_dictionary_modification_counter = system_dictionary_modification_counter; 84 _num_inlined_bytecodes = 0; 85 assert(task == NULL || thread->task() == task, "sanity"); 86 _task = task; 87 _log = NULL; 88 89 // Temporary buffer for creating symbols and such. 90 _name_buffer = NULL; 91 _name_buffer_len = 0; 92 93 _arena = &_ciEnv_arena; 94 _factory = new (_arena) ciObjectFactory(_arena, 128); 95 96 // Preload commonly referenced system ciObjects. 97 98 // During VM initialization, these instances have not yet been created. 99 // Assertions ensure that these instances are not accessed before 100 // their initialization. 101 102 assert(Universe::is_fully_initialized(), "should be complete"); 103 104 oop o = Universe::null_ptr_exception_instance(); 105 assert(o != NULL, "should have been initialized"); 106 _NullPointerException_instance = get_object(o)->as_instance(); 107 o = Universe::arithmetic_exception_instance(); 108 assert(o != NULL, "should have been initialized"); 109 _ArithmeticException_instance = get_object(o)->as_instance(); 110 111 _ArrayIndexOutOfBoundsException_instance = NULL; 112 _ArrayStoreException_instance = NULL; 113 _ClassCastException_instance = NULL; 114 } 115 116 ciEnv::ciEnv(Arena* arena) { 117 ASSERT_IN_VM; 118 119 // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc. 120 CompilerThread* current_thread = CompilerThread::current(); 121 assert(current_thread->env() == NULL, "must be"); 122 current_thread->set_env(this); 123 assert(ciEnv::current() == this, "sanity"); 124 125 _oop_recorder = NULL; 126 _debug_info = NULL; 127 _dependencies = NULL; 128 _failure_reason = NULL; 129 _compilable = MethodCompilable_never; 130 _break_at_compile = false; 131 _compiler_data = NULL; 132 #ifndef PRODUCT 133 assert(firstEnv, "must be first"); 134 firstEnv = false; 135 #endif /* !PRODUCT */ 136 137 _system_dictionary_modification_counter = 0; 138 _num_inlined_bytecodes = 0; 139 _task = NULL; 140 _log = NULL; 141 142 // Temporary buffer for creating symbols and such. 143 _name_buffer = NULL; 144 _name_buffer_len = 0; 145 146 _arena = arena; 147 _factory = new (_arena) ciObjectFactory(_arena, 128); 148 149 // Preload commonly referenced system ciObjects. 150 151 // During VM initialization, these instances have not yet been created. 152 // Assertions ensure that these instances are not accessed before 153 // their initialization. 154 155 assert(Universe::is_fully_initialized(), "must be"); 156 157 oop o = Universe::null_ptr_exception_instance(); 158 assert(o != NULL, "should have been initialized"); 159 _NullPointerException_instance = get_object(o)->as_instance(); 160 o = Universe::arithmetic_exception_instance(); 161 assert(o != NULL, "should have been initialized"); 162 _ArithmeticException_instance = get_object(o)->as_instance(); 163 164 _ArrayIndexOutOfBoundsException_instance = NULL; 165 _ArrayStoreException_instance = NULL; 166 _ClassCastException_instance = NULL; 167 } 168 169 ciEnv::~ciEnv() { 170 CompilerThread* current_thread = CompilerThread::current(); 171 current_thread->set_env(NULL); 172 } 173 174 // ------------------------------------------------------------------ 175 // Cache Jvmti state 176 void ciEnv::cache_jvmti_state() { 177 VM_ENTRY_MARK; 178 // Get Jvmti capabilities under lock to get consistant values. 179 MutexLocker mu(JvmtiThreadState_lock); 180 _jvmti_can_hotswap_or_post_breakpoint = JvmtiExport::can_hotswap_or_post_breakpoint(); 181 _jvmti_can_examine_or_deopt_anywhere = JvmtiExport::can_examine_or_deopt_anywhere(); 182 _jvmti_can_access_local_variables = JvmtiExport::can_access_local_variables(); 183 _jvmti_can_post_exceptions = JvmtiExport::can_post_exceptions(); 184 } 185 186 // ------------------------------------------------------------------ 187 // Cache DTrace flags 188 void ciEnv::cache_dtrace_flags() { 189 // Need lock? 190 _dtrace_extended_probes = ExtendedDTraceProbes; 191 if (_dtrace_extended_probes) { 192 _dtrace_monitor_probes = true; 193 _dtrace_method_probes = true; 194 _dtrace_alloc_probes = true; 195 } else { 196 _dtrace_monitor_probes = DTraceMonitorProbes; 197 _dtrace_method_probes = DTraceMethodProbes; 198 _dtrace_alloc_probes = DTraceAllocProbes; 199 } 200 } 201 202 // ------------------------------------------------------------------ 203 // helper for lazy exception creation 204 ciInstance* ciEnv::get_or_create_exception(jobject& handle, symbolHandle name) { 205 VM_ENTRY_MARK; 206 if (handle == NULL) { 207 // Cf. universe.cpp, creation of Universe::_null_ptr_exception_instance. 208 klassOop k = SystemDictionary::find(name, Handle(), Handle(), THREAD); 209 jobject objh = NULL; 210 if (!HAS_PENDING_EXCEPTION && k != NULL) { 211 oop obj = instanceKlass::cast(k)->allocate_permanent_instance(THREAD); 212 if (!HAS_PENDING_EXCEPTION) 213 objh = JNIHandles::make_global(obj); 214 } 215 if (HAS_PENDING_EXCEPTION) { 216 CLEAR_PENDING_EXCEPTION; 217 } else { 218 handle = objh; 219 } 220 } 221 oop obj = JNIHandles::resolve(handle); 222 return obj == NULL? NULL: get_object(obj)->as_instance(); 223 } 224 225 // ------------------------------------------------------------------ 226 // ciEnv::ArrayIndexOutOfBoundsException_instance, etc. 227 ciInstance* ciEnv::ArrayIndexOutOfBoundsException_instance() { 228 if (_ArrayIndexOutOfBoundsException_instance == NULL) { 229 _ArrayIndexOutOfBoundsException_instance 230 = get_or_create_exception(_ArrayIndexOutOfBoundsException_handle, 231 vmSymbolHandles::java_lang_ArrayIndexOutOfBoundsException()); 232 } 233 return _ArrayIndexOutOfBoundsException_instance; 234 } 235 ciInstance* ciEnv::ArrayStoreException_instance() { 236 if (_ArrayStoreException_instance == NULL) { 237 _ArrayStoreException_instance 238 = get_or_create_exception(_ArrayStoreException_handle, 239 vmSymbolHandles::java_lang_ArrayStoreException()); 240 } 241 return _ArrayStoreException_instance; 242 } 243 ciInstance* ciEnv::ClassCastException_instance() { 244 if (_ClassCastException_instance == NULL) { 245 _ClassCastException_instance 246 = get_or_create_exception(_ClassCastException_handle, 247 vmSymbolHandles::java_lang_ClassCastException()); 248 } 249 return _ClassCastException_instance; 250 } 251 252 // ------------------------------------------------------------------ 253 // ciEnv::get_method_from_handle 254 ciMethod* ciEnv::get_method_from_handle(jobject method) { 255 VM_ENTRY_MARK; 256 return get_object(JNIHandles::resolve(method))->as_method(); 257 } 258 259 // ------------------------------------------------------------------ 260 // ciEnv::make_array 261 ciArray* ciEnv::make_system_array(GrowableArray<ciObject*>* objects) { 262 VM_ENTRY_MARK; 263 int length = objects->length(); 264 objArrayOop a = oopFactory::new_system_objArray(length, THREAD); 265 if (HAS_PENDING_EXCEPTION) { 266 CLEAR_PENDING_EXCEPTION; 267 record_out_of_memory_failure(); 268 return NULL; 269 } 270 for (int i = 0; i < length; i++) { 271 a->obj_at_put(i, objects->at(i)->get_oop()); 272 } 273 assert(a->is_perm(), ""); 274 return get_object(a)->as_array(); 275 } 276 277 278 // ------------------------------------------------------------------ 279 // ciEnv::array_element_offset_in_bytes 280 int ciEnv::array_element_offset_in_bytes(ciArray* a_h, ciObject* o_h) { 281 VM_ENTRY_MARK; 282 objArrayOop a = (objArrayOop)a_h->get_oop(); 283 assert(a->is_objArray(), ""); 284 int length = a->length(); 285 oop o = o_h->get_oop(); 286 for (int i = 0; i < length; i++) { 287 if (a->obj_at(i) == o) return i; 288 } 289 return -1; 290 } 291 292 293 // ------------------------------------------------------------------ 294 // ciEnv::check_klass_accessiblity 295 // 296 // Note: the logic of this method should mirror the logic of 297 // constantPoolOopDesc::verify_constant_pool_resolve. 298 bool ciEnv::check_klass_accessibility(ciKlass* accessing_klass, 299 klassOop resolved_klass) { 300 if (accessing_klass == NULL || !accessing_klass->is_loaded()) { 301 return true; 302 } 303 if (accessing_klass->is_obj_array()) { 304 accessing_klass = accessing_klass->as_obj_array_klass()->base_element_klass(); 305 } 306 if (!accessing_klass->is_instance_klass()) { 307 return true; 308 } 309 310 if (resolved_klass->klass_part()->oop_is_objArray()) { 311 // Find the element klass, if this is an array. 312 resolved_klass = objArrayKlass::cast(resolved_klass)->bottom_klass(); 313 } 314 if (resolved_klass->klass_part()->oop_is_instance()) { 315 return Reflection::verify_class_access(accessing_klass->get_klassOop(), 316 resolved_klass, 317 true); 318 } 319 return true; 320 } 321 322 // ------------------------------------------------------------------ 323 // ciEnv::get_klass_by_name_impl 324 ciKlass* ciEnv::get_klass_by_name_impl(ciKlass* accessing_klass, 325 ciSymbol* name, 326 bool require_local) { 327 ASSERT_IN_VM; 328 EXCEPTION_CONTEXT; 329 330 // Now we need to check the SystemDictionary 331 symbolHandle sym(THREAD, name->get_symbolOop()); 332 if (sym->byte_at(0) == 'L' && 333 sym->byte_at(sym->utf8_length()-1) == ';') { 334 // This is a name from a signature. Strip off the trimmings. 335 sym = oopFactory::new_symbol_handle(sym->as_utf8()+1, 336 sym->utf8_length()-2, 337 KILL_COMPILE_ON_FATAL_(_unloaded_ciinstance_klass)); 338 name = get_object(sym())->as_symbol(); 339 } 340 341 // Check for prior unloaded klass. The SystemDictionary's answers 342 // can vary over time but the compiler needs consistency. 343 ciKlass* unloaded_klass = check_get_unloaded_klass(accessing_klass, name); 344 if (unloaded_klass != NULL) { 345 if (require_local) return NULL; 346 return unloaded_klass; 347 } 348 349 Handle loader(THREAD, (oop)NULL); 350 Handle domain(THREAD, (oop)NULL); 351 if (accessing_klass != NULL) { 352 loader = Handle(THREAD, accessing_klass->loader()); 353 domain = Handle(THREAD, accessing_klass->protection_domain()); 354 } 355 356 // setup up the proper type to return on OOM 357 ciKlass* fail_type; 358 if (sym->byte_at(0) == '[') { 359 fail_type = _unloaded_ciobjarrayklass; 360 } else { 361 fail_type = _unloaded_ciinstance_klass; 362 } 363 klassOop found_klass; 364 if (!require_local) { 365 found_klass = 366 SystemDictionary::find_constrained_instance_or_array_klass(sym, loader, 367 KILL_COMPILE_ON_FATAL_(fail_type)); 368 } else { 369 found_klass = 370 SystemDictionary::find_instance_or_array_klass(sym, loader, domain, 371 KILL_COMPILE_ON_FATAL_(fail_type)); 372 } 373 374 if (found_klass != NULL) { 375 // Found it. Build a CI handle. 376 return get_object(found_klass)->as_klass(); 377 } 378 379 // If we fail to find an array klass, look again for its element type. 380 // The element type may be available either locally or via constraints. 381 // In either case, if we can find the element type in the system dictionary, 382 // we must build an array type around it. The CI requires array klasses 383 // to be loaded if their element klasses are loaded, except when memory 384 // is exhausted. 385 if (sym->byte_at(0) == '[' && 386 (sym->byte_at(1) == '[' || sym->byte_at(1) == 'L')) { 387 // We have an unloaded array. 388 // Build it on the fly if the element class exists. 389 symbolOop elem_sym = oopFactory::new_symbol(sym->as_utf8()+1, 390 sym->utf8_length()-1, 391 KILL_COMPILE_ON_FATAL_(fail_type)); 392 // Get element ciKlass recursively. 393 ciKlass* elem_klass = 394 get_klass_by_name_impl(accessing_klass, 395 get_object(elem_sym)->as_symbol(), 396 require_local); 397 if (elem_klass != NULL && elem_klass->is_loaded()) { 398 // Now make an array for it 399 return ciObjArrayKlass::make_impl(elem_klass); 400 } 401 } 402 403 if (require_local) return NULL; 404 // Not yet loaded into the VM, or not governed by loader constraints. 405 // Make a CI representative for it. 406 return get_unloaded_klass(accessing_klass, name); 407 } 408 409 // ------------------------------------------------------------------ 410 // ciEnv::get_klass_by_name 411 ciKlass* ciEnv::get_klass_by_name(ciKlass* accessing_klass, 412 ciSymbol* klass_name, 413 bool require_local) { 414 GUARDED_VM_ENTRY(return get_klass_by_name_impl(accessing_klass, 415 klass_name, 416 require_local);) 417 } 418 419 // ------------------------------------------------------------------ 420 // ciEnv::get_klass_by_index_impl 421 // 422 // Implementation of get_klass_by_index. 423 ciKlass* ciEnv::get_klass_by_index_impl(constantPoolHandle cpool, 424 int index, 425 bool& is_accessible, 426 ciInstanceKlass* accessor) { 427 EXCEPTION_CONTEXT; 428 KlassHandle klass (THREAD, constantPoolOopDesc::klass_at_if_loaded(cpool, index)); 429 symbolHandle klass_name; 430 if (klass.is_null()) { 431 // The klass has not been inserted into the constant pool. 432 // Try to look it up by name. 433 { 434 // We have to lock the cpool to keep the oop from being resolved 435 // while we are accessing it. 436 ObjectLocker ol(cpool, THREAD); 437 438 constantTag tag = cpool->tag_at(index); 439 if (tag.is_klass()) { 440 // The klass has been inserted into the constant pool 441 // very recently. 442 klass = KlassHandle(THREAD, cpool->resolved_klass_at(index)); 443 } else if (tag.is_symbol()) { 444 klass_name = symbolHandle(THREAD, cpool->symbol_at(index)); 445 } else { 446 assert(cpool->tag_at(index).is_unresolved_klass(), "wrong tag"); 447 klass_name = symbolHandle(THREAD, cpool->unresolved_klass_at(index)); 448 } 449 } 450 } 451 452 if (klass.is_null()) { 453 // Not found in constant pool. Use the name to do the lookup. 454 ciKlass* k = get_klass_by_name_impl(accessor, 455 get_object(klass_name())->as_symbol(), 456 false); 457 // Calculate accessibility the hard way. 458 if (!k->is_loaded()) { 459 is_accessible = false; 460 } else if (k->loader() != accessor->loader() && 461 get_klass_by_name_impl(accessor, k->name(), true) == NULL) { 462 // Loaded only remotely. Not linked yet. 463 is_accessible = false; 464 } else { 465 // Linked locally, and we must also check public/private, etc. 466 is_accessible = check_klass_accessibility(accessor, k->get_klassOop()); 467 } 468 return k; 469 } 470 471 // Check for prior unloaded klass. The SystemDictionary's answers 472 // can vary over time but the compiler needs consistency. 473 ciSymbol* name = get_object(klass()->klass_part()->name())->as_symbol(); 474 ciKlass* unloaded_klass = check_get_unloaded_klass(accessor, name); 475 if (unloaded_klass != NULL) { 476 is_accessible = false; 477 return unloaded_klass; 478 } 479 480 // It is known to be accessible, since it was found in the constant pool. 481 is_accessible = true; 482 return get_object(klass())->as_klass(); 483 } 484 485 // ------------------------------------------------------------------ 486 // ciEnv::get_klass_by_index 487 // 488 // Get a klass from the constant pool. 489 ciKlass* ciEnv::get_klass_by_index(constantPoolHandle cpool, 490 int index, 491 bool& is_accessible, 492 ciInstanceKlass* accessor) { 493 GUARDED_VM_ENTRY(return get_klass_by_index_impl(cpool, index, is_accessible, accessor);) 494 } 495 496 // ------------------------------------------------------------------ 497 // ciEnv::get_constant_by_index_impl 498 // 499 // Implementation of get_constant_by_index(). 500 ciConstant ciEnv::get_constant_by_index_impl(constantPoolHandle cpool, 501 int index, 502 ciInstanceKlass* accessor) { 503 EXCEPTION_CONTEXT; 504 constantTag tag = cpool->tag_at(index); 505 if (tag.is_int()) { 506 return ciConstant(T_INT, (jint)cpool->int_at(index)); 507 } else if (tag.is_long()) { 508 return ciConstant((jlong)cpool->long_at(index)); 509 } else if (tag.is_float()) { 510 return ciConstant((jfloat)cpool->float_at(index)); 511 } else if (tag.is_double()) { 512 return ciConstant((jdouble)cpool->double_at(index)); 513 } else if (tag.is_string() || tag.is_unresolved_string()) { 514 oop string = NULL; 515 if (cpool->is_pseudo_string_at(index)) { 516 string = cpool->pseudo_string_at(index); 517 } else { 518 string = cpool->string_at(index, THREAD); 519 if (HAS_PENDING_EXCEPTION) { 520 CLEAR_PENDING_EXCEPTION; 521 record_out_of_memory_failure(); 522 return ciConstant(); 523 } 524 } 525 ciObject* constant = get_object(string); 526 assert (constant->is_instance(), "must be an instance, or not? "); 527 return ciConstant(T_OBJECT, constant); 528 } else if (tag.is_klass() || tag.is_unresolved_klass()) { 529 // 4881222: allow ldc to take a class type 530 bool ignore; 531 ciKlass* klass = get_klass_by_index_impl(cpool, index, ignore, accessor); 532 if (HAS_PENDING_EXCEPTION) { 533 CLEAR_PENDING_EXCEPTION; 534 record_out_of_memory_failure(); 535 return ciConstant(); 536 } 537 assert (klass->is_instance_klass() || klass->is_array_klass(), 538 "must be an instance or array klass "); 539 return ciConstant(T_OBJECT, klass); 540 } else if (tag.is_object()) { 541 oop obj = cpool->object_at(index); 542 assert(obj->is_instance(), "must be an instance"); 543 ciObject* ciobj = get_object(obj); 544 return ciConstant(T_OBJECT, ciobj); 545 } else { 546 ShouldNotReachHere(); 547 return ciConstant(); 548 } 549 } 550 551 // ------------------------------------------------------------------ 552 // ciEnv::is_unresolved_string_impl 553 // 554 // Implementation of is_unresolved_string(). 555 bool ciEnv::is_unresolved_string_impl(instanceKlass* accessor, int index) const { 556 EXCEPTION_CONTEXT; 557 assert(accessor->is_linked(), "must be linked before accessing constant pool"); 558 constantPoolOop cpool = accessor->constants(); 559 constantTag tag = cpool->tag_at(index); 560 return tag.is_unresolved_string(); 561 } 562 563 // ------------------------------------------------------------------ 564 // ciEnv::is_unresolved_klass_impl 565 // 566 // Implementation of is_unresolved_klass(). 567 bool ciEnv::is_unresolved_klass_impl(instanceKlass* accessor, int index) const { 568 EXCEPTION_CONTEXT; 569 assert(accessor->is_linked(), "must be linked before accessing constant pool"); 570 constantPoolOop cpool = accessor->constants(); 571 constantTag tag = cpool->tag_at(index); 572 return tag.is_unresolved_klass(); 573 } 574 575 // ------------------------------------------------------------------ 576 // ciEnv::get_constant_by_index 577 // 578 // Pull a constant out of the constant pool. How appropriate. 579 // 580 // Implementation note: this query is currently in no way cached. 581 ciConstant ciEnv::get_constant_by_index(constantPoolHandle cpool, 582 int index, 583 ciInstanceKlass* accessor) { 584 GUARDED_VM_ENTRY(return get_constant_by_index_impl(cpool, index, accessor);) 585 } 586 587 // ------------------------------------------------------------------ 588 // ciEnv::is_unresolved_string 589 // 590 // Check constant pool 591 // 592 // Implementation note: this query is currently in no way cached. 593 bool ciEnv::is_unresolved_string(ciInstanceKlass* accessor, 594 int index) const { 595 GUARDED_VM_ENTRY(return is_unresolved_string_impl(accessor->get_instanceKlass(), index); ) 596 } 597 598 // ------------------------------------------------------------------ 599 // ciEnv::is_unresolved_klass 600 // 601 // Check constant pool 602 // 603 // Implementation note: this query is currently in no way cached. 604 bool ciEnv::is_unresolved_klass(ciInstanceKlass* accessor, 605 int index) const { 606 GUARDED_VM_ENTRY(return is_unresolved_klass_impl(accessor->get_instanceKlass(), index); ) 607 } 608 609 // ------------------------------------------------------------------ 610 // ciEnv::get_field_by_index_impl 611 // 612 // Implementation of get_field_by_index. 613 // 614 // Implementation note: the results of field lookups are cached 615 // in the accessor klass. 616 ciField* ciEnv::get_field_by_index_impl(ciInstanceKlass* accessor, 617 int index) { 618 ciConstantPoolCache* cache = accessor->field_cache(); 619 if (cache == NULL) { 620 ciField* field = new (arena()) ciField(accessor, index); 621 return field; 622 } else { 623 ciField* field = (ciField*)cache->get(index); 624 if (field == NULL) { 625 field = new (arena()) ciField(accessor, index); 626 cache->insert(index, field); 627 } 628 return field; 629 } 630 } 631 632 // ------------------------------------------------------------------ 633 // ciEnv::get_field_by_index 634 // 635 // Get a field by index from a klass's constant pool. 636 ciField* ciEnv::get_field_by_index(ciInstanceKlass* accessor, 637 int index) { 638 GUARDED_VM_ENTRY(return get_field_by_index_impl(accessor, index);) 639 } 640 641 // ------------------------------------------------------------------ 642 // ciEnv::lookup_method 643 // 644 // Perform an appropriate method lookup based on accessor, holder, 645 // name, signature, and bytecode. 646 methodOop ciEnv::lookup_method(instanceKlass* accessor, 647 instanceKlass* holder, 648 symbolOop name, 649 symbolOop sig, 650 Bytecodes::Code bc) { 651 EXCEPTION_CONTEXT; 652 KlassHandle h_accessor(THREAD, accessor); 653 KlassHandle h_holder(THREAD, holder); 654 symbolHandle h_name(THREAD, name); 655 symbolHandle h_sig(THREAD, sig); 656 LinkResolver::check_klass_accessability(h_accessor, h_holder, KILL_COMPILE_ON_FATAL_(NULL)); 657 methodHandle dest_method; 658 switch (bc) { 659 case Bytecodes::_invokestatic: 660 dest_method = 661 LinkResolver::resolve_static_call_or_null(h_holder, h_name, h_sig, h_accessor); 662 break; 663 case Bytecodes::_invokespecial: 664 dest_method = 665 LinkResolver::resolve_special_call_or_null(h_holder, h_name, h_sig, h_accessor); 666 break; 667 case Bytecodes::_invokeinterface: 668 dest_method = 669 LinkResolver::linktime_resolve_interface_method_or_null(h_holder, h_name, h_sig, 670 h_accessor, true); 671 break; 672 case Bytecodes::_invokevirtual: 673 dest_method = 674 LinkResolver::linktime_resolve_virtual_method_or_null(h_holder, h_name, h_sig, 675 h_accessor, true); 676 break; 677 default: ShouldNotReachHere(); 678 } 679 680 return dest_method(); 681 } 682 683 684 // ------------------------------------------------------------------ 685 // ciEnv::get_method_by_index_impl 686 ciMethod* ciEnv::get_method_by_index_impl(constantPoolHandle cpool, 687 int index, Bytecodes::Code bc, 688 ciInstanceKlass* accessor) { 689 int holder_index = cpool->klass_ref_index_at(index); 690 bool holder_is_accessible; 691 ciKlass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor); 692 ciInstanceKlass* declared_holder = get_instance_klass_for_declared_method_holder(holder); 693 694 // Get the method's name and signature. 695 symbolOop name_sym = cpool->name_ref_at(index); 696 symbolOop sig_sym = cpool->signature_ref_at(index); 697 698 if (holder_is_accessible) { // Our declared holder is loaded. 699 instanceKlass* lookup = declared_holder->get_instanceKlass(); 700 methodOop m = lookup_method(accessor->get_instanceKlass(), lookup, name_sym, sig_sym, bc); 701 if (m != NULL) { 702 // We found the method. 703 return get_object(m)->as_method(); 704 } 705 } 706 707 // Either the declared holder was not loaded, or the method could 708 // not be found. Create a dummy ciMethod to represent the failed 709 // lookup. 710 711 return get_unloaded_method(declared_holder, 712 get_object(name_sym)->as_symbol(), 713 get_object(sig_sym)->as_symbol()); 714 } 715 716 717 // ------------------------------------------------------------------ 718 // ciEnv::get_fake_invokedynamic_method_impl 719 ciMethod* ciEnv::get_fake_invokedynamic_method_impl(constantPoolHandle cpool, 720 int index, Bytecodes::Code bc) { 721 assert(bc == Bytecodes::_invokedynamic, "must be invokedynamic"); 722 723 // Get the CallSite from the constant pool cache. 724 ConstantPoolCacheEntry* cpc_entry = cpool->cache()->secondary_entry_at(index); 725 assert(cpc_entry != NULL && cpc_entry->is_secondary_entry(), "sanity"); 726 Handle call_site = cpc_entry->f1(); 727 728 // Call site might not be linked yet. 729 if (call_site.is_null()) { 730 ciInstanceKlass* mh_klass = get_object(SystemDictionary::MethodHandle_klass())->as_instance_klass(); 731 ciSymbol* sig_sym = get_object(cpool->signature_ref_at(index))->as_symbol(); 732 return get_unloaded_method(mh_klass, ciSymbol::invoke_name(), sig_sym); 733 } 734 735 // Get the methodOop from the CallSite. 736 methodOop method_oop = (methodOop) java_dyn_CallSite::vmmethod(call_site()); 737 assert(method_oop != NULL, "sanity"); 738 assert(method_oop->is_method_handle_invoke(), "consistent"); 739 740 return get_object(method_oop)->as_method(); 741 } 742 743 744 // ------------------------------------------------------------------ 745 // ciEnv::get_instance_klass_for_declared_method_holder 746 ciInstanceKlass* ciEnv::get_instance_klass_for_declared_method_holder(ciKlass* method_holder) { 747 // For the case of <array>.clone(), the method holder can be a ciArrayKlass 748 // instead of a ciInstanceKlass. For that case simply pretend that the 749 // declared holder is Object.clone since that's where the call will bottom out. 750 // A more correct fix would trickle out through many interfaces in CI, 751 // requiring ciInstanceKlass* to become ciKlass* and many more places would 752 // require checks to make sure the expected type was found. Given that this 753 // only occurs for clone() the more extensive fix seems like overkill so 754 // instead we simply smear the array type into Object. 755 if (method_holder->is_instance_klass()) { 756 return method_holder->as_instance_klass(); 757 } else if (method_holder->is_array_klass()) { 758 return current()->Object_klass(); 759 } else { 760 ShouldNotReachHere(); 761 } 762 return NULL; 763 } 764 765 766 // ------------------------------------------------------------------ 767 // ciEnv::get_method_by_index 768 ciMethod* ciEnv::get_method_by_index(constantPoolHandle cpool, 769 int index, Bytecodes::Code bc, 770 ciInstanceKlass* accessor) { 771 if (bc == Bytecodes::_invokedynamic) { 772 GUARDED_VM_ENTRY(return get_fake_invokedynamic_method_impl(cpool, index, bc);) 773 } else { 774 GUARDED_VM_ENTRY(return get_method_by_index_impl(cpool, index, bc, accessor);) 775 } 776 } 777 778 779 // ------------------------------------------------------------------ 780 // ciEnv::name_buffer 781 char *ciEnv::name_buffer(int req_len) { 782 if (_name_buffer_len < req_len) { 783 if (_name_buffer == NULL) { 784 _name_buffer = (char*)arena()->Amalloc(sizeof(char)*req_len); 785 _name_buffer_len = req_len; 786 } else { 787 _name_buffer = 788 (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len); 789 _name_buffer_len = req_len; 790 } 791 } 792 return _name_buffer; 793 } 794 795 // ------------------------------------------------------------------ 796 // ciEnv::is_in_vm 797 bool ciEnv::is_in_vm() { 798 return JavaThread::current()->thread_state() == _thread_in_vm; 799 } 800 801 bool ciEnv::system_dictionary_modification_counter_changed() { 802 return _system_dictionary_modification_counter != SystemDictionary::number_of_modifications(); 803 } 804 805 // ------------------------------------------------------------------ 806 // ciEnv::check_for_system_dictionary_modification 807 // Check for changes to the system dictionary during compilation 808 // class loads, evolution, breakpoints 809 void ciEnv::check_for_system_dictionary_modification(ciMethod* target) { 810 if (failing()) return; // no need for further checks 811 812 // Dependencies must be checked when the system dictionary changes. 813 // If logging is enabled all violated dependences will be recorded in 814 // the log. In debug mode check dependencies even if the system 815 // dictionary hasn't changed to verify that no invalid dependencies 816 // were inserted. Any violated dependences in this case are dumped to 817 // the tty. 818 819 bool counter_changed = system_dictionary_modification_counter_changed(); 820 bool test_deps = counter_changed; 821 DEBUG_ONLY(test_deps = true); 822 if (!test_deps) return; 823 824 bool print_failures = false; 825 DEBUG_ONLY(print_failures = !counter_changed); 826 827 bool keep_going = (print_failures || xtty != NULL); 828 829 int violated = 0; 830 831 for (Dependencies::DepStream deps(dependencies()); deps.next(); ) { 832 klassOop witness = deps.check_dependency(); 833 if (witness != NULL) { 834 ++violated; 835 if (print_failures) deps.print_dependency(witness, /*verbose=*/ true); 836 // If there's no log and we're not sanity-checking, we're done. 837 if (!keep_going) break; 838 } 839 } 840 841 if (violated != 0) { 842 assert(counter_changed, "failed dependencies, but counter didn't change"); 843 record_failure("concurrent class loading"); 844 } 845 } 846 847 // ------------------------------------------------------------------ 848 // ciEnv::register_method 849 void ciEnv::register_method(ciMethod* target, 850 int entry_bci, 851 CodeOffsets* offsets, 852 int orig_pc_offset, 853 CodeBuffer* code_buffer, 854 int frame_words, 855 OopMapSet* oop_map_set, 856 ExceptionHandlerTable* handler_table, 857 ImplicitExceptionTable* inc_table, 858 AbstractCompiler* compiler, 859 int comp_level, 860 bool has_debug_info, 861 bool has_unsafe_access) { 862 VM_ENTRY_MARK; 863 nmethod* nm = NULL; 864 { 865 // To prevent compile queue updates. 866 MutexLocker locker(MethodCompileQueue_lock, THREAD); 867 868 // Prevent SystemDictionary::add_to_hierarchy from running 869 // and invalidating our dependencies until we install this method. 870 MutexLocker ml(Compile_lock); 871 872 // Change in Jvmti state may invalidate compilation. 873 if (!failing() && 874 ( (!jvmti_can_hotswap_or_post_breakpoint() && 875 JvmtiExport::can_hotswap_or_post_breakpoint()) || 876 (!jvmti_can_examine_or_deopt_anywhere() && 877 JvmtiExport::can_examine_or_deopt_anywhere()) || 878 (!jvmti_can_access_local_variables() && 879 JvmtiExport::can_access_local_variables()) || 880 (!jvmti_can_post_exceptions() && 881 JvmtiExport::can_post_exceptions()) )) { 882 record_failure("Jvmti state change invalidated dependencies"); 883 } 884 885 // Change in DTrace flags may invalidate compilation. 886 if (!failing() && 887 ( (!dtrace_extended_probes() && ExtendedDTraceProbes) || 888 (!dtrace_method_probes() && DTraceMethodProbes) || 889 (!dtrace_alloc_probes() && DTraceAllocProbes) )) { 890 record_failure("DTrace flags change invalidated dependencies"); 891 } 892 893 if (!failing()) { 894 if (log() != NULL) { 895 // Log the dependencies which this compilation declares. 896 dependencies()->log_all_dependencies(); 897 } 898 899 // Encode the dependencies now, so we can check them right away. 900 dependencies()->encode_content_bytes(); 901 902 // Check for {class loads, evolution, breakpoints} during compilation 903 check_for_system_dictionary_modification(target); 904 } 905 906 methodHandle method(THREAD, target->get_methodOop()); 907 908 if (failing()) { 909 // While not a true deoptimization, it is a preemptive decompile. 910 methodDataOop mdo = method()->method_data(); 911 if (mdo != NULL) { 912 mdo->inc_decompile_count(); 913 } 914 915 // All buffers in the CodeBuffer are allocated in the CodeCache. 916 // If the code buffer is created on each compile attempt 917 // as in C2, then it must be freed. 918 code_buffer->free_blob(); 919 return; 920 } 921 922 assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry"); 923 assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry"); 924 925 nm = nmethod::new_nmethod(method, 926 compile_id(), 927 entry_bci, 928 offsets, 929 orig_pc_offset, 930 debug_info(), dependencies(), code_buffer, 931 frame_words, oop_map_set, 932 handler_table, inc_table, 933 compiler, comp_level); 934 935 // Free codeBlobs 936 code_buffer->free_blob(); 937 938 // stress test 6243940 by immediately making the method 939 // non-entrant behind the system's back. This has serious 940 // side effects on the code cache and is not meant for 941 // general stress testing 942 if (nm != NULL && StressNonEntrant) { 943 MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag); 944 NativeJump::patch_verified_entry(nm->entry_point(), nm->verified_entry_point(), 945 SharedRuntime::get_handle_wrong_method_stub()); 946 } 947 948 if (nm == NULL) { 949 // The CodeCache is full. Print out warning and disable compilation. 950 record_failure("code cache is full"); 951 UseInterpreter = true; 952 if (UseCompiler || AlwaysCompileLoopMethods ) { 953 #ifndef PRODUCT 954 warning("CodeCache is full. Compiler has been disabled"); 955 if (CompileTheWorld || ExitOnFullCodeCache) { 956 before_exit(JavaThread::current()); 957 exit_globals(); // will delete tty 958 vm_direct_exit(CompileTheWorld ? 0 : 1); 959 } 960 #endif 961 UseCompiler = false; 962 AlwaysCompileLoopMethods = false; 963 } 964 } else { 965 NOT_PRODUCT(nm->set_has_debug_info(has_debug_info); ) 966 nm->set_has_unsafe_access(has_unsafe_access); 967 968 // Record successful registration. 969 // (Put nm into the task handle *before* publishing to the Java heap.) 970 if (task() != NULL) task()->set_code(nm); 971 972 if (entry_bci == InvocationEntryBci) { 973 #ifdef TIERED 974 // If there is an old version we're done with it 975 nmethod* old = method->code(); 976 if (TraceMethodReplacement && old != NULL) { 977 ResourceMark rm; 978 char *method_name = method->name_and_sig_as_C_string(); 979 tty->print_cr("Replacing method %s", method_name); 980 } 981 if (old != NULL ) { 982 old->make_not_entrant(); 983 } 984 #endif // TIERED 985 if (TraceNMethodInstalls ) { 986 ResourceMark rm; 987 char *method_name = method->name_and_sig_as_C_string(); 988 ttyLocker ttyl; 989 tty->print_cr("Installing method (%d) %s ", 990 comp_level, 991 method_name); 992 } 993 // Allow the code to be executed 994 method->set_code(method, nm); 995 } else { 996 if (TraceNMethodInstalls ) { 997 ResourceMark rm; 998 char *method_name = method->name_and_sig_as_C_string(); 999 ttyLocker ttyl; 1000 tty->print_cr("Installing osr method (%d) %s @ %d", 1001 comp_level, 1002 method_name, 1003 entry_bci); 1004 } 1005 instanceKlass::cast(method->method_holder())->add_osr_nmethod(nm); 1006 1007 } 1008 } 1009 } 1010 // JVMTI -- compiled method notification (must be done outside lock) 1011 if (nm != NULL) { 1012 nm->post_compiled_method_load_event(); 1013 } 1014 1015 } 1016 1017 1018 // ------------------------------------------------------------------ 1019 // ciEnv::find_system_klass 1020 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) { 1021 VM_ENTRY_MARK; 1022 return get_klass_by_name_impl(NULL, klass_name, false); 1023 } 1024 1025 // ------------------------------------------------------------------ 1026 // ciEnv::comp_level 1027 int ciEnv::comp_level() { 1028 if (task() == NULL) return CompLevel_full_optimization; 1029 return task()->comp_level(); 1030 } 1031 1032 // ------------------------------------------------------------------ 1033 // ciEnv::compile_id 1034 uint ciEnv::compile_id() { 1035 if (task() == NULL) return 0; 1036 return task()->compile_id(); 1037 } 1038 1039 // ------------------------------------------------------------------ 1040 // ciEnv::notice_inlined_method() 1041 void ciEnv::notice_inlined_method(ciMethod* method) { 1042 _num_inlined_bytecodes += method->code_size(); 1043 } 1044 1045 // ------------------------------------------------------------------ 1046 // ciEnv::num_inlined_bytecodes() 1047 int ciEnv::num_inlined_bytecodes() const { 1048 return _num_inlined_bytecodes; 1049 } 1050 1051 // ------------------------------------------------------------------ 1052 // ciEnv::record_failure() 1053 void ciEnv::record_failure(const char* reason) { 1054 if (log() != NULL) { 1055 log()->elem("failure reason='%s'", reason); 1056 } 1057 if (_failure_reason == NULL) { 1058 // Record the first failure reason. 1059 _failure_reason = reason; 1060 } 1061 } 1062 1063 // ------------------------------------------------------------------ 1064 // ciEnv::record_method_not_compilable() 1065 void ciEnv::record_method_not_compilable(const char* reason, bool all_tiers) { 1066 int new_compilable = 1067 all_tiers ? MethodCompilable_never : MethodCompilable_not_at_tier ; 1068 1069 // Only note transitions to a worse state 1070 if (new_compilable > _compilable) { 1071 if (log() != NULL) { 1072 if (all_tiers) { 1073 log()->elem("method_not_compilable"); 1074 } else { 1075 log()->elem("method_not_compilable_at_tier"); 1076 } 1077 } 1078 _compilable = new_compilable; 1079 1080 // Reset failure reason; this one is more important. 1081 _failure_reason = NULL; 1082 record_failure(reason); 1083 } 1084 } 1085 1086 // ------------------------------------------------------------------ 1087 // ciEnv::record_out_of_memory_failure() 1088 void ciEnv::record_out_of_memory_failure() { 1089 // If memory is low, we stop compiling methods. 1090 record_method_not_compilable("out of memory"); 1091 }