2554 _total_args_passed = total_args_passed; 2555 _saved_sig = NEW_C_HEAP_ARRAY(BasicType, _total_args_passed, mtCode); 2556 memcpy(_saved_sig, sig_bt, _total_args_passed * sizeof(BasicType)); 2557 } 2558 2559 2560 bool AdapterHandlerEntry::compare_code(unsigned char* buffer, int length, int total_args_passed, BasicType* sig_bt) { 2561 if (length != _code_length) { 2562 return false; 2563 } 2564 for (int i = 0; i < length; i++) { 2565 if (buffer[i] != _saved_code[i]) { 2566 return false; 2567 } 2568 } 2569 return true; 2570 } 2571 #endif 2572 2573 2574 // Create a native wrapper for this native method. The wrapper converts the 2575 // java compiled calling convention to the native convention, handlizes 2576 // arguments, and transitions to native. On return from the native we transition 2577 // back to java blocking if a safepoint is in progress. 2578 nmethod *AdapterHandlerLibrary::create_native_wrapper(methodHandle method, int compile_id) { 2579 ResourceMark rm; 2580 nmethod* nm = NULL; 2581 2582 assert(method->is_native(), "must be native"); 2583 assert(method->is_method_handle_intrinsic() || 2584 method->has_native_function(), "must have something valid to call!"); 2585 2586 { 2587 // perform the work while holding the lock, but perform any printing outside the lock 2588 MutexLocker mu(AdapterHandlerLibrary_lock); 2589 // See if somebody beat us to it 2590 nm = method->code(); 2591 if (nm) { 2592 return nm; 2593 } 2594 2595 ResourceMark rm; 2596 2597 BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache 2598 if (buf != NULL) { 2599 CodeBuffer buffer(buf); 2600 double locs_buf[20]; 2601 buffer.insts()->initialize_shared_locs((relocInfo*)locs_buf, sizeof(locs_buf) / sizeof(relocInfo)); 2602 MacroAssembler _masm(&buffer); 2603 2604 // Fill in the signature array, for the calling-convention call. 2605 const int total_args_passed = method->size_of_parameters(); 2606 2607 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_args_passed); 2608 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed); 2609 int i=0; 2610 if( !method->is_static() ) // Pass in receiver first 2611 sig_bt[i++] = T_OBJECT; 2612 SignatureStream ss(method->signature()); 2613 for( ; !ss.at_return_type(); ss.next()) { 2614 sig_bt[i++] = ss.type(); // Collect remaining bits of signature 2615 if( ss.type() == T_LONG || ss.type() == T_DOUBLE ) 2616 sig_bt[i++] = T_VOID; // Longs & doubles take 2 Java slots 2617 } 2618 assert(i == total_args_passed, ""); 2619 BasicType ret_type = ss.type(); 2620 2621 // Now get the compiled-Java layout as input (or output) arguments. 2622 // NOTE: Stubs for compiled entry points of method handle intrinsics 2623 // are just trampolines so the argument registers must be outgoing ones. 2624 const bool is_outgoing = method->is_method_handle_intrinsic(); 2625 int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, is_outgoing); 2626 2627 // Generate the compiled-to-native wrapper code 2628 nm = SharedRuntime::generate_native_wrapper(&_masm, 2629 method, 2630 compile_id, 2631 sig_bt, 2632 regs, 2633 ret_type); 2634 } 2635 } 2636 2637 // Must unlock before calling set_code 2638 2639 // Install the generated code. 2640 if (nm != NULL) { 2641 if (PrintCompilation) { 2642 ttyLocker ttyl; 2643 CompileTask::print_compilation(tty, nm, method->is_static() ? "(static)" : ""); 2644 } 2645 method->set_code(method, nm); 2646 nm->post_compiled_method_load_event(); 2647 } else { 2648 // CodeCache is full, disable compilation 2649 CompileBroker::handle_full_code_cache(); 2650 } 2651 return nm; 2652 } 2653 2654 JRT_ENTRY_NO_ASYNC(void, SharedRuntime::block_for_jni_critical(JavaThread* thread)) 2655 assert(thread == JavaThread::current(), "must be"); 2656 // The code is about to enter a JNI lazy critical native method and 2657 // _needs_gc is true, so if this thread is already in a critical 2658 // section then just return, otherwise this thread should block 2659 // until needs_gc has been cleared. 2660 if (thread->in_critical()) { 2661 return; 2662 } 2663 // Lock and unlock a critical section to give the system a chance to block 2664 GC_locker::lock_critical(thread); 2665 GC_locker::unlock_critical(thread); 2666 JRT_END 2667 2668 #ifdef HAVE_DTRACE_H 2669 // Create a dtrace nmethod for this method. The wrapper converts the 2670 // java compiled calling convention to the native convention, makes a dummy call 2671 // (actually nops for the size of the call instruction, which become a trap if | 2554 _total_args_passed = total_args_passed; 2555 _saved_sig = NEW_C_HEAP_ARRAY(BasicType, _total_args_passed, mtCode); 2556 memcpy(_saved_sig, sig_bt, _total_args_passed * sizeof(BasicType)); 2557 } 2558 2559 2560 bool AdapterHandlerEntry::compare_code(unsigned char* buffer, int length, int total_args_passed, BasicType* sig_bt) { 2561 if (length != _code_length) { 2562 return false; 2563 } 2564 for (int i = 0; i < length; i++) { 2565 if (buffer[i] != _saved_code[i]) { 2566 return false; 2567 } 2568 } 2569 return true; 2570 } 2571 #endif 2572 2573 2574 /** 2575 * Create a native wrapper for this native method. The wrapper converts the 2576 * Java-compiled calling convention to the native convention, handles 2577 * arguments, and transitions to native. On return from the native we transition 2578 * back to java blocking if a safepoint is in progress. 2579 */ 2580 void AdapterHandlerLibrary::create_native_wrapper(methodHandle method) { 2581 ResourceMark rm; 2582 nmethod* nm = NULL; 2583 2584 assert(method->is_native(), "must be native"); 2585 assert(method->is_method_handle_intrinsic() || 2586 method->has_native_function(), "must have something valid to call!"); 2587 2588 { 2589 // Perform the work while holding the lock, but perform any printing outside the lock 2590 MutexLocker mu(AdapterHandlerLibrary_lock); 2591 // See if somebody beat us to it 2592 nm = method->code(); 2593 if (nm != NULL) { 2594 return; 2595 } 2596 2597 const int compile_id = CompileBroker::assign_compile_id(method, CompileBroker::standard_entry_bci); 2598 assert(compile_id > 0, "Must generate native wrapper"); 2599 2600 2601 ResourceMark rm; 2602 BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache 2603 if (buf != NULL) { 2604 CodeBuffer buffer(buf); 2605 double locs_buf[20]; 2606 buffer.insts()->initialize_shared_locs((relocInfo*)locs_buf, sizeof(locs_buf) / sizeof(relocInfo)); 2607 MacroAssembler _masm(&buffer); 2608 2609 // Fill in the signature array, for the calling-convention call. 2610 const int total_args_passed = method->size_of_parameters(); 2611 2612 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_args_passed); 2613 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed); 2614 int i=0; 2615 if( !method->is_static() ) // Pass in receiver first 2616 sig_bt[i++] = T_OBJECT; 2617 SignatureStream ss(method->signature()); 2618 for( ; !ss.at_return_type(); ss.next()) { 2619 sig_bt[i++] = ss.type(); // Collect remaining bits of signature 2620 if( ss.type() == T_LONG || ss.type() == T_DOUBLE ) 2621 sig_bt[i++] = T_VOID; // Longs & doubles take 2 Java slots 2622 } 2623 assert(i == total_args_passed, ""); 2624 BasicType ret_type = ss.type(); 2625 2626 // Now get the compiled-Java layout as input (or output) arguments. 2627 // NOTE: Stubs for compiled entry points of method handle intrinsics 2628 // are just trampolines so the argument registers must be outgoing ones. 2629 const bool is_outgoing = method->is_method_handle_intrinsic(); 2630 int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, is_outgoing); 2631 2632 // Generate the compiled-to-native wrapper code 2633 nm = SharedRuntime::generate_native_wrapper(&_masm, method, compile_id, sig_bt, regs, ret_type); 2634 2635 if (nm != NULL) { 2636 method->set_code(method, nm); 2637 } 2638 } 2639 } // Unlock AdapterHandlerLibrary_lock 2640 2641 2642 // Install the generated code. 2643 if (nm != NULL) { 2644 if (PrintCompilation) { 2645 ttyLocker ttyl; 2646 CompileTask::print_compilation(tty, nm, method->is_static() ? "(static)" : ""); 2647 } 2648 nm->post_compiled_method_load_event(); 2649 } else { 2650 // CodeCache is full, disable compilation 2651 CompileBroker::handle_full_code_cache(); 2652 } 2653 } 2654 2655 JRT_ENTRY_NO_ASYNC(void, SharedRuntime::block_for_jni_critical(JavaThread* thread)) 2656 assert(thread == JavaThread::current(), "must be"); 2657 // The code is about to enter a JNI lazy critical native method and 2658 // _needs_gc is true, so if this thread is already in a critical 2659 // section then just return, otherwise this thread should block 2660 // until needs_gc has been cleared. 2661 if (thread->in_critical()) { 2662 return; 2663 } 2664 // Lock and unlock a critical section to give the system a chance to block 2665 GC_locker::lock_critical(thread); 2666 GC_locker::unlock_critical(thread); 2667 JRT_END 2668 2669 #ifdef HAVE_DTRACE_H 2670 // Create a dtrace nmethod for this method. The wrapper converts the 2671 // java compiled calling convention to the native convention, makes a dummy call 2672 // (actually nops for the size of the call instruction, which become a trap if |