comparison src/share/vm/oops/methodOop.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 161ae369407d
children 977007096840
comparison
equal deleted inserted replaced
6241:aba91a731143 6266:1d7922586cf6
38 #include "oops/methodDataOop.hpp" 38 #include "oops/methodDataOop.hpp"
39 #include "oops/methodOop.hpp" 39 #include "oops/methodOop.hpp"
40 #include "oops/oop.inline.hpp" 40 #include "oops/oop.inline.hpp"
41 #include "oops/symbol.hpp" 41 #include "oops/symbol.hpp"
42 #include "prims/jvmtiExport.hpp" 42 #include "prims/jvmtiExport.hpp"
43 #include "prims/methodHandleWalk.hpp" 43 #include "prims/methodHandles.hpp"
44 #include "prims/nativeLookup.hpp" 44 #include "prims/nativeLookup.hpp"
45 #include "runtime/arguments.hpp" 45 #include "runtime/arguments.hpp"
46 #include "runtime/compilationPolicy.hpp" 46 #include "runtime/compilationPolicy.hpp"
47 #include "runtime/frame.inline.hpp" 47 #include "runtime/frame.inline.hpp"
48 #include "runtime/handles.inline.hpp" 48 #include "runtime/handles.inline.hpp"
550 } 550 }
551 551
552 552
553 void methodOopDesc::set_native_function(address function, bool post_event_flag) { 553 void methodOopDesc::set_native_function(address function, bool post_event_flag) {
554 assert(function != NULL, "use clear_native_function to unregister natives"); 554 assert(function != NULL, "use clear_native_function to unregister natives");
555 assert(!is_method_handle_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
555 address* native_function = native_function_addr(); 556 address* native_function = native_function_addr();
556 557
557 // We can see racers trying to place the same native function into place. Once 558 // We can see racers trying to place the same native function into place. Once
558 // is plenty. 559 // is plenty.
559 address current = *native_function; 560 address current = *native_function;
579 } 580 }
580 } 581 }
581 582
582 583
583 bool methodOopDesc::has_native_function() const { 584 bool methodOopDesc::has_native_function() const {
585 assert(!is_method_handle_intrinsic(), "");
584 address func = native_function(); 586 address func = native_function();
585 return (func != NULL && func != SharedRuntime::native_method_throw_unsatisfied_link_error_entry()); 587 return (func != NULL && func != SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
586 } 588 }
587 589
588 590
589 void methodOopDesc::clear_native_function() { 591 void methodOopDesc::clear_native_function() {
592 // Note: is_method_handle_intrinsic() is allowed here.
590 set_native_function( 593 set_native_function(
591 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), 594 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
592 !native_bind_event_is_interesting); 595 !native_bind_event_is_interesting);
593 clear_code(); 596 clear_code();
594 } 597 }
604 *signature_handler = handler; 607 *signature_handler = handler;
605 } 608 }
606 609
607 610
608 bool methodOopDesc::is_not_compilable(int comp_level) const { 611 bool methodOopDesc::is_not_compilable(int comp_level) const {
609 if (is_method_handle_invoke()) {
610 // compilers must recognize this method specially, or not at all
611 return true;
612 }
613 if (number_of_breakpoints() > 0) { 612 if (number_of_breakpoints() > 0) {
614 return true; 613 return true;
615 } 614 }
616 if (comp_level == CompLevel_any) { 615 if (comp_level == CompLevel_any) {
617 return is_not_c1_compilable() || is_not_c2_compilable(); 616 return is_not_c1_compilable() || is_not_c2_compilable();
707 assert(this == h_method(), "wrong h_method()" ); 706 assert(this == h_method(), "wrong h_method()" );
708 address entry = Interpreter::entry_for_method(h_method); 707 address entry = Interpreter::entry_for_method(h_method);
709 assert(entry != NULL, "interpreter entry must be non-null"); 708 assert(entry != NULL, "interpreter entry must be non-null");
710 // Sets both _i2i_entry and _from_interpreted_entry 709 // Sets both _i2i_entry and _from_interpreted_entry
711 set_interpreter_entry(entry); 710 set_interpreter_entry(entry);
712 if (is_native() && !is_method_handle_invoke()) { 711 if (is_native() && !is_method_handle_intrinsic()) {
713 set_native_function( 712 set_native_function(
714 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), 713 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
715 !native_bind_event_is_interesting); 714 !native_bind_event_is_interesting);
716 } 715 }
717 716
795 } 794 }
796 795
797 OrderAccess::storestore(); 796 OrderAccess::storestore();
798 #ifdef SHARK 797 #ifdef SHARK
799 mh->_from_interpreted_entry = code->insts_begin(); 798 mh->_from_interpreted_entry = code->insts_begin();
800 #else 799 #else //!SHARK
801 mh->_from_compiled_entry = code->verified_entry_point(); 800 mh->_from_compiled_entry = code->verified_entry_point();
802 OrderAccess::storestore(); 801 OrderAccess::storestore();
803 // Instantly compiled code can execute. 802 // Instantly compiled code can execute.
804 mh->_from_interpreted_entry = mh->get_i2c_entry(); 803 if (!mh->is_method_handle_intrinsic())
805 #endif // SHARK 804 mh->_from_interpreted_entry = mh->get_i2c_entry();
806 805 #endif //!SHARK
807 } 806 }
808 807
809 808
810 bool methodOopDesc::is_overridden_in(klassOop k) const { 809 bool methodOopDesc::is_overridden_in(klassOop k) const {
811 instanceKlass* ik = instanceKlass::cast(k); 810 instanceKlass* ik = instanceKlass::cast(k);
853 852
854 // caching this method should be just fine 853 // caching this method should be just fine
855 return false; 854 return false;
856 } 855 }
857 856
858 bool methodOopDesc::is_method_handle_invoke_name(vmSymbols::SID name_sid) {
859 switch (name_sid) {
860 case vmSymbols::VM_SYMBOL_ENUM_NAME(invokeExact_name):
861 case vmSymbols::VM_SYMBOL_ENUM_NAME(invoke_name):
862 return true;
863 }
864 if (AllowInvokeGeneric
865 && name_sid == vmSymbols::VM_SYMBOL_ENUM_NAME(invokeGeneric_name))
866 return true;
867 return false;
868 }
869
870 // Constant pool structure for invoke methods: 857 // Constant pool structure for invoke methods:
871 enum { 858 enum {
872 _imcp_invoke_name = 1, // utf8: 'invokeExact' or 'invokeGeneric' 859 _imcp_invoke_name = 1, // utf8: 'invokeExact', etc.
873 _imcp_invoke_signature, // utf8: (variable Symbol*) 860 _imcp_invoke_signature, // utf8: (variable Symbol*)
874 _imcp_method_type_value, // string: (variable java/lang/invoke/MethodType, sic)
875 _imcp_limit 861 _imcp_limit
876 }; 862 };
877 863
878 oop methodOopDesc::method_handle_type() const { 864 // Test if this method is an MH adapter frame generated by Java code.
879 if (!is_method_handle_invoke()) { assert(false, "caller resp."); return NULL; } 865 // Cf. java/lang/invoke/InvokerBytecodeGenerator
880 oop mt = constants()->resolved_string_at(_imcp_method_type_value); 866 bool methodOopDesc::is_compiled_lambda_form() const {
881 assert(mt->klass() == SystemDictionary::MethodType_klass(), ""); 867 return intrinsic_id() == vmIntrinsics::_compiledLambdaForm;
882 return mt; 868 }
883 } 869
884 870 // Test if this method is an internal MH primitive method.
885 jint* methodOopDesc::method_type_offsets_chain() { 871 bool methodOopDesc::is_method_handle_intrinsic() const {
886 static jint pchase[] = { -1, -1, -1, -1 }; 872 vmIntrinsics::ID iid = intrinsic_id();
887 if (pchase[0] == -1) { 873 return (MethodHandles::is_signature_polymorphic(iid) &&
888 jint step0 = in_bytes(const_offset()); 874 MethodHandles::is_signature_polymorphic_intrinsic(iid));
889 jint step1 = in_bytes(constMethodOopDesc::constants_offset()); 875 }
890 jint step2 = (constantPoolOopDesc::header_size() + _imcp_method_type_value) * HeapWordSize; 876
891 // do this in reverse to avoid races: 877 bool methodOopDesc::has_member_arg() const {
892 OrderAccess::release_store(&pchase[2], step2); 878 vmIntrinsics::ID iid = intrinsic_id();
893 OrderAccess::release_store(&pchase[1], step1); 879 return (MethodHandles::is_signature_polymorphic(iid) &&
894 OrderAccess::release_store(&pchase[0], step0); 880 MethodHandles::has_member_arg(iid));
895 } 881 }
896 return pchase; 882
897 } 883 // Make an instance of a signature-polymorphic internal MH primitive.
898 884 methodHandle methodOopDesc::make_method_handle_intrinsic(vmIntrinsics::ID iid,
899 //------------------------------------------------------------------------------ 885 Symbol* signature,
900 // methodOopDesc::is_method_handle_adapter 886 TRAPS) {
901 //
902 // Tests if this method is an internal adapter frame from the
903 // MethodHandleCompiler.
904 // Must be consistent with MethodHandleCompiler::get_method_oop().
905 bool methodOopDesc::is_method_handle_adapter() const {
906 if (is_synthetic() &&
907 !is_native() && // has code from MethodHandleCompiler
908 is_method_handle_invoke_name(name()) &&
909 MethodHandleCompiler::klass_is_method_handle_adapter_holder(method_holder())) {
910 assert(!is_method_handle_invoke(), "disjoint");
911 return true;
912 } else {
913 return false;
914 }
915 }
916
917 methodHandle methodOopDesc::make_invoke_method(KlassHandle holder,
918 Symbol* name,
919 Symbol* signature,
920 Handle method_type, TRAPS) {
921 ResourceMark rm; 887 ResourceMark rm;
922 methodHandle empty; 888 methodHandle empty;
923 889
924 assert(holder() == SystemDictionary::MethodHandle_klass(), 890 KlassHandle holder = SystemDictionary::MethodHandle_klass();
925 "must be a JSR 292 magic type"); 891 Symbol* name = MethodHandles::signature_polymorphic_intrinsic_name(iid);
926 892 assert(iid == MethodHandles::signature_polymorphic_name_id(name), "");
927 if (TraceMethodHandles) { 893 if (TraceMethodHandles) {
928 tty->print("Creating invoke method for "); 894 tty->print_cr("make_method_handle_intrinsic MH.%s%s", name->as_C_string(), signature->as_C_string());
929 signature->print_value();
930 tty->cr();
931 } 895 }
932 896
933 // invariant: cp->symbol_at_put is preceded by a refcount increment (more usually a lookup) 897 // invariant: cp->symbol_at_put is preceded by a refcount increment (more usually a lookup)
934 name->increment_refcount(); 898 name->increment_refcount();
935 signature->increment_refcount(); 899 signature->increment_refcount();
936 900
937 // record non-BCP method types in the constant pool 901 int cp_length = _imcp_limit;
938 GrowableArray<KlassHandle>* extra_klasses = NULL;
939 for (int i = -1, len = java_lang_invoke_MethodType::ptype_count(method_type()); i < len; i++) {
940 oop ptype = (i == -1
941 ? java_lang_invoke_MethodType::rtype(method_type())
942 : java_lang_invoke_MethodType::ptype(method_type(), i));
943 klassOop klass = check_non_bcp_klass(java_lang_Class::as_klassOop(ptype));
944 if (klass != NULL) {
945 if (extra_klasses == NULL)
946 extra_klasses = new GrowableArray<KlassHandle>(len+1);
947 bool dup = false;
948 for (int j = 0; j < extra_klasses->length(); j++) {
949 if (extra_klasses->at(j) == klass) { dup = true; break; }
950 }
951 if (!dup)
952 extra_klasses->append(KlassHandle(THREAD, klass));
953 }
954 }
955
956 int extra_klass_count = (extra_klasses == NULL ? 0 : extra_klasses->length());
957 int cp_length = _imcp_limit + extra_klass_count;
958 constantPoolHandle cp; 902 constantPoolHandle cp;
959 { 903 {
960 constantPoolOop cp_oop = oopFactory::new_constantPool(cp_length, IsSafeConc, CHECK_(empty)); 904 constantPoolOop cp_oop = oopFactory::new_constantPool(cp_length, IsSafeConc, CHECK_(empty));
961 cp = constantPoolHandle(THREAD, cp_oop); 905 cp = constantPoolHandle(THREAD, cp_oop);
962 } 906 }
963 cp->symbol_at_put(_imcp_invoke_name, name); 907 cp->symbol_at_put(_imcp_invoke_name, name);
964 cp->symbol_at_put(_imcp_invoke_signature, signature); 908 cp->symbol_at_put(_imcp_invoke_signature, signature);
965 cp->string_at_put(_imcp_method_type_value, Universe::the_null_string());
966 for (int j = 0; j < extra_klass_count; j++) {
967 KlassHandle klass = extra_klasses->at(j);
968 cp->klass_at_put(_imcp_limit + j, klass());
969 }
970 cp->set_preresolution(); 909 cp->set_preresolution();
971 cp->set_pool_holder(holder()); 910 cp->set_pool_holder(holder());
972 911
973 // set up the fancy stuff: 912 // decide on access bits: public or not?
974 cp->pseudo_string_at_put(_imcp_method_type_value, method_type()); 913 int flags_bits = (JVM_ACC_NATIVE | JVM_ACC_SYNTHETIC | JVM_ACC_FINAL);
914 bool must_be_static = MethodHandles::is_signature_polymorphic_static(iid);
915 if (must_be_static) flags_bits |= JVM_ACC_STATIC;
916 assert((flags_bits & JVM_ACC_PUBLIC) == 0, "do not expose these methods");
917
975 methodHandle m; 918 methodHandle m;
976 { 919 {
977 int flags_bits = (JVM_MH_INVOKE_BITS | JVM_ACC_PUBLIC | JVM_ACC_FINAL);
978 methodOop m_oop = oopFactory::new_method(0, accessFlags_from(flags_bits), 920 methodOop m_oop = oopFactory::new_method(0, accessFlags_from(flags_bits),
979 0, 0, 0, 0, IsSafeConc, CHECK_(empty)); 921 0, 0, 0, 0, IsSafeConc, CHECK_(empty));
980 m = methodHandle(THREAD, m_oop); 922 m = methodHandle(THREAD, m_oop);
981 } 923 }
982 m->set_constants(cp()); 924 m->set_constants(cp());
983 m->set_name_index(_imcp_invoke_name); 925 m->set_name_index(_imcp_invoke_name);
984 m->set_signature_index(_imcp_invoke_signature); 926 m->set_signature_index(_imcp_invoke_signature);
985 assert(is_method_handle_invoke_name(m->name()), ""); 927 assert(MethodHandles::is_signature_polymorphic_name(m->name()), "");
986 assert(m->signature() == signature, ""); 928 assert(m->signature() == signature, "");
987 assert(m->is_method_handle_invoke(), "");
988 #ifdef CC_INTERP 929 #ifdef CC_INTERP
989 ResultTypeFinder rtf(signature); 930 ResultTypeFinder rtf(signature);
990 m->set_result_index(rtf.type()); 931 m->set_result_index(rtf.type());
991 #endif 932 #endif
992 m->compute_size_of_parameters(THREAD); 933 m->compute_size_of_parameters(THREAD);
993 m->init_intrinsic_id(); 934 m->init_intrinsic_id();
994 assert(m->intrinsic_id() == vmIntrinsics::_invokeExact || 935 assert(m->is_method_handle_intrinsic(), "");
995 m->intrinsic_id() == vmIntrinsics::_invokeGeneric, "must be an invoker"); 936 #ifdef ASSERT
937 if (!MethodHandles::is_signature_polymorphic(m->intrinsic_id())) m->print();
938 assert(MethodHandles::is_signature_polymorphic(m->intrinsic_id()), "must be an invoker");
939 assert(m->intrinsic_id() == iid, "correctly predicted iid");
940 #endif //ASSERT
996 941
997 // Finally, set up its entry points. 942 // Finally, set up its entry points.
998 assert(m->method_handle_type() == method_type(), "");
999 assert(m->can_be_statically_bound(), ""); 943 assert(m->can_be_statically_bound(), "");
1000 m->set_vtable_index(methodOopDesc::nonvirtual_vtable_index); 944 m->set_vtable_index(methodOopDesc::nonvirtual_vtable_index);
1001 m->link_method(m, CHECK_(empty)); 945 m->link_method(m, CHECK_(empty));
1002
1003 #ifdef ASSERT
1004 // Make sure the pointer chase works.
1005 address p = (address) m();
1006 for (jint* pchase = method_type_offsets_chain(); (*pchase) != -1; pchase++) {
1007 p = *(address*)(p + (*pchase));
1008 }
1009 assert((oop)p == method_type(), "pointer chase is correct");
1010 #endif
1011 946
1012 if (TraceMethodHandles && (Verbose || WizardMode)) 947 if (TraceMethodHandles && (Verbose || WizardMode))
1013 m->print_on(tty); 948 m->print_on(tty);
1014 949
1015 return m; 950 return m;
1023 } 958 }
1024 return NULL; 959 return NULL;
1025 } 960 }
1026 961
1027 962
1028 methodHandle methodOopDesc:: clone_with_new_data(methodHandle m, u_char* new_code, int new_code_length, 963 methodHandle methodOopDesc::clone_with_new_data(methodHandle m, u_char* new_code, int new_code_length,
1029 u_char* new_compressed_linenumber_table, int new_compressed_linenumber_size, TRAPS) { 964 u_char* new_compressed_linenumber_table, int new_compressed_linenumber_size, TRAPS) {
1030 // Code below does not work for native methods - they should never get rewritten anyway 965 // Code below does not work for native methods - they should never get rewritten anyway
1031 assert(!m->is_native(), "cannot rewrite native methods"); 966 assert(!m->is_native(), "cannot rewrite native methods");
1032 // Allocate new methodOop 967 // Allocate new methodOop
1033 AccessFlags flags = m->access_flags(); 968 AccessFlags flags = m->access_flags();
1140 vmSymbols::SID klass_id = klass_id_for_intrinsics(method_holder()); 1075 vmSymbols::SID klass_id = klass_id_for_intrinsics(method_holder());
1141 assert(klass_id != vmSymbols::NO_SID, "caller responsibility"); 1076 assert(klass_id != vmSymbols::NO_SID, "caller responsibility");
1142 1077
1143 // ditto for method and signature: 1078 // ditto for method and signature:
1144 vmSymbols::SID name_id = vmSymbols::find_sid(name()); 1079 vmSymbols::SID name_id = vmSymbols::find_sid(name());
1145 if (name_id == vmSymbols::NO_SID) return; 1080 if (klass_id != vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle)
1081 && name_id == vmSymbols::NO_SID)
1082 return;
1146 vmSymbols::SID sig_id = vmSymbols::find_sid(signature()); 1083 vmSymbols::SID sig_id = vmSymbols::find_sid(signature());
1147 if (klass_id != vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle) 1084 if (klass_id != vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle)
1148 && sig_id == vmSymbols::NO_SID) return; 1085 && sig_id == vmSymbols::NO_SID) return;
1149 jshort flags = access_flags().as_short(); 1086 jshort flags = access_flags().as_short();
1150 1087
1169 } 1106 }
1170 break; 1107 break;
1171 1108
1172 // Signature-polymorphic methods: MethodHandle.invoke*, InvokeDynamic.*. 1109 // Signature-polymorphic methods: MethodHandle.invoke*, InvokeDynamic.*.
1173 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle): 1110 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle):
1174 if (is_static() || !is_native()) break; 1111 if (!is_native()) break;
1175 switch (name_id) { 1112 id = MethodHandles::signature_polymorphic_name_id(method_holder(), name());
1176 case vmSymbols::VM_SYMBOL_ENUM_NAME(invokeGeneric_name): 1113 if (is_static() != MethodHandles::is_signature_polymorphic_static(id))
1177 if (!AllowInvokeGeneric) break; 1114 id = vmIntrinsics::_none;
1178 case vmSymbols::VM_SYMBOL_ENUM_NAME(invoke_name):
1179 id = vmIntrinsics::_invokeGeneric;
1180 break;
1181 case vmSymbols::VM_SYMBOL_ENUM_NAME(invokeExact_name):
1182 id = vmIntrinsics::_invokeExact;
1183 break;
1184 }
1185 break;
1186 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_InvokeDynamic):
1187 if (!is_static() || !is_native()) break;
1188 id = vmIntrinsics::_invokeDynamic;
1189 break; 1115 break;
1190 } 1116 }
1191 1117
1192 if (id != vmIntrinsics::_none) { 1118 if (id != vmIntrinsics::_none) {
1193 // Set up its iid. It is an alias method. 1119 // Set up its iid. It is an alias method.
1196 } 1122 }
1197 } 1123 }
1198 1124
1199 // These two methods are static since a GC may move the methodOopDesc 1125 // These two methods are static since a GC may move the methodOopDesc
1200 bool methodOopDesc::load_signature_classes(methodHandle m, TRAPS) { 1126 bool methodOopDesc::load_signature_classes(methodHandle m, TRAPS) {
1127 if (THREAD->is_Compiler_thread()) {
1128 // There is nothing useful this routine can do from within the Compile thread.
1129 // Hopefully, the signature contains only well-known classes.
1130 // We could scan for this and return true/false, but the caller won't care.
1131 return false;
1132 }
1201 bool sig_is_loaded = true; 1133 bool sig_is_loaded = true;
1202 Handle class_loader(THREAD, instanceKlass::cast(m->method_holder())->class_loader()); 1134 Handle class_loader(THREAD, instanceKlass::cast(m->method_holder())->class_loader());
1203 Handle protection_domain(THREAD, Klass::cast(m->method_holder())->protection_domain()); 1135 Handle protection_domain(THREAD, Klass::cast(m->method_holder())->protection_domain());
1204 ResourceMark rm(THREAD); 1136 ResourceMark rm(THREAD);
1205 Symbol* signature = m->signature(); 1137 Symbol* signature = m->signature();
1249 #else 1181 #else
1250 st->print(" %s::", method_holder()->klass_part()->internal_name()); 1182 st->print(" %s::", method_holder()->klass_part()->internal_name());
1251 #endif 1183 #endif
1252 name()->print_symbol_on(st); 1184 name()->print_symbol_on(st);
1253 if (WizardMode) signature()->print_symbol_on(st); 1185 if (WizardMode) signature()->print_symbol_on(st);
1186 else if (MethodHandles::is_signature_polymorphic(intrinsic_id()))
1187 MethodHandles::print_as_basic_type_signature_on(st, signature(), true);
1254 } 1188 }
1255 1189
1256 // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array 1190 // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array
1257 static void reorder_based_on_method_index(objArrayOop methods, 1191 static void reorder_based_on_method_index(objArrayOop methods,
1258 objArrayOop annotations, 1192 objArrayOop annotations,