61 */ |
61 */ |
62 |
62 |
63 bool MethodHandles::_enabled = false; // set true after successful native linkage |
63 bool MethodHandles::_enabled = false; // set true after successful native linkage |
64 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL; |
64 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL; |
65 |
65 |
66 |
|
67 /** |
66 /** |
68 * Generates method handle adapters. Returns 'false' if memory allocation |
67 * Generates method handle adapters. Returns 'false' if memory allocation |
69 * failed and true otherwise. |
68 * failed and true otherwise. |
70 */ |
69 */ |
71 bool MethodHandles::generate_adapters() { |
70 void MethodHandles::generate_adapters() { |
72 if (SystemDictionary::MethodHandle_klass() == NULL) { |
71 assert(SystemDictionary::MethodHandle_klass() != NULL, "should be present"); |
73 return true; |
|
74 } |
|
75 |
|
76 assert(_adapter_code == NULL, "generate only once"); |
72 assert(_adapter_code == NULL, "generate only once"); |
77 |
73 |
78 ResourceMark rm; |
74 ResourceMark rm; |
79 TraceTime timer("MethodHandles adapters generation", TraceStartupTime); |
75 TraceTime timer("MethodHandles adapters generation", TraceStartupTime); |
80 _adapter_code = MethodHandlesAdapterBlob::create(adapter_code_size); |
76 _adapter_code = MethodHandlesAdapterBlob::create(adapter_code_size); |
81 if (_adapter_code == NULL) { |
|
82 return false; |
|
83 } |
|
84 |
|
85 CodeBuffer code(_adapter_code); |
77 CodeBuffer code(_adapter_code); |
86 MethodHandlesAdapterGenerator g(&code); |
78 MethodHandlesAdapterGenerator g(&code); |
87 g.generate(); |
79 g.generate(); |
88 code.log_section_sizes("MethodHandlesAdapterBlob"); |
80 code.log_section_sizes("MethodHandlesAdapterBlob"); |
89 return true; |
|
90 } |
81 } |
91 |
82 |
92 //------------------------------------------------------------------------------ |
83 //------------------------------------------------------------------------------ |
93 // MethodHandlesAdapterGenerator::generate |
84 // MethodHandlesAdapterGenerator::generate |
94 // |
85 // |
1434 {CC "invoke", CC "([" OBJ ")" OBJ, FN_PTR(MH_invoke_UOE)}, |
1425 {CC "invoke", CC "([" OBJ ")" OBJ, FN_PTR(MH_invoke_UOE)}, |
1435 {CC "invokeExact", CC "([" OBJ ")" OBJ, FN_PTR(MH_invokeExact_UOE)} |
1426 {CC "invokeExact", CC "([" OBJ ")" OBJ, FN_PTR(MH_invokeExact_UOE)} |
1436 }; |
1427 }; |
1437 |
1428 |
1438 /** |
1429 /** |
1439 * Helper method to register native methods. |
|
1440 */ |
|
1441 static bool register_natives(JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods) { |
|
1442 int status = env->RegisterNatives(clazz, methods, nMethods); |
|
1443 if (status != JNI_OK || env->ExceptionOccurred()) { |
|
1444 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); |
|
1445 env->ExceptionClear(); |
|
1446 return false; |
|
1447 } |
|
1448 return true; |
|
1449 } |
|
1450 |
|
1451 /** |
|
1452 * This one function is exported, used by NativeLookup. |
1430 * This one function is exported, used by NativeLookup. |
1453 */ |
1431 */ |
1454 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { |
1432 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { |
1455 assert(!MethodHandles::enabled(), "must not be enabled"); |
1433 assert(!MethodHandles::enabled(), "must not be enabled"); |
1456 bool enable_MH = true; |
1434 assert(SystemDictionary::MethodHandle_klass() != NULL, "should be present"); |
1457 |
1435 |
1458 jclass MH_class = NULL; |
1436 oop mirror = SystemDictionary::MethodHandle_klass()->java_mirror(); |
1459 if (SystemDictionary::MethodHandle_klass() == NULL) { |
1437 jclass MH_class = (jclass) JNIHandles::make_local(env, mirror); |
1460 enable_MH = false; |
1438 |
1461 } else { |
1439 { |
1462 oop mirror = SystemDictionary::MethodHandle_klass()->java_mirror(); |
|
1463 MH_class = (jclass) JNIHandles::make_local(env, mirror); |
|
1464 } |
|
1465 |
|
1466 if (enable_MH) { |
|
1467 ThreadToNativeFromVM ttnfv(thread); |
1440 ThreadToNativeFromVM ttnfv(thread); |
1468 |
1441 |
1469 if (enable_MH) { |
1442 int status = env->RegisterNatives(MHN_class, MHN_methods, sizeof(MHN_methods)/sizeof(JNINativeMethod)); |
1470 enable_MH = register_natives(env, MHN_class, MHN_methods, sizeof(MHN_methods)/sizeof(JNINativeMethod)); |
1443 guarantee(status == JNI_OK && !env->ExceptionOccurred(), |
1471 } |
1444 "register java.lang.invoke.MethodHandleNative natives"); |
1472 if (enable_MH) { |
1445 |
1473 enable_MH = register_natives(env, MH_class, MH_methods, sizeof(MH_methods)/sizeof(JNINativeMethod)); |
1446 status = env->RegisterNatives(MH_class, MH_methods, sizeof(MH_methods)/sizeof(JNINativeMethod)); |
1474 } |
1447 guarantee(status == JNI_OK && !env->ExceptionOccurred(), |
|
1448 "register java.lang.invoke.MethodHandle natives"); |
1475 } |
1449 } |
1476 |
1450 |
1477 if (TraceInvokeDynamic) { |
1451 if (TraceInvokeDynamic) { |
1478 tty->print_cr("MethodHandle support loaded (using LambdaForms)"); |
1452 tty->print_cr("MethodHandle support loaded (using LambdaForms)"); |
1479 } |
1453 } |
1480 |
1454 |
1481 if (enable_MH) { |
1455 MethodHandles::set_enabled(true); |
1482 if (MethodHandles::generate_adapters() == false) { |
|
1483 THROW_MSG(vmSymbols::java_lang_VirtualMachineError(), "Out of space in CodeCache for method handle adapters"); |
|
1484 } |
|
1485 MethodHandles::set_enabled(true); |
|
1486 } |
|
1487 } |
1456 } |
1488 JVM_END |
1457 JVM_END |