comparison src/share/vm/runtime/sharedRuntime.cpp @ 6266:1d7922586cf6

7023639: JSR 292 method handle invocation needs a fast path for compiled code 6984705: JSR 292 method handle creation should not go through JNI Summary: remove assembly code for JDK 7 chained method handles Reviewed-by: jrose, twisti, kvn, mhaupt Contributed-by: John Rose <john.r.rose@oracle.com>, Christian Thalinger <christian.thalinger@oracle.com>, Michael Haupt <michael.haupt@oracle.com>
author twisti
date Tue, 24 Jul 2012 10:51:00 -0700
parents d2a62e0f25eb
children 93c71eb28866
comparison
equal deleted inserted replaced
6241:aba91a731143 6266:1d7922586cf6
86 RuntimeStub* SharedRuntime::_resolve_opt_virtual_call_blob; 86 RuntimeStub* SharedRuntime::_resolve_opt_virtual_call_blob;
87 RuntimeStub* SharedRuntime::_resolve_virtual_call_blob; 87 RuntimeStub* SharedRuntime::_resolve_virtual_call_blob;
88 RuntimeStub* SharedRuntime::_resolve_static_call_blob; 88 RuntimeStub* SharedRuntime::_resolve_static_call_blob;
89 89
90 DeoptimizationBlob* SharedRuntime::_deopt_blob; 90 DeoptimizationBlob* SharedRuntime::_deopt_blob;
91 RicochetBlob* SharedRuntime::_ricochet_blob;
92
93 SafepointBlob* SharedRuntime::_polling_page_safepoint_handler_blob; 91 SafepointBlob* SharedRuntime::_polling_page_safepoint_handler_blob;
94 SafepointBlob* SharedRuntime::_polling_page_return_handler_blob; 92 SafepointBlob* SharedRuntime::_polling_page_return_handler_blob;
95 93
96 #ifdef COMPILER2 94 #ifdef COMPILER2
97 UncommonTrapBlob* SharedRuntime::_uncommon_trap_blob; 95 UncommonTrapBlob* SharedRuntime::_uncommon_trap_blob;
107 _resolve_static_call_blob = generate_resolve_blob(CAST_FROM_FN_PTR(address, SharedRuntime::resolve_static_call_C), "resolve_static_call"); 105 _resolve_static_call_blob = generate_resolve_blob(CAST_FROM_FN_PTR(address, SharedRuntime::resolve_static_call_C), "resolve_static_call");
108 106
109 _polling_page_safepoint_handler_blob = generate_handler_blob(CAST_FROM_FN_PTR(address, SafepointSynchronize::handle_polling_page_exception), false); 107 _polling_page_safepoint_handler_blob = generate_handler_blob(CAST_FROM_FN_PTR(address, SafepointSynchronize::handle_polling_page_exception), false);
110 _polling_page_return_handler_blob = generate_handler_blob(CAST_FROM_FN_PTR(address, SafepointSynchronize::handle_polling_page_exception), true); 108 _polling_page_return_handler_blob = generate_handler_blob(CAST_FROM_FN_PTR(address, SafepointSynchronize::handle_polling_page_exception), true);
111 109
112 generate_ricochet_blob();
113 generate_deopt_blob(); 110 generate_deopt_blob();
114 111
115 #ifdef COMPILER2 112 #ifdef COMPILER2
116 generate_uncommon_trap_blob(); 113 generate_uncommon_trap_blob();
117 #endif // COMPILER2 114 #endif // COMPILER2
118 } 115 }
119
120 //----------------------------generate_ricochet_blob---------------------------
121 void SharedRuntime::generate_ricochet_blob() {
122 if (!EnableInvokeDynamic) return; // leave it as a null
123
124 // allocate space for the code
125 ResourceMark rm;
126 // setup code generation tools
127 CodeBuffer buffer("ricochet_blob", 256 LP64_ONLY(+ 256), 256); // XXX x86 LP64L: 512, 512
128 MacroAssembler* masm = new MacroAssembler(&buffer);
129
130 int bounce_offset = -1, exception_offset = -1, frame_size_in_words = -1;
131 MethodHandles::RicochetFrame::generate_ricochet_blob(masm, &bounce_offset, &exception_offset, &frame_size_in_words);
132
133 // -------------
134 // make sure all code is generated
135 masm->flush();
136
137 // failed to generate?
138 if (bounce_offset < 0 || exception_offset < 0 || frame_size_in_words < 0) {
139 assert(false, "bad ricochet blob");
140 return;
141 }
142
143 _ricochet_blob = RicochetBlob::create(&buffer, bounce_offset, exception_offset, frame_size_in_words);
144 }
145
146 116
147 #include <math.h> 117 #include <math.h>
148 118
149 #ifndef USDT2 119 #ifndef USDT2
150 HS_DTRACE_PROBE_DECL4(hotspot, object__alloc, Thread*, char*, int, size_t); 120 HS_DTRACE_PROBE_DECL4(hotspot, object__alloc, Thread*, char*, int, size_t);
524 return StubRoutines::catch_exception_entry(); 494 return StubRoutines::catch_exception_entry();
525 } 495 }
526 // Interpreted code 496 // Interpreted code
527 if (Interpreter::contains(return_address)) { 497 if (Interpreter::contains(return_address)) {
528 return Interpreter::rethrow_exception_entry(); 498 return Interpreter::rethrow_exception_entry();
529 }
530 // Ricochet frame unwind code
531 if (SharedRuntime::ricochet_blob() != NULL && SharedRuntime::ricochet_blob()->returns_to_bounce_addr(return_address)) {
532 return SharedRuntime::ricochet_blob()->exception_addr();
533 } 499 }
534 500
535 guarantee(blob == NULL || !blob->is_runtime_stub(), "caller should have skipped stub"); 501 guarantee(blob == NULL || !blob->is_runtime_stub(), "caller should have skipped stub");
536 guarantee(!VtableStubs::contains(return_address), "NULL exceptions in vtables should have been handled already!"); 502 guarantee(!VtableStubs::contains(return_address), "NULL exceptions in vtables should have been handled already!");
537 503
766 java_lang_Throwable::fill_in_stack_trace(exception); 732 java_lang_Throwable::fill_in_stack_trace(exception);
767 } 733 }
768 throw_and_post_jvmti_exception(thread, exception); 734 throw_and_post_jvmti_exception(thread, exception);
769 JRT_END 735 JRT_END
770 736
771 JRT_ENTRY(void, SharedRuntime::throw_WrongMethodTypeException(JavaThread* thread, oopDesc* required, oopDesc* actual))
772 assert(thread == JavaThread::current() && required->is_oop() && actual->is_oop(), "bad args");
773 ResourceMark rm;
774 char* message = SharedRuntime::generate_wrong_method_type_message(thread, required, actual);
775 throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_invoke_WrongMethodTypeException(), message);
776 JRT_END
777
778 address SharedRuntime::continuation_for_implicit_exception(JavaThread* thread, 737 address SharedRuntime::continuation_for_implicit_exception(JavaThread* thread,
779 address pc, 738 address pc,
780 SharedRuntime::ImplicitExceptionKind exception_kind) 739 SharedRuntime::ImplicitExceptionKind exception_kind)
781 { 740 {
782 address target_pc = NULL; 741 address target_pc = NULL;
852 // exception happened inside inline-cache check code 811 // exception happened inside inline-cache check code
853 // => the nmethod is not yet active (i.e., the frame 812 // => the nmethod is not yet active (i.e., the frame
854 // is not set up yet) => use return address pushed by 813 // is not set up yet) => use return address pushed by
855 // caller => don't push another return address 814 // caller => don't push another return address
856 Events::log_exception(thread, "NullPointerException in IC check " INTPTR_FORMAT, pc); 815 Events::log_exception(thread, "NullPointerException in IC check " INTPTR_FORMAT, pc);
816 return StubRoutines::throw_NullPointerException_at_call_entry();
817 }
818
819 if (nm->method()->is_method_handle_intrinsic()) {
820 // exception happened inside MH dispatch code, similar to a vtable stub
821 Events::log_exception(thread, "NullPointerException in MH adapter " INTPTR_FORMAT, pc);
857 return StubRoutines::throw_NullPointerException_at_call_entry(); 822 return StubRoutines::throw_NullPointerException_at_call_entry();
858 } 823 }
859 824
860 #ifndef PRODUCT 825 #ifndef PRODUCT
861 _implicit_null_throws++; 826 _implicit_null_throws++;
1043 Handle nullHandle; //create a handy null handle for exception returns 1008 Handle nullHandle; //create a handy null handle for exception returns
1044 1009
1045 assert(!vfst.at_end(), "Java frame must exist"); 1010 assert(!vfst.at_end(), "Java frame must exist");
1046 1011
1047 // Find caller and bci from vframe 1012 // Find caller and bci from vframe
1048 methodHandle caller (THREAD, vfst.method()); 1013 methodHandle caller(THREAD, vfst.method());
1049 int bci = vfst.bci(); 1014 int bci = vfst.bci();
1050 1015
1051 // Find bytecode 1016 // Find bytecode
1052 Bytecode_invoke bytecode(caller, bci); 1017 Bytecode_invoke bytecode(caller, bci);
1053 bc = bytecode.java_code(); 1018 bc = bytecode.invoke_code();
1054 int bytecode_index = bytecode.index(); 1019 int bytecode_index = bytecode.index();
1055 1020
1056 // Find receiver for non-static call 1021 // Find receiver for non-static call
1057 if (bc != Bytecodes::_invokestatic) { 1022 if (bc != Bytecodes::_invokestatic &&
1023 bc != Bytecodes::_invokedynamic) {
1058 // This register map must be update since we need to find the receiver for 1024 // This register map must be update since we need to find the receiver for
1059 // compiled frames. The receiver might be in a register. 1025 // compiled frames. The receiver might be in a register.
1060 RegisterMap reg_map2(thread); 1026 RegisterMap reg_map2(thread);
1061 frame stubFrame = thread->last_frame(); 1027 frame stubFrame = thread->last_frame();
1062 // Caller-frame is a compiled frame 1028 // Caller-frame is a compiled frame
1073 THROW_(vmSymbols::java_lang_NullPointerException(), nullHandle); 1039 THROW_(vmSymbols::java_lang_NullPointerException(), nullHandle);
1074 } 1040 }
1075 } 1041 }
1076 1042
1077 // Resolve method. This is parameterized by bytecode. 1043 // Resolve method. This is parameterized by bytecode.
1078 constantPoolHandle constants (THREAD, caller->constants()); 1044 constantPoolHandle constants(THREAD, caller->constants());
1079 assert (receiver.is_null() || receiver->is_oop(), "wrong receiver"); 1045 assert(receiver.is_null() || receiver->is_oop(), "wrong receiver");
1080 LinkResolver::resolve_invoke(callinfo, receiver, constants, bytecode_index, bc, CHECK_(nullHandle)); 1046 LinkResolver::resolve_invoke(callinfo, receiver, constants, bytecode_index, bc, CHECK_(nullHandle));
1081 1047
1082 #ifdef ASSERT 1048 #ifdef ASSERT
1083 // Check that the receiver klass is of the right subtype and that it is initialized for virtual calls 1049 // Check that the receiver klass is of the right subtype and that it is initialized for virtual calls
1084 if (bc != Bytecodes::_invokestatic && bc != Bytecodes::_invokedynamic) { 1050 if (bc != Bytecodes::_invokestatic && bc != Bytecodes::_invokedynamic) {
1085 assert(receiver.not_null(), "should have thrown exception"); 1051 assert(receiver.not_null(), "should have thrown exception");
1086 KlassHandle receiver_klass (THREAD, receiver->klass()); 1052 KlassHandle receiver_klass(THREAD, receiver->klass());
1087 klassOop rk = constants->klass_ref_at(bytecode_index, CHECK_(nullHandle)); 1053 klassOop rk = constants->klass_ref_at(bytecode_index, CHECK_(nullHandle));
1088 // klass is already loaded 1054 // klass is already loaded
1089 KlassHandle static_receiver_klass (THREAD, rk); 1055 KlassHandle static_receiver_klass(THREAD, rk);
1090 assert(receiver_klass->is_subtype_of(static_receiver_klass()), "actual receiver must be subclass of static receiver klass"); 1056 // Method handle invokes might have been optimized to a direct call
1057 // so don't check for the receiver class.
1058 // FIXME this weakens the assert too much
1059 methodHandle callee = callinfo.selected_method();
1060 assert(receiver_klass->is_subtype_of(static_receiver_klass()) ||
1061 callee->is_method_handle_intrinsic() ||
1062 callee->is_compiled_lambda_form(),
1063 "actual receiver must be subclass of static receiver klass");
1091 if (receiver_klass->oop_is_instance()) { 1064 if (receiver_klass->oop_is_instance()) {
1092 if (instanceKlass::cast(receiver_klass())->is_not_initialized()) { 1065 if (instanceKlass::cast(receiver_klass())->is_not_initialized()) {
1093 tty->print_cr("ERROR: Klass not yet initialized!!"); 1066 tty->print_cr("ERROR: Klass not yet initialized!!");
1094 receiver_klass.print(); 1067 receiver_klass.print();
1095 } 1068 }
1096 assert (!instanceKlass::cast(receiver_klass())->is_not_initialized(), "receiver_klass must be initialized"); 1069 assert(!instanceKlass::cast(receiver_klass())->is_not_initialized(), "receiver_klass must be initialized");
1097 } 1070 }
1098 } 1071 }
1099 #endif 1072 #endif
1100 1073
1101 return receiver; 1074 return receiver;
1184 Bytecodes::Code invoke_code = Bytecodes::_illegal; 1157 Bytecodes::Code invoke_code = Bytecodes::_illegal;
1185 Handle receiver = find_callee_info(thread, invoke_code, 1158 Handle receiver = find_callee_info(thread, invoke_code,
1186 call_info, CHECK_(methodHandle())); 1159 call_info, CHECK_(methodHandle()));
1187 methodHandle callee_method = call_info.selected_method(); 1160 methodHandle callee_method = call_info.selected_method();
1188 1161
1189 assert((!is_virtual && invoke_code == Bytecodes::_invokestatic) || 1162 assert((!is_virtual && invoke_code == Bytecodes::_invokestatic ) ||
1190 ( is_virtual && invoke_code != Bytecodes::_invokestatic), "inconsistent bytecode"); 1163 (!is_virtual && invoke_code == Bytecodes::_invokehandle ) ||
1164 (!is_virtual && invoke_code == Bytecodes::_invokedynamic) ||
1165 ( is_virtual && invoke_code != Bytecodes::_invokestatic ), "inconsistent bytecode");
1191 1166
1192 #ifndef PRODUCT 1167 #ifndef PRODUCT
1193 // tracing/debugging/statistics 1168 // tracing/debugging/statistics
1194 int *addr = (is_optimized) ? (&_resolve_opt_virtual_ctr) : 1169 int *addr = (is_optimized) ? (&_resolve_opt_virtual_ctr) :
1195 (is_virtual) ? (&_resolve_virtual_ctr) : 1170 (is_virtual) ? (&_resolve_virtual_ctr) :
1200 ResourceMark rm(thread); 1175 ResourceMark rm(thread);
1201 tty->print("resolving %s%s (%s) call to", 1176 tty->print("resolving %s%s (%s) call to",
1202 (is_optimized) ? "optimized " : "", (is_virtual) ? "virtual" : "static", 1177 (is_optimized) ? "optimized " : "", (is_virtual) ? "virtual" : "static",
1203 Bytecodes::name(invoke_code)); 1178 Bytecodes::name(invoke_code));
1204 callee_method->print_short_name(tty); 1179 callee_method->print_short_name(tty);
1205 tty->print_cr(" code: " INTPTR_FORMAT, callee_method->code()); 1180 tty->print_cr(" at pc: " INTPTR_FORMAT " to code: " INTPTR_FORMAT, caller_frame.pc(), callee_method->code());
1206 } 1181 }
1207 #endif 1182 #endif
1208 1183
1209 // JSR 292 1184 // JSR 292 key invariant:
1210 // If the resolved method is a MethodHandle invoke target the call 1185 // If the resolved method is a MethodHandle invoke target the call
1211 // site must be a MethodHandle call site. 1186 // site must be a MethodHandle call site, because the lambda form might tail-call
1212 if (callee_method->is_method_handle_invoke()) { 1187 // leaving the stack in a state unknown to either caller or callee
1213 assert(caller_nm->is_method_handle_return(caller_frame.pc()), "must be MH call site"); 1188 // TODO detune for now but we might need it again
1214 } 1189 // assert(!callee_method->is_compiled_lambda_form() ||
1190 // caller_nm->is_method_handle_return(caller_frame.pc()), "must be MH call site");
1215 1191
1216 // Compute entry points. This might require generation of C2I converter 1192 // Compute entry points. This might require generation of C2I converter
1217 // frames, so we cannot be holding any locks here. Furthermore, the 1193 // frames, so we cannot be holding any locks here. Furthermore, the
1218 // computation of the entry points is independent of patching the call. We 1194 // computation of the entry points is independent of patching the call. We
1219 // always return the entry-point, but we only patch the stub if the call has 1195 // always return the entry-point, but we only patch the stub if the call has
1282 RegisterMap reg_map(thread, false); 1258 RegisterMap reg_map(thread, false);
1283 frame stub_frame = thread->last_frame(); 1259 frame stub_frame = thread->last_frame();
1284 assert(stub_frame.is_runtime_frame(), "sanity check"); 1260 assert(stub_frame.is_runtime_frame(), "sanity check");
1285 frame caller_frame = stub_frame.sender(&reg_map); 1261 frame caller_frame = stub_frame.sender(&reg_map);
1286 assert(!caller_frame.is_interpreted_frame() && !caller_frame.is_entry_frame(), "unexpected frame"); 1262 assert(!caller_frame.is_interpreted_frame() && !caller_frame.is_entry_frame(), "unexpected frame");
1287 assert(!caller_frame.is_ricochet_frame(), "unexpected frame");
1288 #endif /* ASSERT */ 1263 #endif /* ASSERT */
1289 1264
1290 methodHandle callee_method; 1265 methodHandle callee_method;
1291 JRT_BLOCK 1266 JRT_BLOCK
1292 callee_method = SharedRuntime::handle_ic_miss_helper(thread, CHECK_NULL); 1267 callee_method = SharedRuntime::handle_ic_miss_helper(thread, CHECK_NULL);
1318 // MethodHandle invokes don't have a CompiledIC and should always 1293 // MethodHandle invokes don't have a CompiledIC and should always
1319 // simply redispatch to the callee_target. 1294 // simply redispatch to the callee_target.
1320 address sender_pc = caller_frame.pc(); 1295 address sender_pc = caller_frame.pc();
1321 CodeBlob* sender_cb = caller_frame.cb(); 1296 CodeBlob* sender_cb = caller_frame.cb();
1322 nmethod* sender_nm = sender_cb->as_nmethod_or_null(); 1297 nmethod* sender_nm = sender_cb->as_nmethod_or_null();
1323 bool is_mh_invoke_via_adapter = false; // Direct c2c call or via adapter?
1324 if (sender_nm != NULL && sender_nm->is_method_handle_return(sender_pc)) {
1325 // If the callee_target is set, then we have come here via an i2c
1326 // adapter.
1327 methodOop callee = thread->callee_target();
1328 if (callee != NULL) {
1329 assert(callee->is_method(), "sanity");
1330 is_mh_invoke_via_adapter = true;
1331 }
1332 }
1333 1298
1334 if (caller_frame.is_interpreted_frame() || 1299 if (caller_frame.is_interpreted_frame() ||
1335 caller_frame.is_entry_frame() || 1300 caller_frame.is_entry_frame()) {
1336 caller_frame.is_ricochet_frame() ||
1337 is_mh_invoke_via_adapter) {
1338 methodOop callee = thread->callee_target(); 1301 methodOop callee = thread->callee_target();
1339 guarantee(callee != NULL && callee->is_method(), "bad handshake"); 1302 guarantee(callee != NULL && callee->is_method(), "bad handshake");
1340 thread->set_vm_result(callee); 1303 thread->set_vm_result(callee);
1341 thread->set_callee_target(NULL); 1304 thread->set_callee_target(NULL);
1342 return callee->get_c2i_entry(); 1305 return callee->get_c2i_entry();
1674 nmethod* nm = cb->as_nmethod_or_null(); 1637 nmethod* nm = cb->as_nmethod_or_null();
1675 assert(nm, "must be"); 1638 assert(nm, "must be");
1676 1639
1677 // Get the return PC for the passed caller PC. 1640 // Get the return PC for the passed caller PC.
1678 address return_pc = caller_pc + frame::pc_return_offset; 1641 address return_pc = caller_pc + frame::pc_return_offset;
1679
1680 // Don't fixup method handle call sites as the executed method
1681 // handle adapters are doing the required MethodHandle chain work.
1682 if (nm->is_method_handle_return(return_pc)) {
1683 return;
1684 }
1685 1642
1686 // There is a benign race here. We could be attempting to patch to a compiled 1643 // There is a benign race here. We could be attempting to patch to a compiled
1687 // entry point at the same time the callee is being deoptimized. If that is 1644 // entry point at the same time the callee is being deoptimized. If that is
1688 // the case then entry_point may in fact point to a c2i and we'd patch the 1645 // the case then entry_point may in fact point to a c2i and we'd patch the
1689 // call site with the same old data. clear_code will set code() to NULL 1646 // call site with the same old data. clear_code will set code() to NULL
1786 Klass* targetKlass = Klass::cast(vfst.method()->constants()->klass_at( 1743 Klass* targetKlass = Klass::cast(vfst.method()->constants()->klass_at(
1787 cc.index(), thread)); 1744 cc.index(), thread));
1788 return generate_class_cast_message(objName, targetKlass->external_name()); 1745 return generate_class_cast_message(objName, targetKlass->external_name());
1789 } 1746 }
1790 1747
1791 char* SharedRuntime::generate_wrong_method_type_message(JavaThread* thread,
1792 oopDesc* required,
1793 oopDesc* actual) {
1794 if (TraceMethodHandles) {
1795 tty->print_cr("WrongMethodType thread="PTR_FORMAT" req="PTR_FORMAT" act="PTR_FORMAT"",
1796 thread, required, actual);
1797 }
1798 assert(EnableInvokeDynamic, "");
1799 oop singleKlass = wrong_method_type_is_for_single_argument(thread, required);
1800 char* message = NULL;
1801 if (singleKlass != NULL) {
1802 const char* objName = "argument or return value";
1803 if (actual != NULL) {
1804 // be flexible about the junk passed in:
1805 klassOop ak = (actual->is_klass()
1806 ? (klassOop)actual
1807 : actual->klass());
1808 objName = Klass::cast(ak)->external_name();
1809 }
1810 Klass* targetKlass = Klass::cast(required->is_klass()
1811 ? (klassOop)required
1812 : java_lang_Class::as_klassOop(required));
1813 message = generate_class_cast_message(objName, targetKlass->external_name());
1814 } else {
1815 // %%% need to get the MethodType string, without messing around too much
1816 const char* desc = NULL;
1817 // Get a signature from the invoke instruction
1818 const char* mhName = "method handle";
1819 const char* targetType = "the required signature";
1820 int targetArity = -1, mhArity = -1;
1821 vframeStream vfst(thread, true);
1822 if (!vfst.at_end()) {
1823 Bytecode_invoke call(vfst.method(), vfst.bci());
1824 methodHandle target;
1825 {
1826 EXCEPTION_MARK;
1827 target = call.static_target(THREAD);
1828 if (HAS_PENDING_EXCEPTION) { CLEAR_PENDING_EXCEPTION; }
1829 }
1830 if (target.not_null()
1831 && target->is_method_handle_invoke()
1832 && required == target->method_handle_type()) {
1833 targetType = target->signature()->as_C_string();
1834 targetArity = ArgumentCount(target->signature()).size();
1835 }
1836 }
1837 KlassHandle kignore; int dmf_flags = 0;
1838 methodHandle actual_method = MethodHandles::decode_method(actual, kignore, dmf_flags);
1839 if ((dmf_flags & ~(MethodHandles::_dmf_has_receiver |
1840 MethodHandles::_dmf_does_dispatch |
1841 MethodHandles::_dmf_from_interface)) != 0)
1842 actual_method = methodHandle(); // MH does extra binds, drops, etc.
1843 bool has_receiver = ((dmf_flags & MethodHandles::_dmf_has_receiver) != 0);
1844 if (actual_method.not_null()) {
1845 mhName = actual_method->signature()->as_C_string();
1846 mhArity = ArgumentCount(actual_method->signature()).size();
1847 if (!actual_method->is_static()) mhArity += 1;
1848 } else if (java_lang_invoke_MethodHandle::is_instance(actual)) {
1849 oopDesc* mhType = java_lang_invoke_MethodHandle::type(actual);
1850 mhArity = java_lang_invoke_MethodType::ptype_count(mhType);
1851 stringStream st;
1852 java_lang_invoke_MethodType::print_signature(mhType, &st);
1853 mhName = st.as_string();
1854 }
1855 if (targetArity != -1 && targetArity != mhArity) {
1856 if (has_receiver && targetArity == mhArity-1)
1857 desc = " cannot be called without a receiver argument as ";
1858 else
1859 desc = " cannot be called with a different arity as ";
1860 }
1861 message = generate_class_cast_message(mhName, targetType,
1862 desc != NULL ? desc :
1863 " cannot be called as ");
1864 }
1865 if (TraceMethodHandles) {
1866 tty->print_cr("WrongMethodType => message=%s", message);
1867 }
1868 return message;
1869 }
1870
1871 oop SharedRuntime::wrong_method_type_is_for_single_argument(JavaThread* thr,
1872 oopDesc* required) {
1873 if (required == NULL) return NULL;
1874 if (required->klass() == SystemDictionary::Class_klass())
1875 return required;
1876 if (required->is_klass())
1877 return Klass::cast(klassOop(required))->java_mirror();
1878 return NULL;
1879 }
1880
1881
1882 char* SharedRuntime::generate_class_cast_message( 1748 char* SharedRuntime::generate_class_cast_message(
1883 const char* objName, const char* targetKlassName, const char* desc) { 1749 const char* objName, const char* targetKlassName, const char* desc) {
1884 size_t msglen = strlen(objName) + strlen(desc) + strlen(targetKlassName) + 1; 1750 size_t msglen = strlen(objName) + strlen(desc) + strlen(targetKlassName) + 1;
1885 1751
1886 char* message = NEW_RESOURCE_ARRAY(char, msglen); 1752 char* message = NEW_RESOURCE_ARRAY(char, msglen);
2117 1983
2118 // A simple wrapper class around the calling convention information 1984 // A simple wrapper class around the calling convention information
2119 // that allows sharing of adapters for the same calling convention. 1985 // that allows sharing of adapters for the same calling convention.
2120 class AdapterFingerPrint : public CHeapObj<mtCode> { 1986 class AdapterFingerPrint : public CHeapObj<mtCode> {
2121 private: 1987 private:
1988 enum {
1989 _basic_type_bits = 4,
1990 _basic_type_mask = right_n_bits(_basic_type_bits),
1991 _basic_types_per_int = BitsPerInt / _basic_type_bits,
1992 _compact_int_count = 3
1993 };
1994 // TO DO: Consider integrating this with a more global scheme for compressing signatures.
1995 // For now, 4 bits per components (plus T_VOID gaps after double/long) is not excessive.
1996
2122 union { 1997 union {
2123 int _compact[3]; 1998 int _compact[_compact_int_count];
2124 int* _fingerprint; 1999 int* _fingerprint;
2125 } _value; 2000 } _value;
2126 int _length; // A negative length indicates the fingerprint is in the compact form, 2001 int _length; // A negative length indicates the fingerprint is in the compact form,
2127 // Otherwise _value._fingerprint is the array. 2002 // Otherwise _value._fingerprint is the array.
2128 2003
2129 // Remap BasicTypes that are handled equivalently by the adapters. 2004 // Remap BasicTypes that are handled equivalently by the adapters.
2130 // These are correct for the current system but someday it might be 2005 // These are correct for the current system but someday it might be
2131 // necessary to make this mapping platform dependent. 2006 // necessary to make this mapping platform dependent.
2132 static BasicType adapter_encoding(BasicType in) { 2007 static int adapter_encoding(BasicType in) {
2133 assert((~0xf & in) == 0, "must fit in 4 bits");
2134 switch(in) { 2008 switch(in) {
2135 case T_BOOLEAN: 2009 case T_BOOLEAN:
2136 case T_BYTE: 2010 case T_BYTE:
2137 case T_SHORT: 2011 case T_SHORT:
2138 case T_CHAR: 2012 case T_CHAR:
2139 // There are all promoted to T_INT in the calling convention 2013 // There are all promoted to T_INT in the calling convention
2140 return T_INT; 2014 return T_INT;
2141 2015
2142 case T_OBJECT: 2016 case T_OBJECT:
2143 case T_ARRAY: 2017 case T_ARRAY:
2018 // In other words, we assume that any register good enough for
2019 // an int or long is good enough for a managed pointer.
2144 #ifdef _LP64 2020 #ifdef _LP64
2145 return T_LONG; 2021 return T_LONG;
2146 #else 2022 #else
2147 return T_INT; 2023 return T_INT;
2148 #endif 2024 #endif
2163 public: 2039 public:
2164 AdapterFingerPrint(int total_args_passed, BasicType* sig_bt) { 2040 AdapterFingerPrint(int total_args_passed, BasicType* sig_bt) {
2165 // The fingerprint is based on the BasicType signature encoded 2041 // The fingerprint is based on the BasicType signature encoded
2166 // into an array of ints with eight entries per int. 2042 // into an array of ints with eight entries per int.
2167 int* ptr; 2043 int* ptr;
2168 int len = (total_args_passed + 7) >> 3; 2044 int len = (total_args_passed + (_basic_types_per_int-1)) / _basic_types_per_int;
2169 if (len <= (int)(sizeof(_value._compact) / sizeof(int))) { 2045 if (len <= _compact_int_count) {
2046 assert(_compact_int_count == 3, "else change next line");
2170 _value._compact[0] = _value._compact[1] = _value._compact[2] = 0; 2047 _value._compact[0] = _value._compact[1] = _value._compact[2] = 0;
2171 // Storing the signature encoded as signed chars hits about 98% 2048 // Storing the signature encoded as signed chars hits about 98%
2172 // of the time. 2049 // of the time.
2173 _length = -len; 2050 _length = -len;
2174 ptr = _value._compact; 2051 ptr = _value._compact;
2180 2057
2181 // Now pack the BasicTypes with 8 per int 2058 // Now pack the BasicTypes with 8 per int
2182 int sig_index = 0; 2059 int sig_index = 0;
2183 for (int index = 0; index < len; index++) { 2060 for (int index = 0; index < len; index++) {
2184 int value = 0; 2061 int value = 0;
2185 for (int byte = 0; byte < 8; byte++) { 2062 for (int byte = 0; byte < _basic_types_per_int; byte++) {
2186 if (sig_index < total_args_passed) { 2063 int bt = ((sig_index < total_args_passed)
2187 value = (value << 4) | adapter_encoding(sig_bt[sig_index++]); 2064 ? adapter_encoding(sig_bt[sig_index++])
2188 } 2065 : 0);
2066 assert((bt & _basic_type_mask) == bt, "must fit in 4 bits");
2067 value = (value << _basic_type_bits) | bt;
2189 } 2068 }
2190 ptr[index] = value; 2069 ptr[index] = value;
2191 } 2070 }
2192 } 2071 }
2193 2072
2233 bool equals(AdapterFingerPrint* other) { 2112 bool equals(AdapterFingerPrint* other) {
2234 if (other->_length != _length) { 2113 if (other->_length != _length) {
2235 return false; 2114 return false;
2236 } 2115 }
2237 if (_length < 0) { 2116 if (_length < 0) {
2117 assert(_compact_int_count == 3, "else change next line");
2238 return _value._compact[0] == other->_value._compact[0] && 2118 return _value._compact[0] == other->_value._compact[0] &&
2239 _value._compact[1] == other->_value._compact[1] && 2119 _value._compact[1] == other->_value._compact[1] &&
2240 _value._compact[2] == other->_value._compact[2]; 2120 _value._compact[2] == other->_value._compact[2];
2241 } else { 2121 } else {
2242 for (int i = 0; i < _length; i++) { 2122 for (int i = 0; i < _length; i++) {
2529 return NULL; // Out of CodeCache space 2409 return NULL; // Out of CodeCache space
2530 } 2410 }
2531 entry->relocate(B->content_begin()); 2411 entry->relocate(B->content_begin());
2532 #ifndef PRODUCT 2412 #ifndef PRODUCT
2533 // debugging suppport 2413 // debugging suppport
2534 if (PrintAdapterHandlers) { 2414 if (PrintAdapterHandlers || PrintStubCode) {
2535 tty->cr(); 2415 entry->print_adapter_on(tty);
2536 tty->print_cr("i2c argument handler #%d for: %s %s (fingerprint = %s, %d bytes generated)", 2416 tty->print_cr("i2c argument handler #%d for: %s %s (%d bytes generated)",
2537 _adapters->number_of_entries(), (method->is_static() ? "static" : "receiver"), 2417 _adapters->number_of_entries(), (method->is_static() ? "static" : "receiver"),
2538 method->signature()->as_C_string(), fingerprint->as_string(), insts_size ); 2418 method->signature()->as_C_string(), insts_size);
2539 tty->print_cr("c2i argument handler starts at %p",entry->get_c2i_entry()); 2419 tty->print_cr("c2i argument handler starts at %p",entry->get_c2i_entry());
2540 Disassembler::decode(entry->get_i2c_entry(), entry->get_i2c_entry() + insts_size); 2420 if (Verbose || PrintStubCode) {
2421 address first_pc = entry->base_address();
2422 if (first_pc != NULL)
2423 Disassembler::decode(first_pc, first_pc + insts_size);
2424 }
2541 } 2425 }
2542 #endif 2426 #endif
2543 2427
2544 _adapters->add(entry); 2428 _adapters->add(entry);
2545 } 2429 }
2559 } 2443 }
2560 } 2444 }
2561 return entry; 2445 return entry;
2562 } 2446 }
2563 2447
2448 address AdapterHandlerEntry::base_address() {
2449 address base = _i2c_entry;
2450 if (base == NULL) base = _c2i_entry;
2451 assert(base <= _c2i_entry || _c2i_entry == NULL, "");
2452 assert(base <= _c2i_unverified_entry || _c2i_unverified_entry == NULL, "");
2453 return base;
2454 }
2455
2564 void AdapterHandlerEntry::relocate(address new_base) { 2456 void AdapterHandlerEntry::relocate(address new_base) {
2565 ptrdiff_t delta = new_base - _i2c_entry; 2457 address old_base = base_address();
2458 assert(old_base != NULL, "");
2459 ptrdiff_t delta = new_base - old_base;
2460 if (_i2c_entry != NULL)
2566 _i2c_entry += delta; 2461 _i2c_entry += delta;
2462 if (_c2i_entry != NULL)
2567 _c2i_entry += delta; 2463 _c2i_entry += delta;
2464 if (_c2i_unverified_entry != NULL)
2568 _c2i_unverified_entry += delta; 2465 _c2i_unverified_entry += delta;
2466 assert(base_address() == new_base, "");
2569 } 2467 }
2570 2468
2571 2469
2572 void AdapterHandlerEntry::deallocate() { 2470 void AdapterHandlerEntry::deallocate() {
2573 delete _fingerprint; 2471 delete _fingerprint;
2612 // back to java blocking if a safepoint is in progress. 2510 // back to java blocking if a safepoint is in progress.
2613 nmethod *AdapterHandlerLibrary::create_native_wrapper(methodHandle method, int compile_id) { 2511 nmethod *AdapterHandlerLibrary::create_native_wrapper(methodHandle method, int compile_id) {
2614 ResourceMark rm; 2512 ResourceMark rm;
2615 nmethod* nm = NULL; 2513 nmethod* nm = NULL;
2616 2514
2617 assert(method->has_native_function(), "must have something valid to call!"); 2515 assert(method->is_native(), "must be native");
2516 assert(method->is_method_handle_intrinsic() ||
2517 method->has_native_function(), "must have something valid to call!");
2618 2518
2619 { 2519 {
2620 // perform the work while holding the lock, but perform any printing outside the lock 2520 // perform the work while holding the lock, but perform any printing outside the lock
2621 MutexLocker mu(AdapterHandlerLibrary_lock); 2521 MutexLocker mu(AdapterHandlerLibrary_lock);
2622 // See if somebody beat us to it 2522 // See if somebody beat us to it
2649 sig_bt[i++] = T_VOID; // Longs & doubles take 2 Java slots 2549 sig_bt[i++] = T_VOID; // Longs & doubles take 2 Java slots
2650 } 2550 }
2651 assert( i==total_args_passed, "" ); 2551 assert( i==total_args_passed, "" );
2652 BasicType ret_type = ss.type(); 2552 BasicType ret_type = ss.type();
2653 2553
2654 // Now get the compiled-Java layout as input arguments 2554 // Now get the compiled-Java layout as input (or output) arguments.
2655 int comp_args_on_stack; 2555 // NOTE: Stubs for compiled entry points of method handle intrinsics
2656 comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, false); 2556 // are just trampolines so the argument registers must be outgoing ones.
2557 const bool is_outgoing = method->is_method_handle_intrinsic();
2558 int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, is_outgoing);
2657 2559
2658 // Generate the compiled-to-native wrapper code 2560 // Generate the compiled-to-native wrapper code
2659 nm = SharedRuntime::generate_native_wrapper(&_masm, 2561 nm = SharedRuntime::generate_native_wrapper(&_masm,
2660 method, 2562 method,
2661 compile_id, 2563 compile_id,
2937 2839
2938 void AdapterHandlerLibrary::print_handler_on(outputStream* st, CodeBlob* b) { 2840 void AdapterHandlerLibrary::print_handler_on(outputStream* st, CodeBlob* b) {
2939 AdapterHandlerTableIterator iter(_adapters); 2841 AdapterHandlerTableIterator iter(_adapters);
2940 while (iter.has_next()) { 2842 while (iter.has_next()) {
2941 AdapterHandlerEntry* a = iter.next(); 2843 AdapterHandlerEntry* a = iter.next();
2942 if ( b == CodeCache::find_blob(a->get_i2c_entry()) ) { 2844 if (b == CodeCache::find_blob(a->get_i2c_entry())) {
2943 st->print("Adapter for signature: "); 2845 st->print("Adapter for signature: ");
2944 st->print_cr("%s i2c: " INTPTR_FORMAT " c2i: " INTPTR_FORMAT " c2iUV: " INTPTR_FORMAT, 2846 a->print_adapter_on(tty);
2945 a->fingerprint()->as_string(),
2946 a->get_i2c_entry(), a->get_c2i_entry(), a->get_c2i_unverified_entry());
2947
2948 return; 2847 return;
2949 } 2848 }
2950 } 2849 }
2951 assert(false, "Should have found handler"); 2850 assert(false, "Should have found handler");
2851 }
2852
2853 void AdapterHandlerEntry::print_adapter_on(outputStream* st) const {
2854 st->print_cr("AHE@" INTPTR_FORMAT ": %s i2c: " INTPTR_FORMAT " c2i: " INTPTR_FORMAT " c2iUV: " INTPTR_FORMAT,
2855 (intptr_t) this, fingerprint()->as_string(),
2856 get_i2c_entry(), get_c2i_entry(), get_c2i_unverified_entry());
2857
2952 } 2858 }
2953 2859
2954 #ifndef PRODUCT 2860 #ifndef PRODUCT
2955 2861
2956 void AdapterHandlerLibrary::print_statistics() { 2862 void AdapterHandlerLibrary::print_statistics() {