1 /* 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/systemDictionary.hpp" 27 #include "code/debugInfoRec.hpp" 28 #include "gc_interface/collectedHeap.inline.hpp" 29 #include "interpreter/bytecodeStream.hpp" 30 #include "interpreter/bytecodeTracer.hpp" 31 #include "interpreter/bytecodes.hpp" 32 #include "interpreter/interpreter.hpp" 33 #include "interpreter/oopMapCache.hpp" 34 #include "memory/gcLocker.hpp" 35 #include "memory/generation.hpp" 36 #include "memory/oopFactory.hpp" 37 #include "oops/klassOop.hpp" 38 #include "oops/methodDataOop.hpp" 39 #include "oops/methodOop.hpp" 40 #include "oops/oop.inline.hpp" 41 #include "oops/symbolOop.hpp" 42 #include "prims/jvmtiExport.hpp" 43 #include "prims/methodHandleWalk.hpp" 44 #include "prims/nativeLookup.hpp" 45 #include "runtime/arguments.hpp" 46 #include "runtime/compilationPolicy.hpp" 47 #include "runtime/frame.inline.hpp" 48 #include "runtime/handles.inline.hpp" 49 #include "runtime/relocator.hpp" 50 #include "runtime/sharedRuntime.hpp" 51 #include "runtime/signature.hpp" 52 #include "utilities/xmlstream.hpp" 53 54 55 // Implementation of methodOopDesc 56 57 address methodOopDesc::get_i2c_entry() { 58 assert(_adapter != NULL, "must have"); 59 return _adapter->get_i2c_entry(); 60 } 61 62 address methodOopDesc::get_c2i_entry() { 63 assert(_adapter != NULL, "must have"); 64 return _adapter->get_c2i_entry(); 65 } 66 67 address methodOopDesc::get_c2i_unverified_entry() { 68 assert(_adapter != NULL, "must have"); 69 return _adapter->get_c2i_unverified_entry(); 70 } 71 72 char* methodOopDesc::name_and_sig_as_C_string() { 73 return name_and_sig_as_C_string(Klass::cast(constants()->pool_holder()), name(), signature()); 74 } 75 76 char* methodOopDesc::name_and_sig_as_C_string(char* buf, int size) { 77 return name_and_sig_as_C_string(Klass::cast(constants()->pool_holder()), name(), signature(), buf, size); 78 } 79 80 char* methodOopDesc::name_and_sig_as_C_string(Klass* klass, symbolOop method_name, symbolOop signature) { 81 const char* klass_name = klass->external_name(); 82 int klass_name_len = (int)strlen(klass_name); 83 int method_name_len = method_name->utf8_length(); 84 int len = klass_name_len + 1 + method_name_len + signature->utf8_length(); 85 char* dest = NEW_RESOURCE_ARRAY(char, len + 1); 86 strcpy(dest, klass_name); 87 dest[klass_name_len] = '.'; 88 strcpy(&dest[klass_name_len + 1], method_name->as_C_string()); 89 strcpy(&dest[klass_name_len + 1 + method_name_len], signature->as_C_string()); 90 dest[len] = 0; 91 return dest; 92 } 93 94 char* methodOopDesc::name_and_sig_as_C_string(Klass* klass, symbolOop method_name, symbolOop signature, char* buf, int size) { 95 symbolOop klass_name = klass->name(); 96 klass_name->as_klass_external_name(buf, size); 97 int len = (int)strlen(buf); 98 99 if (len < size - 1) { 100 buf[len++] = '.'; 101 102 method_name->as_C_string(&(buf[len]), size - len); 103 len = (int)strlen(buf); 104 105 signature->as_C_string(&(buf[len]), size - len); 106 } 107 108 return buf; 109 } 110 111 int methodOopDesc::fast_exception_handler_bci_for(KlassHandle ex_klass, int throw_bci, TRAPS) { 112 // exception table holds quadruple entries of the form (beg_bci, end_bci, handler_bci, klass_index) 113 const int beg_bci_offset = 0; 114 const int end_bci_offset = 1; 115 const int handler_bci_offset = 2; 116 const int klass_index_offset = 3; 117 const int entry_size = 4; 118 // access exception table 119 typeArrayHandle table (THREAD, constMethod()->exception_table()); 120 int length = table->length(); 121 assert(length % entry_size == 0, "exception table format has changed"); 122 // iterate through all entries sequentially 123 constantPoolHandle pool(THREAD, constants()); 124 for (int i = 0; i < length; i += entry_size) { 125 int beg_bci = table->int_at(i + beg_bci_offset); 126 int end_bci = table->int_at(i + end_bci_offset); 127 assert(beg_bci <= end_bci, "inconsistent exception table"); 128 if (beg_bci <= throw_bci && throw_bci < end_bci) { 129 // exception handler bci range covers throw_bci => investigate further 130 int handler_bci = table->int_at(i + handler_bci_offset); 131 int klass_index = table->int_at(i + klass_index_offset); 132 if (klass_index == 0) { 133 return handler_bci; 134 } else if (ex_klass.is_null()) { 135 return handler_bci; 136 } else { 137 // we know the exception class => get the constraint class 138 // this may require loading of the constraint class; if verification 139 // fails or some other exception occurs, return handler_bci 140 klassOop k = pool->klass_at(klass_index, CHECK_(handler_bci)); 141 KlassHandle klass = KlassHandle(THREAD, k); 142 assert(klass.not_null(), "klass not loaded"); 143 if (ex_klass->is_subtype_of(klass())) { 144 return handler_bci; 145 } 146 } 147 } 148 } 149 150 return -1; 151 } 152 153 methodOop methodOopDesc::method_from_bcp(address bcp) { 154 debug_only(static int count = 0; count++); 155 assert(Universe::heap()->is_in_permanent(bcp), "bcp not in perm_gen"); 156 // TO DO: this may be unsafe in some configurations 157 HeapWord* p = Universe::heap()->block_start(bcp); 158 assert(Universe::heap()->block_is_obj(p), "must be obj"); 159 assert(oop(p)->is_constMethod(), "not a method"); 160 return constMethodOop(p)->method(); 161 } 162 163 164 void methodOopDesc::mask_for(int bci, InterpreterOopMap* mask) { 165 166 Thread* myThread = Thread::current(); 167 methodHandle h_this(myThread, this); 168 #ifdef ASSERT 169 bool has_capability = myThread->is_VM_thread() || 170 myThread->is_ConcurrentGC_thread() || 171 myThread->is_GC_task_thread(); 172 173 if (!has_capability) { 174 if (!VerifyStack && !VerifyLastFrame) { 175 // verify stack calls this outside VM thread 176 warning("oopmap should only be accessed by the " 177 "VM, GC task or CMS threads (or during debugging)"); 178 InterpreterOopMap local_mask; 179 instanceKlass::cast(method_holder())->mask_for(h_this, bci, &local_mask); 180 local_mask.print(); 181 } 182 } 183 #endif 184 instanceKlass::cast(method_holder())->mask_for(h_this, bci, mask); 185 return; 186 } 187 188 189 int methodOopDesc::bci_from(address bcp) const { 190 assert(is_native() && bcp == code_base() || contains(bcp) || is_error_reported(), "bcp doesn't belong to this method"); 191 return bcp - code_base(); 192 } 193 194 195 // Return (int)bcx if it appears to be a valid BCI. 196 // Return bci_from((address)bcx) if it appears to be a valid BCP. 197 // Return -1 otherwise. 198 // Used by profiling code, when invalid data is a possibility. 199 // The caller is responsible for validating the methodOop itself. 200 int methodOopDesc::validate_bci_from_bcx(intptr_t bcx) const { 201 // keep bci as -1 if not a valid bci 202 int bci = -1; 203 if (bcx == 0 || (address)bcx == code_base()) { 204 // code_size() may return 0 and we allow 0 here 205 // the method may be native 206 bci = 0; 207 } else if (frame::is_bci(bcx)) { 208 if (bcx < code_size()) { 209 bci = (int)bcx; 210 } 211 } else if (contains((address)bcx)) { 212 bci = (address)bcx - code_base(); 213 } 214 // Assert that if we have dodged any asserts, bci is negative. 215 assert(bci == -1 || bci == bci_from(bcp_from(bci)), "sane bci if >=0"); 216 return bci; 217 } 218 219 address methodOopDesc::bcp_from(int bci) const { 220 assert((is_native() && bci == 0) || (!is_native() && 0 <= bci && bci < code_size()), "illegal bci"); 221 address bcp = code_base() + bci; 222 assert(is_native() && bcp == code_base() || contains(bcp), "bcp doesn't belong to this method"); 223 return bcp; 224 } 225 226 227 int methodOopDesc::object_size(bool is_native) { 228 // If native, then include pointers for native_function and signature_handler 229 int extra_bytes = (is_native) ? 2*sizeof(address*) : 0; 230 int extra_words = align_size_up(extra_bytes, BytesPerWord) / BytesPerWord; 231 return align_object_size(header_size() + extra_words); 232 } 233 234 235 symbolOop methodOopDesc::klass_name() const { 236 klassOop k = method_holder(); 237 assert(k->is_klass(), "must be klass"); 238 instanceKlass* ik = (instanceKlass*) k->klass_part(); 239 return ik->name(); 240 } 241 242 243 void methodOopDesc::set_interpreter_kind() { 244 int kind = Interpreter::method_kind(methodOop(this)); 245 assert(kind != Interpreter::invalid, 246 "interpreter entry must be valid"); 247 set_interpreter_kind(kind); 248 } 249 250 251 // Attempt to return method oop to original state. Clear any pointers 252 // (to objects outside the shared spaces). We won't be able to predict 253 // where they should point in a new JVM. Further initialize some 254 // entries now in order allow them to be write protected later. 255 256 void methodOopDesc::remove_unshareable_info() { 257 unlink_method(); 258 set_interpreter_kind(); 259 } 260 261 262 bool methodOopDesc::was_executed_more_than(int n) { 263 // Invocation counter is reset when the methodOop is compiled. 264 // If the method has compiled code we therefore assume it has 265 // be excuted more than n times. 266 if (is_accessor() || is_empty_method() || (code() != NULL)) { 267 // interpreter doesn't bump invocation counter of trivial methods 268 // compiler does not bump invocation counter of compiled methods 269 return true; 270 } 271 else if (_invocation_counter.carry() || (method_data() != NULL && method_data()->invocation_counter()->carry())) { 272 // The carry bit is set when the counter overflows and causes 273 // a compilation to occur. We don't know how many times 274 // the counter has been reset, so we simply assume it has 275 // been executed more than n times. 276 return true; 277 } else { 278 return invocation_count() > n; 279 } 280 } 281 282 #ifndef PRODUCT 283 void methodOopDesc::print_invocation_count() { 284 if (is_static()) tty->print("static "); 285 if (is_final()) tty->print("final "); 286 if (is_synchronized()) tty->print("synchronized "); 287 if (is_native()) tty->print("native "); 288 method_holder()->klass_part()->name()->print_symbol_on(tty); 289 tty->print("."); 290 name()->print_symbol_on(tty); 291 signature()->print_symbol_on(tty); 292 293 if (WizardMode) { 294 // dump the size of the byte codes 295 tty->print(" {%d}", code_size()); 296 } 297 tty->cr(); 298 299 tty->print_cr (" interpreter_invocation_count: %8d ", interpreter_invocation_count()); 300 tty->print_cr (" invocation_counter: %8d ", invocation_count()); 301 tty->print_cr (" backedge_counter: %8d ", backedge_count()); 302 if (CountCompiledCalls) { 303 tty->print_cr (" compiled_invocation_count: %8d ", compiled_invocation_count()); 304 } 305 306 } 307 #endif 308 309 // Build a methodDataOop object to hold information about this method 310 // collected in the interpreter. 311 void methodOopDesc::build_interpreter_method_data(methodHandle method, TRAPS) { 312 // Grab a lock here to prevent multiple 313 // methodDataOops from being created. 314 MutexLocker ml(MethodData_lock, THREAD); 315 if (method->method_data() == NULL) { 316 methodDataOop method_data = oopFactory::new_methodData(method, CHECK); 317 method->set_method_data(method_data); 318 if (PrintMethodData && (Verbose || WizardMode)) { 319 ResourceMark rm(THREAD); 320 tty->print("build_interpreter_method_data for "); 321 method->print_name(tty); 322 tty->cr(); 323 // At the end of the run, the MDO, full of data, will be dumped. 324 } 325 } 326 } 327 328 void methodOopDesc::cleanup_inline_caches() { 329 // The current system doesn't use inline caches in the interpreter 330 // => nothing to do (keep this method around for future use) 331 } 332 333 334 int methodOopDesc::extra_stack_words() { 335 // not an inline function, to avoid a header dependency on Interpreter 336 return extra_stack_entries() * Interpreter::stackElementSize; 337 } 338 339 340 void methodOopDesc::compute_size_of_parameters(Thread *thread) { 341 symbolHandle h_signature(thread, signature()); 342 ArgumentSizeComputer asc(h_signature); 343 set_size_of_parameters(asc.size() + (is_static() ? 0 : 1)); 344 } 345 346 #ifdef CC_INTERP 347 void methodOopDesc::set_result_index(BasicType type) { 348 _result_index = Interpreter::BasicType_as_index(type); 349 } 350 #endif 351 352 BasicType methodOopDesc::result_type() const { 353 ResultTypeFinder rtf(signature()); 354 return rtf.type(); 355 } 356 357 358 bool methodOopDesc::is_empty_method() const { 359 return code_size() == 1 360 && *code_base() == Bytecodes::_return; 361 } 362 363 364 bool methodOopDesc::is_vanilla_constructor() const { 365 // Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method 366 // which only calls the superclass vanilla constructor and possibly does stores of 367 // zero constants to local fields: 368 // 369 // aload_0 370 // invokespecial 371 // indexbyte1 372 // indexbyte2 373 // 374 // followed by an (optional) sequence of: 375 // 376 // aload_0 377 // aconst_null / iconst_0 / fconst_0 / dconst_0 378 // putfield 379 // indexbyte1 380 // indexbyte2 381 // 382 // followed by: 383 // 384 // return 385 386 assert(name() == vmSymbols::object_initializer_name(), "Should only be called for default constructors"); 387 assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors"); 388 int size = code_size(); 389 // Check if size match 390 if (size == 0 || size % 5 != 0) return false; 391 address cb = code_base(); 392 int last = size - 1; 393 if (cb[0] != Bytecodes::_aload_0 || cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) { 394 // Does not call superclass default constructor 395 return false; 396 } 397 // Check optional sequence 398 for (int i = 4; i < last; i += 5) { 399 if (cb[i] != Bytecodes::_aload_0) return false; 400 if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false; 401 if (cb[i+2] != Bytecodes::_putfield) return false; 402 } 403 return true; 404 } 405 406 407 bool methodOopDesc::compute_has_loops_flag() { 408 BytecodeStream bcs(methodOop(this)); 409 Bytecodes::Code bc; 410 411 while ((bc = bcs.next()) >= 0) { 412 switch( bc ) { 413 case Bytecodes::_ifeq: 414 case Bytecodes::_ifnull: 415 case Bytecodes::_iflt: 416 case Bytecodes::_ifle: 417 case Bytecodes::_ifne: 418 case Bytecodes::_ifnonnull: 419 case Bytecodes::_ifgt: 420 case Bytecodes::_ifge: 421 case Bytecodes::_if_icmpeq: 422 case Bytecodes::_if_icmpne: 423 case Bytecodes::_if_icmplt: 424 case Bytecodes::_if_icmpgt: 425 case Bytecodes::_if_icmple: 426 case Bytecodes::_if_icmpge: 427 case Bytecodes::_if_acmpeq: 428 case Bytecodes::_if_acmpne: 429 case Bytecodes::_goto: 430 case Bytecodes::_jsr: 431 if( bcs.dest() < bcs.next_bci() ) _access_flags.set_has_loops(); 432 break; 433 434 case Bytecodes::_goto_w: 435 case Bytecodes::_jsr_w: 436 if( bcs.dest_w() < bcs.next_bci() ) _access_flags.set_has_loops(); 437 break; 438 } 439 } 440 _access_flags.set_loops_flag_init(); 441 return _access_flags.has_loops(); 442 } 443 444 445 bool methodOopDesc::is_final_method() const { 446 // %%% Should return true for private methods also, 447 // since there is no way to override them. 448 return is_final() || Klass::cast(method_holder())->is_final(); 449 } 450 451 452 bool methodOopDesc::is_strict_method() const { 453 return is_strict(); 454 } 455 456 457 bool methodOopDesc::can_be_statically_bound() const { 458 if (is_final_method()) return true; 459 return vtable_index() == nonvirtual_vtable_index; 460 } 461 462 463 bool methodOopDesc::is_accessor() const { 464 if (code_size() != 5) return false; 465 if (size_of_parameters() != 1) return false; 466 methodOop m = (methodOop)this; // pass to code_at() to avoid method_from_bcp 467 if (Bytecodes::java_code_at(code_base()+0, m) != Bytecodes::_aload_0 ) return false; 468 if (Bytecodes::java_code_at(code_base()+1, m) != Bytecodes::_getfield) return false; 469 if (Bytecodes::java_code_at(code_base()+4, m) != Bytecodes::_areturn && 470 Bytecodes::java_code_at(code_base()+4, m) != Bytecodes::_ireturn ) return false; 471 return true; 472 } 473 474 475 bool methodOopDesc::is_initializer() const { 476 return name() == vmSymbols::object_initializer_name() || name() == vmSymbols::class_initializer_name(); 477 } 478 479 480 objArrayHandle methodOopDesc::resolved_checked_exceptions_impl(methodOop this_oop, TRAPS) { 481 int length = this_oop->checked_exceptions_length(); 482 if (length == 0) { // common case 483 return objArrayHandle(THREAD, Universe::the_empty_class_klass_array()); 484 } else { 485 methodHandle h_this(THREAD, this_oop); 486 objArrayOop m_oop = oopFactory::new_objArray(SystemDictionary::Class_klass(), length, CHECK_(objArrayHandle())); 487 objArrayHandle mirrors (THREAD, m_oop); 488 for (int i = 0; i < length; i++) { 489 CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe 490 klassOop k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle())); 491 assert(Klass::cast(k)->is_subclass_of(SystemDictionary::Throwable_klass()), "invalid exception class"); 492 mirrors->obj_at_put(i, Klass::cast(k)->java_mirror()); 493 } 494 return mirrors; 495 } 496 }; 497 498 499 int methodOopDesc::line_number_from_bci(int bci) const { 500 if (bci == SynchronizationEntryBCI) bci = 0; 501 assert(bci == 0 || 0 <= bci && bci < code_size(), "illegal bci"); 502 int best_bci = 0; 503 int best_line = -1; 504 505 if (has_linenumber_table()) { 506 // The line numbers are a short array of 2-tuples [start_pc, line_number]. 507 // Not necessarily sorted and not necessarily one-to-one. 508 CompressedLineNumberReadStream stream(compressed_linenumber_table()); 509 while (stream.read_pair()) { 510 if (stream.bci() == bci) { 511 // perfect match 512 return stream.line(); 513 } else { 514 // update best_bci/line 515 if (stream.bci() < bci && stream.bci() >= best_bci) { 516 best_bci = stream.bci(); 517 best_line = stream.line(); 518 } 519 } 520 } 521 } 522 return best_line; 523 } 524 525 526 bool methodOopDesc::is_klass_loaded_by_klass_index(int klass_index) const { 527 if( _constants->tag_at(klass_index).is_unresolved_klass() ) { 528 Thread *thread = Thread::current(); 529 symbolHandle klass_name(thread, _constants->klass_name_at(klass_index)); 530 Handle loader(thread, instanceKlass::cast(method_holder())->class_loader()); 531 Handle prot (thread, Klass::cast(method_holder())->protection_domain()); 532 return SystemDictionary::find(klass_name, loader, prot, thread) != NULL; 533 } else { 534 return true; 535 } 536 } 537 538 539 bool methodOopDesc::is_klass_loaded(int refinfo_index, bool must_be_resolved) const { 540 int klass_index = _constants->klass_ref_index_at(refinfo_index); 541 if (must_be_resolved) { 542 // Make sure klass is resolved in constantpool. 543 if (constants()->tag_at(klass_index).is_unresolved_klass()) return false; 544 } 545 return is_klass_loaded_by_klass_index(klass_index); 546 } 547 548 549 void methodOopDesc::set_native_function(address function, bool post_event_flag) { 550 assert(function != NULL, "use clear_native_function to unregister natives"); 551 address* native_function = native_function_addr(); 552 553 // We can see racers trying to place the same native function into place. Once 554 // is plenty. 555 address current = *native_function; 556 if (current == function) return; 557 if (post_event_flag && JvmtiExport::should_post_native_method_bind() && 558 function != NULL) { 559 // native_method_throw_unsatisfied_link_error_entry() should only 560 // be passed when post_event_flag is false. 561 assert(function != 562 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), 563 "post_event_flag mis-match"); 564 565 // post the bind event, and possible change the bind function 566 JvmtiExport::post_native_method_bind(this, &function); 567 } 568 *native_function = function; 569 // This function can be called more than once. We must make sure that we always 570 // use the latest registered method -> check if a stub already has been generated. 571 // If so, we have to make it not_entrant. 572 nmethod* nm = code(); // Put it into local variable to guard against concurrent updates 573 if (nm != NULL) { 574 nm->make_not_entrant(); 575 } 576 } 577 578 579 bool methodOopDesc::has_native_function() const { 580 address func = native_function(); 581 return (func != NULL && func != SharedRuntime::native_method_throw_unsatisfied_link_error_entry()); 582 } 583 584 585 void methodOopDesc::clear_native_function() { 586 set_native_function( 587 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), 588 !native_bind_event_is_interesting); 589 clear_code(); 590 } 591 592 593 void methodOopDesc::set_signature_handler(address handler) { 594 address* signature_handler = signature_handler_addr(); 595 *signature_handler = handler; 596 } 597 598 599 bool methodOopDesc::is_not_compilable(int comp_level) const { 600 if (is_method_handle_invoke()) { 601 // compilers must recognize this method specially, or not at all 602 return true; 603 } 604 if (number_of_breakpoints() > 0) { 605 return true; 606 } 607 if (comp_level == CompLevel_any) { 608 return is_not_c1_compilable() || is_not_c2_compilable(); 609 } 610 if (is_c1_compile(comp_level)) { 611 return is_not_c1_compilable(); 612 } 613 if (is_c2_compile(comp_level)) { 614 return is_not_c2_compilable(); 615 } 616 return false; 617 } 618 619 // call this when compiler finds that this method is not compilable 620 void methodOopDesc::set_not_compilable(int comp_level, bool report) { 621 if (PrintCompilation && report) { 622 ttyLocker ttyl; 623 tty->print("made not compilable "); 624 this->print_short_name(tty); 625 int size = this->code_size(); 626 if (size > 0) 627 tty->print(" (%d bytes)", size); 628 tty->cr(); 629 } 630 if ((TraceDeoptimization || LogCompilation) && (xtty != NULL)) { 631 ttyLocker ttyl; 632 xtty->begin_elem("make_not_compilable thread='%d'", (int) os::current_thread_id()); 633 xtty->method(methodOop(this)); 634 xtty->stamp(); 635 xtty->end_elem(); 636 } 637 if (comp_level == CompLevel_all) { 638 set_not_c1_compilable(); 639 set_not_c2_compilable(); 640 } else { 641 if (is_c1_compile(comp_level)) { 642 set_not_c1_compilable(); 643 } else 644 if (is_c2_compile(comp_level)) { 645 set_not_c2_compilable(); 646 } 647 } 648 CompilationPolicy::policy()->disable_compilation(this); 649 } 650 651 // Revert to using the interpreter and clear out the nmethod 652 void methodOopDesc::clear_code() { 653 654 // this may be NULL if c2i adapters have not been made yet 655 // Only should happen at allocate time. 656 if (_adapter == NULL) { 657 _from_compiled_entry = NULL; 658 } else { 659 _from_compiled_entry = _adapter->get_c2i_entry(); 660 } 661 OrderAccess::storestore(); 662 _from_interpreted_entry = _i2i_entry; 663 OrderAccess::storestore(); 664 _code = NULL; 665 } 666 667 // Called by class data sharing to remove any entry points (which are not shared) 668 void methodOopDesc::unlink_method() { 669 _code = NULL; 670 _i2i_entry = NULL; 671 _from_interpreted_entry = NULL; 672 if (is_native()) { 673 *native_function_addr() = NULL; 674 set_signature_handler(NULL); 675 } 676 NOT_PRODUCT(set_compiled_invocation_count(0);) 677 invocation_counter()->reset(); 678 backedge_counter()->reset(); 679 _adapter = NULL; 680 _from_compiled_entry = NULL; 681 assert(_method_data == NULL, "unexpected method data?"); 682 set_method_data(NULL); 683 set_interpreter_throwout_count(0); 684 set_interpreter_invocation_count(0); 685 } 686 687 // Called when the method_holder is getting linked. Setup entrypoints so the method 688 // is ready to be called from interpreter, compiler, and vtables. 689 void methodOopDesc::link_method(methodHandle h_method, TRAPS) { 690 assert(_i2i_entry == NULL, "should only be called once"); 691 assert(_adapter == NULL, "init'd to NULL" ); 692 assert( _code == NULL, "nothing compiled yet" ); 693 694 // Setup interpreter entrypoint 695 assert(this == h_method(), "wrong h_method()" ); 696 address entry = Interpreter::entry_for_method(h_method); 697 assert(entry != NULL, "interpreter entry must be non-null"); 698 // Sets both _i2i_entry and _from_interpreted_entry 699 set_interpreter_entry(entry); 700 if (is_native() && !is_method_handle_invoke()) { 701 set_native_function( 702 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), 703 !native_bind_event_is_interesting); 704 } 705 706 // Setup compiler entrypoint. This is made eagerly, so we do not need 707 // special handling of vtables. An alternative is to make adapters more 708 // lazily by calling make_adapter() from from_compiled_entry() for the 709 // normal calls. For vtable calls life gets more complicated. When a 710 // call-site goes mega-morphic we need adapters in all methods which can be 711 // called from the vtable. We need adapters on such methods that get loaded 712 // later. Ditto for mega-morphic itable calls. If this proves to be a 713 // problem we'll make these lazily later. 714 (void) make_adapters(h_method, CHECK); 715 716 // ONLY USE the h_method now as make_adapter may have blocked 717 718 } 719 720 address methodOopDesc::make_adapters(methodHandle mh, TRAPS) { 721 // Adapters for compiled code are made eagerly here. They are fairly 722 // small (generally < 100 bytes) and quick to make (and cached and shared) 723 // so making them eagerly shouldn't be too expensive. 724 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh); 725 if (adapter == NULL ) { 726 THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), "out of space in CodeCache for adapters"); 727 } 728 729 mh->set_adapter_entry(adapter); 730 mh->_from_compiled_entry = adapter->get_c2i_entry(); 731 return adapter->get_c2i_entry(); 732 } 733 734 // The verified_code_entry() must be called when a invoke is resolved 735 // on this method. 736 737 // It returns the compiled code entry point, after asserting not null. 738 // This function is called after potential safepoints so that nmethod 739 // or adapter that it points to is still live and valid. 740 // This function must not hit a safepoint! 741 address methodOopDesc::verified_code_entry() { 742 debug_only(No_Safepoint_Verifier nsv;) 743 nmethod *code = (nmethod *)OrderAccess::load_ptr_acquire(&_code); 744 if (code == NULL && UseCodeCacheFlushing) { 745 nmethod *saved_code = CodeCache::find_and_remove_saved_code(this); 746 if (saved_code != NULL) { 747 methodHandle method(this); 748 assert( ! saved_code->is_osr_method(), "should not get here for osr" ); 749 set_code( method, saved_code ); 750 } 751 } 752 753 assert(_from_compiled_entry != NULL, "must be set"); 754 return _from_compiled_entry; 755 } 756 757 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all 758 // (could be racing a deopt). 759 // Not inline to avoid circular ref. 760 bool methodOopDesc::check_code() const { 761 // cached in a register or local. There's a race on the value of the field. 762 nmethod *code = (nmethod *)OrderAccess::load_ptr_acquire(&_code); 763 return code == NULL || (code->method() == NULL) || (code->method() == (methodOop)this && !code->is_osr_method()); 764 } 765 766 // Install compiled code. Instantly it can execute. 767 void methodOopDesc::set_code(methodHandle mh, nmethod *code) { 768 assert( code, "use clear_code to remove code" ); 769 assert( mh->check_code(), "" ); 770 771 guarantee(mh->adapter() != NULL, "Adapter blob must already exist!"); 772 773 // These writes must happen in this order, because the interpreter will 774 // directly jump to from_interpreted_entry which jumps to an i2c adapter 775 // which jumps to _from_compiled_entry. 776 mh->_code = code; // Assign before allowing compiled code to exec 777 778 int comp_level = code->comp_level(); 779 // In theory there could be a race here. In practice it is unlikely 780 // and not worth worrying about. 781 if (comp_level > mh->highest_comp_level()) { 782 mh->set_highest_comp_level(comp_level); 783 } 784 785 OrderAccess::storestore(); 786 #ifdef SHARK 787 mh->_from_interpreted_entry = code->insts_begin(); 788 #else 789 mh->_from_compiled_entry = code->verified_entry_point(); 790 OrderAccess::storestore(); 791 // Instantly compiled code can execute. 792 mh->_from_interpreted_entry = mh->get_i2c_entry(); 793 #endif // SHARK 794 795 } 796 797 798 bool methodOopDesc::is_overridden_in(klassOop k) const { 799 instanceKlass* ik = instanceKlass::cast(k); 800 801 if (ik->is_interface()) return false; 802 803 // If method is an interface, we skip it - except if it 804 // is a miranda method 805 if (instanceKlass::cast(method_holder())->is_interface()) { 806 // Check that method is not a miranda method 807 if (ik->lookup_method(name(), signature()) == NULL) { 808 // No implementation exist - so miranda method 809 return false; 810 } 811 return true; 812 } 813 814 assert(ik->is_subclass_of(method_holder()), "should be subklass"); 815 assert(ik->vtable() != NULL, "vtable should exist"); 816 if (vtable_index() == nonvirtual_vtable_index) { 817 return false; 818 } else { 819 methodOop vt_m = ik->method_at_vtable(vtable_index()); 820 return vt_m != methodOop(this); 821 } 822 } 823 824 825 // give advice about whether this methodOop should be cached or not 826 bool methodOopDesc::should_not_be_cached() const { 827 if (is_old()) { 828 // This method has been redefined. It is either EMCP or obsolete 829 // and we don't want to cache it because that would pin the method 830 // down and prevent it from being collectible if and when it 831 // finishes executing. 832 return true; 833 } 834 835 if (mark()->should_not_be_cached()) { 836 // It is either not safe or not a good idea to cache this 837 // method at this time because of the state of the embedded 838 // markOop. See markOop.cpp for the gory details. 839 return true; 840 } 841 842 // caching this method should be just fine 843 return false; 844 } 845 846 bool methodOopDesc::is_method_handle_invoke_name(vmSymbols::SID name_sid) { 847 switch (name_sid) { 848 case vmSymbols::VM_SYMBOL_ENUM_NAME(invokeExact_name): 849 case vmSymbols::VM_SYMBOL_ENUM_NAME(invokeGeneric_name): 850 return true; 851 } 852 if (AllowTransitionalJSR292 853 && name_sid == vmSymbols::VM_SYMBOL_ENUM_NAME(invoke_name)) 854 return true; 855 return false; 856 } 857 858 // Constant pool structure for invoke methods: 859 enum { 860 _imcp_invoke_name = 1, // utf8: 'invokeExact' or 'invokeGeneric' 861 _imcp_invoke_signature, // utf8: (variable symbolOop) 862 _imcp_method_type_value, // string: (variable java/dyn/MethodType, sic) 863 _imcp_limit 864 }; 865 866 oop methodOopDesc::method_handle_type() const { 867 if (!is_method_handle_invoke()) { assert(false, "caller resp."); return NULL; } 868 oop mt = constants()->resolved_string_at(_imcp_method_type_value); 869 assert(mt->klass() == SystemDictionary::MethodType_klass(), ""); 870 return mt; 871 } 872 873 jint* methodOopDesc::method_type_offsets_chain() { 874 static jint pchase[] = { -1, -1, -1 }; 875 if (pchase[0] == -1) { 876 jint step0 = in_bytes(constants_offset()); 877 jint step1 = (constantPoolOopDesc::header_size() + _imcp_method_type_value) * HeapWordSize; 878 // do this in reverse to avoid races: 879 OrderAccess::release_store(&pchase[1], step1); 880 OrderAccess::release_store(&pchase[0], step0); 881 } 882 return pchase; 883 } 884 885 //------------------------------------------------------------------------------ 886 // methodOopDesc::is_method_handle_adapter 887 // 888 // Tests if this method is an internal adapter frame from the 889 // MethodHandleCompiler. 890 // Must be consistent with MethodHandleCompiler::get_method_oop(). 891 bool methodOopDesc::is_method_handle_adapter() const { 892 if (is_synthetic() && 893 !is_native() && // has code from MethodHandleCompiler 894 is_method_handle_invoke_name(name()) && 895 MethodHandleCompiler::klass_is_method_handle_adapter_holder(method_holder())) { 896 assert(!is_method_handle_invoke(), "disjoint"); 897 return true; 898 } else { 899 return false; 900 } 901 } 902 903 methodHandle methodOopDesc::make_invoke_method(KlassHandle holder, 904 symbolHandle name, 905 symbolHandle signature, 906 Handle method_type, TRAPS) { 907 methodHandle empty; 908 909 assert(holder() == SystemDictionary::MethodHandle_klass(), 910 "must be a JSR 292 magic type"); 911 912 if (TraceMethodHandles) { 913 tty->print("Creating invoke method for "); 914 signature->print_value(); 915 tty->cr(); 916 } 917 918 constantPoolHandle cp; 919 { 920 constantPoolOop cp_oop = oopFactory::new_constantPool(_imcp_limit, IsSafeConc, CHECK_(empty)); 921 cp = constantPoolHandle(THREAD, cp_oop); 922 } 923 cp->symbol_at_put(_imcp_invoke_name, name()); 924 cp->symbol_at_put(_imcp_invoke_signature, signature()); 925 cp->string_at_put(_imcp_method_type_value, vmSymbols::void_signature()); 926 cp->set_pool_holder(holder()); 927 928 // set up the fancy stuff: 929 cp->pseudo_string_at_put(_imcp_method_type_value, method_type()); 930 methodHandle m; 931 { 932 int flags_bits = (JVM_MH_INVOKE_BITS | JVM_ACC_PUBLIC | JVM_ACC_FINAL); 933 methodOop m_oop = oopFactory::new_method(0, accessFlags_from(flags_bits), 934 0, 0, 0, IsSafeConc, CHECK_(empty)); 935 m = methodHandle(THREAD, m_oop); 936 } 937 m->set_constants(cp()); 938 m->set_name_index(_imcp_invoke_name); 939 m->set_signature_index(_imcp_invoke_signature); 940 assert(is_method_handle_invoke_name(m->name()), ""); 941 assert(m->signature() == signature(), ""); 942 assert(m->is_method_handle_invoke(), ""); 943 #ifdef CC_INTERP 944 ResultTypeFinder rtf(signature()); 945 m->set_result_index(rtf.type()); 946 #endif 947 m->compute_size_of_parameters(THREAD); 948 m->set_exception_table(Universe::the_empty_int_array()); 949 m->init_intrinsic_id(); 950 assert(m->intrinsic_id() == vmIntrinsics::_invokeExact || 951 m->intrinsic_id() == vmIntrinsics::_invokeGeneric, "must be an invoker"); 952 953 // Finally, set up its entry points. 954 assert(m->method_handle_type() == method_type(), ""); 955 assert(m->can_be_statically_bound(), ""); 956 m->set_vtable_index(methodOopDesc::nonvirtual_vtable_index); 957 m->link_method(m, CHECK_(empty)); 958 959 #ifdef ASSERT 960 // Make sure the pointer chase works. 961 address p = (address) m(); 962 for (jint* pchase = method_type_offsets_chain(); (*pchase) != -1; pchase++) { 963 p = *(address*)(p + (*pchase)); 964 } 965 assert((oop)p == method_type(), "pointer chase is correct"); 966 #endif 967 968 if (TraceMethodHandles && (Verbose || WizardMode)) 969 m->print_on(tty); 970 971 return m; 972 } 973 974 975 976 methodHandle methodOopDesc:: clone_with_new_data(methodHandle m, u_char* new_code, int new_code_length, 977 u_char* new_compressed_linenumber_table, int new_compressed_linenumber_size, TRAPS) { 978 // Code below does not work for native methods - they should never get rewritten anyway 979 assert(!m->is_native(), "cannot rewrite native methods"); 980 // Allocate new methodOop 981 AccessFlags flags = m->access_flags(); 982 int checked_exceptions_len = m->checked_exceptions_length(); 983 int localvariable_len = m->localvariable_table_length(); 984 // Allocate newm_oop with the is_conc_safe parameter set 985 // to IsUnsafeConc to indicate that newm_oop is not yet 986 // safe for concurrent processing by a GC. 987 methodOop newm_oop = oopFactory::new_method(new_code_length, 988 flags, 989 new_compressed_linenumber_size, 990 localvariable_len, 991 checked_exceptions_len, 992 IsUnsafeConc, 993 CHECK_(methodHandle())); 994 methodHandle newm (THREAD, newm_oop); 995 int new_method_size = newm->method_size(); 996 // Create a shallow copy of methodOopDesc part, but be careful to preserve the new constMethodOop 997 constMethodOop newcm = newm->constMethod(); 998 int new_const_method_size = newm->constMethod()->object_size(); 999 1000 memcpy(newm(), m(), sizeof(methodOopDesc)); 1001 // Create shallow copy of constMethodOopDesc, but be careful to preserve the methodOop 1002 // is_conc_safe is set to false because that is the value of 1003 // is_conc_safe initialzied into newcm and the copy should 1004 // not overwrite that value. During the window during which it is 1005 // tagged as unsafe, some extra work could be needed during precleaning 1006 // or concurrent marking but those phases will be correct. Setting and 1007 // resetting is done in preference to a careful copying into newcm to 1008 // avoid having to know the precise layout of a constMethodOop. 1009 m->constMethod()->set_is_conc_safe(false); 1010 memcpy(newcm, m->constMethod(), sizeof(constMethodOopDesc)); 1011 m->constMethod()->set_is_conc_safe(true); 1012 // Reset correct method/const method, method size, and parameter info 1013 newcm->set_method(newm()); 1014 newm->set_constMethod(newcm); 1015 assert(newcm->method() == newm(), "check"); 1016 newm->constMethod()->set_code_size(new_code_length); 1017 newm->constMethod()->set_constMethod_size(new_const_method_size); 1018 newm->set_method_size(new_method_size); 1019 assert(newm->code_size() == new_code_length, "check"); 1020 assert(newm->checked_exceptions_length() == checked_exceptions_len, "check"); 1021 assert(newm->localvariable_table_length() == localvariable_len, "check"); 1022 // Copy new byte codes 1023 memcpy(newm->code_base(), new_code, new_code_length); 1024 // Copy line number table 1025 if (new_compressed_linenumber_size > 0) { 1026 memcpy(newm->compressed_linenumber_table(), 1027 new_compressed_linenumber_table, 1028 new_compressed_linenumber_size); 1029 } 1030 // Copy checked_exceptions 1031 if (checked_exceptions_len > 0) { 1032 memcpy(newm->checked_exceptions_start(), 1033 m->checked_exceptions_start(), 1034 checked_exceptions_len * sizeof(CheckedExceptionElement)); 1035 } 1036 // Copy local variable number table 1037 if (localvariable_len > 0) { 1038 memcpy(newm->localvariable_table_start(), 1039 m->localvariable_table_start(), 1040 localvariable_len * sizeof(LocalVariableTableElement)); 1041 } 1042 1043 // Only set is_conc_safe to true when changes to newcm are 1044 // complete. 1045 newcm->set_is_conc_safe(true); 1046 return newm; 1047 } 1048 1049 vmSymbols::SID methodOopDesc::klass_id_for_intrinsics(klassOop holder) { 1050 // if loader is not the default loader (i.e., != NULL), we can't know the intrinsics 1051 // because we are not loading from core libraries 1052 if (instanceKlass::cast(holder)->class_loader() != NULL) 1053 return vmSymbols::NO_SID; // regardless of name, no intrinsics here 1054 1055 // see if the klass name is well-known: 1056 symbolOop klass_name = instanceKlass::cast(holder)->name(); 1057 return vmSymbols::find_sid(klass_name); 1058 } 1059 1060 void methodOopDesc::init_intrinsic_id() { 1061 assert(_intrinsic_id == vmIntrinsics::_none, "do this just once"); 1062 const uintptr_t max_id_uint = right_n_bits((int)(sizeof(_intrinsic_id) * BitsPerByte)); 1063 assert((uintptr_t)vmIntrinsics::ID_LIMIT <= max_id_uint, "else fix size"); 1064 assert(intrinsic_id_size_in_bytes() == sizeof(_intrinsic_id), ""); 1065 1066 // the klass name is well-known: 1067 vmSymbols::SID klass_id = klass_id_for_intrinsics(method_holder()); 1068 assert(klass_id != vmSymbols::NO_SID, "caller responsibility"); 1069 1070 // ditto for method and signature: 1071 vmSymbols::SID name_id = vmSymbols::find_sid(name()); 1072 if (name_id == vmSymbols::NO_SID) return; 1073 vmSymbols::SID sig_id = vmSymbols::find_sid(signature()); 1074 if (klass_id != vmSymbols::VM_SYMBOL_ENUM_NAME(java_dyn_MethodHandle) 1075 && sig_id == vmSymbols::NO_SID) return; 1076 jshort flags = access_flags().as_short(); 1077 1078 vmIntrinsics::ID id = vmIntrinsics::find_id(klass_id, name_id, sig_id, flags); 1079 if (id != vmIntrinsics::_none) { 1080 set_intrinsic_id(id); 1081 return; 1082 } 1083 1084 // A few slightly irregular cases: 1085 switch (klass_id) { 1086 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_StrictMath): 1087 // Second chance: check in regular Math. 1088 switch (name_id) { 1089 case vmSymbols::VM_SYMBOL_ENUM_NAME(min_name): 1090 case vmSymbols::VM_SYMBOL_ENUM_NAME(max_name): 1091 case vmSymbols::VM_SYMBOL_ENUM_NAME(sqrt_name): 1092 // pretend it is the corresponding method in the non-strict class: 1093 klass_id = vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_Math); 1094 id = vmIntrinsics::find_id(klass_id, name_id, sig_id, flags); 1095 break; 1096 } 1097 break; 1098 1099 // Signature-polymorphic methods: MethodHandle.invoke*, InvokeDynamic.*. 1100 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_dyn_MethodHandle): 1101 if (is_static() || !is_native()) break; 1102 switch (name_id) { 1103 case vmSymbols::VM_SYMBOL_ENUM_NAME(invokeGeneric_name): 1104 id = vmIntrinsics::_invokeGeneric; 1105 break; 1106 case vmSymbols::VM_SYMBOL_ENUM_NAME(invokeExact_name): 1107 id = vmIntrinsics::_invokeExact; 1108 break; 1109 case vmSymbols::VM_SYMBOL_ENUM_NAME(invoke_name): 1110 if (AllowTransitionalJSR292) id = vmIntrinsics::_invokeExact; 1111 break; 1112 } 1113 break; 1114 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_dyn_InvokeDynamic): 1115 if (!is_static() || !is_native()) break; 1116 id = vmIntrinsics::_invokeDynamic; 1117 break; 1118 } 1119 1120 if (id != vmIntrinsics::_none) { 1121 // Set up its iid. It is an alias method. 1122 set_intrinsic_id(id); 1123 return; 1124 } 1125 } 1126 1127 // These two methods are static since a GC may move the methodOopDesc 1128 bool methodOopDesc::load_signature_classes(methodHandle m, TRAPS) { 1129 bool sig_is_loaded = true; 1130 Handle class_loader(THREAD, instanceKlass::cast(m->method_holder())->class_loader()); 1131 Handle protection_domain(THREAD, Klass::cast(m->method_holder())->protection_domain()); 1132 symbolHandle signature(THREAD, m->signature()); 1133 for(SignatureStream ss(signature); !ss.is_done(); ss.next()) { 1134 if (ss.is_object()) { 1135 symbolOop sym = ss.as_symbol(CHECK_(false)); 1136 symbolHandle name (THREAD, sym); 1137 klassOop klass = SystemDictionary::resolve_or_null(name, class_loader, 1138 protection_domain, THREAD); 1139 // We are loading classes eagerly. If a ClassNotFoundException or 1140 // a LinkageError was generated, be sure to ignore it. 1141 if (HAS_PENDING_EXCEPTION) { 1142 if (PENDING_EXCEPTION->is_a(SystemDictionary::ClassNotFoundException_klass()) || 1143 PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) { 1144 CLEAR_PENDING_EXCEPTION; 1145 } else { 1146 return false; 1147 } 1148 } 1149 if( klass == NULL) { sig_is_loaded = false; } 1150 } 1151 } 1152 return sig_is_loaded; 1153 } 1154 1155 bool methodOopDesc::has_unloaded_classes_in_signature(methodHandle m, TRAPS) { 1156 Handle class_loader(THREAD, instanceKlass::cast(m->method_holder())->class_loader()); 1157 Handle protection_domain(THREAD, Klass::cast(m->method_holder())->protection_domain()); 1158 symbolHandle signature(THREAD, m->signature()); 1159 for(SignatureStream ss(signature); !ss.is_done(); ss.next()) { 1160 if (ss.type() == T_OBJECT) { 1161 symbolHandle name(THREAD, ss.as_symbol_or_null()); 1162 if (name() == NULL) return true; 1163 klassOop klass = SystemDictionary::find(name, class_loader, protection_domain, THREAD); 1164 if (klass == NULL) return true; 1165 } 1166 } 1167 return false; 1168 } 1169 1170 // Exposed so field engineers can debug VM 1171 void methodOopDesc::print_short_name(outputStream* st) { 1172 ResourceMark rm; 1173 #ifdef PRODUCT 1174 st->print(" %s::", method_holder()->klass_part()->external_name()); 1175 #else 1176 st->print(" %s::", method_holder()->klass_part()->internal_name()); 1177 #endif 1178 name()->print_symbol_on(st); 1179 if (WizardMode) signature()->print_symbol_on(st); 1180 } 1181 1182 1183 extern "C" { 1184 static int method_compare(methodOop* a, methodOop* b) { 1185 return (*a)->name()->fast_compare((*b)->name()); 1186 } 1187 1188 // Prevent qsort from reordering a previous valid sort by 1189 // considering the address of the methodOops if two methods 1190 // would otherwise compare as equal. Required to preserve 1191 // optimal access order in the shared archive. Slower than 1192 // method_compare, only used for shared archive creation. 1193 static int method_compare_idempotent(methodOop* a, methodOop* b) { 1194 int i = method_compare(a, b); 1195 if (i != 0) return i; 1196 return ( a < b ? -1 : (a == b ? 0 : 1)); 1197 } 1198 1199 // We implement special compare versions for narrow oops to avoid 1200 // testing for UseCompressedOops on every comparison. 1201 static int method_compare_narrow(narrowOop* a, narrowOop* b) { 1202 methodOop m = (methodOop)oopDesc::load_decode_heap_oop(a); 1203 methodOop n = (methodOop)oopDesc::load_decode_heap_oop(b); 1204 return m->name()->fast_compare(n->name()); 1205 } 1206 1207 static int method_compare_narrow_idempotent(narrowOop* a, narrowOop* b) { 1208 int i = method_compare_narrow(a, b); 1209 if (i != 0) return i; 1210 return ( a < b ? -1 : (a == b ? 0 : 1)); 1211 } 1212 1213 typedef int (*compareFn)(const void*, const void*); 1214 } 1215 1216 1217 // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array 1218 static void reorder_based_on_method_index(objArrayOop methods, 1219 objArrayOop annotations, 1220 GrowableArray<oop>* temp_array) { 1221 if (annotations == NULL) { 1222 return; 1223 } 1224 1225 int length = methods->length(); 1226 int i; 1227 // Copy to temp array 1228 temp_array->clear(); 1229 for (i = 0; i < length; i++) { 1230 temp_array->append(annotations->obj_at(i)); 1231 } 1232 1233 // Copy back using old method indices 1234 for (i = 0; i < length; i++) { 1235 methodOop m = (methodOop) methods->obj_at(i); 1236 annotations->obj_at_put(i, temp_array->at(m->method_idnum())); 1237 } 1238 } 1239 1240 1241 // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array 1242 void methodOopDesc::sort_methods(objArrayOop methods, 1243 objArrayOop methods_annotations, 1244 objArrayOop methods_parameter_annotations, 1245 objArrayOop methods_default_annotations, 1246 bool idempotent) { 1247 int length = methods->length(); 1248 if (length > 1) { 1249 bool do_annotations = false; 1250 if (methods_annotations != NULL || 1251 methods_parameter_annotations != NULL || 1252 methods_default_annotations != NULL) { 1253 do_annotations = true; 1254 } 1255 if (do_annotations) { 1256 // Remember current method ordering so we can reorder annotations 1257 for (int i = 0; i < length; i++) { 1258 methodOop m = (methodOop) methods->obj_at(i); 1259 m->set_method_idnum(i); 1260 } 1261 } 1262 1263 // Use a simple bubble sort for small number of methods since 1264 // qsort requires a functional pointer call for each comparison. 1265 if (length < 8) { 1266 bool sorted = true; 1267 for (int i=length-1; i>0; i--) { 1268 for (int j=0; j<i; j++) { 1269 methodOop m1 = (methodOop)methods->obj_at(j); 1270 methodOop m2 = (methodOop)methods->obj_at(j+1); 1271 if ((uintptr_t)m1->name() > (uintptr_t)m2->name()) { 1272 methods->obj_at_put(j, m2); 1273 methods->obj_at_put(j+1, m1); 1274 sorted = false; 1275 } 1276 } 1277 if (sorted) break; 1278 sorted = true; 1279 } 1280 } else { 1281 compareFn compare = 1282 (UseCompressedOops ? 1283 (compareFn) (idempotent ? method_compare_narrow_idempotent : method_compare_narrow): 1284 (compareFn) (idempotent ? method_compare_idempotent : method_compare)); 1285 qsort(methods->base(), length, heapOopSize, compare); 1286 } 1287 1288 // Sort annotations if necessary 1289 assert(methods_annotations == NULL || methods_annotations->length() == methods->length(), ""); 1290 assert(methods_parameter_annotations == NULL || methods_parameter_annotations->length() == methods->length(), ""); 1291 assert(methods_default_annotations == NULL || methods_default_annotations->length() == methods->length(), ""); 1292 if (do_annotations) { 1293 ResourceMark rm; 1294 // Allocate temporary storage 1295 GrowableArray<oop>* temp_array = new GrowableArray<oop>(length); 1296 reorder_based_on_method_index(methods, methods_annotations, temp_array); 1297 reorder_based_on_method_index(methods, methods_parameter_annotations, temp_array); 1298 reorder_based_on_method_index(methods, methods_default_annotations, temp_array); 1299 } 1300 1301 // Reset method ordering 1302 for (int i = 0; i < length; i++) { 1303 methodOop m = (methodOop) methods->obj_at(i); 1304 m->set_method_idnum(i); 1305 } 1306 } 1307 } 1308 1309 1310 //----------------------------------------------------------------------------------- 1311 // Non-product code 1312 1313 #ifndef PRODUCT 1314 class SignatureTypePrinter : public SignatureTypeNames { 1315 private: 1316 outputStream* _st; 1317 bool _use_separator; 1318 1319 void type_name(const char* name) { 1320 if (_use_separator) _st->print(", "); 1321 _st->print(name); 1322 _use_separator = true; 1323 } 1324 1325 public: 1326 SignatureTypePrinter(symbolHandle signature, outputStream* st) : SignatureTypeNames(signature) { 1327 _st = st; 1328 _use_separator = false; 1329 } 1330 1331 void print_parameters() { _use_separator = false; iterate_parameters(); } 1332 void print_returntype() { _use_separator = false; iterate_returntype(); } 1333 }; 1334 1335 1336 void methodOopDesc::print_name(outputStream* st) { 1337 Thread *thread = Thread::current(); 1338 ResourceMark rm(thread); 1339 SignatureTypePrinter sig(signature(), st); 1340 st->print("%s ", is_static() ? "static" : "virtual"); 1341 sig.print_returntype(); 1342 st->print(" %s.", method_holder()->klass_part()->internal_name()); 1343 name()->print_symbol_on(st); 1344 st->print("("); 1345 sig.print_parameters(); 1346 st->print(")"); 1347 } 1348 1349 1350 void methodOopDesc::print_codes_on(outputStream* st) const { 1351 print_codes_on(0, code_size(), st); 1352 } 1353 1354 void methodOopDesc::print_codes_on(int from, int to, outputStream* st) const { 1355 Thread *thread = Thread::current(); 1356 ResourceMark rm(thread); 1357 methodHandle mh (thread, (methodOop)this); 1358 BytecodeStream s(mh); 1359 s.set_interval(from, to); 1360 BytecodeTracer::set_closure(BytecodeTracer::std_closure()); 1361 while (s.next() >= 0) BytecodeTracer::trace(mh, s.bcp(), st); 1362 } 1363 #endif // not PRODUCT 1364 1365 1366 // Simple compression of line number tables. We use a regular compressed stream, except that we compress deltas 1367 // between (bci,line) pairs since they are smaller. If (bci delta, line delta) fits in (5-bit unsigned, 3-bit unsigned) 1368 // we save it as one byte, otherwise we write a 0xFF escape character and use regular compression. 0x0 is used 1369 // as end-of-stream terminator. 1370 1371 void CompressedLineNumberWriteStream::write_pair_regular(int bci_delta, int line_delta) { 1372 // bci and line number does not compress into single byte. 1373 // Write out escape character and use regular compression for bci and line number. 1374 write_byte((jubyte)0xFF); 1375 write_signed_int(bci_delta); 1376 write_signed_int(line_delta); 1377 } 1378 1379 // See comment in methodOop.hpp which explains why this exists. 1380 #if defined(_M_AMD64) && MSC_VER >= 1400 1381 #pragma optimize("", off) 1382 void CompressedLineNumberWriteStream::write_pair(int bci, int line) { 1383 write_pair_inline(bci, line); 1384 } 1385 #pragma optimize("", on) 1386 #endif 1387 1388 CompressedLineNumberReadStream::CompressedLineNumberReadStream(u_char* buffer) : CompressedReadStream(buffer) { 1389 _bci = 0; 1390 _line = 0; 1391 }; 1392 1393 1394 bool CompressedLineNumberReadStream::read_pair() { 1395 jubyte next = read_byte(); 1396 // Check for terminator 1397 if (next == 0) return false; 1398 if (next == 0xFF) { 1399 // Escape character, regular compression used 1400 _bci += read_signed_int(); 1401 _line += read_signed_int(); 1402 } else { 1403 // Single byte compression used 1404 _bci += next >> 3; 1405 _line += next & 0x7; 1406 } 1407 return true; 1408 } 1409 1410 1411 Bytecodes::Code methodOopDesc::orig_bytecode_at(int bci) { 1412 BreakpointInfo* bp = instanceKlass::cast(method_holder())->breakpoints(); 1413 for (; bp != NULL; bp = bp->next()) { 1414 if (bp->match(this, bci)) { 1415 return bp->orig_bytecode(); 1416 } 1417 } 1418 ShouldNotReachHere(); 1419 return Bytecodes::_shouldnotreachhere; 1420 } 1421 1422 void methodOopDesc::set_orig_bytecode_at(int bci, Bytecodes::Code code) { 1423 assert(code != Bytecodes::_breakpoint, "cannot patch breakpoints this way"); 1424 BreakpointInfo* bp = instanceKlass::cast(method_holder())->breakpoints(); 1425 for (; bp != NULL; bp = bp->next()) { 1426 if (bp->match(this, bci)) { 1427 bp->set_orig_bytecode(code); 1428 // and continue, in case there is more than one 1429 } 1430 } 1431 } 1432 1433 void methodOopDesc::set_breakpoint(int bci) { 1434 instanceKlass* ik = instanceKlass::cast(method_holder()); 1435 BreakpointInfo *bp = new BreakpointInfo(this, bci); 1436 bp->set_next(ik->breakpoints()); 1437 ik->set_breakpoints(bp); 1438 // do this last: 1439 bp->set(this); 1440 } 1441 1442 static void clear_matches(methodOop m, int bci) { 1443 instanceKlass* ik = instanceKlass::cast(m->method_holder()); 1444 BreakpointInfo* prev_bp = NULL; 1445 BreakpointInfo* next_bp; 1446 for (BreakpointInfo* bp = ik->breakpoints(); bp != NULL; bp = next_bp) { 1447 next_bp = bp->next(); 1448 // bci value of -1 is used to delete all breakpoints in method m (ex: clear_all_breakpoint). 1449 if (bci >= 0 ? bp->match(m, bci) : bp->match(m)) { 1450 // do this first: 1451 bp->clear(m); 1452 // unhook it 1453 if (prev_bp != NULL) 1454 prev_bp->set_next(next_bp); 1455 else 1456 ik->set_breakpoints(next_bp); 1457 delete bp; 1458 // When class is redefined JVMTI sets breakpoint in all versions of EMCP methods 1459 // at same location. So we have multiple matching (method_index and bci) 1460 // BreakpointInfo nodes in BreakpointInfo list. We should just delete one 1461 // breakpoint for clear_breakpoint request and keep all other method versions 1462 // BreakpointInfo for future clear_breakpoint request. 1463 // bcivalue of -1 is used to clear all breakpoints (see clear_all_breakpoints) 1464 // which is being called when class is unloaded. We delete all the Breakpoint 1465 // information for all versions of method. We may not correctly restore the original 1466 // bytecode in all method versions, but that is ok. Because the class is being unloaded 1467 // so these methods won't be used anymore. 1468 if (bci >= 0) { 1469 break; 1470 } 1471 } else { 1472 // This one is a keeper. 1473 prev_bp = bp; 1474 } 1475 } 1476 } 1477 1478 void methodOopDesc::clear_breakpoint(int bci) { 1479 assert(bci >= 0, ""); 1480 clear_matches(this, bci); 1481 } 1482 1483 void methodOopDesc::clear_all_breakpoints() { 1484 clear_matches(this, -1); 1485 } 1486 1487 1488 int methodOopDesc::invocation_count() { 1489 if (TieredCompilation) { 1490 const methodDataOop mdo = method_data(); 1491 if (invocation_counter()->carry() || ((mdo != NULL) ? mdo->invocation_counter()->carry() : false)) { 1492 return InvocationCounter::count_limit; 1493 } else { 1494 return invocation_counter()->count() + ((mdo != NULL) ? mdo->invocation_counter()->count() : 0); 1495 } 1496 } else { 1497 return invocation_counter()->count(); 1498 } 1499 } 1500 1501 int methodOopDesc::backedge_count() { 1502 if (TieredCompilation) { 1503 const methodDataOop mdo = method_data(); 1504 if (backedge_counter()->carry() || ((mdo != NULL) ? mdo->backedge_counter()->carry() : false)) { 1505 return InvocationCounter::count_limit; 1506 } else { 1507 return backedge_counter()->count() + ((mdo != NULL) ? mdo->backedge_counter()->count() : 0); 1508 } 1509 } else { 1510 return backedge_counter()->count(); 1511 } 1512 } 1513 1514 int methodOopDesc::highest_comp_level() const { 1515 methodDataOop mdo = method_data(); 1516 if (mdo != NULL) { 1517 return mdo->highest_comp_level(); 1518 } else { 1519 return CompLevel_none; 1520 } 1521 } 1522 1523 int methodOopDesc::highest_osr_comp_level() const { 1524 methodDataOop mdo = method_data(); 1525 if (mdo != NULL) { 1526 return mdo->highest_osr_comp_level(); 1527 } else { 1528 return CompLevel_none; 1529 } 1530 } 1531 1532 void methodOopDesc::set_highest_comp_level(int level) { 1533 methodDataOop mdo = method_data(); 1534 if (mdo != NULL) { 1535 mdo->set_highest_comp_level(level); 1536 } 1537 } 1538 1539 void methodOopDesc::set_highest_osr_comp_level(int level) { 1540 methodDataOop mdo = method_data(); 1541 if (mdo != NULL) { 1542 mdo->set_highest_osr_comp_level(level); 1543 } 1544 } 1545 1546 BreakpointInfo::BreakpointInfo(methodOop m, int bci) { 1547 _bci = bci; 1548 _name_index = m->name_index(); 1549 _signature_index = m->signature_index(); 1550 _orig_bytecode = (Bytecodes::Code) *m->bcp_from(_bci); 1551 if (_orig_bytecode == Bytecodes::_breakpoint) 1552 _orig_bytecode = m->orig_bytecode_at(_bci); 1553 _next = NULL; 1554 } 1555 1556 void BreakpointInfo::set(methodOop method) { 1557 #ifdef ASSERT 1558 { 1559 Bytecodes::Code code = (Bytecodes::Code) *method->bcp_from(_bci); 1560 if (code == Bytecodes::_breakpoint) 1561 code = method->orig_bytecode_at(_bci); 1562 assert(orig_bytecode() == code, "original bytecode must be the same"); 1563 } 1564 #endif 1565 *method->bcp_from(_bci) = Bytecodes::_breakpoint; 1566 method->incr_number_of_breakpoints(); 1567 SystemDictionary::notice_modification(); 1568 { 1569 // Deoptimize all dependents on this method 1570 Thread *thread = Thread::current(); 1571 HandleMark hm(thread); 1572 methodHandle mh(thread, method); 1573 Universe::flush_dependents_on_method(mh); 1574 } 1575 } 1576 1577 void BreakpointInfo::clear(methodOop method) { 1578 *method->bcp_from(_bci) = orig_bytecode(); 1579 assert(method->number_of_breakpoints() > 0, "must not go negative"); 1580 method->decr_number_of_breakpoints(); 1581 }