comparison src/share/vm/prims/methodHandles.cpp @ 4137:04b9a2566eec

Merge with hsx23/hotspot.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 17 Dec 2011 21:40:27 +0100
parents 44ce519bc3d1
children 15d394228cfa
comparison
equal deleted inserted replaced
3737:9dc19b7d89a3 4137:04b9a2566eec
156 "adapter_fold/2/ref", 156 "adapter_fold/2/ref",
157 "adapter_fold/3/ref", 157 "adapter_fold/3/ref",
158 "adapter_fold/4/ref", 158 "adapter_fold/4/ref",
159 "adapter_fold/5/ref", 159 "adapter_fold/5/ref",
160 160
161 "adapter_opt_profiling",
162
161 NULL 163 NULL
162 }; 164 };
163 165
164 // Adapters. 166 // Adapters.
165 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL; 167 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL;
202 ResourceMark rm; 204 ResourceMark rm;
203 TraceTime timer("MethodHandles adapters generation", TraceStartupTime); 205 TraceTime timer("MethodHandles adapters generation", TraceStartupTime);
204 _adapter_code = MethodHandlesAdapterBlob::create(adapter_code_size); 206 _adapter_code = MethodHandlesAdapterBlob::create(adapter_code_size);
205 if (_adapter_code == NULL) 207 if (_adapter_code == NULL)
206 vm_exit_out_of_memory(adapter_code_size, "CodeCache: no room for MethodHandles adapters"); 208 vm_exit_out_of_memory(adapter_code_size, "CodeCache: no room for MethodHandles adapters");
207 CodeBuffer code(_adapter_code); 209 {
208 MethodHandlesAdapterGenerator g(&code); 210 CodeBuffer code(_adapter_code);
209 g.generate(); 211 MethodHandlesAdapterGenerator g(&code);
212 g.generate();
213 code.log_section_sizes("MethodHandlesAdapterBlob");
214 }
210 } 215 }
211 216
212 //------------------------------------------------------------------------------ 217 //------------------------------------------------------------------------------
213 // MethodHandlesAdapterGenerator::generate 218 // MethodHandlesAdapterGenerator::generate
214 // 219 //
516 if (target_oop->klass() == SystemDictionary::reflect_Field_klass()) { 521 if (target_oop->klass() == SystemDictionary::reflect_Field_klass()) {
517 oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder() 522 oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder()
518 int slot = java_lang_reflect_Field::slot(target_oop); // fd.index() 523 int slot = java_lang_reflect_Field::slot(target_oop); // fd.index()
519 int mods = java_lang_reflect_Field::modifiers(target_oop); 524 int mods = java_lang_reflect_Field::modifiers(target_oop);
520 klassOop k = java_lang_Class::as_klassOop(clazz); 525 klassOop k = java_lang_Class::as_klassOop(clazz);
521 int offset = instanceKlass::cast(k)->offset_from_fields(slot); 526 int offset = instanceKlass::cast(k)->field_offset(slot);
522 init_MemberName(mname_oop, k, accessFlags_from(mods), offset); 527 init_MemberName(mname_oop, k, accessFlags_from(mods), offset);
523 } else { 528 } else {
524 KlassHandle receiver_limit; int decode_flags = 0; 529 KlassHandle receiver_limit; int decode_flags = 0;
525 methodHandle m = MethodHandles::decode_method(target_oop, receiver_limit, decode_flags); 530 methodHandle m = MethodHandles::decode_method(target_oop, receiver_limit, decode_flags);
526 bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0); 531 bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0);
1014 CompilationPolicy::must_be_compiled(m)) 1019 CompilationPolicy::must_be_compiled(m))
1015 && !instanceKlass::cast(m->method_holder())->is_not_initialized() 1020 && !instanceKlass::cast(m->method_holder())->is_not_initialized()
1016 && CompilationPolicy::can_be_compiled(m)) { 1021 && CompilationPolicy::can_be_compiled(m)) {
1017 // Force compilation 1022 // Force compilation
1018 CompileBroker::compile_method(m, InvocationEntryBci, 1023 CompileBroker::compile_method(m, InvocationEntryBci,
1019 CompLevel_initial_compile, 1024 CompilationPolicy::policy()->initial_compile_level(),
1020 methodHandle(), 0, "MethodHandleNatives.getTarget", 1025 methodHandle(), 0, "MethodHandleNatives.getTarget",
1021 CHECK_NULL); 1026 CHECK_NULL);
1022 } 1027 }
1023 // Now wrap m in a DirectMethodHandle. 1028 // Now wrap m in a DirectMethodHandle.
1024 instanceKlassHandle dmh_klass(THREAD, SystemDictionary::DirectMethodHandle_klass()); 1029 instanceKlassHandle dmh_klass(THREAD, SystemDictionary::DirectMethodHandle_klass());
1628 if (mh.is_null() || m.is_null() || 1633 if (mh.is_null() || m.is_null() ||
1629 (!do_dispatch && m->is_abstract())) { 1634 (!do_dispatch && m->is_abstract())) {
1630 THROW(vmSymbols::java_lang_InternalError()); 1635 THROW(vmSymbols::java_lang_InternalError());
1631 } 1636 }
1632 1637
1633 java_lang_invoke_MethodHandle::init_vmslots(mh());
1634
1635 if (VerifyMethodHandles) { 1638 if (VerifyMethodHandles) {
1636 // The privileged code which invokes this routine should not make 1639 // The privileged code which invokes this routine should not make
1637 // a mistake about types, but it's better to verify. 1640 // a mistake about types, but it's better to verify.
1638 verify_DirectMethodHandle(mh, m, CHECK); 1641 verify_DirectMethodHandle(mh, m, CHECK);
1639 } 1642 }
1752 receiver_klass, 1755 receiver_klass,
1753 CHECK); 1756 CHECK);
1754 if (m.is_null()) { THROW(vmSymbols::java_lang_InternalError()); } 1757 if (m.is_null()) { THROW(vmSymbols::java_lang_InternalError()); }
1755 if (m->is_abstract()) { THROW(vmSymbols::java_lang_AbstractMethodError()); } 1758 if (m->is_abstract()) { THROW(vmSymbols::java_lang_AbstractMethodError()); }
1756 1759
1757 java_lang_invoke_MethodHandle::init_vmslots(mh());
1758 int vmargslot = m->size_of_parameters() - 1; 1760 int vmargslot = m->size_of_parameters() - 1;
1759 assert(java_lang_invoke_BoundMethodHandle::vmargslot(mh()) == vmargslot, ""); 1761 assert(java_lang_invoke_BoundMethodHandle::vmargslot(mh()) == vmargslot, "");
1760 1762
1761 if (VerifyMethodHandles) { 1763 if (VerifyMethodHandles) {
1762 verify_BoundMethodHandle_with_receiver(mh, m, CHECK); 1764 verify_BoundMethodHandle_with_receiver(mh, m, CHECK);
1858 // Check arguments. 1860 // Check arguments.
1859 if (mh.is_null() || target.is_null() || !java_lang_invoke_MethodHandle::is_instance(target())) { 1861 if (mh.is_null() || target.is_null() || !java_lang_invoke_MethodHandle::is_instance(target())) {
1860 THROW(vmSymbols::java_lang_InternalError()); 1862 THROW(vmSymbols::java_lang_InternalError());
1861 } 1863 }
1862 1864
1863 java_lang_invoke_MethodHandle::init_vmslots(mh());
1864 int argslot = java_lang_invoke_BoundMethodHandle::vmargslot(mh()); 1865 int argslot = java_lang_invoke_BoundMethodHandle::vmargslot(mh());
1865 1866
1866 if (VerifyMethodHandles) { 1867 if (VerifyMethodHandles) {
1867 int insert_after = argnum - 1; 1868 int insert_after = argnum - 1;
1868 verify_vmargslot(mh, insert_after, argslot, CHECK); 1869 verify_vmargslot(mh, insert_after, argslot, CHECK);
2651 jint new_conversion = adapter_conversion(conv_op, src, dest, stack_move, vminfo); 2652 jint new_conversion = adapter_conversion(conv_op, src, dest, stack_move, vminfo);
2652 2653
2653 // Finalize the conversion field. (Note that it is final to Java code.) 2654 // Finalize the conversion field. (Note that it is final to Java code.)
2654 java_lang_invoke_AdapterMethodHandle::set_conversion(mh(), new_conversion); 2655 java_lang_invoke_AdapterMethodHandle::set_conversion(mh(), new_conversion);
2655 2656
2657 if (java_lang_invoke_CountingMethodHandle::is_instance(mh())) {
2658 assert(ek_orig == _adapter_retype_only, "only one handled");
2659 ek_opt = _adapter_opt_profiling;
2660 }
2661
2656 // Done! 2662 // Done!
2657 java_lang_invoke_MethodHandle::set_vmentry(mh(), entry(ek_opt)); 2663 java_lang_invoke_MethodHandle::set_vmentry(mh(), entry(ek_opt));
2658 2664
2659 // There should be enough memory barriers on exit from native methods 2665 // There should be enough memory barriers on exit from native methods
2660 // to ensure that the MH is fully initialized to all threads before 2666 // to ensure that the MH is fully initialized to all threads before
2677 SystemDictionaryHandles::Object_klass(), 2683 SystemDictionaryHandles::Object_klass(),
2678 THREAD); 2684 THREAD);
2679 java_lang_invoke_MethodTypeForm::init_vmlayout(mtform(), cookie); 2685 java_lang_invoke_MethodTypeForm::init_vmlayout(mtform(), cookie);
2680 } 2686 }
2681 } 2687 }
2688 assert(java_lang_invoke_MethodTypeForm::vmslots(mtform()) == argument_slot_count(mtype()), "must agree");
2682 } 2689 }
2683 2690
2684 #ifdef ASSERT 2691 #ifdef ASSERT
2685 2692
2686 extern "C" 2693 extern "C"
2711 CompilationPolicy::must_be_compiled(m)) 2718 CompilationPolicy::must_be_compiled(m))
2712 && !instanceKlass::cast(m->method_holder())->is_not_initialized() 2719 && !instanceKlass::cast(m->method_holder())->is_not_initialized()
2713 && CompilationPolicy::can_be_compiled(m)) { 2720 && CompilationPolicy::can_be_compiled(m)) {
2714 // Force compilation 2721 // Force compilation
2715 CompileBroker::compile_method(m, InvocationEntryBci, 2722 CompileBroker::compile_method(m, InvocationEntryBci,
2716 CompLevel_initial_compile, 2723 CompilationPolicy::policy()->initial_compile_level(),
2717 methodHandle(), 0, "StressMethodHandleWalk", 2724 methodHandle(), 0, "StressMethodHandleWalk",
2718 CHECK); 2725 CHECK);
2719 } 2726 }
2720 } 2727 }
2721 } 2728 }
2903 case MethodHandles::GC_JVM_STACK_MOVE_UNIT: 2910 case MethodHandles::GC_JVM_STACK_MOVE_UNIT:
2904 // return number of words per slot, signed according to stack direction 2911 // return number of words per slot, signed according to stack direction
2905 return MethodHandles::stack_move_unit(); 2912 return MethodHandles::stack_move_unit();
2906 case MethodHandles::GC_CONV_OP_IMPLEMENTED_MASK: 2913 case MethodHandles::GC_CONV_OP_IMPLEMENTED_MASK:
2907 return MethodHandles::adapter_conversion_ops_supported_mask(); 2914 return MethodHandles::adapter_conversion_ops_supported_mask();
2908 case MethodHandles::GC_OP_ROT_ARGS_DOWN_LIMIT_BIAS: 2915 case MethodHandles::GC_COUNT_GWT:
2909 return MethodHandles::OP_ROT_ARGS_DOWN_LIMIT_BIAS; 2916 #ifdef COMPILER2
2917 return true;
2918 #else
2919 return false;
2920 #endif
2910 } 2921 }
2911 return 0; 2922 return 0;
2912 } 2923 }
2913 JVM_END 2924 JVM_END
2914 2925
3068 // TO DO: expand at least some of the MemberNames, to avoid massive callbacks 3079 // TO DO: expand at least some of the MemberNames, to avoid massive callbacks
3069 return res; 3080 return res;
3070 } 3081 }
3071 JVM_END 3082 JVM_END
3072 3083
3084 JVM_ENTRY(void, MHN_setCallSiteTargetNormal(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
3085 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
3086 Handle target (THREAD, JNIHandles::resolve(target_jh));
3087 {
3088 // Walk all nmethods depending on this call site.
3089 MutexLocker mu(Compile_lock, thread);
3090 Universe::flush_dependents_on(call_site, target);
3091 }
3092 java_lang_invoke_CallSite::set_target(call_site(), target());
3093 }
3094 JVM_END
3095
3096 JVM_ENTRY(void, MHN_setCallSiteTargetVolatile(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
3097 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
3098 Handle target (THREAD, JNIHandles::resolve(target_jh));
3099 {
3100 // Walk all nmethods depending on this call site.
3101 MutexLocker mu(Compile_lock, thread);
3102 Universe::flush_dependents_on(call_site, target);
3103 }
3104 java_lang_invoke_CallSite::set_target_volatile(call_site(), target());
3105 }
3106 JVM_END
3107
3073 methodOop MethodHandles::resolve_raise_exception_method(TRAPS) { 3108 methodOop MethodHandles::resolve_raise_exception_method(TRAPS) {
3074 if (_raise_exception_method != NULL) { 3109 if (_raise_exception_method != NULL) {
3075 // no need to do it twice 3110 // no need to do it twice
3076 return raise_exception_method(); 3111 return raise_exception_method();
3077 } 3112 }
3124 JVM_END 3159 JVM_END
3125 3160
3126 3161
3127 /// JVM_RegisterMethodHandleMethods 3162 /// JVM_RegisterMethodHandleMethods
3128 3163
3164 #undef CS // Solaris builds complain
3165
3129 #define LANG "Ljava/lang/" 3166 #define LANG "Ljava/lang/"
3130 #define JLINV "Ljava/lang/invoke/" 3167 #define JLINV "Ljava/lang/invoke/"
3131 3168
3132 #define OBJ LANG"Object;" 3169 #define OBJ LANG"Object;"
3133 #define CLS LANG"Class;" 3170 #define CLS LANG"Class;"
3134 #define STRG LANG"String;" 3171 #define STRG LANG"String;"
3172 #define CS JLINV"CallSite;"
3135 #define MT JLINV"MethodType;" 3173 #define MT JLINV"MethodType;"
3136 #define MH JLINV"MethodHandle;" 3174 #define MH JLINV"MethodHandle;"
3137 #define MEM JLINV"MemberName;" 3175 #define MEM JLINV"MemberName;"
3138 #define AMH JLINV"AdapterMethodHandle;" 3176 #define AMH JLINV"AdapterMethodHandle;"
3139 #define BMH JLINV"BoundMethodHandle;" 3177 #define BMH JLINV"BoundMethodHandle;"
3140 #define DMH JLINV"DirectMethodHandle;" 3178 #define DMH JLINV"DirectMethodHandle;"
3141 3179
3142 #define CC (char*) /*cast a literal from (const char*)*/ 3180 #define CC (char*) /*cast a literal from (const char*)*/
3143 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) 3181 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
3144 3182
3145 // These are the native methods on sun.invoke.MethodHandleNatives. 3183 // These are the native methods on java.lang.invoke.MethodHandleNatives.
3146 static JNINativeMethod methods[] = { 3184 static JNINativeMethod methods[] = {
3147 // void init(MemberName self, AccessibleObject ref) 3185 // void init(MemberName self, AccessibleObject ref)
3148 {CC"init", CC"("AMH""MH"I)V", FN_PTR(MHN_init_AMH)}, 3186 {CC"init", CC"("AMH""MH"I)V", FN_PTR(MHN_init_AMH)},
3149 {CC"init", CC"("BMH""OBJ"I)V", FN_PTR(MHN_init_BMH)}, 3187 {CC"init", CC"("BMH""OBJ"I)V", FN_PTR(MHN_init_BMH)},
3150 {CC"init", CC"("DMH""OBJ"Z"CLS")V", FN_PTR(MHN_init_DMH)}, 3188 {CC"init", CC"("DMH""OBJ"Z"CLS")V", FN_PTR(MHN_init_DMH)},
3151 {CC"init", CC"("MT")V", FN_PTR(MHN_init_MT)}, 3189 {CC"init", CC"("MT")V", FN_PTR(MHN_init_MT)},
3152 {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHN_init_Mem)}, 3190 {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHN_init_Mem)},
3153 {CC"expand", CC"("MEM")V", FN_PTR(MHN_expand_Mem)}, 3191 {CC"expand", CC"("MEM")V", FN_PTR(MHN_expand_Mem)},
3154 {CC"resolve", CC"("MEM""CLS")V", FN_PTR(MHN_resolve_Mem)}, 3192 {CC"resolve", CC"("MEM""CLS")V", FN_PTR(MHN_resolve_Mem)},
3155 {CC"getTarget", CC"("MH"I)"OBJ, FN_PTR(MHN_getTarget)}, 3193 {CC"getTarget", CC"("MH"I)"OBJ, FN_PTR(MHN_getTarget)},
3156 {CC"getConstant", CC"(I)I", FN_PTR(MHN_getConstant)}, 3194 {CC"getConstant", CC"(I)I", FN_PTR(MHN_getConstant)},
3157 // static native int getNamedCon(int which, Object[] name) 3195 // static native int getNamedCon(int which, Object[] name)
3158 {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHN_getNamedCon)}, 3196 {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHN_getNamedCon)},
3159 // static native int getMembers(Class<?> defc, String matchName, String matchSig, 3197 // static native int getMembers(Class<?> defc, String matchName, String matchSig,
3160 // int matchFlags, Class<?> caller, int skip, MemberName[] results); 3198 // int matchFlags, Class<?> caller, int skip, MemberName[] results);
3161 {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)} 3199 {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)}
3200 };
3201
3202 static JNINativeMethod call_site_methods[] = {
3203 {CC"setCallSiteTargetNormal", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetNormal)},
3204 {CC"setCallSiteTargetVolatile", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetVolatile)}
3162 }; 3205 };
3163 3206
3164 static JNINativeMethod invoke_methods[] = { 3207 static JNINativeMethod invoke_methods[] = {
3165 // void init(MemberName self, AccessibleObject ref) 3208 // void init(MemberName self, AccessibleObject ref)
3166 {CC"invoke", CC"(["OBJ")"OBJ, FN_PTR(MH_invoke_UOE)}, 3209 {CC"invoke", CC"(["OBJ")"OBJ, FN_PTR(MH_invoke_UOE)},
3167 {CC"invokeExact", CC"(["OBJ")"OBJ, FN_PTR(MH_invokeExact_UOE)} 3210 {CC"invokeExact", CC"(["OBJ")"OBJ, FN_PTR(MH_invokeExact_UOE)}
3168 }; 3211 };
3169 3212
3170 // This one function is exported, used by NativeLookup. 3213 // This one function is exported, used by NativeLookup.
3171 3214
3172 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { 3215 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) {
3175 if (!EnableInvokeDynamic) { 3218 if (!EnableInvokeDynamic) {
3176 warning("JSR 292 is disabled in this JVM. Use -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic to enable."); 3219 warning("JSR 292 is disabled in this JVM. Use -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic to enable.");
3177 return; // bind nothing 3220 return; // bind nothing
3178 } 3221 }
3179 3222
3223 assert(!MethodHandles::enabled(), "must not be enabled");
3180 bool enable_MH = true; 3224 bool enable_MH = true;
3181 3225
3182 { 3226 {
3183 ThreadToNativeFromVM ttnfv(thread); 3227 ThreadToNativeFromVM ttnfv(thread);
3184
3185 int status = env->RegisterNatives(MHN_class, methods, sizeof(methods)/sizeof(JNINativeMethod)); 3228 int status = env->RegisterNatives(MHN_class, methods, sizeof(methods)/sizeof(JNINativeMethod));
3186 if (!env->ExceptionOccurred()) { 3229 if (!env->ExceptionOccurred()) {
3187 const char* L_MH_name = (JLINV "MethodHandle"); 3230 const char* L_MH_name = (JLINV "MethodHandle");
3188 const char* MH_name = L_MH_name+1; 3231 const char* MH_name = L_MH_name+1;
3189 jclass MH_class = env->FindClass(MH_name); 3232 jclass MH_class = env->FindClass(MH_name);
3190 status = env->RegisterNatives(MH_class, invoke_methods, sizeof(invoke_methods)/sizeof(JNINativeMethod)); 3233 status = env->RegisterNatives(MH_class, invoke_methods, sizeof(invoke_methods)/sizeof(JNINativeMethod));
3191 } 3234 }
3192 if (env->ExceptionOccurred()) { 3235 if (env->ExceptionOccurred()) {
3193 MethodHandles::set_enabled(false);
3194 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); 3236 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support.");
3195 enable_MH = false; 3237 enable_MH = false;
3238 env->ExceptionClear();
3239 }
3240
3241 status = env->RegisterNatives(MHN_class, call_site_methods, sizeof(call_site_methods)/sizeof(JNINativeMethod));
3242 if (env->ExceptionOccurred()) {
3243 // Exception is okay until 7087357
3196 env->ExceptionClear(); 3244 env->ExceptionClear();
3197 } 3245 }
3198 } 3246 }
3199 3247
3200 if (enable_MH) { 3248 if (enable_MH) {