1.1 --- a/src/share/vm/prims/methodHandles.cpp Mon Jul 23 13:04:59 2012 -0700 1.2 +++ b/src/share/vm/prims/methodHandles.cpp Tue Jul 24 10:51:00 2012 -0700 1.3 @@ -30,166 +30,30 @@ 1.4 #include "memory/allocation.inline.hpp" 1.5 #include "memory/oopFactory.hpp" 1.6 #include "prims/methodHandles.hpp" 1.7 -#include "prims/methodHandleWalk.hpp" 1.8 #include "runtime/compilationPolicy.hpp" 1.9 #include "runtime/javaCalls.hpp" 1.10 #include "runtime/reflection.hpp" 1.11 #include "runtime/signature.hpp" 1.12 #include "runtime/stubRoutines.hpp" 1.13 1.14 + 1.15 /* 1.16 * JSR 292 reference implementation: method handles 1.17 + * The JDK 7 reference implementation represented method handle 1.18 + * combinations as chains. Each link in the chain had a "vmentry" 1.19 + * field which pointed at a bit of assembly code which performed 1.20 + * one transformation before dispatching to the next link in the chain. 1.21 + * 1.22 + * The current reference implementation pushes almost all code generation 1.23 + * responsibility to (trusted) Java code. A method handle contains a 1.24 + * pointer to its "LambdaForm", which embodies all details of the method 1.25 + * handle's behavior. The LambdaForm is a normal Java object, managed 1.26 + * by a runtime coded in Java. 1.27 */ 1.28 1.29 bool MethodHandles::_enabled = false; // set true after successful native linkage 1.30 - 1.31 -MethodHandleEntry* MethodHandles::_entries[MethodHandles::_EK_LIMIT] = {NULL}; 1.32 -const char* MethodHandles::_entry_names[_EK_LIMIT+1] = { 1.33 - "raise_exception", 1.34 - "invokestatic", // how a MH emulates invokestatic 1.35 - "invokespecial", // ditto for the other invokes... 1.36 - "invokevirtual", 1.37 - "invokeinterface", 1.38 - "bound_ref", // these are for BMH... 1.39 - "bound_int", 1.40 - "bound_long", 1.41 - "bound_ref_direct", // (direct versions have a direct methodOop) 1.42 - "bound_int_direct", 1.43 - "bound_long_direct", 1.44 - 1.45 - // starting at _adapter_mh_first: 1.46 - "adapter_retype_only", // these are for AMH... 1.47 - "adapter_retype_raw", 1.48 - "adapter_check_cast", 1.49 - "adapter_prim_to_prim", 1.50 - "adapter_ref_to_prim", 1.51 - "adapter_prim_to_ref", 1.52 - "adapter_swap_args", 1.53 - "adapter_rot_args", 1.54 - "adapter_dup_args", 1.55 - "adapter_drop_args", 1.56 - "adapter_collect_args", 1.57 - "adapter_spread_args", 1.58 - "adapter_fold_args", 1.59 - "adapter_unused_13", 1.60 - 1.61 - // optimized adapter types: 1.62 - "adapter_swap_args/1", 1.63 - "adapter_swap_args/2", 1.64 - "adapter_rot_args/1,up", 1.65 - "adapter_rot_args/1,down", 1.66 - "adapter_rot_args/2,up", 1.67 - "adapter_rot_args/2,down", 1.68 - "adapter_prim_to_prim/i2i", 1.69 - "adapter_prim_to_prim/l2i", 1.70 - "adapter_prim_to_prim/d2f", 1.71 - "adapter_prim_to_prim/i2l", 1.72 - "adapter_prim_to_prim/f2d", 1.73 - "adapter_ref_to_prim/unboxi", 1.74 - "adapter_ref_to_prim/unboxl", 1.75 - 1.76 - // return value handlers for collect/filter/fold adapters: 1.77 - "return/ref", 1.78 - "return/int", 1.79 - "return/long", 1.80 - "return/float", 1.81 - "return/double", 1.82 - "return/void", 1.83 - "return/S0/ref", 1.84 - "return/S1/ref", 1.85 - "return/S2/ref", 1.86 - "return/S3/ref", 1.87 - "return/S4/ref", 1.88 - "return/S5/ref", 1.89 - "return/any", 1.90 - 1.91 - // spreading (array length cases 0, 1, ...) 1.92 - "adapter_spread/0", 1.93 - "adapter_spread/1/ref", 1.94 - "adapter_spread/2/ref", 1.95 - "adapter_spread/3/ref", 1.96 - "adapter_spread/4/ref", 1.97 - "adapter_spread/5/ref", 1.98 - "adapter_spread/ref", 1.99 - "adapter_spread/byte", 1.100 - "adapter_spread/char", 1.101 - "adapter_spread/short", 1.102 - "adapter_spread/int", 1.103 - "adapter_spread/long", 1.104 - "adapter_spread/float", 1.105 - "adapter_spread/double", 1.106 - 1.107 - // blocking filter/collect conversions: 1.108 - "adapter_collect/ref", 1.109 - "adapter_collect/int", 1.110 - "adapter_collect/long", 1.111 - "adapter_collect/float", 1.112 - "adapter_collect/double", 1.113 - "adapter_collect/void", 1.114 - "adapter_collect/0/ref", 1.115 - "adapter_collect/1/ref", 1.116 - "adapter_collect/2/ref", 1.117 - "adapter_collect/3/ref", 1.118 - "adapter_collect/4/ref", 1.119 - "adapter_collect/5/ref", 1.120 - "adapter_filter/S0/ref", 1.121 - "adapter_filter/S1/ref", 1.122 - "adapter_filter/S2/ref", 1.123 - "adapter_filter/S3/ref", 1.124 - "adapter_filter/S4/ref", 1.125 - "adapter_filter/S5/ref", 1.126 - "adapter_collect/2/S0/ref", 1.127 - "adapter_collect/2/S1/ref", 1.128 - "adapter_collect/2/S2/ref", 1.129 - "adapter_collect/2/S3/ref", 1.130 - "adapter_collect/2/S4/ref", 1.131 - "adapter_collect/2/S5/ref", 1.132 - 1.133 - // blocking fold conversions: 1.134 - "adapter_fold/ref", 1.135 - "adapter_fold/int", 1.136 - "adapter_fold/long", 1.137 - "adapter_fold/float", 1.138 - "adapter_fold/double", 1.139 - "adapter_fold/void", 1.140 - "adapter_fold/1/ref", 1.141 - "adapter_fold/2/ref", 1.142 - "adapter_fold/3/ref", 1.143 - "adapter_fold/4/ref", 1.144 - "adapter_fold/5/ref", 1.145 - 1.146 - "adapter_opt_profiling", 1.147 - 1.148 - NULL 1.149 -}; 1.150 - 1.151 -// Adapters. 1.152 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL; 1.153 1.154 -jobject MethodHandles::_raise_exception_method; 1.155 - 1.156 -address MethodHandles::_adapter_return_handlers[CONV_TYPE_MASK+1]; 1.157 - 1.158 -#ifdef ASSERT 1.159 -bool MethodHandles::spot_check_entry_names() { 1.160 - assert(!strcmp(entry_name(_invokestatic_mh), "invokestatic"), ""); 1.161 - assert(!strcmp(entry_name(_bound_ref_mh), "bound_ref"), ""); 1.162 - assert(!strcmp(entry_name(_adapter_retype_only), "adapter_retype_only"), ""); 1.163 - assert(!strcmp(entry_name(_adapter_fold_args), "adapter_fold_args"), ""); 1.164 - assert(!strcmp(entry_name(_adapter_opt_unboxi), "adapter_ref_to_prim/unboxi"), ""); 1.165 - assert(!strcmp(entry_name(_adapter_opt_spread_char), "adapter_spread/char"), ""); 1.166 - assert(!strcmp(entry_name(_adapter_opt_spread_double), "adapter_spread/double"), ""); 1.167 - assert(!strcmp(entry_name(_adapter_opt_collect_int), "adapter_collect/int"), ""); 1.168 - assert(!strcmp(entry_name(_adapter_opt_collect_0_ref), "adapter_collect/0/ref"), ""); 1.169 - assert(!strcmp(entry_name(_adapter_opt_collect_2_S3_ref), "adapter_collect/2/S3/ref"), ""); 1.170 - assert(!strcmp(entry_name(_adapter_opt_filter_S5_ref), "adapter_filter/S5/ref"), ""); 1.171 - assert(!strcmp(entry_name(_adapter_opt_fold_3_ref), "adapter_fold/3/ref"), ""); 1.172 - assert(!strcmp(entry_name(_adapter_opt_fold_void), "adapter_fold/void"), ""); 1.173 - return true; 1.174 -} 1.175 -#endif 1.176 - 1.177 - 1.178 //------------------------------------------------------------------------------ 1.179 // MethodHandles::generate_adapters 1.180 // 1.181 @@ -216,36 +80,20 @@ 1.182 // 1.183 void MethodHandlesAdapterGenerator::generate() { 1.184 // Generate generic method handle adapters. 1.185 - for (MethodHandles::EntryKind ek = MethodHandles::_EK_FIRST; 1.186 - ek < MethodHandles::_EK_LIMIT; 1.187 - ek = MethodHandles::EntryKind(1 + (int)ek)) { 1.188 - if (MethodHandles::ek_supported(ek)) { 1.189 - StubCodeMark mark(this, "MethodHandle", MethodHandles::entry_name(ek)); 1.190 - MethodHandles::generate_method_handle_stub(_masm, ek); 1.191 + // Generate interpreter entries 1.192 + for (Interpreter::MethodKind mk = Interpreter::method_handle_invoke_FIRST; 1.193 + mk <= Interpreter::method_handle_invoke_LAST; 1.194 + mk = Interpreter::MethodKind(1 + (int)mk)) { 1.195 + vmIntrinsics::ID iid = Interpreter::method_handle_intrinsic(mk); 1.196 + StubCodeMark mark(this, "MethodHandle::interpreter_entry", vmIntrinsics::name_at(iid)); 1.197 + address entry = MethodHandles::generate_method_handle_interpreter_entry(_masm, iid); 1.198 + if (entry != NULL) { 1.199 + Interpreter::set_entry_for_kind(mk, entry); 1.200 } 1.201 + // If the entry is not set, it will throw AbstractMethodError. 1.202 } 1.203 } 1.204 1.205 - 1.206 -//------------------------------------------------------------------------------ 1.207 -// MethodHandles::ek_supported 1.208 -// 1.209 -bool MethodHandles::ek_supported(MethodHandles::EntryKind ek) { 1.210 - MethodHandles::EntryKind ek_orig = MethodHandles::ek_original_kind(ek); 1.211 - switch (ek_orig) { 1.212 - case _adapter_unused_13: 1.213 - return false; // not defined yet 1.214 - case _adapter_prim_to_ref: 1.215 - return conv_op_supported(java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF); 1.216 - case _adapter_collect_args: 1.217 - return conv_op_supported(java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS); 1.218 - case _adapter_fold_args: 1.219 - return conv_op_supported(java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS); 1.220 - } 1.221 - return true; 1.222 -} 1.223 - 1.224 - 1.225 void MethodHandles::set_enabled(bool z) { 1.226 if (_enabled != z) { 1.227 guarantee(z && EnableInvokeDynamic, "can only enable once, and only if -XX:+EnableInvokeDynamic"); 1.228 @@ -253,217 +101,6 @@ 1.229 } 1.230 } 1.231 1.232 -// Note: A method which does not have a TRAPS argument cannot block in the GC 1.233 -// or throw exceptions. Such methods are used in this file to do something quick 1.234 -// and local, like parse a data structure. For speed, such methods work on plain 1.235 -// oops, not handles. Trapping methods uniformly operate on handles. 1.236 - 1.237 -methodHandle MethodHandles::decode_vmtarget(oop vmtarget, int vmindex, oop mtype, 1.238 - KlassHandle& receiver_limit_result, int& decode_flags_result) { 1.239 - if (vmtarget == NULL) return methodHandle(); 1.240 - assert(methodOopDesc::nonvirtual_vtable_index < 0, "encoding"); 1.241 - if (vmindex < 0) { 1.242 - // this DMH performs no dispatch; it is directly bound to a methodOop 1.243 - // A MemberName may either be directly bound to a methodOop, 1.244 - // or it may use the klass/index form; both forms mean the same thing. 1.245 - methodOop m = decode_methodOop(methodOop(vmtarget), decode_flags_result); 1.246 - if ((decode_flags_result & _dmf_has_receiver) != 0 1.247 - && java_lang_invoke_MethodType::is_instance(mtype)) { 1.248 - // Extract receiver type restriction from mtype.ptypes[0]. 1.249 - objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(mtype); 1.250 - oop ptype0 = (ptypes == NULL || ptypes->length() < 1) ? oop(NULL) : ptypes->obj_at(0); 1.251 - if (java_lang_Class::is_instance(ptype0)) 1.252 - receiver_limit_result = java_lang_Class::as_klassOop(ptype0); 1.253 - } 1.254 - if (vmindex == methodOopDesc::nonvirtual_vtable_index) { 1.255 - // this DMH can be an "invokespecial" version 1.256 - decode_flags_result &= ~_dmf_does_dispatch; 1.257 - } else { 1.258 - assert(vmindex == methodOopDesc::invalid_vtable_index, "random vmindex?"); 1.259 - } 1.260 - return m; 1.261 - } else { 1.262 - assert(vmtarget->is_klass(), "must be class or interface"); 1.263 - decode_flags_result |= MethodHandles::_dmf_does_dispatch; 1.264 - decode_flags_result |= MethodHandles::_dmf_has_receiver; 1.265 - receiver_limit_result = (klassOop)vmtarget; 1.266 - Klass* tk = Klass::cast((klassOop)vmtarget); 1.267 - if (tk->is_interface()) { 1.268 - // an itable linkage is <interface, itable index> 1.269 - decode_flags_result |= MethodHandles::_dmf_from_interface; 1.270 - return klassItable::method_for_itable_index((klassOop)vmtarget, vmindex); 1.271 - } else { 1.272 - if (!tk->oop_is_instance()) 1.273 - tk = instanceKlass::cast(SystemDictionary::Object_klass()); 1.274 - return ((instanceKlass*)tk)->method_at_vtable(vmindex); 1.275 - } 1.276 - } 1.277 -} 1.278 - 1.279 -// MemberName and DirectMethodHandle have the same linkage to the JVM internals. 1.280 -// (MemberName is the non-operational name used for queries and setup.) 1.281 - 1.282 -methodHandle MethodHandles::decode_DirectMethodHandle(oop mh, KlassHandle& receiver_limit_result, int& decode_flags_result) { 1.283 - oop vmtarget = java_lang_invoke_DirectMethodHandle::vmtarget(mh); 1.284 - int vmindex = java_lang_invoke_DirectMethodHandle::vmindex(mh); 1.285 - oop mtype = java_lang_invoke_DirectMethodHandle::type(mh); 1.286 - return decode_vmtarget(vmtarget, vmindex, mtype, receiver_limit_result, decode_flags_result); 1.287 -} 1.288 - 1.289 -methodHandle MethodHandles::decode_BoundMethodHandle(oop mh, KlassHandle& receiver_limit_result, int& decode_flags_result) { 1.290 - assert(java_lang_invoke_BoundMethodHandle::is_instance(mh), ""); 1.291 - assert(mh->klass() != SystemDictionary::AdapterMethodHandle_klass(), ""); 1.292 - for (oop bmh = mh;;) { 1.293 - // Bound MHs can be stacked to bind several arguments. 1.294 - oop target = java_lang_invoke_MethodHandle::vmtarget(bmh); 1.295 - if (target == NULL) return methodHandle(); 1.296 - decode_flags_result |= MethodHandles::_dmf_binds_argument; 1.297 - klassOop tk = target->klass(); 1.298 - if (tk == SystemDictionary::BoundMethodHandle_klass()) { 1.299 - bmh = target; 1.300 - continue; 1.301 - } else { 1.302 - if (java_lang_invoke_MethodHandle::is_subclass(tk)) { 1.303 - //assert(tk == SystemDictionary::DirectMethodHandle_klass(), "end of BMH chain must be DMH"); 1.304 - return decode_MethodHandle(target, receiver_limit_result, decode_flags_result); 1.305 - } else { 1.306 - // Optimized case: binding a receiver to a non-dispatched DMH 1.307 - // short-circuits directly to the methodOop. 1.308 - // (It might be another argument besides a receiver also.) 1.309 - assert(target->is_method(), "must be a simple method"); 1.310 - decode_flags_result |= MethodHandles::_dmf_binds_method; 1.311 - methodOop m = (methodOop) target; 1.312 - if (!m->is_static()) 1.313 - decode_flags_result |= MethodHandles::_dmf_has_receiver; 1.314 - return m; 1.315 - } 1.316 - } 1.317 - } 1.318 -} 1.319 - 1.320 -methodHandle MethodHandles::decode_AdapterMethodHandle(oop mh, KlassHandle& receiver_limit_result, int& decode_flags_result) { 1.321 - assert(mh->klass() == SystemDictionary::AdapterMethodHandle_klass(), ""); 1.322 - for (oop amh = mh;;) { 1.323 - // Adapter MHs can be stacked to convert several arguments. 1.324 - int conv_op = adapter_conversion_op(java_lang_invoke_AdapterMethodHandle::conversion(amh)); 1.325 - decode_flags_result |= (_dmf_adapter_lsb << conv_op) & _DMF_ADAPTER_MASK; 1.326 - oop target = java_lang_invoke_MethodHandle::vmtarget(amh); 1.327 - if (target == NULL) return methodHandle(); 1.328 - klassOop tk = target->klass(); 1.329 - if (tk == SystemDictionary::AdapterMethodHandle_klass()) { 1.330 - amh = target; 1.331 - continue; 1.332 - } else { 1.333 - // must be a BMH (which will bind some more arguments) or a DMH (for the final call) 1.334 - return MethodHandles::decode_MethodHandle(target, receiver_limit_result, decode_flags_result); 1.335 - } 1.336 - } 1.337 -} 1.338 - 1.339 -methodHandle MethodHandles::decode_MethodHandle(oop mh, KlassHandle& receiver_limit_result, int& decode_flags_result) { 1.340 - if (mh == NULL) return methodHandle(); 1.341 - klassOop mhk = mh->klass(); 1.342 - assert(java_lang_invoke_MethodHandle::is_subclass(mhk), "must be a MethodHandle"); 1.343 - if (mhk == SystemDictionary::DirectMethodHandle_klass()) { 1.344 - return decode_DirectMethodHandle(mh, receiver_limit_result, decode_flags_result); 1.345 - } else if (mhk == SystemDictionary::BoundMethodHandle_klass()) { 1.346 - return decode_BoundMethodHandle(mh, receiver_limit_result, decode_flags_result); 1.347 - } else if (mhk == SystemDictionary::AdapterMethodHandle_klass()) { 1.348 - return decode_AdapterMethodHandle(mh, receiver_limit_result, decode_flags_result); 1.349 - } else if (java_lang_invoke_BoundMethodHandle::is_subclass(mhk)) { 1.350 - // could be a JavaMethodHandle (but not an adapter MH) 1.351 - return decode_BoundMethodHandle(mh, receiver_limit_result, decode_flags_result); 1.352 - } else { 1.353 - assert(false, "cannot parse this MH"); 1.354 - return methodHandle(); // random MH? 1.355 - } 1.356 -} 1.357 - 1.358 -methodOop MethodHandles::decode_methodOop(methodOop m, int& decode_flags_result) { 1.359 - assert(m->is_method(), ""); 1.360 - if (m->is_static()) { 1.361 - // check that signature begins '(L' or '([' (not '(I', '()', etc.) 1.362 - Symbol* sig = m->signature(); 1.363 - BasicType recv_bt = char2type(sig->byte_at(1)); 1.364 - // Note: recv_bt might be T_ILLEGAL if byte_at(2) is ')' 1.365 - assert(sig->byte_at(0) == '(', "must be method sig"); 1.366 -// if (recv_bt == T_OBJECT || recv_bt == T_ARRAY) 1.367 -// decode_flags_result |= _dmf_has_receiver; 1.368 - } else { 1.369 - // non-static method 1.370 - decode_flags_result |= _dmf_has_receiver; 1.371 - if (!m->can_be_statically_bound() && !m->is_initializer()) { 1.372 - decode_flags_result |= _dmf_does_dispatch; 1.373 - if (Klass::cast(m->method_holder())->is_interface()) 1.374 - decode_flags_result |= _dmf_from_interface; 1.375 - } 1.376 - } 1.377 - return m; 1.378 -} 1.379 - 1.380 - 1.381 -// A trusted party is handing us a cookie to determine a method. 1.382 -// Let's boil it down to the method oop they really want. 1.383 -methodHandle MethodHandles::decode_method(oop x, KlassHandle& receiver_limit_result, int& decode_flags_result) { 1.384 - decode_flags_result = 0; 1.385 - receiver_limit_result = KlassHandle(); 1.386 - klassOop xk = x->klass(); 1.387 - if (xk == Universe::methodKlassObj()) { 1.388 - return decode_methodOop((methodOop) x, decode_flags_result); 1.389 - } else if (xk == SystemDictionary::MemberName_klass()) { 1.390 - // Note: This only works if the MemberName has already been resolved. 1.391 - return decode_MemberName(x, receiver_limit_result, decode_flags_result); 1.392 - } else if (java_lang_invoke_MethodHandle::is_subclass(xk)) { 1.393 - return decode_MethodHandle(x, receiver_limit_result, decode_flags_result); 1.394 - } else if (xk == SystemDictionary::reflect_Method_klass()) { 1.395 - oop clazz = java_lang_reflect_Method::clazz(x); 1.396 - int slot = java_lang_reflect_Method::slot(x); 1.397 - klassOop k = java_lang_Class::as_klassOop(clazz); 1.398 - if (k != NULL && Klass::cast(k)->oop_is_instance()) 1.399 - return decode_methodOop(instanceKlass::cast(k)->method_with_idnum(slot), 1.400 - decode_flags_result); 1.401 - } else if (xk == SystemDictionary::reflect_Constructor_klass()) { 1.402 - oop clazz = java_lang_reflect_Constructor::clazz(x); 1.403 - int slot = java_lang_reflect_Constructor::slot(x); 1.404 - klassOop k = java_lang_Class::as_klassOop(clazz); 1.405 - if (k != NULL && Klass::cast(k)->oop_is_instance()) 1.406 - return decode_methodOop(instanceKlass::cast(k)->method_with_idnum(slot), 1.407 - decode_flags_result); 1.408 - } else { 1.409 - // unrecognized object 1.410 - assert(!x->is_method(), "already checked"); 1.411 - assert(!java_lang_invoke_MemberName::is_instance(x), "already checked"); 1.412 - } 1.413 - return methodHandle(); 1.414 -} 1.415 - 1.416 - 1.417 -int MethodHandles::decode_MethodHandle_stack_pushes(oop mh) { 1.418 - if (mh->klass() == SystemDictionary::DirectMethodHandle_klass()) 1.419 - return 0; // no push/pop 1.420 - int this_vmslots = java_lang_invoke_MethodHandle::vmslots(mh); 1.421 - int last_vmslots = 0; 1.422 - oop last_mh = mh; 1.423 - for (;;) { 1.424 - oop target = java_lang_invoke_MethodHandle::vmtarget(last_mh); 1.425 - if (target->klass() == SystemDictionary::DirectMethodHandle_klass()) { 1.426 - last_vmslots = java_lang_invoke_MethodHandle::vmslots(target); 1.427 - break; 1.428 - } else if (!java_lang_invoke_MethodHandle::is_instance(target)) { 1.429 - // might be klass or method 1.430 - assert(target->is_method(), "must get here with a direct ref to method"); 1.431 - last_vmslots = methodOop(target)->size_of_parameters(); 1.432 - break; 1.433 - } 1.434 - last_mh = target; 1.435 - } 1.436 - // If I am called with fewer VM slots than my ultimate callee, 1.437 - // it must be that I push the additionally needed slots. 1.438 - // Likewise if am called with more VM slots, I will pop them. 1.439 - return (last_vmslots - this_vmslots); 1.440 -} 1.441 - 1.442 - 1.443 // MemberName support 1.444 1.445 // import java_lang_invoke_MemberName.* 1.446 @@ -472,10 +109,11 @@ 1.447 IS_CONSTRUCTOR = java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR, 1.448 IS_FIELD = java_lang_invoke_MemberName::MN_IS_FIELD, 1.449 IS_TYPE = java_lang_invoke_MemberName::MN_IS_TYPE, 1.450 + REFERENCE_KIND_SHIFT = java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT, 1.451 + REFERENCE_KIND_MASK = java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK, 1.452 SEARCH_SUPERCLASSES = java_lang_invoke_MemberName::MN_SEARCH_SUPERCLASSES, 1.453 SEARCH_INTERFACES = java_lang_invoke_MemberName::MN_SEARCH_INTERFACES, 1.454 - ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE, 1.455 - VM_INDEX_UNINITIALIZED = java_lang_invoke_MemberName::VM_INDEX_UNINITIALIZED 1.456 + ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE 1.457 }; 1.458 1.459 Handle MethodHandles::new_MemberName(TRAPS) { 1.460 @@ -485,72 +123,265 @@ 1.461 return Handle(THREAD, k->allocate_instance(THREAD)); 1.462 } 1.463 1.464 -void MethodHandles::init_MemberName(oop mname_oop, oop target_oop) { 1.465 - if (target_oop->klass() == SystemDictionary::reflect_Field_klass()) { 1.466 +oop MethodHandles::init_MemberName(oop mname_oop, oop target_oop) { 1.467 + klassOop target_klass = target_oop->klass(); 1.468 + if (target_klass == SystemDictionary::reflect_Field_klass()) { 1.469 oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder() 1.470 int slot = java_lang_reflect_Field::slot(target_oop); // fd.index() 1.471 int mods = java_lang_reflect_Field::modifiers(target_oop); 1.472 + oop type = java_lang_reflect_Field::type(target_oop); 1.473 + oop name = java_lang_reflect_Field::name(target_oop); 1.474 klassOop k = java_lang_Class::as_klassOop(clazz); 1.475 - int offset = instanceKlass::cast(k)->field_offset(slot); 1.476 - init_MemberName(mname_oop, k, accessFlags_from(mods), offset); 1.477 - } else { 1.478 - KlassHandle receiver_limit; int decode_flags = 0; 1.479 - methodHandle m = MethodHandles::decode_method(target_oop, receiver_limit, decode_flags); 1.480 - bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0); 1.481 - init_MemberName(mname_oop, m(), do_dispatch); 1.482 + intptr_t offset = instanceKlass::cast(k)->field_offset(slot); 1.483 + return init_field_MemberName(mname_oop, k, accessFlags_from(mods), type, name, offset); 1.484 + } else if (target_klass == SystemDictionary::reflect_Method_klass()) { 1.485 + oop clazz = java_lang_reflect_Method::clazz(target_oop); 1.486 + int slot = java_lang_reflect_Method::slot(target_oop); 1.487 + klassOop k = java_lang_Class::as_klassOop(clazz); 1.488 + if (k != NULL && Klass::cast(k)->oop_is_instance()) { 1.489 + methodOop m = instanceKlass::cast(k)->method_with_idnum(slot); 1.490 + return init_method_MemberName(mname_oop, m, true, k); 1.491 + } 1.492 + } else if (target_klass == SystemDictionary::reflect_Constructor_klass()) { 1.493 + oop clazz = java_lang_reflect_Constructor::clazz(target_oop); 1.494 + int slot = java_lang_reflect_Constructor::slot(target_oop); 1.495 + klassOop k = java_lang_Class::as_klassOop(clazz); 1.496 + if (k != NULL && Klass::cast(k)->oop_is_instance()) { 1.497 + methodOop m = instanceKlass::cast(k)->method_with_idnum(slot); 1.498 + return init_method_MemberName(mname_oop, m, false, k); 1.499 + } 1.500 + } else if (target_klass == SystemDictionary::MemberName_klass()) { 1.501 + // Note: This only works if the MemberName has already been resolved. 1.502 + oop clazz = java_lang_invoke_MemberName::clazz(target_oop); 1.503 + int flags = java_lang_invoke_MemberName::flags(target_oop); 1.504 + oop vmtarget = java_lang_invoke_MemberName::vmtarget(target_oop); 1.505 + intptr_t vmindex = java_lang_invoke_MemberName::vmindex(target_oop); 1.506 + klassOop k = java_lang_Class::as_klassOop(clazz); 1.507 + int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; 1.508 + if (vmtarget == NULL) return NULL; // not resolved 1.509 + if ((flags & IS_FIELD) != 0) { 1.510 + assert(vmtarget->is_klass(), "field vmtarget is klassOop"); 1.511 + int basic_mods = (ref_kind_is_static(ref_kind) ? JVM_ACC_STATIC : 0); 1.512 + // FIXME: how does k (receiver_limit) contribute? 1.513 + return init_field_MemberName(mname_oop, klassOop(vmtarget), accessFlags_from(basic_mods), NULL, NULL, vmindex); 1.514 + } else if ((flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) { 1.515 + assert(vmtarget->is_method(), "method or constructor vmtarget is methodOop"); 1.516 + return init_method_MemberName(mname_oop, methodOop(vmtarget), ref_kind_does_dispatch(ref_kind), k); 1.517 + } else { 1.518 + return NULL; 1.519 + } 1.520 } 1.521 + return NULL; 1.522 } 1.523 1.524 -void MethodHandles::init_MemberName(oop mname_oop, methodOop m, bool do_dispatch) { 1.525 - int flags = ((m->is_initializer() ? IS_CONSTRUCTOR : IS_METHOD) 1.526 - | (jushort)( m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS )); 1.527 - oop vmtarget = m; 1.528 - int vmindex = methodOopDesc::invalid_vtable_index; // implies no info yet 1.529 - if (!do_dispatch || (flags & IS_CONSTRUCTOR) || m->can_be_statically_bound()) 1.530 - vmindex = methodOopDesc::nonvirtual_vtable_index; // implies never any dispatch 1.531 - assert(vmindex != VM_INDEX_UNINITIALIZED, "Java sentinel value"); 1.532 +oop MethodHandles::init_method_MemberName(oop mname_oop, methodOop m, bool do_dispatch, 1.533 + klassOop receiver_limit) { 1.534 + AccessFlags mods = m->access_flags(); 1.535 + int flags = (jushort)( mods.as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS ); 1.536 + int vmindex = methodOopDesc::nonvirtual_vtable_index; // implies never any dispatch 1.537 + klassOop mklass = m->method_holder(); 1.538 + if (receiver_limit == NULL) 1.539 + receiver_limit = mklass; 1.540 + if (m->is_initializer()) { 1.541 + flags |= IS_CONSTRUCTOR | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT); 1.542 + } else if (mods.is_static()) { 1.543 + flags |= IS_METHOD | (JVM_REF_invokeStatic << REFERENCE_KIND_SHIFT); 1.544 + } else if (receiver_limit != mklass && 1.545 + !Klass::cast(receiver_limit)->is_subtype_of(mklass)) { 1.546 + return NULL; // bad receiver limit 1.547 + } else if (Klass::cast(receiver_limit)->is_interface() && 1.548 + Klass::cast(mklass)->is_interface()) { 1.549 + flags |= IS_METHOD | (JVM_REF_invokeInterface << REFERENCE_KIND_SHIFT); 1.550 + receiver_limit = mklass; // ignore passed-in limit; interfaces are interconvertible 1.551 + vmindex = klassItable::compute_itable_index(m); 1.552 + } else if (mklass != receiver_limit && Klass::cast(mklass)->is_interface()) { 1.553 + flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT); 1.554 + // it is a miranda method, so m->vtable_index is not what we want 1.555 + ResourceMark rm; 1.556 + klassVtable* vt = instanceKlass::cast(receiver_limit)->vtable(); 1.557 + vmindex = vt->index_of_miranda(m->name(), m->signature()); 1.558 + } else if (!do_dispatch || m->can_be_statically_bound()) { 1.559 + flags |= IS_METHOD | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT); 1.560 + } else { 1.561 + flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT); 1.562 + vmindex = m->vtable_index(); 1.563 + } 1.564 + 1.565 + java_lang_invoke_MemberName::set_flags(mname_oop, flags); 1.566 + java_lang_invoke_MemberName::set_vmtarget(mname_oop, m); 1.567 + java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex); // vtable/itable index 1.568 + java_lang_invoke_MemberName::set_clazz(mname_oop, Klass::cast(receiver_limit)->java_mirror()); 1.569 + // Note: name and type can be lazily computed by resolve_MemberName, 1.570 + // if Java code needs them as resolved String and MethodType objects. 1.571 + // The clazz must be eagerly stored, because it provides a GC 1.572 + // root to help keep alive the methodOop. 1.573 + // If relevant, the vtable or itable value is stored as vmindex. 1.574 + // This is done eagerly, since it is readily available without 1.575 + // constructing any new objects. 1.576 + // TO DO: maybe intern mname_oop 1.577 + return mname_oop; 1.578 +} 1.579 + 1.580 +Handle MethodHandles::init_method_MemberName(oop mname_oop, CallInfo& info, TRAPS) { 1.581 + Handle empty; 1.582 + if (info.resolved_appendix().not_null()) { 1.583 + // The resolved MemberName must not be accompanied by an appendix argument, 1.584 + // since there is no way to bind this value into the MemberName. 1.585 + // Caller is responsible to prevent this from happening. 1.586 + THROW_MSG_(vmSymbols::java_lang_InternalError(), "appendix", empty); 1.587 + } 1.588 + methodHandle m = info.resolved_method(); 1.589 + KlassHandle defc = info.resolved_klass(); 1.590 + int vmindex = -1; 1.591 + if (defc->is_interface() && Klass::cast(m->method_holder())->is_interface()) { 1.592 + // LinkResolver does not report itable indexes! (fix this?) 1.593 + vmindex = klassItable::compute_itable_index(m()); 1.594 + } else if (m->can_be_statically_bound()) { 1.595 + // LinkResolver reports vtable index even for final methods! 1.596 + vmindex = methodOopDesc::nonvirtual_vtable_index; 1.597 + } else { 1.598 + vmindex = info.vtable_index(); 1.599 + } 1.600 + oop res = init_method_MemberName(mname_oop, m(), (vmindex >= 0), defc()); 1.601 + assert(res == NULL || (java_lang_invoke_MemberName::vmindex(res) == vmindex), ""); 1.602 + return Handle(THREAD, res); 1.603 +} 1.604 + 1.605 +oop MethodHandles::init_field_MemberName(oop mname_oop, klassOop field_holder, 1.606 + AccessFlags mods, oop type, oop name, 1.607 + intptr_t offset, bool is_setter) { 1.608 + int flags = (jushort)( mods.as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS ); 1.609 + flags |= IS_FIELD | ((mods.is_static() ? JVM_REF_getStatic : JVM_REF_getField) << REFERENCE_KIND_SHIFT); 1.610 + if (is_setter) flags += ((JVM_REF_putField - JVM_REF_getField) << REFERENCE_KIND_SHIFT); 1.611 + oop vmtarget = field_holder; 1.612 + int vmindex = offset; // determines the field uniquely when combined with static bit 1.613 + java_lang_invoke_MemberName::set_flags(mname_oop, flags); 1.614 java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget); 1.615 java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex); 1.616 - java_lang_invoke_MemberName::set_flags(mname_oop, flags); 1.617 - java_lang_invoke_MemberName::set_clazz(mname_oop, Klass::cast(m->method_holder())->java_mirror()); 1.618 + java_lang_invoke_MemberName::set_clazz(mname_oop, Klass::cast(field_holder)->java_mirror()); 1.619 + if (name != NULL) 1.620 + java_lang_invoke_MemberName::set_name(mname_oop, name); 1.621 + if (type != NULL) 1.622 + java_lang_invoke_MemberName::set_type(mname_oop, type); 1.623 + // Note: name and type can be lazily computed by resolve_MemberName, 1.624 + // if Java code needs them as resolved String and Class objects. 1.625 + // Note that the incoming type oop might be pre-resolved (non-null). 1.626 + // The base clazz and field offset (vmindex) must be eagerly stored, 1.627 + // because they unambiguously identify the field. 1.628 + // Although the fieldDescriptor::_index would also identify the field, 1.629 + // we do not use it, because it is harder to decode. 1.630 + // TO DO: maybe intern mname_oop 1.631 + return mname_oop; 1.632 } 1.633 1.634 -void MethodHandles::init_MemberName(oop mname_oop, klassOop field_holder, AccessFlags mods, int offset) { 1.635 - int flags = (IS_FIELD | (jushort)( mods.as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS )); 1.636 - oop vmtarget = field_holder; 1.637 - int vmindex = offset; // determines the field uniquely when combined with static bit 1.638 - assert(vmindex != VM_INDEX_UNINITIALIZED, "bad alias on vmindex"); 1.639 - java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget); 1.640 - java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex); 1.641 - java_lang_invoke_MemberName::set_flags(mname_oop, flags); 1.642 - java_lang_invoke_MemberName::set_clazz(mname_oop, Klass::cast(field_holder)->java_mirror()); 1.643 +Handle MethodHandles::init_field_MemberName(oop mname_oop, FieldAccessInfo& info, TRAPS) { 1.644 + return Handle(); 1.645 +#if 0 1.646 + KlassHandle field_holder = info.klass(); 1.647 + intptr_t field_offset = info.field_offset(); 1.648 + return init_field_MemberName(mname_oop, field_holder(), 1.649 + info.access_flags(), 1.650 + type, name, 1.651 + field_offset, false /*is_setter*/); 1.652 +#endif 1.653 } 1.654 1.655 1.656 -methodHandle MethodHandles::decode_MemberName(oop mname, KlassHandle& receiver_limit_result, int& decode_flags_result) { 1.657 - methodHandle empty; 1.658 - int flags = java_lang_invoke_MemberName::flags(mname); 1.659 - if ((flags & (IS_METHOD | IS_CONSTRUCTOR)) == 0) return empty; // not invocable 1.660 - oop vmtarget = java_lang_invoke_MemberName::vmtarget(mname); 1.661 - int vmindex = java_lang_invoke_MemberName::vmindex(mname); 1.662 - if (vmindex == VM_INDEX_UNINITIALIZED) return empty; // not resolved 1.663 - methodHandle m = decode_vmtarget(vmtarget, vmindex, NULL, receiver_limit_result, decode_flags_result); 1.664 - oop clazz = java_lang_invoke_MemberName::clazz(mname); 1.665 - if (clazz != NULL && java_lang_Class::is_instance(clazz)) { 1.666 - klassOop klass = java_lang_Class::as_klassOop(clazz); 1.667 - if (klass != NULL) receiver_limit_result = klass; 1.668 - } 1.669 - return m; 1.670 +// JVM 2.9 Special Methods: 1.671 +// A method is signature polymorphic if and only if all of the following conditions hold : 1.672 +// * It is declared in the java.lang.invoke.MethodHandle class. 1.673 +// * It has a single formal parameter of type Object[]. 1.674 +// * It has a return type of Object. 1.675 +// * It has the ACC_VARARGS and ACC_NATIVE flags set. 1.676 +bool MethodHandles::is_method_handle_invoke_name(klassOop klass, Symbol* name) { 1.677 + if (klass == NULL) 1.678 + return false; 1.679 + // The following test will fail spuriously during bootstrap of MethodHandle itself: 1.680 + // if (klass != SystemDictionary::MethodHandle_klass()) 1.681 + // Test the name instead: 1.682 + if (Klass::cast(klass)->name() != vmSymbols::java_lang_invoke_MethodHandle()) 1.683 + return false; 1.684 + Symbol* poly_sig = vmSymbols::object_array_object_signature(); 1.685 + methodOop m = instanceKlass::cast(klass)->find_method(name, poly_sig); 1.686 + if (m == NULL) return false; 1.687 + int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS; 1.688 + int flags = m->access_flags().as_int(); 1.689 + return (flags & required) == required; 1.690 } 1.691 1.692 + 1.693 +Symbol* MethodHandles::signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid) { 1.694 + assert(is_signature_polymorphic_intrinsic(iid), err_msg("iid=%d", iid)); 1.695 + switch (iid) { 1.696 + case vmIntrinsics::_invokeBasic: return vmSymbols::invokeBasic_name(); 1.697 + case vmIntrinsics::_linkToVirtual: return vmSymbols::linkToVirtual_name(); 1.698 + case vmIntrinsics::_linkToStatic: return vmSymbols::linkToStatic_name(); 1.699 + case vmIntrinsics::_linkToSpecial: return vmSymbols::linkToSpecial_name(); 1.700 + case vmIntrinsics::_linkToInterface: return vmSymbols::linkToInterface_name(); 1.701 + } 1.702 + assert(false, ""); 1.703 + return 0; 1.704 +} 1.705 + 1.706 +int MethodHandles::signature_polymorphic_intrinsic_ref_kind(vmIntrinsics::ID iid) { 1.707 + switch (iid) { 1.708 + case vmIntrinsics::_invokeBasic: return 0; 1.709 + case vmIntrinsics::_linkToVirtual: return JVM_REF_invokeVirtual; 1.710 + case vmIntrinsics::_linkToStatic: return JVM_REF_invokeStatic; 1.711 + case vmIntrinsics::_linkToSpecial: return JVM_REF_invokeSpecial; 1.712 + case vmIntrinsics::_linkToInterface: return JVM_REF_invokeInterface; 1.713 + } 1.714 + assert(false, err_msg("iid=%d", iid)); 1.715 + return 0; 1.716 +} 1.717 + 1.718 +vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Symbol* name) { 1.719 + vmSymbols::SID name_id = vmSymbols::find_sid(name); 1.720 + switch (name_id) { 1.721 + // The ID _invokeGeneric stands for all non-static signature-polymorphic methods, except built-ins. 1.722 + case vmSymbols::VM_SYMBOL_ENUM_NAME(invoke_name): return vmIntrinsics::_invokeGeneric; 1.723 + // The only built-in non-static signature-polymorphic method is MethodHandle.invokeBasic: 1.724 + case vmSymbols::VM_SYMBOL_ENUM_NAME(invokeBasic_name): return vmIntrinsics::_invokeBasic; 1.725 + 1.726 + // There is one static signature-polymorphic method for each JVM invocation mode. 1.727 + case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToVirtual_name): return vmIntrinsics::_linkToVirtual; 1.728 + case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToStatic_name): return vmIntrinsics::_linkToStatic; 1.729 + case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToSpecial_name): return vmIntrinsics::_linkToSpecial; 1.730 + case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToInterface_name): return vmIntrinsics::_linkToInterface; 1.731 + } 1.732 + 1.733 + // Cover the case of invokeExact and any future variants of invokeFoo. 1.734 + klassOop mh_klass = SystemDictionary::well_known_klass( 1.735 + SystemDictionary::WK_KLASS_ENUM_NAME(MethodHandle_klass) ); 1.736 + if (mh_klass != NULL && is_method_handle_invoke_name(mh_klass, name)) 1.737 + return vmIntrinsics::_invokeGeneric; 1.738 + 1.739 + // Note: The pseudo-intrinsic _compiledLambdaForm is never linked against. 1.740 + // Instead it is used to mark lambda forms bound to invokehandle or invokedynamic. 1.741 + return vmIntrinsics::_none; 1.742 +} 1.743 + 1.744 +vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(klassOop klass, Symbol* name) { 1.745 + if (klass != NULL && 1.746 + Klass::cast(klass)->name() == vmSymbols::java_lang_invoke_MethodHandle()) { 1.747 + vmIntrinsics::ID iid = signature_polymorphic_name_id(name); 1.748 + if (iid != vmIntrinsics::_none) 1.749 + return iid; 1.750 + if (is_method_handle_invoke_name(klass, name)) 1.751 + return vmIntrinsics::_invokeGeneric; 1.752 + } 1.753 + return vmIntrinsics::_none; 1.754 +} 1.755 + 1.756 + 1.757 // convert the external string or reflective type to an internal signature 1.758 -Symbol* MethodHandles::convert_to_signature(oop type_str, bool polymorphic, TRAPS) { 1.759 +Symbol* MethodHandles::lookup_signature(oop type_str, bool intern_if_not_found, TRAPS) { 1.760 if (java_lang_invoke_MethodType::is_instance(type_str)) { 1.761 - return java_lang_invoke_MethodType::as_signature(type_str, polymorphic, CHECK_NULL); 1.762 + return java_lang_invoke_MethodType::as_signature(type_str, intern_if_not_found, CHECK_NULL); 1.763 } else if (java_lang_Class::is_instance(type_str)) { 1.764 return java_lang_Class::as_signature(type_str, false, CHECK_NULL); 1.765 } else if (java_lang_String::is_instance(type_str)) { 1.766 - if (polymorphic) { 1.767 + if (intern_if_not_found) { 1.768 return java_lang_String::as_symbol(type_str, CHECK_NULL); 1.769 } else { 1.770 return java_lang_String::as_symbol_or_null(type_str); 1.771 @@ -560,121 +391,303 @@ 1.772 } 1.773 } 1.774 1.775 +static const char OBJ_SIG[] = "Ljava/lang/Object;"; 1.776 +enum { OBJ_SIG_LEN = 18 }; 1.777 + 1.778 +bool MethodHandles::is_basic_type_signature(Symbol* sig) { 1.779 + assert(vmSymbols::object_signature()->utf8_length() == (int)OBJ_SIG_LEN, ""); 1.780 + assert(vmSymbols::object_signature()->equals(OBJ_SIG), ""); 1.781 + const int len = sig->utf8_length(); 1.782 + for (int i = 0; i < len; i++) { 1.783 + switch (sig->byte_at(i)) { 1.784 + case 'L': 1.785 + // only java/lang/Object is valid here 1.786 + if (sig->index_of_at(i, OBJ_SIG, OBJ_SIG_LEN) != i) 1.787 + return false; 1.788 + i += OBJ_SIG_LEN-1; //-1 because of i++ in loop 1.789 + continue; 1.790 + case '(': case ')': case 'V': 1.791 + case 'I': case 'J': case 'F': case 'D': 1.792 + continue; 1.793 + //case '[': 1.794 + //case 'Z': case 'B': case 'C': case 'S': 1.795 + default: 1.796 + return false; 1.797 + } 1.798 + } 1.799 + return true; 1.800 +} 1.801 + 1.802 +Symbol* MethodHandles::lookup_basic_type_signature(Symbol* sig, bool keep_last_arg, TRAPS) { 1.803 + Symbol* bsig = NULL; 1.804 + if (sig == NULL) { 1.805 + return sig; 1.806 + } else if (is_basic_type_signature(sig)) { 1.807 + sig->increment_refcount(); 1.808 + return sig; // that was easy 1.809 + } else if (sig->byte_at(0) != '(') { 1.810 + BasicType bt = char2type(sig->byte_at(0)); 1.811 + if (is_subword_type(bt)) { 1.812 + bsig = vmSymbols::int_signature(); 1.813 + } else { 1.814 + assert(bt == T_OBJECT || bt == T_ARRAY, "is_basic_type_signature was false"); 1.815 + bsig = vmSymbols::object_signature(); 1.816 + } 1.817 + } else { 1.818 + ResourceMark rm; 1.819 + stringStream buffer(128); 1.820 + buffer.put('('); 1.821 + int arg_pos = 0, keep_arg_pos = -1; 1.822 + if (keep_last_arg) 1.823 + keep_arg_pos = ArgumentCount(sig).size() - 1; 1.824 + for (SignatureStream ss(sig); !ss.is_done(); ss.next()) { 1.825 + BasicType bt = ss.type(); 1.826 + size_t this_arg_pos = buffer.size(); 1.827 + if (ss.at_return_type()) { 1.828 + buffer.put(')'); 1.829 + } 1.830 + if (arg_pos == keep_arg_pos) { 1.831 + buffer.write((char*) ss.raw_bytes(), 1.832 + (int) ss.raw_length()); 1.833 + } else if (bt == T_OBJECT || bt == T_ARRAY) { 1.834 + buffer.write(OBJ_SIG, OBJ_SIG_LEN); 1.835 + } else { 1.836 + if (is_subword_type(bt)) 1.837 + bt = T_INT; 1.838 + buffer.put(type2char(bt)); 1.839 + } 1.840 + arg_pos++; 1.841 + } 1.842 + const char* sigstr = buffer.base(); 1.843 + int siglen = (int) buffer.size(); 1.844 + bsig = SymbolTable::new_symbol(sigstr, siglen, THREAD); 1.845 + } 1.846 + assert(is_basic_type_signature(bsig) || 1.847 + // detune assert in case the injected argument is not a basic type: 1.848 + keep_last_arg, ""); 1.849 + return bsig; 1.850 +} 1.851 + 1.852 +void MethodHandles::print_as_basic_type_signature_on(outputStream* st, 1.853 + Symbol* sig, 1.854 + bool keep_arrays, 1.855 + bool keep_basic_names) { 1.856 + st = st ? st : tty; 1.857 + int len = sig->utf8_length(); 1.858 + int array = 0; 1.859 + bool prev_type = false; 1.860 + for (int i = 0; i < len; i++) { 1.861 + char ch = sig->byte_at(i); 1.862 + switch (ch) { 1.863 + case '(': case ')': 1.864 + prev_type = false; 1.865 + st->put(ch); 1.866 + continue; 1.867 + case '[': 1.868 + if (!keep_basic_names && keep_arrays) 1.869 + st->put(ch); 1.870 + array++; 1.871 + continue; 1.872 + case 'L': 1.873 + { 1.874 + if (prev_type) st->put(','); 1.875 + int start = i+1, slash = start; 1.876 + while (++i < len && (ch = sig->byte_at(i)) != ';') { 1.877 + if (ch == '/' || ch == '.' || ch == '$') slash = i+1; 1.878 + } 1.879 + if (slash < i) start = slash; 1.880 + if (!keep_basic_names) { 1.881 + st->put('L'); 1.882 + } else { 1.883 + for (int j = start; j < i; j++) 1.884 + st->put(sig->byte_at(j)); 1.885 + prev_type = true; 1.886 + } 1.887 + break; 1.888 + } 1.889 + default: 1.890 + { 1.891 + if (array && char2type(ch) != T_ILLEGAL && !keep_arrays) { 1.892 + ch = '['; 1.893 + array = 0; 1.894 + } 1.895 + if (prev_type) st->put(','); 1.896 + const char* n = NULL; 1.897 + if (keep_basic_names) 1.898 + n = type2name(char2type(ch)); 1.899 + if (n == NULL) { 1.900 + // unknown letter, or we don't want to know its name 1.901 + st->put(ch); 1.902 + } else { 1.903 + st->print(n); 1.904 + prev_type = true; 1.905 + } 1.906 + break; 1.907 + } 1.908 + } 1.909 + // Switch break goes here to take care of array suffix: 1.910 + if (prev_type) { 1.911 + while (array > 0) { 1.912 + st->print("[]"); 1.913 + --array; 1.914 + } 1.915 + } 1.916 + array = 0; 1.917 + } 1.918 +} 1.919 + 1.920 + 1.921 + 1.922 +static oop object_java_mirror() { 1.923 + return Klass::cast(SystemDictionary::Object_klass())->java_mirror(); 1.924 +} 1.925 + 1.926 +static oop field_name_or_null(Symbol* s) { 1.927 + if (s == NULL) return NULL; 1.928 + return StringTable::lookup(s); 1.929 +} 1.930 + 1.931 +static oop field_signature_type_or_null(Symbol* s) { 1.932 + if (s == NULL) return NULL; 1.933 + BasicType bt = FieldType::basic_type(s); 1.934 + if (is_java_primitive(bt)) { 1.935 + assert(s->utf8_length() == 1, ""); 1.936 + return java_lang_Class::primitive_mirror(bt); 1.937 + } 1.938 + // Here are some more short cuts for common types. 1.939 + // They are optional, since reference types can be resolved lazily. 1.940 + if (bt == T_OBJECT) { 1.941 + if (s == vmSymbols::object_signature()) { 1.942 + return object_java_mirror(); 1.943 + } else if (s == vmSymbols::class_signature()) { 1.944 + return Klass::cast(SystemDictionary::Class_klass())->java_mirror(); 1.945 + } else if (s == vmSymbols::string_signature()) { 1.946 + return Klass::cast(SystemDictionary::String_klass())->java_mirror(); 1.947 + } else { 1.948 + int len = s->utf8_length(); 1.949 + if (s->byte_at(0) == 'L' && s->byte_at(len-1) == ';') { 1.950 + TempNewSymbol cname = SymbolTable::probe((const char*)&s->bytes()[1], len-2); 1.951 + if (cname == NULL) return NULL; 1.952 + klassOop wkk = SystemDictionary::find_well_known_klass(cname); 1.953 + if (wkk == NULL) return NULL; 1.954 + return Klass::cast(wkk)->java_mirror(); 1.955 + } 1.956 + } 1.957 + } 1.958 + return NULL; 1.959 +} 1.960 + 1.961 // An unresolved member name is a mere symbolic reference. 1.962 // Resolving it plants a vmtarget/vmindex in it, 1.963 // which refers dirctly to JVM internals. 1.964 -void MethodHandles::resolve_MemberName(Handle mname, TRAPS) { 1.965 +Handle MethodHandles::resolve_MemberName(Handle mname, TRAPS) { 1.966 + Handle empty; 1.967 assert(java_lang_invoke_MemberName::is_instance(mname()), ""); 1.968 -#ifdef ASSERT 1.969 - // If this assert throws, renegotiate the sentinel value used by the Java code, 1.970 - // so that it is distinct from any valid vtable index value, and any special 1.971 - // values defined in methodOopDesc::VtableIndexFlag. 1.972 - // The point of the slop is to give the Java code and the JVM some room 1.973 - // to independently specify sentinel values. 1.974 - const int sentinel_slop = 10; 1.975 - const int sentinel_limit = methodOopDesc::highest_unused_vtable_index_value - sentinel_slop; 1.976 - assert(VM_INDEX_UNINITIALIZED < sentinel_limit, "Java sentinel != JVM sentinels"); 1.977 -#endif 1.978 - if (java_lang_invoke_MemberName::vmindex(mname()) != VM_INDEX_UNINITIALIZED) 1.979 - return; // already resolved 1.980 + 1.981 + if (java_lang_invoke_MemberName::vmtarget(mname()) != NULL) { 1.982 + // Already resolved. 1.983 + DEBUG_ONLY(int vmindex = java_lang_invoke_MemberName::vmindex(mname())); 1.984 + assert(vmindex >= methodOopDesc::nonvirtual_vtable_index, ""); 1.985 + return mname; 1.986 + } 1.987 + 1.988 Handle defc_oop(THREAD, java_lang_invoke_MemberName::clazz(mname())); 1.989 Handle name_str(THREAD, java_lang_invoke_MemberName::name( mname())); 1.990 Handle type_str(THREAD, java_lang_invoke_MemberName::type( mname())); 1.991 int flags = java_lang_invoke_MemberName::flags(mname()); 1.992 + int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; 1.993 + if (!ref_kind_is_valid(ref_kind)) { 1.994 + THROW_MSG_(vmSymbols::java_lang_InternalError(), "obsolete MemberName format", empty); 1.995 + } 1.996 + 1.997 + DEBUG_ONLY(int old_vmindex); 1.998 + assert((old_vmindex = java_lang_invoke_MemberName::vmindex(mname())) == 0, "clean input"); 1.999 1.1000 if (defc_oop.is_null() || name_str.is_null() || type_str.is_null()) { 1.1001 - THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to resolve"); 1.1002 + THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "nothing to resolve", empty); 1.1003 } 1.1004 1.1005 instanceKlassHandle defc; 1.1006 { 1.1007 klassOop defc_klassOop = java_lang_Class::as_klassOop(defc_oop()); 1.1008 - if (defc_klassOop == NULL) return; // a primitive; no resolution possible 1.1009 + if (defc_klassOop == NULL) return empty; // a primitive; no resolution possible 1.1010 if (!Klass::cast(defc_klassOop)->oop_is_instance()) { 1.1011 - if (!Klass::cast(defc_klassOop)->oop_is_array()) return; 1.1012 + if (!Klass::cast(defc_klassOop)->oop_is_array()) return empty; 1.1013 defc_klassOop = SystemDictionary::Object_klass(); 1.1014 } 1.1015 defc = instanceKlassHandle(THREAD, defc_klassOop); 1.1016 } 1.1017 if (defc.is_null()) { 1.1018 - THROW_MSG(vmSymbols::java_lang_InternalError(), "primitive class"); 1.1019 + THROW_MSG_(vmSymbols::java_lang_InternalError(), "primitive class", empty); 1.1020 } 1.1021 - defc->link_class(CHECK); // possible safepoint 1.1022 + defc->link_class(CHECK_(empty)); // possible safepoint 1.1023 1.1024 // convert the external string name to an internal symbol 1.1025 TempNewSymbol name = java_lang_String::as_symbol_or_null(name_str()); 1.1026 - if (name == NULL) return; // no such name 1.1027 + if (name == NULL) return empty; // no such name 1.1028 if (name == vmSymbols::class_initializer_name()) 1.1029 - return; // illegal name 1.1030 + return empty; // illegal name 1.1031 1.1032 - Handle polymorphic_method_type; 1.1033 - bool polymorphic_signature = false; 1.1034 + vmIntrinsics::ID mh_invoke_id = vmIntrinsics::_none; 1.1035 if ((flags & ALL_KINDS) == IS_METHOD && 1.1036 - (defc() == SystemDictionary::MethodHandle_klass() && 1.1037 - methodOopDesc::is_method_handle_invoke_name(name))) { 1.1038 - polymorphic_signature = true; 1.1039 + (defc() == SystemDictionary::MethodHandle_klass()) && 1.1040 + (ref_kind == JVM_REF_invokeVirtual || 1.1041 + ref_kind == JVM_REF_invokeSpecial || 1.1042 + // static invocation mode is required for _linkToVirtual, etc.: 1.1043 + ref_kind == JVM_REF_invokeStatic)) { 1.1044 + vmIntrinsics::ID iid = signature_polymorphic_name_id(name); 1.1045 + if (iid != vmIntrinsics::_none && 1.1046 + ((ref_kind == JVM_REF_invokeStatic) == is_signature_polymorphic_static(iid))) { 1.1047 + // Virtual methods invoke and invokeExact, plus internal invokers like _invokeBasic. 1.1048 + // For a static reference it could an internal linkage routine like _linkToVirtual, etc. 1.1049 + mh_invoke_id = iid; 1.1050 + } 1.1051 } 1.1052 1.1053 // convert the external string or reflective type to an internal signature 1.1054 - TempNewSymbol type = convert_to_signature(type_str(), polymorphic_signature, CHECK); 1.1055 - if (java_lang_invoke_MethodType::is_instance(type_str()) && polymorphic_signature) { 1.1056 - polymorphic_method_type = type_str; // preserve exactly 1.1057 - } 1.1058 - if (type == NULL) return; // no such signature exists in the VM 1.1059 + TempNewSymbol type = lookup_signature(type_str(), (mh_invoke_id != vmIntrinsics::_none), CHECK_(empty)); 1.1060 + if (type == NULL) return empty; // no such signature exists in the VM 1.1061 1.1062 // Time to do the lookup. 1.1063 switch (flags & ALL_KINDS) { 1.1064 case IS_METHOD: 1.1065 { 1.1066 CallInfo result; 1.1067 + bool do_dispatch = true; // default, neutral setting 1.1068 { 1.1069 - EXCEPTION_MARK; 1.1070 - if ((flags & JVM_ACC_STATIC) != 0) { 1.1071 + assert(!HAS_PENDING_EXCEPTION, ""); 1.1072 + if (ref_kind == JVM_REF_invokeStatic) { 1.1073 + //do_dispatch = false; // no need, since statics are never dispatched 1.1074 LinkResolver::resolve_static_call(result, 1.1075 defc, name, type, KlassHandle(), false, false, THREAD); 1.1076 - } else if (defc->is_interface()) { 1.1077 + } else if (ref_kind == JVM_REF_invokeInterface) { 1.1078 LinkResolver::resolve_interface_call(result, Handle(), defc, 1.1079 defc, name, type, KlassHandle(), false, false, THREAD); 1.1080 - } else { 1.1081 + } else if (mh_invoke_id != vmIntrinsics::_none) { 1.1082 + assert(!is_signature_polymorphic_static(mh_invoke_id), ""); 1.1083 + LinkResolver::resolve_handle_call(result, 1.1084 + defc, name, type, KlassHandle(), THREAD); 1.1085 + } else if (ref_kind == JVM_REF_invokeSpecial) { 1.1086 + do_dispatch = false; // force non-virtual linkage 1.1087 + LinkResolver::resolve_special_call(result, 1.1088 + defc, name, type, KlassHandle(), false, THREAD); 1.1089 + } else if (ref_kind == JVM_REF_invokeVirtual) { 1.1090 LinkResolver::resolve_virtual_call(result, Handle(), defc, 1.1091 defc, name, type, KlassHandle(), false, false, THREAD); 1.1092 + } else { 1.1093 + assert(false, err_msg("ref_kind=%d", ref_kind)); 1.1094 } 1.1095 if (HAS_PENDING_EXCEPTION) { 1.1096 - CLEAR_PENDING_EXCEPTION; 1.1097 - break; // go to second chance 1.1098 + return empty; 1.1099 } 1.1100 } 1.1101 - methodHandle m = result.resolved_method(); 1.1102 - oop vmtarget = NULL; 1.1103 - int vmindex = methodOopDesc::nonvirtual_vtable_index; 1.1104 - if (defc->is_interface()) { 1.1105 - vmindex = klassItable::compute_itable_index(m()); 1.1106 - assert(vmindex >= 0, ""); 1.1107 - } else if (result.has_vtable_index()) { 1.1108 - vmindex = result.vtable_index(); 1.1109 - assert(vmindex >= 0, ""); 1.1110 - } 1.1111 - assert(vmindex != VM_INDEX_UNINITIALIZED, ""); 1.1112 - if (vmindex < 0) { 1.1113 - assert(result.is_statically_bound(), ""); 1.1114 - vmtarget = m(); 1.1115 - } else { 1.1116 - vmtarget = result.resolved_klass()->as_klassOop(); 1.1117 - } 1.1118 - int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS); 1.1119 - java_lang_invoke_MemberName::set_vmtarget(mname(), vmtarget); 1.1120 - java_lang_invoke_MemberName::set_vmindex(mname(), vmindex); 1.1121 - java_lang_invoke_MemberName::set_modifiers(mname(), mods); 1.1122 - DEBUG_ONLY(KlassHandle junk1; int junk2); 1.1123 - assert(decode_MemberName(mname(), junk1, junk2) == result.resolved_method(), 1.1124 - "properly stored for later decoding"); 1.1125 - return; 1.1126 + return init_method_MemberName(mname(), result, THREAD); 1.1127 } 1.1128 case IS_CONSTRUCTOR: 1.1129 { 1.1130 CallInfo result; 1.1131 { 1.1132 - EXCEPTION_MARK; 1.1133 + assert(!HAS_PENDING_EXCEPTION, ""); 1.1134 if (name == vmSymbols::object_initializer_name()) { 1.1135 LinkResolver::resolve_special_call(result, 1.1136 defc, name, type, KlassHandle(), false, THREAD); 1.1137 @@ -682,22 +695,11 @@ 1.1138 break; // will throw after end of switch 1.1139 } 1.1140 if (HAS_PENDING_EXCEPTION) { 1.1141 - CLEAR_PENDING_EXCEPTION; 1.1142 - return; 1.1143 + return empty; 1.1144 } 1.1145 } 1.1146 assert(result.is_statically_bound(), ""); 1.1147 - methodHandle m = result.resolved_method(); 1.1148 - oop vmtarget = m(); 1.1149 - int vmindex = methodOopDesc::nonvirtual_vtable_index; 1.1150 - int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS); 1.1151 - java_lang_invoke_MemberName::set_vmtarget(mname(), vmtarget); 1.1152 - java_lang_invoke_MemberName::set_vmindex(mname(), vmindex); 1.1153 - java_lang_invoke_MemberName::set_modifiers(mname(), mods); 1.1154 - DEBUG_ONLY(KlassHandle junk1; int junk2); 1.1155 - assert(decode_MemberName(mname(), junk1, junk2) == result.resolved_method(), 1.1156 - "properly stored for later decoding"); 1.1157 - return; 1.1158 + return init_method_MemberName(mname(), result, THREAD); 1.1159 } 1.1160 case IS_FIELD: 1.1161 { 1.1162 @@ -705,54 +707,20 @@ 1.1163 fieldDescriptor fd; // find_field initializes fd if found 1.1164 KlassHandle sel_klass(THREAD, instanceKlass::cast(defc())->find_field(name, type, &fd)); 1.1165 // check if field exists; i.e., if a klass containing the field def has been selected 1.1166 - if (sel_klass.is_null()) return; 1.1167 - oop vmtarget = sel_klass->as_klassOop(); 1.1168 - int vmindex = fd.offset(); 1.1169 - int mods = (fd.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS); 1.1170 - if (vmindex == VM_INDEX_UNINITIALIZED) break; // should not happen 1.1171 - java_lang_invoke_MemberName::set_vmtarget(mname(), vmtarget); 1.1172 - java_lang_invoke_MemberName::set_vmindex(mname(), vmindex); 1.1173 - java_lang_invoke_MemberName::set_modifiers(mname(), mods); 1.1174 - return; 1.1175 + if (sel_klass.is_null()) return empty; // should not happen 1.1176 + oop type = field_signature_type_or_null(fd.signature()); 1.1177 + oop name = field_name_or_null(fd.name()); 1.1178 + bool is_setter = (ref_kind_is_valid(ref_kind) && ref_kind_is_setter(ref_kind)); 1.1179 + mname = Handle(THREAD, 1.1180 + init_field_MemberName(mname(), sel_klass->as_klassOop(), 1.1181 + fd.access_flags(), type, name, fd.offset(), is_setter)); 1.1182 + return mname; 1.1183 } 1.1184 default: 1.1185 - THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); 1.1186 + THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format", empty); 1.1187 } 1.1188 1.1189 - // Second chance. 1.1190 - if (polymorphic_method_type.not_null()) { 1.1191 - // Look on a non-null class loader. 1.1192 - Handle cur_class_loader; 1.1193 - const int nptypes = java_lang_invoke_MethodType::ptype_count(polymorphic_method_type()); 1.1194 - for (int i = 0; i <= nptypes; i++) { 1.1195 - oop type_mirror; 1.1196 - if (i < nptypes) type_mirror = java_lang_invoke_MethodType::ptype(polymorphic_method_type(), i); 1.1197 - else type_mirror = java_lang_invoke_MethodType::rtype(polymorphic_method_type()); 1.1198 - klassOop example_type = java_lang_Class::as_klassOop(type_mirror); 1.1199 - if (example_type == NULL) continue; 1.1200 - oop class_loader = Klass::cast(example_type)->class_loader(); 1.1201 - if (class_loader == NULL || class_loader == cur_class_loader()) continue; 1.1202 - cur_class_loader = Handle(THREAD, class_loader); 1.1203 - methodOop m = SystemDictionary::find_method_handle_invoke(name, 1.1204 - type, 1.1205 - KlassHandle(THREAD, example_type), 1.1206 - THREAD); 1.1207 - if (HAS_PENDING_EXCEPTION) { 1.1208 - CLEAR_PENDING_EXCEPTION; 1.1209 - m = NULL; 1.1210 - // try again with a different class loader... 1.1211 - } 1.1212 - if (m != NULL && 1.1213 - m->is_method_handle_invoke() && 1.1214 - java_lang_invoke_MethodType::equals(polymorphic_method_type(), m->method_handle_type())) { 1.1215 - int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS); 1.1216 - java_lang_invoke_MemberName::set_vmtarget(mname(), m); 1.1217 - java_lang_invoke_MemberName::set_vmindex(mname(), m->vtable_index()); 1.1218 - java_lang_invoke_MemberName::set_modifiers(mname(), mods); 1.1219 - return; 1.1220 - } 1.1221 - } 1.1222 - } 1.1223 + return empty; 1.1224 } 1.1225 1.1226 // Conversely, a member name which is only initialized from JVM internals 1.1227 @@ -763,7 +731,7 @@ 1.1228 assert(java_lang_invoke_MemberName::is_instance(mname()), ""); 1.1229 oop vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); 1.1230 int vmindex = java_lang_invoke_MemberName::vmindex(mname()); 1.1231 - if (vmtarget == NULL || vmindex == VM_INDEX_UNINITIALIZED) { 1.1232 + if (vmtarget == NULL) { 1.1233 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand"); 1.1234 } 1.1235 1.1236 @@ -784,14 +752,12 @@ 1.1237 case IS_METHOD: 1.1238 case IS_CONSTRUCTOR: 1.1239 { 1.1240 - KlassHandle receiver_limit; int decode_flags = 0; 1.1241 - methodHandle m = decode_vmtarget(vmtarget, vmindex, NULL, receiver_limit, decode_flags); 1.1242 + assert(vmtarget->is_method(), "method or constructor vmtarget is methodOop"); 1.1243 + methodHandle m(THREAD, methodOop(vmtarget)); 1.1244 + DEBUG_ONLY(vmtarget = NULL); // safety 1.1245 if (m.is_null()) break; 1.1246 if (!have_defc) { 1.1247 klassOop defc = m->method_holder(); 1.1248 - if (receiver_limit.not_null() && receiver_limit() != defc 1.1249 - && Klass::cast(receiver_limit())->is_subtype_of(defc)) 1.1250 - defc = receiver_limit(); 1.1251 java_lang_invoke_MemberName::set_clazz(mname(), Klass::cast(defc)->java_mirror()); 1.1252 } 1.1253 if (!have_name) { 1.1254 @@ -808,9 +774,10 @@ 1.1255 case IS_FIELD: 1.1256 { 1.1257 // This is taken from LinkResolver::resolve_field, sans access checks. 1.1258 - if (!vmtarget->is_klass()) break; 1.1259 + assert(vmtarget->is_klass(), "field vmtarget is klassOop"); 1.1260 if (!Klass::cast((klassOop) vmtarget)->oop_is_instance()) break; 1.1261 instanceKlassHandle defc(THREAD, (klassOop) vmtarget); 1.1262 + DEBUG_ONLY(vmtarget = NULL); // safety 1.1263 bool is_static = ((flags & JVM_ACC_STATIC) != 0); 1.1264 fieldDescriptor fd; // find_field initializes fd if found 1.1265 if (!defc->find_field_from_offset(vmindex, is_static, &fd)) 1.1266 @@ -824,7 +791,11 @@ 1.1267 java_lang_invoke_MemberName::set_name(mname(), name()); 1.1268 } 1.1269 if (!have_type) { 1.1270 - Handle type = java_lang_String::create_from_symbol(fd.signature(), CHECK); 1.1271 + // If it is a primitive field type, don't mess with short strings like "I". 1.1272 + Handle type = field_signature_type_or_null(fd.signature()); 1.1273 + if (type.is_null()) { 1.1274 + java_lang_String::create_from_symbol(fd.signature(), CHECK); 1.1275 + } 1.1276 java_lang_invoke_MemberName::set_type(mname(), type()); 1.1277 } 1.1278 return; 1.1279 @@ -882,7 +853,13 @@ 1.1280 oop result = results->obj_at(rfill++); 1.1281 if (!java_lang_invoke_MemberName::is_instance(result)) 1.1282 return -99; // caller bug! 1.1283 - MethodHandles::init_MemberName(result, st.klass()->as_klassOop(), st.access_flags(), st.offset()); 1.1284 + oop type = field_signature_type_or_null(st.signature()); 1.1285 + oop name = field_name_or_null(st.name()); 1.1286 + oop saved = MethodHandles::init_field_MemberName(result, st.klass()->as_klassOop(), 1.1287 + st.access_flags(), type, name, 1.1288 + st.offset()); 1.1289 + if (saved != result) 1.1290 + results->obj_at_put(rfill-1, saved); // show saved instance to user 1.1291 } else if (++overflow >= overflow_limit) { 1.1292 match_flags = 0; break; // got tired of looking at overflow 1.1293 } 1.1294 @@ -930,7 +907,9 @@ 1.1295 oop result = results->obj_at(rfill++); 1.1296 if (!java_lang_invoke_MemberName::is_instance(result)) 1.1297 return -99; // caller bug! 1.1298 - MethodHandles::init_MemberName(result, m, true); 1.1299 + oop saved = MethodHandles::init_method_MemberName(result, m, true, NULL); 1.1300 + if (saved != result) 1.1301 + results->obj_at_put(rfill-1, saved); // show saved instance to user 1.1302 } else if (++overflow >= overflow_limit) { 1.1303 match_flags = 0; break; // got tired of looking at overflow 1.1304 } 1.1305 @@ -941,1925 +920,16 @@ 1.1306 return rfill + overflow; 1.1307 } 1.1308 1.1309 - 1.1310 -// Decode this java.lang.Class object into an instanceKlass, if possible. 1.1311 -// Throw IAE if not 1.1312 -instanceKlassHandle MethodHandles::resolve_instance_klass(oop java_mirror_oop, TRAPS) { 1.1313 - instanceKlassHandle empty; 1.1314 - klassOop caller = NULL; 1.1315 - if (java_lang_Class::is_instance(java_mirror_oop)) { 1.1316 - caller = java_lang_Class::as_klassOop(java_mirror_oop); 1.1317 - } 1.1318 - if (caller == NULL || !Klass::cast(caller)->oop_is_instance()) { 1.1319 - THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "not a class", empty); 1.1320 - } 1.1321 - return instanceKlassHandle(THREAD, caller); 1.1322 -} 1.1323 - 1.1324 - 1.1325 - 1.1326 -// Decode the vmtarget field of a method handle. 1.1327 -// Sanitize out methodOops, klassOops, and any other non-Java data. 1.1328 -// This is for debugging and reflection. 1.1329 -oop MethodHandles::encode_target(Handle mh, int format, TRAPS) { 1.1330 - assert(java_lang_invoke_MethodHandle::is_instance(mh()), "must be a MH"); 1.1331 - if (format == ETF_FORCE_DIRECT_HANDLE || 1.1332 - format == ETF_COMPILE_DIRECT_HANDLE) { 1.1333 - // Internal function for stress testing. 1.1334 - Handle mt = java_lang_invoke_MethodHandle::type(mh()); 1.1335 - int invocation_count = 10000; 1.1336 - TempNewSymbol signature = java_lang_invoke_MethodType::as_signature(mt(), true, CHECK_NULL); 1.1337 - bool omit_receiver_argument = true; 1.1338 - MethodHandleCompiler mhc(mh, vmSymbols::invoke_name(), signature, invocation_count, omit_receiver_argument, CHECK_NULL); 1.1339 - methodHandle m = mhc.compile(CHECK_NULL); 1.1340 - if (StressMethodHandleWalk && Verbose || PrintMiscellaneous) { 1.1341 - tty->print_cr("MethodHandleNatives.getTarget(%s)", 1.1342 - format == ETF_FORCE_DIRECT_HANDLE ? "FORCE_DIRECT" : "COMPILE_DIRECT"); 1.1343 - if (Verbose) { 1.1344 - m->print_codes(); 1.1345 - } 1.1346 - } 1.1347 - if (StressMethodHandleWalk) { 1.1348 - InterpreterOopMap mask; 1.1349 - OopMapCache::compute_one_oop_map(m, m->code_size() - 1, &mask); 1.1350 - } 1.1351 - if ((format == ETF_COMPILE_DIRECT_HANDLE || 1.1352 - CompilationPolicy::must_be_compiled(m)) 1.1353 - && !instanceKlass::cast(m->method_holder())->is_not_initialized() 1.1354 - && CompilationPolicy::can_be_compiled(m)) { 1.1355 - // Force compilation 1.1356 - CompileBroker::compile_method(m, InvocationEntryBci, 1.1357 - CompilationPolicy::policy()->initial_compile_level(), 1.1358 - methodHandle(), 0, "MethodHandleNatives.getTarget", 1.1359 - CHECK_NULL); 1.1360 - } 1.1361 - // Now wrap m in a DirectMethodHandle. 1.1362 - instanceKlassHandle dmh_klass(THREAD, SystemDictionary::DirectMethodHandle_klass()); 1.1363 - Handle dmh = dmh_klass->allocate_instance_handle(CHECK_NULL); 1.1364 - JavaValue ignore_result(T_VOID); 1.1365 - Symbol* init_name = vmSymbols::object_initializer_name(); 1.1366 - Symbol* init_sig = vmSymbols::notifyGenericMethodType_signature(); 1.1367 - JavaCalls::call_special(&ignore_result, dmh, 1.1368 - SystemDictionaryHandles::MethodHandle_klass(), init_name, init_sig, 1.1369 - java_lang_invoke_MethodHandle::type(mh()), CHECK_NULL); 1.1370 - MethodHandles::init_DirectMethodHandle(dmh, m, false, CHECK_NULL); 1.1371 - return dmh(); 1.1372 - } 1.1373 - if (format == ETF_HANDLE_OR_METHOD_NAME) { 1.1374 - oop target = java_lang_invoke_MethodHandle::vmtarget(mh()); 1.1375 - if (target == NULL) { 1.1376 - return NULL; // unformed MH 1.1377 - } 1.1378 - klassOop tklass = target->klass(); 1.1379 - if (Klass::cast(tklass)->is_subclass_of(SystemDictionary::Object_klass())) { 1.1380 - return target; // target is another MH (or something else?) 1.1381 - } 1.1382 - } 1.1383 - if (format == ETF_DIRECT_HANDLE) { 1.1384 - oop target = mh(); 1.1385 - for (;;) { 1.1386 - if (target->klass() == SystemDictionary::DirectMethodHandle_klass()) { 1.1387 - return target; 1.1388 - } 1.1389 - if (!java_lang_invoke_MethodHandle::is_instance(target)){ 1.1390 - return NULL; // unformed MH 1.1391 - } 1.1392 - target = java_lang_invoke_MethodHandle::vmtarget(target); 1.1393 - } 1.1394 - } 1.1395 - // cases of metadata in MH.vmtarget: 1.1396 - // - AMH can have methodOop for static invoke with bound receiver 1.1397 - // - DMH can have methodOop for static invoke (on variable receiver) 1.1398 - // - DMH can have klassOop for dispatched (non-static) invoke 1.1399 - KlassHandle receiver_limit; int decode_flags = 0; 1.1400 - methodHandle m = decode_MethodHandle(mh(), receiver_limit, decode_flags); 1.1401 - if (m.is_null()) return NULL; 1.1402 - switch (format) { 1.1403 - case ETF_REFLECT_METHOD: 1.1404 - // same as jni_ToReflectedMethod: 1.1405 - if (m->is_initializer()) { 1.1406 - return Reflection::new_constructor(m, THREAD); 1.1407 - } else { 1.1408 - return Reflection::new_method(m, UseNewReflection, false, THREAD); 1.1409 - } 1.1410 - 1.1411 - case ETF_HANDLE_OR_METHOD_NAME: // method, not handle 1.1412 - case ETF_METHOD_NAME: 1.1413 - { 1.1414 - if (SystemDictionary::MemberName_klass() == NULL) break; 1.1415 - instanceKlassHandle mname_klass(THREAD, SystemDictionary::MemberName_klass()); 1.1416 - mname_klass->initialize(CHECK_NULL); 1.1417 - Handle mname = mname_klass->allocate_instance_handle(CHECK_NULL); // possible safepoint 1.1418 - java_lang_invoke_MemberName::set_vmindex(mname(), VM_INDEX_UNINITIALIZED); 1.1419 - bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0); 1.1420 - init_MemberName(mname(), m(), do_dispatch); 1.1421 - expand_MemberName(mname, 0, CHECK_NULL); 1.1422 - return mname(); 1.1423 - } 1.1424 - } 1.1425 - 1.1426 - // Unknown format code. 1.1427 - char msg[50]; 1.1428 - jio_snprintf(msg, sizeof(msg), "unknown getTarget format=%d", format); 1.1429 - THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), msg); 1.1430 -} 1.1431 - 1.1432 -static const char* always_null_names[] = { 1.1433 - "java/lang/Void", 1.1434 - "java/lang/Null", 1.1435 - //"java/lang/Nothing", 1.1436 - "sun/dyn/empty/Empty", 1.1437 - "sun/invoke/empty/Empty", 1.1438 - NULL 1.1439 -}; 1.1440 - 1.1441 -static bool is_always_null_type(klassOop klass) { 1.1442 - if (klass == NULL) return false; // safety 1.1443 - if (!Klass::cast(klass)->oop_is_instance()) return false; 1.1444 - instanceKlass* ik = instanceKlass::cast(klass); 1.1445 - // Must be on the boot class path: 1.1446 - if (ik->class_loader() != NULL) return false; 1.1447 - // Check the name. 1.1448 - Symbol* name = ik->name(); 1.1449 - for (int i = 0; ; i++) { 1.1450 - const char* test_name = always_null_names[i]; 1.1451 - if (test_name == NULL) break; 1.1452 - if (name->equals(test_name)) 1.1453 - return true; 1.1454 - } 1.1455 - return false; 1.1456 -} 1.1457 - 1.1458 -bool MethodHandles::class_cast_needed(klassOop src, klassOop dst) { 1.1459 - if (dst == NULL) return true; 1.1460 - if (src == NULL) return (dst != SystemDictionary::Object_klass()); 1.1461 - if (src == dst || dst == SystemDictionary::Object_klass()) 1.1462 - return false; // quickest checks 1.1463 - Klass* srck = Klass::cast(src); 1.1464 - Klass* dstk = Klass::cast(dst); 1.1465 - if (dstk->is_interface()) { 1.1466 - // interface receivers can safely be viewed as untyped, 1.1467 - // because interface calls always include a dynamic check 1.1468 - //dstk = Klass::cast(SystemDictionary::Object_klass()); 1.1469 - return false; 1.1470 - } 1.1471 - if (srck->is_interface()) { 1.1472 - // interface arguments must be viewed as untyped 1.1473 - //srck = Klass::cast(SystemDictionary::Object_klass()); 1.1474 - return true; 1.1475 - } 1.1476 - if (is_always_null_type(src)) { 1.1477 - // some source types are known to be never instantiated; 1.1478 - // they represent references which are always null 1.1479 - // such null references never fail to convert safely 1.1480 - return false; 1.1481 - } 1.1482 - return !srck->is_subclass_of(dstk->as_klassOop()); 1.1483 -} 1.1484 - 1.1485 -static oop object_java_mirror() { 1.1486 - return Klass::cast(SystemDictionary::Object_klass())->java_mirror(); 1.1487 -} 1.1488 - 1.1489 -bool MethodHandles::is_float_fixed_reinterpretation_cast(BasicType src, BasicType dst) { 1.1490 - if (src == T_FLOAT) return dst == T_INT; 1.1491 - if (src == T_INT) return dst == T_FLOAT; 1.1492 - if (src == T_DOUBLE) return dst == T_LONG; 1.1493 - if (src == T_LONG) return dst == T_DOUBLE; 1.1494 - return false; 1.1495 -} 1.1496 - 1.1497 -bool MethodHandles::same_basic_type_for_arguments(BasicType src, 1.1498 - BasicType dst, 1.1499 - bool raw, 1.1500 - bool for_return) { 1.1501 - if (for_return) { 1.1502 - // return values can always be forgotten: 1.1503 - if (dst == T_VOID) return true; 1.1504 - if (src == T_VOID) return raw && (dst == T_INT); 1.1505 - // We allow caller to receive a garbage int, which is harmless. 1.1506 - // This trick is pulled by trusted code (see VerifyType.canPassRaw). 1.1507 - } 1.1508 - assert(src != T_VOID && dst != T_VOID, "should not be here"); 1.1509 - if (src == dst) return true; 1.1510 - if (type2size[src] != type2size[dst]) return false; 1.1511 - if (src == T_OBJECT || dst == T_OBJECT) return false; 1.1512 - if (raw) return true; // bitwise reinterpretation; caller guarantees safety 1.1513 - // allow reinterpretation casts for integral widening 1.1514 - if (is_subword_type(src)) { // subwords can fit in int or other subwords 1.1515 - if (dst == T_INT) // any subword fits in an int 1.1516 - return true; 1.1517 - if (src == T_BOOLEAN) // boolean fits in any subword 1.1518 - return is_subword_type(dst); 1.1519 - if (src == T_BYTE && dst == T_SHORT) 1.1520 - return true; // remaining case: byte fits in short 1.1521 - } 1.1522 - // allow float/fixed reinterpretation casts 1.1523 - if (is_float_fixed_reinterpretation_cast(src, dst)) 1.1524 - return true; 1.1525 - return false; 1.1526 -} 1.1527 - 1.1528 -const char* MethodHandles::check_method_receiver(methodOop m, 1.1529 - klassOop passed_recv_type) { 1.1530 - assert(!m->is_static(), "caller resp."); 1.1531 - if (passed_recv_type == NULL) 1.1532 - return "receiver type is primitive"; 1.1533 - if (class_cast_needed(passed_recv_type, m->method_holder())) { 1.1534 - Klass* formal = Klass::cast(m->method_holder()); 1.1535 - return SharedRuntime::generate_class_cast_message("receiver type", 1.1536 - formal->external_name()); 1.1537 - } 1.1538 - return NULL; // checks passed 1.1539 -} 1.1540 - 1.1541 -// Verify that m's signature can be called type-safely by a method handle 1.1542 -// of the given method type 'mtype'. 1.1543 -// It takes a TRAPS argument because it must perform symbol lookups. 1.1544 -void MethodHandles::verify_method_signature(methodHandle m, 1.1545 - Handle mtype, 1.1546 - int first_ptype_pos, 1.1547 - KlassHandle insert_ptype, 1.1548 - TRAPS) { 1.1549 - Handle mhi_type; 1.1550 - if (m->is_method_handle_invoke()) { 1.1551 - // use this more exact typing instead of the symbolic signature: 1.1552 - mhi_type = Handle(THREAD, m->method_handle_type()); 1.1553 - } 1.1554 - objArrayHandle ptypes(THREAD, java_lang_invoke_MethodType::ptypes(mtype())); 1.1555 - int pnum = first_ptype_pos; 1.1556 - int pmax = ptypes->length(); 1.1557 - int anum = 0; // method argument 1.1558 - const char* err = NULL; 1.1559 - ResourceMark rm(THREAD); 1.1560 - for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) { 1.1561 - oop ptype_oop = NULL; 1.1562 - if (ss.at_return_type()) { 1.1563 - if (pnum != pmax) 1.1564 - { err = "too many arguments"; break; } 1.1565 - ptype_oop = java_lang_invoke_MethodType::rtype(mtype()); 1.1566 - } else { 1.1567 - if (pnum >= pmax) 1.1568 - { err = "not enough arguments"; break; } 1.1569 - if (pnum >= 0) 1.1570 - ptype_oop = ptypes->obj_at(pnum); 1.1571 - else if (insert_ptype.is_null()) 1.1572 - ptype_oop = NULL; 1.1573 - else 1.1574 - ptype_oop = insert_ptype->java_mirror(); 1.1575 - pnum += 1; 1.1576 - anum += 1; 1.1577 - } 1.1578 - KlassHandle pklass; 1.1579 - BasicType ptype = T_OBJECT; 1.1580 - bool have_ptype = false; 1.1581 - // missing ptype_oop does not match any non-reference; use Object to report the error 1.1582 - pklass = SystemDictionaryHandles::Object_klass(); 1.1583 - if (ptype_oop != NULL) { 1.1584 - have_ptype = true; 1.1585 - klassOop pklass_oop = NULL; 1.1586 - ptype = java_lang_Class::as_BasicType(ptype_oop, &pklass_oop); 1.1587 - pklass = KlassHandle(THREAD, pklass_oop); 1.1588 - } 1.1589 - ptype_oop = NULL; //done with this 1.1590 - KlassHandle aklass; 1.1591 - BasicType atype = ss.type(); 1.1592 - if (atype == T_ARRAY) atype = T_OBJECT; // fold all refs to T_OBJECT 1.1593 - if (atype == T_OBJECT) { 1.1594 - if (!have_ptype) { 1.1595 - // null matches any reference 1.1596 - continue; 1.1597 - } 1.1598 - if (mhi_type.is_null()) { 1.1599 - // If we fail to resolve types at this point, we will usually throw an error. 1.1600 - TempNewSymbol name = ss.as_symbol_or_null(); 1.1601 - if (name != NULL) { 1.1602 - instanceKlass* mk = instanceKlass::cast(m->method_holder()); 1.1603 - Handle loader(THREAD, mk->class_loader()); 1.1604 - Handle domain(THREAD, mk->protection_domain()); 1.1605 - klassOop aklass_oop = SystemDictionary::resolve_or_null(name, loader, domain, CHECK); 1.1606 - if (aklass_oop != NULL) 1.1607 - aklass = KlassHandle(THREAD, aklass_oop); 1.1608 - if (aklass.is_null() && 1.1609 - pklass.not_null() && 1.1610 - loader.is_null() && 1.1611 - pklass->name() == name) 1.1612 - // accept name equivalence here, since that's the best we can do 1.1613 - aklass = pklass; 1.1614 - } 1.1615 - } else { 1.1616 - // for method handle invokers we don't look at the name in the signature 1.1617 - oop atype_oop; 1.1618 - if (ss.at_return_type()) 1.1619 - atype_oop = java_lang_invoke_MethodType::rtype(mhi_type()); 1.1620 - else 1.1621 - atype_oop = java_lang_invoke_MethodType::ptype(mhi_type(), anum-1); 1.1622 - klassOop aklass_oop = NULL; 1.1623 - atype = java_lang_Class::as_BasicType(atype_oop, &aklass_oop); 1.1624 - aklass = KlassHandle(THREAD, aklass_oop); 1.1625 - } 1.1626 - } 1.1627 - if (!ss.at_return_type()) { 1.1628 - err = check_argument_type_change(ptype, pklass(), atype, aklass(), anum); 1.1629 - } else { 1.1630 - err = check_return_type_change(atype, aklass(), ptype, pklass()); // note reversal! 1.1631 - } 1.1632 - if (err != NULL) break; 1.1633 - } 1.1634 - 1.1635 - if (err != NULL) { 1.1636 -#ifndef PRODUCT 1.1637 - if (PrintMiscellaneous && (Verbose || WizardMode)) { 1.1638 - tty->print("*** verify_method_signature failed: "); 1.1639 - java_lang_invoke_MethodType::print_signature(mtype(), tty); 1.1640 - tty->cr(); 1.1641 - tty->print_cr(" first_ptype_pos = %d, insert_ptype = "UINTX_FORMAT, first_ptype_pos, insert_ptype()); 1.1642 - tty->print(" Failing method: "); 1.1643 - m->print(); 1.1644 - } 1.1645 -#endif //PRODUCT 1.1646 - THROW_MSG(vmSymbols::java_lang_InternalError(), err); 1.1647 - } 1.1648 -} 1.1649 - 1.1650 -// Main routine for verifying the MethodHandle.type of a proposed 1.1651 -// direct or bound-direct method handle. 1.1652 -void MethodHandles::verify_method_type(methodHandle m, 1.1653 - Handle mtype, 1.1654 - bool has_bound_recv, 1.1655 - KlassHandle bound_recv_type, 1.1656 - TRAPS) { 1.1657 - bool m_needs_receiver = !m->is_static(); 1.1658 - 1.1659 - const char* err = NULL; 1.1660 - 1.1661 - int first_ptype_pos = m_needs_receiver ? 1 : 0; 1.1662 - if (has_bound_recv) { 1.1663 - first_ptype_pos -= 1; // ptypes do not include the bound argument; start earlier in them 1.1664 - if (m_needs_receiver && bound_recv_type.is_null()) 1.1665 - { err = "bound receiver is not an object"; goto die; } 1.1666 - } 1.1667 - 1.1668 - if (m_needs_receiver && err == NULL) { 1.1669 - objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(mtype()); 1.1670 - if (ptypes->length() < first_ptype_pos) 1.1671 - { err = "receiver argument is missing"; goto die; } 1.1672 - if (has_bound_recv) 1.1673 - err = check_method_receiver(m(), bound_recv_type->as_klassOop()); 1.1674 - else 1.1675 - err = check_method_receiver(m(), java_lang_Class::as_klassOop(ptypes->obj_at(first_ptype_pos-1))); 1.1676 - if (err != NULL) goto die; 1.1677 - } 1.1678 - 1.1679 - // Check the other arguments for mistypes. 1.1680 - verify_method_signature(m, mtype, first_ptype_pos, bound_recv_type, CHECK); 1.1681 - return; 1.1682 - 1.1683 - die: 1.1684 - THROW_MSG(vmSymbols::java_lang_InternalError(), err); 1.1685 -} 1.1686 - 1.1687 -void MethodHandles::verify_vmslots(Handle mh, TRAPS) { 1.1688 - // Verify vmslots. 1.1689 - int check_slots = argument_slot_count(java_lang_invoke_MethodHandle::type(mh())); 1.1690 - if (java_lang_invoke_MethodHandle::vmslots(mh()) != check_slots) { 1.1691 - THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in BMH"); 1.1692 - } 1.1693 -} 1.1694 - 1.1695 -void MethodHandles::verify_vmargslot(Handle mh, int argnum, int argslot, TRAPS) { 1.1696 - // Verify that argslot points at the given argnum. 1.1697 - int check_slot = argument_slot(java_lang_invoke_MethodHandle::type(mh()), argnum); 1.1698 - if (argslot != check_slot || argslot < 0) { 1.1699 - ResourceMark rm; 1.1700 - const char* fmt = "for argnum of %d, vmargslot is %d, should be %d"; 1.1701 - size_t msglen = strlen(fmt) + 3*11 + 1; 1.1702 - char* msg = NEW_RESOURCE_ARRAY(char, msglen); 1.1703 - jio_snprintf(msg, msglen, fmt, argnum, argslot, check_slot); 1.1704 - THROW_MSG(vmSymbols::java_lang_InternalError(), msg); 1.1705 - } 1.1706 -} 1.1707 - 1.1708 -// Verify the correspondence between two method types. 1.1709 -// Apart from the advertised changes, caller method type X must 1.1710 -// be able to invoke the callee method Y type with no violations 1.1711 -// of type integrity. 1.1712 -// Return NULL if all is well, else a short error message. 1.1713 -const char* MethodHandles::check_method_type_change(oop src_mtype, int src_beg, int src_end, 1.1714 - int insert_argnum, oop insert_type, 1.1715 - int change_argnum, oop change_type, 1.1716 - int delete_argnum, 1.1717 - oop dst_mtype, int dst_beg, int dst_end, 1.1718 - bool raw) { 1.1719 - objArrayOop src_ptypes = java_lang_invoke_MethodType::ptypes(src_mtype); 1.1720 - objArrayOop dst_ptypes = java_lang_invoke_MethodType::ptypes(dst_mtype); 1.1721 - 1.1722 - int src_max = src_ptypes->length(); 1.1723 - int dst_max = dst_ptypes->length(); 1.1724 - 1.1725 - if (src_end == -1) src_end = src_max; 1.1726 - if (dst_end == -1) dst_end = dst_max; 1.1727 - 1.1728 - assert(0 <= src_beg && src_beg <= src_end && src_end <= src_max, "oob"); 1.1729 - assert(0 <= dst_beg && dst_beg <= dst_end && dst_end <= dst_max, "oob"); 1.1730 - 1.1731 - // pending actions; set to -1 when done: 1.1732 - int ins_idx = insert_argnum, chg_idx = change_argnum, del_idx = delete_argnum; 1.1733 - 1.1734 - const char* err = NULL; 1.1735 - 1.1736 - // Walk along each array of parameter types, including a virtual 1.1737 - // NULL end marker at the end of each. 1.1738 - for (int src_idx = src_beg, dst_idx = dst_beg; 1.1739 - (src_idx <= src_end && dst_idx <= dst_end); 1.1740 - src_idx++, dst_idx++) { 1.1741 - oop src_type = (src_idx == src_end) ? oop(NULL) : src_ptypes->obj_at(src_idx); 1.1742 - oop dst_type = (dst_idx == dst_end) ? oop(NULL) : dst_ptypes->obj_at(dst_idx); 1.1743 - bool fix_null_src_type = false; 1.1744 - 1.1745 - // Perform requested edits. 1.1746 - if (ins_idx == src_idx) { 1.1747 - // note that the inserted guy is never affected by a change or deletion 1.1748 - ins_idx = -1; 1.1749 - src_type = insert_type; 1.1750 - fix_null_src_type = true; 1.1751 - --src_idx; // back up to process src type on next loop 1.1752 - src_idx = src_end; 1.1753 - } else { 1.1754 - // note that the changed guy can be immediately deleted 1.1755 - if (chg_idx == src_idx) { 1.1756 - chg_idx = -1; 1.1757 - assert(src_idx < src_end, "oob"); 1.1758 - src_type = change_type; 1.1759 - fix_null_src_type = true; 1.1760 - } 1.1761 - if (del_idx == src_idx) { 1.1762 - del_idx = -1; 1.1763 - assert(src_idx < src_end, "oob"); 1.1764 - --dst_idx; 1.1765 - continue; // rerun loop after skipping this position 1.1766 - } 1.1767 - } 1.1768 - 1.1769 - if (src_type == NULL && fix_null_src_type) 1.1770 - // explicit null in this case matches any dest reference 1.1771 - src_type = (java_lang_Class::is_primitive(dst_type) ? object_java_mirror() : dst_type); 1.1772 - 1.1773 - // Compare the two argument types. 1.1774 - if (src_type != dst_type) { 1.1775 - if (src_type == NULL) return "not enough arguments"; 1.1776 - if (dst_type == NULL) return "too many arguments"; 1.1777 - err = check_argument_type_change(src_type, dst_type, dst_idx, raw); 1.1778 - if (err != NULL) return err; 1.1779 - } 1.1780 - } 1.1781 - 1.1782 - // Now compare return types also. 1.1783 - oop src_rtype = java_lang_invoke_MethodType::rtype(src_mtype); 1.1784 - oop dst_rtype = java_lang_invoke_MethodType::rtype(dst_mtype); 1.1785 - if (src_rtype != dst_rtype) { 1.1786 - err = check_return_type_change(dst_rtype, src_rtype, raw); // note reversal! 1.1787 - if (err != NULL) return err; 1.1788 - } 1.1789 - 1.1790 - assert(err == NULL, ""); 1.1791 - return NULL; // all is well 1.1792 -} 1.1793 - 1.1794 - 1.1795 -const char* MethodHandles::check_argument_type_change(BasicType src_type, 1.1796 - klassOop src_klass, 1.1797 - BasicType dst_type, 1.1798 - klassOop dst_klass, 1.1799 - int argnum, 1.1800 - bool raw) { 1.1801 - const char* err = NULL; 1.1802 - const bool for_return = (argnum < 0); 1.1803 - 1.1804 - // just in case: 1.1805 - if (src_type == T_ARRAY) src_type = T_OBJECT; 1.1806 - if (dst_type == T_ARRAY) dst_type = T_OBJECT; 1.1807 - 1.1808 - // Produce some nice messages if VerifyMethodHandles is turned on: 1.1809 - if (!same_basic_type_for_arguments(src_type, dst_type, raw, for_return)) { 1.1810 - if (src_type == T_OBJECT) { 1.1811 - if (raw && is_java_primitive(dst_type)) 1.1812 - return NULL; // ref-to-prim discards ref and returns zero 1.1813 - err = (!for_return 1.1814 - ? "type mismatch: passing a %s for method argument #%d, which expects primitive %s" 1.1815 - : "type mismatch: returning a %s, but caller expects primitive %s"); 1.1816 - } else if (dst_type == T_OBJECT) { 1.1817 - err = (!for_return 1.1818 - ? "type mismatch: passing a primitive %s for method argument #%d, which expects %s" 1.1819 - : "type mismatch: returning a primitive %s, but caller expects %s"); 1.1820 - } else { 1.1821 - err = (!for_return 1.1822 - ? "type mismatch: passing a %s for method argument #%d, which expects %s" 1.1823 - : "type mismatch: returning a %s, but caller expects %s"); 1.1824 - } 1.1825 - } else if (src_type == T_OBJECT && dst_type == T_OBJECT && 1.1826 - class_cast_needed(src_klass, dst_klass)) { 1.1827 - if (!class_cast_needed(dst_klass, src_klass)) { 1.1828 - if (raw) 1.1829 - return NULL; // reverse cast is OK; the MH target is trusted to enforce it 1.1830 - err = (!for_return 1.1831 - ? "cast required: passing a %s for method argument #%d, which expects %s" 1.1832 - : "cast required: returning a %s, but caller expects %s"); 1.1833 - } else { 1.1834 - err = (!for_return 1.1835 - ? "reference mismatch: passing a %s for method argument #%d, which expects %s" 1.1836 - : "reference mismatch: returning a %s, but caller expects %s"); 1.1837 - } 1.1838 - } else { 1.1839 - // passed the obstacle course 1.1840 - return NULL; 1.1841 - } 1.1842 - 1.1843 - // format, format, format 1.1844 - const char* src_name = type2name(src_type); 1.1845 - const char* dst_name = type2name(dst_type); 1.1846 - if (src_name == NULL) src_name = "unknown type"; 1.1847 - if (dst_name == NULL) dst_name = "unknown type"; 1.1848 - if (src_type == T_OBJECT) 1.1849 - src_name = (src_klass != NULL) ? Klass::cast(src_klass)->external_name() : "an unresolved class"; 1.1850 - if (dst_type == T_OBJECT) 1.1851 - dst_name = (dst_klass != NULL) ? Klass::cast(dst_klass)->external_name() : "an unresolved class"; 1.1852 - 1.1853 - size_t msglen = strlen(err) + strlen(src_name) + strlen(dst_name) + (argnum < 10 ? 1 : 11); 1.1854 - char* msg = NEW_RESOURCE_ARRAY(char, msglen + 1); 1.1855 - if (!for_return) { 1.1856 - assert(strstr(err, "%d") != NULL, ""); 1.1857 - jio_snprintf(msg, msglen, err, src_name, argnum, dst_name); 1.1858 - } else { 1.1859 - assert(strstr(err, "%d") == NULL, ""); 1.1860 - jio_snprintf(msg, msglen, err, src_name, dst_name); 1.1861 - } 1.1862 - return msg; 1.1863 -} 1.1864 - 1.1865 -// Compute the depth within the stack of the given argument, i.e., 1.1866 -// the combined size of arguments to the right of the given argument. 1.1867 -// For the last argument (ptypes.length-1) this will be zero. 1.1868 -// For the first argument (0) this will be the size of all 1.1869 -// arguments but that one. For the special number -1, this 1.1870 -// will be the size of all arguments, including the first. 1.1871 -// If the argument is neither -1 nor a valid argument index, 1.1872 -// then return a negative number. Otherwise, the result 1.1873 -// is in the range [0..vmslots] inclusive. 1.1874 -int MethodHandles::argument_slot(oop method_type, int arg) { 1.1875 - objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(method_type); 1.1876 - int argslot = 0; 1.1877 - int len = ptypes->length(); 1.1878 - if (arg < -1 || arg >= len) return -99; 1.1879 - for (int i = len-1; i > arg; i--) { 1.1880 - BasicType bt = java_lang_Class::as_BasicType(ptypes->obj_at(i)); 1.1881 - argslot += type2size[bt]; 1.1882 - } 1.1883 - assert(argument_slot_to_argnum(method_type, argslot) == arg, "inverse works"); 1.1884 - return argslot; 1.1885 -} 1.1886 - 1.1887 -// Given a slot number, return the argument number. 1.1888 -int MethodHandles::argument_slot_to_argnum(oop method_type, int query_argslot) { 1.1889 - objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(method_type); 1.1890 - int argslot = 0; 1.1891 - int len = ptypes->length(); 1.1892 - for (int i = len-1; i >= 0; i--) { 1.1893 - if (query_argslot == argslot) return i; 1.1894 - BasicType bt = java_lang_Class::as_BasicType(ptypes->obj_at(i)); 1.1895 - argslot += type2size[bt]; 1.1896 - } 1.1897 - // return pseudo-arg deepest in stack: 1.1898 - if (query_argslot == argslot) return -1; 1.1899 - return -99; // oob slot, or splitting a double-slot arg 1.1900 -} 1.1901 - 1.1902 -methodHandle MethodHandles::dispatch_decoded_method(methodHandle m, 1.1903 - KlassHandle receiver_limit, 1.1904 - int decode_flags, 1.1905 - KlassHandle receiver_klass, 1.1906 - TRAPS) { 1.1907 - assert((decode_flags & ~_DMF_DIRECT_MASK) == 0, "must be direct method reference"); 1.1908 - assert((decode_flags & _dmf_has_receiver) != 0, "must have a receiver or first reference argument"); 1.1909 - 1.1910 - if (!m->is_static() && 1.1911 - (receiver_klass.is_null() || !receiver_klass->is_subtype_of(m->method_holder()))) 1.1912 - // given type does not match class of method, or receiver is null! 1.1913 - // caller should have checked this, but let's be extra careful... 1.1914 - return methodHandle(); 1.1915 - 1.1916 - if (receiver_limit.not_null() && 1.1917 - (receiver_klass.not_null() && !receiver_klass->is_subtype_of(receiver_limit()))) 1.1918 - // given type is not limited to the receiver type 1.1919 - // note that a null receiver can match any reference value, for a static method 1.1920 - return methodHandle(); 1.1921 - 1.1922 - if (!(decode_flags & MethodHandles::_dmf_does_dispatch)) { 1.1923 - // pre-dispatched or static method (null receiver is OK for static) 1.1924 - return m; 1.1925 - 1.1926 - } else if (receiver_klass.is_null()) { 1.1927 - // null receiver value; cannot dispatch 1.1928 - return methodHandle(); 1.1929 - 1.1930 - } else if (!(decode_flags & MethodHandles::_dmf_from_interface)) { 1.1931 - // perform virtual dispatch 1.1932 - int vtable_index = m->vtable_index(); 1.1933 - guarantee(vtable_index >= 0, "valid vtable index"); 1.1934 - 1.1935 - // receiver_klass might be an arrayKlassOop but all vtables start at 1.1936 - // the same place. The cast is to avoid virtual call and assertion. 1.1937 - // See also LinkResolver::runtime_resolve_virtual_method. 1.1938 - instanceKlass* inst = (instanceKlass*)Klass::cast(receiver_klass()); 1.1939 - DEBUG_ONLY(inst->verify_vtable_index(vtable_index)); 1.1940 - methodOop m_oop = inst->method_at_vtable(vtable_index); 1.1941 - return methodHandle(THREAD, m_oop); 1.1942 - 1.1943 - } else { 1.1944 - // perform interface dispatch 1.1945 - int itable_index = klassItable::compute_itable_index(m()); 1.1946 - guarantee(itable_index >= 0, "valid itable index"); 1.1947 - instanceKlass* inst = instanceKlass::cast(receiver_klass()); 1.1948 - methodOop m_oop = inst->method_at_itable(m->method_holder(), itable_index, THREAD); 1.1949 - return methodHandle(THREAD, m_oop); 1.1950 - } 1.1951 -} 1.1952 - 1.1953 -void MethodHandles::verify_DirectMethodHandle(Handle mh, methodHandle m, TRAPS) { 1.1954 - // Verify type. 1.1955 - Handle mtype(THREAD, java_lang_invoke_MethodHandle::type(mh())); 1.1956 - verify_method_type(m, mtype, false, KlassHandle(), CHECK); 1.1957 - 1.1958 - // Verify vmslots. 1.1959 - if (java_lang_invoke_MethodHandle::vmslots(mh()) != m->size_of_parameters()) { 1.1960 - THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in DMH"); 1.1961 - } 1.1962 -} 1.1963 - 1.1964 -void MethodHandles::init_DirectMethodHandle(Handle mh, methodHandle m, bool do_dispatch, TRAPS) { 1.1965 - // Check arguments. 1.1966 - if (mh.is_null() || m.is_null() || 1.1967 - (!do_dispatch && m->is_abstract())) { 1.1968 - THROW(vmSymbols::java_lang_InternalError()); 1.1969 - } 1.1970 - 1.1971 - if (VerifyMethodHandles) { 1.1972 - // The privileged code which invokes this routine should not make 1.1973 - // a mistake about types, but it's better to verify. 1.1974 - verify_DirectMethodHandle(mh, m, CHECK); 1.1975 - } 1.1976 - 1.1977 - // Finally, after safety checks are done, link to the target method. 1.1978 - // We will follow the same path as the latter part of 1.1979 - // InterpreterRuntime::resolve_invoke(), which first finds the method 1.1980 - // and then decides how to populate the constant pool cache entry 1.1981 - // that links the interpreter calls to the method. We need the same 1.1982 - // bits, and will use the same calling sequence code. 1.1983 - 1.1984 - int vmindex = methodOopDesc::garbage_vtable_index; 1.1985 - Handle vmtarget; 1.1986 - 1.1987 - instanceKlass::cast(m->method_holder())->link_class(CHECK); 1.1988 - 1.1989 - MethodHandleEntry* me = NULL; 1.1990 - if (do_dispatch && Klass::cast(m->method_holder())->is_interface()) { 1.1991 - // We are simulating an invokeinterface instruction. 1.1992 - // (We might also be simulating an invokevirtual on a miranda method, 1.1993 - // but it is safe to treat it as an invokeinterface.) 1.1994 - assert(!m->can_be_statically_bound(), "no final methods on interfaces"); 1.1995 - vmindex = klassItable::compute_itable_index(m()); 1.1996 - assert(vmindex >= 0, "(>=0) == do_dispatch"); 1.1997 - // Set up same bits as ConstantPoolCacheEntry::set_interface_call(). 1.1998 - vmtarget = m->method_holder(); // the interface 1.1999 - me = MethodHandles::entry(MethodHandles::_invokeinterface_mh); 1.2000 - } else if (!do_dispatch || m->can_be_statically_bound()) { 1.2001 - // We are simulating an invokestatic or invokespecial instruction. 1.2002 - // Set up the method pointer, just like ConstantPoolCacheEntry::set_method(). 1.2003 - vmtarget = m; 1.2004 - // this does not help dispatch, but it will make it possible to parse this MH: 1.2005 - vmindex = methodOopDesc::nonvirtual_vtable_index; 1.2006 - assert(vmindex < 0, "(>=0) == do_dispatch"); 1.2007 - if (!m->is_static()) { 1.2008 - me = MethodHandles::entry(MethodHandles::_invokespecial_mh); 1.2009 - } else { 1.2010 - me = MethodHandles::entry(MethodHandles::_invokestatic_mh); 1.2011 - // Part of the semantics of a static call is an initialization barrier. 1.2012 - // For a DMH, it is done now, when the handle is created. 1.2013 - Klass* k = Klass::cast(m->method_holder()); 1.2014 - if (k->should_be_initialized()) { 1.2015 - k->initialize(CHECK); // possible safepoint 1.2016 - } 1.2017 - } 1.2018 - } else { 1.2019 - // We are simulating an invokevirtual instruction. 1.2020 - // Set up the vtable index, just like ConstantPoolCacheEntry::set_method(). 1.2021 - // The key logic is LinkResolver::runtime_resolve_virtual_method. 1.2022 - vmindex = m->vtable_index(); 1.2023 - vmtarget = m->method_holder(); 1.2024 - me = MethodHandles::entry(MethodHandles::_invokevirtual_mh); 1.2025 - } 1.2026 - 1.2027 - if (me == NULL) { THROW(vmSymbols::java_lang_InternalError()); } 1.2028 - 1.2029 - java_lang_invoke_DirectMethodHandle::set_vmtarget(mh(), vmtarget()); 1.2030 - java_lang_invoke_DirectMethodHandle::set_vmindex( mh(), vmindex); 1.2031 - DEBUG_ONLY(KlassHandle rlimit; int flags); 1.2032 - assert(MethodHandles::decode_method(mh(), rlimit, flags) == m, 1.2033 - "properly stored for later decoding"); 1.2034 - DEBUG_ONLY(bool actual_do_dispatch = ((flags & _dmf_does_dispatch) != 0)); 1.2035 - assert(!(actual_do_dispatch && !do_dispatch), 1.2036 - "do not perform dispatch if !do_dispatch specified"); 1.2037 - assert(actual_do_dispatch == (vmindex >= 0), "proper later decoding of do_dispatch"); 1.2038 - assert(decode_MethodHandle_stack_pushes(mh()) == 0, "DMH does not move stack"); 1.2039 - 1.2040 - // Done! 1.2041 - java_lang_invoke_MethodHandle::set_vmentry(mh(), me); 1.2042 -} 1.2043 - 1.2044 -void MethodHandles::verify_BoundMethodHandle_with_receiver(Handle mh, 1.2045 - methodHandle m, 1.2046 - TRAPS) { 1.2047 - // Verify type. 1.2048 - KlassHandle bound_recv_type; 1.2049 - { 1.2050 - oop receiver = java_lang_invoke_BoundMethodHandle::argument(mh()); 1.2051 - if (receiver != NULL) 1.2052 - bound_recv_type = KlassHandle(THREAD, receiver->klass()); 1.2053 - } 1.2054 - Handle mtype(THREAD, java_lang_invoke_MethodHandle::type(mh())); 1.2055 - verify_method_type(m, mtype, true, bound_recv_type, CHECK); 1.2056 - 1.2057 - int receiver_pos = m->size_of_parameters() - 1; 1.2058 - 1.2059 - // Verify MH.vmargslot, which should point at the bound receiver. 1.2060 - verify_vmargslot(mh, -1, java_lang_invoke_BoundMethodHandle::vmargslot(mh()), CHECK); 1.2061 - //verify_vmslots(mh, CHECK); 1.2062 - 1.2063 - // Verify vmslots. 1.2064 - if (java_lang_invoke_MethodHandle::vmslots(mh()) != receiver_pos) { 1.2065 - THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in BMH (receiver)"); 1.2066 - } 1.2067 -} 1.2068 - 1.2069 -// Initialize a BMH with a receiver bound directly to a methodOop. 1.2070 -void MethodHandles::init_BoundMethodHandle_with_receiver(Handle mh, 1.2071 - methodHandle original_m, 1.2072 - KlassHandle receiver_limit, 1.2073 - int decode_flags, 1.2074 - TRAPS) { 1.2075 - // Check arguments. 1.2076 - if (mh.is_null() || original_m.is_null()) { 1.2077 - THROW(vmSymbols::java_lang_InternalError()); 1.2078 - } 1.2079 - 1.2080 - KlassHandle receiver_klass; 1.2081 - { 1.2082 - oop receiver_oop = java_lang_invoke_BoundMethodHandle::argument(mh()); 1.2083 - if (receiver_oop != NULL) 1.2084 - receiver_klass = KlassHandle(THREAD, receiver_oop->klass()); 1.2085 - } 1.2086 - methodHandle m = dispatch_decoded_method(original_m, 1.2087 - receiver_limit, decode_flags, 1.2088 - receiver_klass, 1.2089 - CHECK); 1.2090 - if (m.is_null()) { THROW(vmSymbols::java_lang_InternalError()); } 1.2091 - if (m->is_abstract()) { THROW(vmSymbols::java_lang_AbstractMethodError()); } 1.2092 - 1.2093 - int vmargslot = m->size_of_parameters() - 1; 1.2094 - assert(java_lang_invoke_BoundMethodHandle::vmargslot(mh()) == vmargslot, ""); 1.2095 - 1.2096 - if (VerifyMethodHandles) { 1.2097 - verify_BoundMethodHandle_with_receiver(mh, m, CHECK); 1.2098 - } 1.2099 - 1.2100 - java_lang_invoke_BoundMethodHandle::set_vmtarget(mh(), m()); 1.2101 - 1.2102 - DEBUG_ONLY(KlassHandle junk1; int junk2); 1.2103 - assert(MethodHandles::decode_method(mh(), junk1, junk2) == m, "properly stored for later decoding"); 1.2104 - assert(decode_MethodHandle_stack_pushes(mh()) == 1, "BMH pushes one stack slot"); 1.2105 - 1.2106 - // Done! 1.2107 - java_lang_invoke_MethodHandle::set_vmentry(mh(), MethodHandles::entry(MethodHandles::_bound_ref_direct_mh)); 1.2108 -} 1.2109 - 1.2110 -void MethodHandles::verify_BoundMethodHandle(Handle mh, Handle target, int argnum, 1.2111 - bool direct_to_method, TRAPS) { 1.2112 - ResourceMark rm; 1.2113 - Handle ptype_handle(THREAD, 1.2114 - java_lang_invoke_MethodType::ptype(java_lang_invoke_MethodHandle::type(target()), argnum)); 1.2115 - KlassHandle ptype_klass; 1.2116 - BasicType ptype = java_lang_Class::as_BasicType(ptype_handle(), &ptype_klass); 1.2117 - int slots_pushed = type2size[ptype]; 1.2118 - 1.2119 - oop argument = java_lang_invoke_BoundMethodHandle::argument(mh()); 1.2120 - 1.2121 - const char* err = NULL; 1.2122 - 1.2123 - switch (ptype) { 1.2124 - case T_OBJECT: 1.2125 - if (argument != NULL) 1.2126 - // we must implicitly convert from the arg type to the outgoing ptype 1.2127 - err = check_argument_type_change(T_OBJECT, argument->klass(), ptype, ptype_klass(), argnum); 1.2128 - break; 1.2129 - 1.2130 - case T_ARRAY: case T_VOID: 1.2131 - assert(false, "array, void do not appear here"); 1.2132 - default: 1.2133 - if (ptype != T_INT && !is_subword_type(ptype)) { 1.2134 - err = "unexpected parameter type"; 1.2135 - break; 1.2136 - } 1.2137 - // check subrange of Integer.value, if necessary 1.2138 - if (argument == NULL || argument->klass() != SystemDictionary::Integer_klass()) { 1.2139 - err = "bound integer argument must be of type java.lang.Integer"; 1.2140 - break; 1.2141 - } 1.2142 - if (ptype != T_INT) { 1.2143 - int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_INT); 1.2144 - jint value = argument->int_field(value_offset); 1.2145 - int vminfo = adapter_unbox_subword_vminfo(ptype); 1.2146 - jint subword = truncate_subword_from_vminfo(value, vminfo); 1.2147 - if (value != subword) { 1.2148 - err = "bound subword value does not fit into the subword type"; 1.2149 - break; 1.2150 - } 1.2151 - } 1.2152 - break; 1.2153 - case T_FLOAT: 1.2154 - case T_DOUBLE: 1.2155 - case T_LONG: 1.2156 - { 1.2157 - // we must implicitly convert from the unboxed arg type to the outgoing ptype 1.2158 - BasicType argbox = java_lang_boxing_object::basic_type(argument); 1.2159 - if (argbox != ptype) { 1.2160 - err = check_argument_type_change(T_OBJECT, (argument == NULL 1.2161 - ? SystemDictionary::Object_klass() 1.2162 - : argument->klass()), 1.2163 - ptype, ptype_klass(), argnum); 1.2164 - assert(err != NULL, "this must be an error"); 1.2165 - } 1.2166 - break; 1.2167 - } 1.2168 - } 1.2169 - 1.2170 - if (err == NULL) { 1.2171 - DEBUG_ONLY(int this_pushes = decode_MethodHandle_stack_pushes(mh())); 1.2172 - if (direct_to_method) { 1.2173 - assert(this_pushes == slots_pushed, "BMH pushes one or two stack slots"); 1.2174 - } else { 1.2175 - int target_pushes = decode_MethodHandle_stack_pushes(target()); 1.2176 - assert(this_pushes == slots_pushed + target_pushes, "BMH stack motion must be correct"); 1.2177 - } 1.2178 - } 1.2179 - 1.2180 - if (err == NULL) { 1.2181 - // Verify the rest of the method type. 1.2182 - err = check_method_type_insertion(java_lang_invoke_MethodHandle::type(mh()), 1.2183 - argnum, ptype_handle(), 1.2184 - java_lang_invoke_MethodHandle::type(target())); 1.2185 - } 1.2186 - 1.2187 - if (err != NULL) { 1.2188 - THROW_MSG(vmSymbols::java_lang_InternalError(), err); 1.2189 - } 1.2190 -} 1.2191 - 1.2192 -void MethodHandles::init_BoundMethodHandle(Handle mh, Handle target, int argnum, TRAPS) { 1.2193 - // Check arguments. 1.2194 - if (mh.is_null() || target.is_null() || !java_lang_invoke_MethodHandle::is_instance(target())) { 1.2195 - THROW(vmSymbols::java_lang_InternalError()); 1.2196 - } 1.2197 - 1.2198 - int argslot = java_lang_invoke_BoundMethodHandle::vmargslot(mh()); 1.2199 - 1.2200 - if (VerifyMethodHandles) { 1.2201 - int insert_after = argnum - 1; 1.2202 - verify_vmargslot(mh, insert_after, argslot, CHECK); 1.2203 - verify_vmslots(mh, CHECK); 1.2204 - } 1.2205 - 1.2206 - // Get bound type and required slots. 1.2207 - BasicType ptype; 1.2208 - { 1.2209 - oop ptype_oop = java_lang_invoke_MethodType::ptype(java_lang_invoke_MethodHandle::type(target()), argnum); 1.2210 - ptype = java_lang_Class::as_BasicType(ptype_oop); 1.2211 - } 1.2212 - int slots_pushed = type2size[ptype]; 1.2213 - 1.2214 - // If (a) the target is a direct non-dispatched method handle, 1.2215 - // or (b) the target is a dispatched direct method handle and we 1.2216 - // are binding the receiver, cut out the middle-man. 1.2217 - // Do this by decoding the DMH and using its methodOop directly as vmtarget. 1.2218 - bool direct_to_method = false; 1.2219 - if (OptimizeMethodHandles && 1.2220 - target->klass() == SystemDictionary::DirectMethodHandle_klass() && 1.2221 - (argnum != 0 || java_lang_invoke_BoundMethodHandle::argument(mh()) != NULL) && 1.2222 - (argnum == 0 || java_lang_invoke_DirectMethodHandle::vmindex(target()) < 0)) { 1.2223 - KlassHandle receiver_limit; int decode_flags = 0; 1.2224 - methodHandle m = decode_method(target(), receiver_limit, decode_flags); 1.2225 - if (m.is_null()) { THROW_MSG(vmSymbols::java_lang_InternalError(), "DMH failed to decode"); } 1.2226 - DEBUG_ONLY(int m_vmslots = m->size_of_parameters() - slots_pushed); // pos. of 1st arg. 1.2227 - assert(java_lang_invoke_BoundMethodHandle::vmslots(mh()) == m_vmslots, "type w/ m sig"); 1.2228 - if (argnum == 0 && (decode_flags & _dmf_has_receiver) != 0) { 1.2229 - init_BoundMethodHandle_with_receiver(mh, m, 1.2230 - receiver_limit, decode_flags, 1.2231 - CHECK); 1.2232 - return; 1.2233 - } 1.2234 - 1.2235 - // Even if it is not a bound receiver, we still might be able 1.2236 - // to bind another argument and still invoke the methodOop directly. 1.2237 - if (!(decode_flags & _dmf_does_dispatch)) { 1.2238 - direct_to_method = true; 1.2239 - java_lang_invoke_BoundMethodHandle::set_vmtarget(mh(), m()); 1.2240 - } 1.2241 - } 1.2242 - if (!direct_to_method) 1.2243 - java_lang_invoke_BoundMethodHandle::set_vmtarget(mh(), target()); 1.2244 - 1.2245 - if (VerifyMethodHandles) { 1.2246 - verify_BoundMethodHandle(mh, target, argnum, direct_to_method, CHECK); 1.2247 - } 1.2248 - 1.2249 - // Next question: Is this a ref, int, or long bound value? 1.2250 - MethodHandleEntry* me = NULL; 1.2251 - if (ptype == T_OBJECT) { 1.2252 - if (direct_to_method) me = MethodHandles::entry(_bound_ref_direct_mh); 1.2253 - else me = MethodHandles::entry(_bound_ref_mh); 1.2254 - } else if (slots_pushed == 2) { 1.2255 - if (direct_to_method) me = MethodHandles::entry(_bound_long_direct_mh); 1.2256 - else me = MethodHandles::entry(_bound_long_mh); 1.2257 - } else if (slots_pushed == 1) { 1.2258 - if (direct_to_method) me = MethodHandles::entry(_bound_int_direct_mh); 1.2259 - else me = MethodHandles::entry(_bound_int_mh); 1.2260 - } else { 1.2261 - assert(false, ""); 1.2262 - } 1.2263 - 1.2264 - // Done! 1.2265 - java_lang_invoke_MethodHandle::set_vmentry(mh(), me); 1.2266 -} 1.2267 - 1.2268 -static void throw_InternalError_for_bad_conversion(int conversion, const char* err, TRAPS) { 1.2269 - char msg[200]; 1.2270 - jio_snprintf(msg, sizeof(msg), "bad adapter (conversion=0x%08x): %s", conversion, err); 1.2271 - THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), msg); 1.2272 -} 1.2273 - 1.2274 -void MethodHandles::verify_AdapterMethodHandle(Handle mh, int argnum, TRAPS) { 1.2275 - ResourceMark rm; 1.2276 - jint conversion = java_lang_invoke_AdapterMethodHandle::conversion(mh()); 1.2277 - int argslot = java_lang_invoke_AdapterMethodHandle::vmargslot(mh()); 1.2278 - 1.2279 - verify_vmargslot(mh, argnum, argslot, CHECK); 1.2280 - verify_vmslots(mh, CHECK); 1.2281 - 1.2282 - jint conv_op = adapter_conversion_op(conversion); 1.2283 - if (!conv_op_valid(conv_op)) { 1.2284 - throw_InternalError_for_bad_conversion(conversion, "unknown conversion op", THREAD); 1.2285 - return; 1.2286 - } 1.2287 - EntryKind ek = adapter_entry_kind(conv_op); 1.2288 - 1.2289 - int stack_move = adapter_conversion_stack_move(conversion); 1.2290 - BasicType src = adapter_conversion_src_type(conversion); 1.2291 - BasicType dest = adapter_conversion_dest_type(conversion); 1.2292 - int vminfo = adapter_conversion_vminfo(conversion); // should be zero 1.2293 - 1.2294 - Handle argument(THREAD, java_lang_invoke_AdapterMethodHandle::argument(mh())); 1.2295 - Handle target(THREAD, java_lang_invoke_AdapterMethodHandle::vmtarget(mh())); 1.2296 - Handle src_mtype(THREAD, java_lang_invoke_MethodHandle::type(mh())); 1.2297 - Handle dst_mtype(THREAD, java_lang_invoke_MethodHandle::type(target())); 1.2298 - Handle arg_mtype; 1.2299 - 1.2300 - const char* err = NULL; 1.2301 - 1.2302 - if (err == NULL) { 1.2303 - // Check that the correct argument is supplied, but only if it is required. 1.2304 - switch (ek) { 1.2305 - case _adapter_check_cast: // target type of cast 1.2306 - case _adapter_ref_to_prim: // wrapper type from which to unbox 1.2307 - case _adapter_spread_args: // array type to spread from 1.2308 - if (!java_lang_Class::is_instance(argument()) 1.2309 - || java_lang_Class::is_primitive(argument())) 1.2310 - { err = "adapter requires argument of type java.lang.Class"; break; } 1.2311 - if (ek == _adapter_spread_args) { 1.2312 - // Make sure it is a suitable collection type. (Array, for now.) 1.2313 - Klass* ak = Klass::cast(java_lang_Class::as_klassOop(argument())); 1.2314 - if (!ak->oop_is_array()) 1.2315 - { err = "spread adapter requires argument representing an array class"; break; } 1.2316 - BasicType et = arrayKlass::cast(ak->as_klassOop())->element_type(); 1.2317 - if (et != dest && stack_move <= 0) 1.2318 - { err = "spread adapter requires array class argument of correct type"; break; } 1.2319 - } 1.2320 - break; 1.2321 - case _adapter_prim_to_ref: // boxer MH to use 1.2322 - case _adapter_collect_args: // method handle which collects the args 1.2323 - case _adapter_fold_args: // method handle which collects the args 1.2324 - if (!java_lang_invoke_MethodHandle::is_instance(argument())) 1.2325 - { err = "MethodHandle adapter argument required"; break; } 1.2326 - arg_mtype = Handle(THREAD, java_lang_invoke_MethodHandle::type(argument())); 1.2327 - break; 1.2328 - default: 1.2329 - if (argument.not_null()) 1.2330 - { err = "adapter has spurious argument"; break; } 1.2331 - break; 1.2332 - } 1.2333 - } 1.2334 - 1.2335 - if (err == NULL) { 1.2336 - // Check that the src/dest types are supplied if needed. 1.2337 - // Also check relevant parameter or return types. 1.2338 - switch (ek) { 1.2339 - case _adapter_check_cast: 1.2340 - if (src != T_OBJECT || dest != T_OBJECT) { 1.2341 - err = "adapter requires object src/dest conversion subfields"; 1.2342 - } 1.2343 - break; 1.2344 - case _adapter_prim_to_prim: 1.2345 - if (!is_java_primitive(src) || !is_java_primitive(dest) || src == dest) { 1.2346 - err = "adapter requires primitive src/dest conversion subfields"; break; 1.2347 - } 1.2348 - if ( (src == T_FLOAT || src == T_DOUBLE) && !(dest == T_FLOAT || dest == T_DOUBLE) || 1.2349 - !(src == T_FLOAT || src == T_DOUBLE) && (dest == T_FLOAT || dest == T_DOUBLE)) { 1.2350 - err = "adapter cannot convert beween floating and fixed-point"; break; 1.2351 - } 1.2352 - break; 1.2353 - case _adapter_ref_to_prim: 1.2354 - if (src != T_OBJECT || !is_java_primitive(dest) 1.2355 - || argument() != Klass::cast(SystemDictionary::box_klass(dest))->java_mirror()) { 1.2356 - err = "adapter requires primitive dest conversion subfield"; break; 1.2357 - } 1.2358 - break; 1.2359 - case _adapter_prim_to_ref: 1.2360 - if (!is_java_primitive(src) || dest != T_OBJECT) { 1.2361 - err = "adapter requires primitive src conversion subfield"; break; 1.2362 - } 1.2363 - break; 1.2364 - case _adapter_swap_args: 1.2365 - { 1.2366 - if (!src || !dest) { 1.2367 - err = "adapter requires src/dest conversion subfields for swap"; break; 1.2368 - } 1.2369 - int src_size = type2size[src]; 1.2370 - if (src_size != type2size[dest]) { 1.2371 - err = "adapter requires equal sizes for src/dest"; break; 1.2372 - } 1.2373 - int src_slot = argslot; 1.2374 - int dest_slot = vminfo; 1.2375 - int src_arg = argnum; 1.2376 - int dest_arg = argument_slot_to_argnum(src_mtype(), dest_slot); 1.2377 - verify_vmargslot(mh, dest_arg, dest_slot, CHECK); 1.2378 - if (!(dest_slot >= src_slot + src_size) && 1.2379 - !(src_slot >= dest_slot + src_size)) { 1.2380 - err = "source, destination slots must be distinct"; break; 1.2381 - } else if (!(src_slot > dest_slot)) { 1.2382 - err = "source of swap must be deeper in stack"; break; 1.2383 - } 1.2384 - err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), dest_arg), 1.2385 - java_lang_invoke_MethodType::ptype(dst_mtype(), src_arg), 1.2386 - dest_arg); 1.2387 - if (err == NULL) 1.2388 - err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), src_arg), 1.2389 - java_lang_invoke_MethodType::ptype(dst_mtype(), dest_arg), 1.2390 - src_arg); 1.2391 - break; 1.2392 - } 1.2393 - case _adapter_rot_args: 1.2394 - { 1.2395 - if (!src || !dest) { 1.2396 - err = "adapter requires src/dest conversion subfields for rotate"; break; 1.2397 - } 1.2398 - int src_slot = argslot; 1.2399 - int limit_raw = vminfo; 1.2400 - bool rot_down = (src_slot < limit_raw); 1.2401 - int limit_bias = (rot_down ? MethodHandles::OP_ROT_ARGS_DOWN_LIMIT_BIAS : 0); 1.2402 - int limit_slot = limit_raw - limit_bias; 1.2403 - int src_arg = argnum; 1.2404 - int limit_arg = argument_slot_to_argnum(src_mtype(), limit_slot); 1.2405 - verify_vmargslot(mh, limit_arg, limit_slot, CHECK); 1.2406 - if (src_slot == limit_slot) { 1.2407 - err = "source, destination slots must be distinct"; break; 1.2408 - } 1.2409 - if (!rot_down) { // rotate slots up == shift arguments left 1.2410 - // limit_slot is an inclusive lower limit 1.2411 - assert((src_slot > limit_slot) && (src_arg < limit_arg), ""); 1.2412 - // rotate up: [limit_slot..src_slot-ss] --> [limit_slot+ss..src_slot] 1.2413 - // that is: [src_arg+1..limit_arg] --> [src_arg..limit_arg-1] 1.2414 - for (int i = src_arg+1; i <= limit_arg && err == NULL; i++) { 1.2415 - err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), i), 1.2416 - java_lang_invoke_MethodType::ptype(dst_mtype(), i-1), 1.2417 - i); 1.2418 - } 1.2419 - } else { // rotate slots down == shfit arguments right 1.2420 - // limit_slot is an exclusive upper limit 1.2421 - assert((src_slot < limit_slot - limit_bias) && (src_arg > limit_arg + limit_bias), ""); 1.2422 - // rotate down: [src_slot+ss..limit_slot) --> [src_slot..limit_slot-ss) 1.2423 - // that is: (limit_arg..src_arg-1] --> (dst_arg+1..src_arg] 1.2424 - for (int i = limit_arg+1; i <= src_arg-1 && err == NULL; i++) { 1.2425 - err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), i), 1.2426 - java_lang_invoke_MethodType::ptype(dst_mtype(), i+1), 1.2427 - i); 1.2428 - } 1.2429 - } 1.2430 - if (err == NULL) { 1.2431 - int dest_arg = (rot_down ? limit_arg+1 : limit_arg); 1.2432 - err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), src_arg), 1.2433 - java_lang_invoke_MethodType::ptype(dst_mtype(), dest_arg), 1.2434 - src_arg); 1.2435 - } 1.2436 - } 1.2437 - break; 1.2438 - case _adapter_spread_args: 1.2439 - case _adapter_collect_args: 1.2440 - case _adapter_fold_args: 1.2441 - { 1.2442 - bool is_spread = (ek == _adapter_spread_args); 1.2443 - bool is_fold = (ek == _adapter_fold_args); 1.2444 - BasicType coll_type = is_spread ? src : dest; 1.2445 - BasicType elem_type = is_spread ? dest : src; 1.2446 - // coll_type is type of args in collected form (or T_VOID if none) 1.2447 - // elem_type is common type of args in spread form (or T_VOID if missing or heterogeneous) 1.2448 - if (coll_type == 0 || elem_type == 0) { 1.2449 - err = "adapter requires src/dest subfields for spread or collect"; break; 1.2450 - } 1.2451 - if (is_spread && coll_type != T_OBJECT) { 1.2452 - err = "spread adapter requires object type for argument bundle"; break; 1.2453 - } 1.2454 - Handle spread_mtype = (is_spread ? dst_mtype : src_mtype); 1.2455 - int spread_slot = argslot; 1.2456 - int spread_arg = argnum; 1.2457 - int slots_pushed = stack_move / stack_move_unit(); 1.2458 - int coll_slot_count = type2size[coll_type]; 1.2459 - int spread_slot_count = (is_spread ? slots_pushed : -slots_pushed) + coll_slot_count; 1.2460 - if (is_fold) spread_slot_count = argument_slot_count(arg_mtype()); 1.2461 - if (!is_spread) { 1.2462 - int init_slots = argument_slot_count(src_mtype()); 1.2463 - int coll_slots = argument_slot_count(arg_mtype()); 1.2464 - if (spread_slot_count > init_slots || 1.2465 - spread_slot_count != coll_slots) { 1.2466 - err = "collect adapter has inconsistent arg counts"; break; 1.2467 - } 1.2468 - int next_slots = argument_slot_count(dst_mtype()); 1.2469 - int unchanged_slots_in = (init_slots - spread_slot_count); 1.2470 - int unchanged_slots_out = (next_slots - coll_slot_count - (is_fold ? spread_slot_count : 0)); 1.2471 - if (unchanged_slots_in != unchanged_slots_out) { 1.2472 - err = "collect adapter continuation has inconsistent arg counts"; break; 1.2473 - } 1.2474 - } 1.2475 - } 1.2476 - break; 1.2477 - default: 1.2478 - if (src != 0 || dest != 0) { 1.2479 - err = "adapter has spurious src/dest conversion subfields"; break; 1.2480 - } 1.2481 - break; 1.2482 - } 1.2483 - } 1.2484 - 1.2485 - if (err == NULL) { 1.2486 - // Check the stack_move subfield. 1.2487 - // It must always report the net change in stack size, positive or negative. 1.2488 - int slots_pushed = stack_move / stack_move_unit(); 1.2489 - switch (ek) { 1.2490 - case _adapter_prim_to_prim: 1.2491 - case _adapter_ref_to_prim: 1.2492 - case _adapter_prim_to_ref: 1.2493 - if (slots_pushed != type2size[dest] - type2size[src]) { 1.2494 - err = "wrong stack motion for primitive conversion"; 1.2495 - } 1.2496 - break; 1.2497 - case _adapter_dup_args: 1.2498 - if (slots_pushed <= 0) { 1.2499 - err = "adapter requires conversion subfield slots_pushed > 0"; 1.2500 - } 1.2501 - break; 1.2502 - case _adapter_drop_args: 1.2503 - if (slots_pushed >= 0) { 1.2504 - err = "adapter requires conversion subfield slots_pushed < 0"; 1.2505 - } 1.2506 - break; 1.2507 - case _adapter_collect_args: 1.2508 - case _adapter_fold_args: 1.2509 - if (slots_pushed > 2) { 1.2510 - err = "adapter requires conversion subfield slots_pushed <= 2"; 1.2511 - } 1.2512 - break; 1.2513 - case _adapter_spread_args: 1.2514 - if (slots_pushed < -1) { 1.2515 - err = "adapter requires conversion subfield slots_pushed >= -1"; 1.2516 - } 1.2517 - break; 1.2518 - default: 1.2519 - if (stack_move != 0) { 1.2520 - err = "adapter has spurious stack_move conversion subfield"; 1.2521 - } 1.2522 - break; 1.2523 - } 1.2524 - if (err == NULL && stack_move != slots_pushed * stack_move_unit()) { 1.2525 - err = "stack_move conversion subfield must be multiple of stack_move_unit"; 1.2526 - } 1.2527 - } 1.2528 - 1.2529 - if (err == NULL) { 1.2530 - // Make sure this adapter's stack pushing is accurately recorded. 1.2531 - int slots_pushed = stack_move / stack_move_unit(); 1.2532 - int this_vmslots = java_lang_invoke_MethodHandle::vmslots(mh()); 1.2533 - int target_vmslots = java_lang_invoke_MethodHandle::vmslots(target()); 1.2534 - int target_pushes = decode_MethodHandle_stack_pushes(target()); 1.2535 - if (slots_pushed != (target_vmslots - this_vmslots)) { 1.2536 - err = "stack_move inconsistent with previous and current MethodType vmslots"; 1.2537 - } else { 1.2538 - int this_pushes = decode_MethodHandle_stack_pushes(mh()); 1.2539 - if (slots_pushed + target_pushes != this_pushes) { 1.2540 - if (this_pushes == 0) 1.2541 - err = "adapter push count not initialized"; 1.2542 - else 1.2543 - err = "adapter push count is wrong"; 1.2544 - } 1.2545 - } 1.2546 - 1.2547 - // While we're at it, check that the stack motion decoder works: 1.2548 - DEBUG_ONLY(int this_pushes = decode_MethodHandle_stack_pushes(mh())); 1.2549 - assert(this_pushes == slots_pushed + target_pushes, "AMH stack motion must be correct"); 1.2550 - } 1.2551 - 1.2552 - if (err == NULL && vminfo != 0) { 1.2553 - switch (ek) { 1.2554 - case _adapter_swap_args: 1.2555 - case _adapter_rot_args: 1.2556 - case _adapter_prim_to_ref: 1.2557 - case _adapter_collect_args: 1.2558 - case _adapter_fold_args: 1.2559 - break; // OK 1.2560 - default: 1.2561 - err = "vminfo subfield is reserved to the JVM"; 1.2562 - } 1.2563 - } 1.2564 - 1.2565 - // Do additional ad hoc checks. 1.2566 - if (err == NULL) { 1.2567 - switch (ek) { 1.2568 - case _adapter_retype_only: 1.2569 - err = check_method_type_passthrough(src_mtype(), dst_mtype(), false); 1.2570 - break; 1.2571 - 1.2572 - case _adapter_retype_raw: 1.2573 - err = check_method_type_passthrough(src_mtype(), dst_mtype(), true); 1.2574 - break; 1.2575 - 1.2576 - case _adapter_check_cast: 1.2577 - { 1.2578 - // The actual value being checked must be a reference: 1.2579 - err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), argnum), 1.2580 - object_java_mirror(), argnum); 1.2581 - if (err != NULL) break; 1.2582 - 1.2583 - // The output of the cast must fit with the destination argument: 1.2584 - Handle cast_class = argument; 1.2585 - err = check_method_type_conversion(src_mtype(), 1.2586 - argnum, cast_class(), 1.2587 - dst_mtype()); 1.2588 - } 1.2589 - break; 1.2590 - 1.2591 - // %%% TO DO: continue in remaining cases to verify src/dst_mtype if VerifyMethodHandles 1.2592 - } 1.2593 - } 1.2594 - 1.2595 - if (err != NULL) { 1.2596 - throw_InternalError_for_bad_conversion(conversion, err, THREAD); 1.2597 - return; 1.2598 - } 1.2599 - 1.2600 -} 1.2601 - 1.2602 -void MethodHandles::init_AdapterMethodHandle(Handle mh, Handle target, int argnum, TRAPS) { 1.2603 - Handle argument = java_lang_invoke_AdapterMethodHandle::argument(mh()); 1.2604 - int argslot = java_lang_invoke_AdapterMethodHandle::vmargslot(mh()); 1.2605 - jint conversion = java_lang_invoke_AdapterMethodHandle::conversion(mh()); 1.2606 - jint conv_op = adapter_conversion_op(conversion); 1.2607 - 1.2608 - // adjust the adapter code to the internal EntryKind enumeration: 1.2609 - EntryKind ek_orig = adapter_entry_kind(conv_op); 1.2610 - EntryKind ek_opt = ek_orig; // may be optimized 1.2611 - EntryKind ek_try; // temp 1.2612 - 1.2613 - // Finalize the vmtarget field (Java initialized it to null). 1.2614 - if (!java_lang_invoke_MethodHandle::is_instance(target())) { 1.2615 - throw_InternalError_for_bad_conversion(conversion, "bad target", THREAD); 1.2616 - return; 1.2617 - } 1.2618 - java_lang_invoke_AdapterMethodHandle::set_vmtarget(mh(), target()); 1.2619 - 1.2620 - int stack_move = adapter_conversion_stack_move(conversion); 1.2621 - BasicType src = adapter_conversion_src_type(conversion); 1.2622 - BasicType dest = adapter_conversion_dest_type(conversion); 1.2623 - int vminfo = adapter_conversion_vminfo(conversion); // should be zero 1.2624 - 1.2625 - int slots_pushed = stack_move / stack_move_unit(); 1.2626 - 1.2627 - if (VerifyMethodHandles) { 1.2628 - verify_AdapterMethodHandle(mh, argnum, CHECK); 1.2629 - } 1.2630 - 1.2631 - const char* err = NULL; 1.2632 - 1.2633 - if (!conv_op_supported(conv_op)) { 1.2634 - err = "adapter not yet implemented in the JVM"; 1.2635 - } 1.2636 - 1.2637 - // Now it's time to finish the case analysis and pick a MethodHandleEntry. 1.2638 - switch (ek_orig) { 1.2639 - case _adapter_retype_only: 1.2640 - case _adapter_retype_raw: 1.2641 - case _adapter_check_cast: 1.2642 - case _adapter_dup_args: 1.2643 - case _adapter_drop_args: 1.2644 - // these work fine via general case code 1.2645 - break; 1.2646 - 1.2647 - case _adapter_prim_to_prim: 1.2648 - { 1.2649 - // Non-subword cases are {int,float,long,double} -> {int,float,long,double}. 1.2650 - // And, the {float,double} -> {int,long} cases must be handled by Java. 1.2651 - switch (type2size[src] *4+ type2size[dest]) { 1.2652 - case 1 *4+ 1: 1.2653 - assert(src == T_INT || is_subword_type(src), "source is not float"); 1.2654 - // Subword-related cases are int -> {boolean,byte,char,short}. 1.2655 - ek_opt = _adapter_opt_i2i; 1.2656 - vminfo = adapter_prim_to_prim_subword_vminfo(dest); 1.2657 - break; 1.2658 - case 2 *4+ 1: 1.2659 - if (src == T_LONG && (dest == T_INT || is_subword_type(dest))) { 1.2660 - ek_opt = _adapter_opt_l2i; 1.2661 - vminfo = adapter_prim_to_prim_subword_vminfo(dest); 1.2662 - } else if (src == T_DOUBLE && dest == T_FLOAT) { 1.2663 - ek_opt = _adapter_opt_d2f; 1.2664 - } else { 1.2665 - goto throw_not_impl; // runs user code, hence could block 1.2666 - } 1.2667 - break; 1.2668 - case 1 *4+ 2: 1.2669 - if ((src == T_INT || is_subword_type(src)) && dest == T_LONG) { 1.2670 - ek_opt = _adapter_opt_i2l; 1.2671 - } else if (src == T_FLOAT && dest == T_DOUBLE) { 1.2672 - ek_opt = _adapter_opt_f2d; 1.2673 - } else { 1.2674 - goto throw_not_impl; // runs user code, hence could block 1.2675 - } 1.2676 - break; 1.2677 - default: 1.2678 - goto throw_not_impl; // runs user code, hence could block 1.2679 - break; 1.2680 - } 1.2681 - } 1.2682 - break; 1.2683 - 1.2684 - case _adapter_ref_to_prim: 1.2685 - { 1.2686 - switch (type2size[dest]) { 1.2687 - case 1: 1.2688 - ek_opt = _adapter_opt_unboxi; 1.2689 - vminfo = adapter_unbox_subword_vminfo(dest); 1.2690 - break; 1.2691 - case 2: 1.2692 - ek_opt = _adapter_opt_unboxl; 1.2693 - break; 1.2694 - default: 1.2695 - goto throw_not_impl; 1.2696 - break; 1.2697 - } 1.2698 - } 1.2699 - break; 1.2700 - 1.2701 - case _adapter_prim_to_ref: 1.2702 - { 1.2703 - // vminfo will be the location to insert the return value 1.2704 - vminfo = argslot; 1.2705 - ek_opt = _adapter_opt_collect_ref; 1.2706 - ensure_vmlayout_field(target, CHECK); 1.2707 - // for MethodHandleWalk: 1.2708 - if (java_lang_invoke_AdapterMethodHandle::is_instance(argument())) 1.2709 - ensure_vmlayout_field(argument, CHECK); 1.2710 - if (!OptimizeMethodHandles) break; 1.2711 - switch (type2size[src]) { 1.2712 - case 1: 1.2713 - ek_try = EntryKind(_adapter_opt_filter_S0_ref + argslot); 1.2714 - if (ek_try < _adapter_opt_collect_LAST && 1.2715 - ek_adapter_opt_collect_slot(ek_try) == argslot) { 1.2716 - assert(ek_adapter_opt_collect_count(ek_try) == 1 && 1.2717 - ek_adapter_opt_collect_type(ek_try) == T_OBJECT, ""); 1.2718 - ek_opt = ek_try; 1.2719 - break; 1.2720 - } 1.2721 - // else downgrade to variable slot: 1.2722 - ek_opt = _adapter_opt_collect_1_ref; 1.2723 - break; 1.2724 - case 2: 1.2725 - ek_try = EntryKind(_adapter_opt_collect_2_S0_ref + argslot); 1.2726 - if (ek_try < _adapter_opt_collect_LAST && 1.2727 - ek_adapter_opt_collect_slot(ek_try) == argslot) { 1.2728 - assert(ek_adapter_opt_collect_count(ek_try) == 2 && 1.2729 - ek_adapter_opt_collect_type(ek_try) == T_OBJECT, ""); 1.2730 - ek_opt = ek_try; 1.2731 - break; 1.2732 - } 1.2733 - // else downgrade to variable slot: 1.2734 - ek_opt = _adapter_opt_collect_2_ref; 1.2735 - break; 1.2736 - default: 1.2737 - goto throw_not_impl; 1.2738 - break; 1.2739 - } 1.2740 - } 1.2741 - break; 1.2742 - 1.2743 - case _adapter_swap_args: 1.2744 - case _adapter_rot_args: 1.2745 - { 1.2746 - int swap_slots = type2size[src]; 1.2747 - int src_slot = argslot; 1.2748 - int dest_slot = vminfo; 1.2749 - int rotate = (ek_orig == _adapter_swap_args) ? 0 : (src_slot > dest_slot) ? 1 : -1; 1.2750 - switch (swap_slots) { 1.2751 - case 1: 1.2752 - ek_opt = (!rotate ? _adapter_opt_swap_1 : 1.2753 - rotate > 0 ? _adapter_opt_rot_1_up : _adapter_opt_rot_1_down); 1.2754 - break; 1.2755 - case 2: 1.2756 - ek_opt = (!rotate ? _adapter_opt_swap_2 : 1.2757 - rotate > 0 ? _adapter_opt_rot_2_up : _adapter_opt_rot_2_down); 1.2758 - break; 1.2759 - default: 1.2760 - goto throw_not_impl; 1.2761 - break; 1.2762 - } 1.2763 - } 1.2764 - break; 1.2765 - 1.2766 - case _adapter_spread_args: 1.2767 - { 1.2768 - // vminfo will be the required length of the array 1.2769 - int array_size = (slots_pushed + 1) / (type2size[dest] == 2 ? 2 : 1); 1.2770 - vminfo = array_size; 1.2771 - // general case 1.2772 - switch (dest) { 1.2773 - case T_BOOLEAN : // fall through to T_BYTE: 1.2774 - case T_BYTE : ek_opt = _adapter_opt_spread_byte; break; 1.2775 - case T_CHAR : ek_opt = _adapter_opt_spread_char; break; 1.2776 - case T_SHORT : ek_opt = _adapter_opt_spread_short; break; 1.2777 - case T_INT : ek_opt = _adapter_opt_spread_int; break; 1.2778 - case T_LONG : ek_opt = _adapter_opt_spread_long; break; 1.2779 - case T_FLOAT : ek_opt = _adapter_opt_spread_float; break; 1.2780 - case T_DOUBLE : ek_opt = _adapter_opt_spread_double; break; 1.2781 - case T_OBJECT : ek_opt = _adapter_opt_spread_ref; break; 1.2782 - case T_VOID : if (array_size != 0) goto throw_not_impl; 1.2783 - ek_opt = _adapter_opt_spread_ref; break; 1.2784 - default : goto throw_not_impl; 1.2785 - } 1.2786 - assert(array_size == 0 || // it doesn't matter what the spreader is 1.2787 - (ek_adapter_opt_spread_count(ek_opt) == -1 && 1.2788 - (ek_adapter_opt_spread_type(ek_opt) == dest || 1.2789 - (ek_adapter_opt_spread_type(ek_opt) == T_BYTE && dest == T_BOOLEAN))), 1.2790 - err_msg("dest=%d ek_opt=%d", dest, ek_opt)); 1.2791 - 1.2792 - if (array_size <= 0) { 1.2793 - // since the general case does not handle length 0, this case is required: 1.2794 - ek_opt = _adapter_opt_spread_0; 1.2795 - break; 1.2796 - } 1.2797 - if (dest == T_OBJECT) { 1.2798 - ek_try = EntryKind(_adapter_opt_spread_1_ref - 1 + array_size); 1.2799 - if (ek_try < _adapter_opt_spread_LAST && 1.2800 - ek_adapter_opt_spread_count(ek_try) == array_size) { 1.2801 - assert(ek_adapter_opt_spread_type(ek_try) == dest, ""); 1.2802 - ek_opt = ek_try; 1.2803 - break; 1.2804 - } 1.2805 - } 1.2806 - break; 1.2807 - } 1.2808 - break; 1.2809 - 1.2810 - case _adapter_collect_args: 1.2811 - { 1.2812 - int elem_slots = argument_slot_count(java_lang_invoke_MethodHandle::type(argument())); 1.2813 - // vminfo will be the location to insert the return value 1.2814 - vminfo = argslot; 1.2815 - ensure_vmlayout_field(target, CHECK); 1.2816 - ensure_vmlayout_field(argument, CHECK); 1.2817 - 1.2818 - // general case: 1.2819 - switch (dest) { 1.2820 - default : if (!is_subword_type(dest)) goto throw_not_impl; 1.2821 - // else fall through: 1.2822 - case T_INT : ek_opt = _adapter_opt_collect_int; break; 1.2823 - case T_LONG : ek_opt = _adapter_opt_collect_long; break; 1.2824 - case T_FLOAT : ek_opt = _adapter_opt_collect_float; break; 1.2825 - case T_DOUBLE : ek_opt = _adapter_opt_collect_double; break; 1.2826 - case T_OBJECT : ek_opt = _adapter_opt_collect_ref; break; 1.2827 - case T_VOID : ek_opt = _adapter_opt_collect_void; break; 1.2828 - } 1.2829 - assert(ek_adapter_opt_collect_slot(ek_opt) == -1 && 1.2830 - ek_adapter_opt_collect_count(ek_opt) == -1 && 1.2831 - (ek_adapter_opt_collect_type(ek_opt) == dest || 1.2832 - ek_adapter_opt_collect_type(ek_opt) == T_INT && is_subword_type(dest)), 1.2833 - ""); 1.2834 - 1.2835 - if (dest == T_OBJECT && elem_slots == 1 && OptimizeMethodHandles) { 1.2836 - // filter operation on a ref 1.2837 - ek_try = EntryKind(_adapter_opt_filter_S0_ref + argslot); 1.2838 - if (ek_try < _adapter_opt_collect_LAST && 1.2839 - ek_adapter_opt_collect_slot(ek_try) == argslot) { 1.2840 - assert(ek_adapter_opt_collect_count(ek_try) == elem_slots && 1.2841 - ek_adapter_opt_collect_type(ek_try) == dest, ""); 1.2842 - ek_opt = ek_try; 1.2843 - break; 1.2844 - } 1.2845 - ek_opt = _adapter_opt_collect_1_ref; 1.2846 - break; 1.2847 - } 1.2848 - 1.2849 - if (dest == T_OBJECT && elem_slots == 2 && OptimizeMethodHandles) { 1.2850 - // filter of two arguments 1.2851 - ek_try = EntryKind(_adapter_opt_collect_2_S0_ref + argslot); 1.2852 - if (ek_try < _adapter_opt_collect_LAST && 1.2853 - ek_adapter_opt_collect_slot(ek_try) == argslot) { 1.2854 - assert(ek_adapter_opt_collect_count(ek_try) == elem_slots && 1.2855 - ek_adapter_opt_collect_type(ek_try) == dest, ""); 1.2856 - ek_opt = ek_try; 1.2857 - break; 1.2858 - } 1.2859 - ek_opt = _adapter_opt_collect_2_ref; 1.2860 - break; 1.2861 - } 1.2862 - 1.2863 - if (dest == T_OBJECT && OptimizeMethodHandles) { 1.2864 - // try to use a fixed length adapter 1.2865 - ek_try = EntryKind(_adapter_opt_collect_0_ref + elem_slots); 1.2866 - if (ek_try < _adapter_opt_collect_LAST && 1.2867 - ek_adapter_opt_collect_count(ek_try) == elem_slots) { 1.2868 - assert(ek_adapter_opt_collect_slot(ek_try) == -1 && 1.2869 - ek_adapter_opt_collect_type(ek_try) == dest, ""); 1.2870 - ek_opt = ek_try; 1.2871 - break; 1.2872 - } 1.2873 - } 1.2874 - 1.2875 - break; 1.2876 - } 1.2877 - 1.2878 - case _adapter_fold_args: 1.2879 - { 1.2880 - int elem_slots = argument_slot_count(java_lang_invoke_MethodHandle::type(argument())); 1.2881 - // vminfo will be the location to insert the return value 1.2882 - vminfo = argslot + elem_slots; 1.2883 - ensure_vmlayout_field(target, CHECK); 1.2884 - ensure_vmlayout_field(argument, CHECK); 1.2885 - 1.2886 - switch (dest) { 1.2887 - default : if (!is_subword_type(dest)) goto throw_not_impl; 1.2888 - // else fall through: 1.2889 - case T_INT : ek_opt = _adapter_opt_fold_int; break; 1.2890 - case T_LONG : ek_opt = _adapter_opt_fold_long; break; 1.2891 - case T_FLOAT : ek_opt = _adapter_opt_fold_float; break; 1.2892 - case T_DOUBLE : ek_opt = _adapter_opt_fold_double; break; 1.2893 - case T_OBJECT : ek_opt = _adapter_opt_fold_ref; break; 1.2894 - case T_VOID : ek_opt = _adapter_opt_fold_void; break; 1.2895 - } 1.2896 - assert(ek_adapter_opt_collect_slot(ek_opt) == -1 && 1.2897 - ek_adapter_opt_collect_count(ek_opt) == -1 && 1.2898 - (ek_adapter_opt_collect_type(ek_opt) == dest || 1.2899 - ek_adapter_opt_collect_type(ek_opt) == T_INT && is_subword_type(dest)), 1.2900 - ""); 1.2901 - 1.2902 - if (dest == T_OBJECT && elem_slots == 0 && OptimizeMethodHandles) { 1.2903 - // if there are no args, just pretend it's a collect 1.2904 - ek_opt = _adapter_opt_collect_0_ref; 1.2905 - break; 1.2906 - } 1.2907 - 1.2908 - if (dest == T_OBJECT && OptimizeMethodHandles) { 1.2909 - // try to use a fixed length adapter 1.2910 - ek_try = EntryKind(_adapter_opt_fold_1_ref - 1 + elem_slots); 1.2911 - if (ek_try < _adapter_opt_fold_LAST && 1.2912 - ek_adapter_opt_collect_count(ek_try) == elem_slots) { 1.2913 - assert(ek_adapter_opt_collect_slot(ek_try) == -1 && 1.2914 - ek_adapter_opt_collect_type(ek_try) == dest, ""); 1.2915 - ek_opt = ek_try; 1.2916 - break; 1.2917 - } 1.2918 - } 1.2919 - 1.2920 - break; 1.2921 - } 1.2922 - 1.2923 - default: 1.2924 - // should have failed much earlier; must be a missing case here 1.2925 - assert(false, "incomplete switch"); 1.2926 - // and fall through: 1.2927 - 1.2928 - throw_not_impl: 1.2929 - if (err == NULL) 1.2930 - err = "unknown adapter type"; 1.2931 - break; 1.2932 - } 1.2933 - 1.2934 - if (err == NULL && (vminfo & CONV_VMINFO_MASK) != vminfo) { 1.2935 - // should not happen, since vminfo is used to encode arg/slot indexes < 255 1.2936 - err = "vminfo overflow"; 1.2937 - } 1.2938 - 1.2939 - if (err == NULL && !have_entry(ek_opt)) { 1.2940 - err = "adapter stub for this kind of method handle is missing"; 1.2941 - } 1.2942 - 1.2943 - if (err == NULL && ek_opt == ek_orig) { 1.2944 - switch (ek_opt) { 1.2945 - case _adapter_prim_to_prim: 1.2946 - case _adapter_ref_to_prim: 1.2947 - case _adapter_prim_to_ref: 1.2948 - case _adapter_swap_args: 1.2949 - case _adapter_rot_args: 1.2950 - case _adapter_collect_args: 1.2951 - case _adapter_fold_args: 1.2952 - case _adapter_spread_args: 1.2953 - // should be handled completely by optimized cases; see above 1.2954 - err = "init_AdapterMethodHandle should not issue this"; 1.2955 - break; 1.2956 - } 1.2957 - } 1.2958 - 1.2959 - if (err != NULL) { 1.2960 - throw_InternalError_for_bad_conversion(conversion, err_msg("%s: conv_op %d ek_opt %d", err, conv_op, ek_opt), THREAD); 1.2961 - return; 1.2962 - } 1.2963 - 1.2964 - // Rebuild the conversion value; maybe parts of it were changed. 1.2965 - jint new_conversion = adapter_conversion(conv_op, src, dest, stack_move, vminfo); 1.2966 - 1.2967 - // Finalize the conversion field. (Note that it is final to Java code.) 1.2968 - java_lang_invoke_AdapterMethodHandle::set_conversion(mh(), new_conversion); 1.2969 - 1.2970 - if (java_lang_invoke_CountingMethodHandle::is_instance(mh())) { 1.2971 - assert(ek_orig == _adapter_retype_only, "only one handled"); 1.2972 - ek_opt = _adapter_opt_profiling; 1.2973 - } 1.2974 - 1.2975 - // Done! 1.2976 - java_lang_invoke_MethodHandle::set_vmentry(mh(), entry(ek_opt)); 1.2977 - 1.2978 - // There should be enough memory barriers on exit from native methods 1.2979 - // to ensure that the MH is fully initialized to all threads before 1.2980 - // Java code can publish it in global data structures. 1.2981 -} 1.2982 - 1.2983 -void MethodHandles::ensure_vmlayout_field(Handle target, TRAPS) { 1.2984 - Handle mtype(THREAD, java_lang_invoke_MethodHandle::type(target())); 1.2985 - Handle mtform(THREAD, java_lang_invoke_MethodType::form(mtype())); 1.2986 - if (mtform.is_null()) { THROW(vmSymbols::java_lang_InternalError()); } 1.2987 - if (java_lang_invoke_MethodTypeForm::vmlayout_offset_in_bytes() > 0) { 1.2988 - if (java_lang_invoke_MethodTypeForm::vmlayout(mtform()) == NULL) { 1.2989 - // fill it in 1.2990 - Handle erased_mtype(THREAD, java_lang_invoke_MethodTypeForm::erasedType(mtform())); 1.2991 - TempNewSymbol erased_signature 1.2992 - = java_lang_invoke_MethodType::as_signature(erased_mtype(), /*intern:*/true, CHECK); 1.2993 - methodOop cookie 1.2994 - = SystemDictionary::find_method_handle_invoke(vmSymbols::invokeExact_name(), 1.2995 - erased_signature, 1.2996 - SystemDictionaryHandles::Object_klass(), 1.2997 - THREAD); 1.2998 - java_lang_invoke_MethodTypeForm::init_vmlayout(mtform(), cookie); 1.2999 - } 1.3000 - } 1.3001 - assert(java_lang_invoke_MethodTypeForm::vmslots(mtform()) == argument_slot_count(mtype()), "must agree"); 1.3002 -} 1.3003 - 1.3004 -#ifdef ASSERT 1.3005 - 1.3006 -extern "C" 1.3007 -void print_method_handle(oop mh); 1.3008 - 1.3009 -static void stress_method_handle_walk_impl(Handle mh, TRAPS) { 1.3010 - if (StressMethodHandleWalk) { 1.3011 - // Exercise the MethodHandleWalk code in various ways and validate 1.3012 - // the resulting method oop. Some of these produce output so they 1.3013 - // are guarded under Verbose. 1.3014 - ResourceMark rm; 1.3015 - HandleMark hm; 1.3016 - if (Verbose) { 1.3017 - print_method_handle(mh()); 1.3018 - } 1.3019 - TempNewSymbol name = SymbolTable::new_symbol("invoke", CHECK); 1.3020 - Handle mt = java_lang_invoke_MethodHandle::type(mh()); 1.3021 - TempNewSymbol signature = java_lang_invoke_MethodType::as_signature(mt(), true, CHECK); 1.3022 - MethodHandleCompiler mhc(mh, name, signature, 10000, false, CHECK); 1.3023 - methodHandle m = mhc.compile(CHECK); 1.3024 - if (Verbose) { 1.3025 - m->print_codes(); 1.3026 - } 1.3027 - InterpreterOopMap mask; 1.3028 - OopMapCache::compute_one_oop_map(m, m->code_size() - 1, &mask); 1.3029 - // compile to object code if -Xcomp or WizardMode 1.3030 - if ((WizardMode || 1.3031 - CompilationPolicy::must_be_compiled(m)) 1.3032 - && !instanceKlass::cast(m->method_holder())->is_not_initialized() 1.3033 - && CompilationPolicy::can_be_compiled(m)) { 1.3034 - // Force compilation 1.3035 - CompileBroker::compile_method(m, InvocationEntryBci, 1.3036 - CompilationPolicy::policy()->initial_compile_level(), 1.3037 - methodHandle(), 0, "StressMethodHandleWalk", 1.3038 - CHECK); 1.3039 - } 1.3040 - } 1.3041 -} 1.3042 - 1.3043 -static void stress_method_handle_walk(Handle mh, TRAPS) { 1.3044 - stress_method_handle_walk_impl(mh, THREAD); 1.3045 - if (HAS_PENDING_EXCEPTION) { 1.3046 - oop ex = PENDING_EXCEPTION; 1.3047 - CLEAR_PENDING_EXCEPTION; 1.3048 - tty->print("StressMethodHandleWalk: "); 1.3049 - java_lang_Throwable::print(ex, tty); 1.3050 - tty->cr(); 1.3051 - } 1.3052 -} 1.3053 -#else 1.3054 - 1.3055 -static void stress_method_handle_walk(Handle mh, TRAPS) {} 1.3056 - 1.3057 -#endif 1.3058 - 1.3059 // 1.3060 -// Here are the native methods on sun.invoke.MethodHandleImpl. 1.3061 +// Here are the native methods in java.lang.invoke.MethodHandleNatives 1.3062 // They are the private interface between this JVM and the HotSpot-specific 1.3063 // Java code that implements JSR 292 method handles. 1.3064 // 1.3065 // Note: We use a JVM_ENTRY macro to define each of these, for this is the way 1.3066 // that intrinsic (non-JNI) native methods are defined in HotSpot. 1.3067 // 1.3068 - 1.3069 -// direct method handles for invokestatic or invokespecial 1.3070 -// void init(DirectMethodHandle self, MemberName ref, boolean doDispatch, Class<?> caller); 1.3071 -JVM_ENTRY(void, MHN_init_DMH(JNIEnv *env, jobject igcls, jobject mh_jh, 1.3072 - jobject target_jh, jboolean do_dispatch, jobject caller_jh)) { 1.3073 - ResourceMark rm; // for error messages 1.3074 - 1.3075 - // This is the guy we are initializing: 1.3076 - if (mh_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "self is null"); } 1.3077 - Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh)); 1.3078 - 1.3079 - // Early returns out of this method leave the DMH in an unfinished state. 1.3080 - assert(java_lang_invoke_MethodHandle::vmentry(mh()) == NULL, "must be safely null"); 1.3081 - 1.3082 - // which method are we really talking about? 1.3083 - if (target_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); } 1.3084 - Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); 1.3085 - if (java_lang_invoke_MemberName::is_instance(target()) && 1.3086 - java_lang_invoke_MemberName::vmindex(target()) == VM_INDEX_UNINITIALIZED) { 1.3087 - MethodHandles::resolve_MemberName(target, CHECK); 1.3088 - } 1.3089 - 1.3090 - KlassHandle receiver_limit; int decode_flags = 0; 1.3091 - methodHandle m = MethodHandles::decode_method(target(), receiver_limit, decode_flags); 1.3092 - if (m.is_null()) { THROW_MSG(vmSymbols::java_lang_InternalError(), "no such method"); } 1.3093 - 1.3094 - // The trusted Java code that calls this method should already have performed 1.3095 - // access checks on behalf of the given caller. But, we can verify this. 1.3096 - if (VerifyMethodHandles && caller_jh != NULL) { 1.3097 - KlassHandle caller(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(caller_jh))); 1.3098 - // If this were a bytecode, the first access check would be against 1.3099 - // the "reference class" mentioned in the CONSTANT_Methodref. 1.3100 - // We don't know at this point which class that was, and if we 1.3101 - // check against m.method_holder we might get the wrong answer. 1.3102 - // So we just make sure to handle this check when the resolution 1.3103 - // happens, when we call resolve_MemberName. 1.3104 - // 1.3105 - // (A public class can inherit public members from private supers, 1.3106 - // and it would be wrong to check access against the private super 1.3107 - // if the original symbolic reference was against the public class.) 1.3108 - // 1.3109 - // If there were a bytecode, the next step would be to lookup the method 1.3110 - // in the reference class, then then check the method's access bits. 1.3111 - // Emulate LinkResolver::check_method_accessability. 1.3112 - klassOop resolved_klass = m->method_holder(); 1.3113 - if (!Reflection::verify_field_access(caller->as_klassOop(), 1.3114 - resolved_klass, resolved_klass, 1.3115 - m->access_flags(), 1.3116 - true)) { 1.3117 - // %%% following cutout belongs in Reflection::verify_field_access? 1.3118 - bool same_pm = Reflection::is_same_package_member(caller->as_klassOop(), 1.3119 - resolved_klass, THREAD); 1.3120 - if (!same_pm) { 1.3121 - THROW_MSG(vmSymbols::java_lang_InternalError(), m->name_and_sig_as_C_string()); 1.3122 - } 1.3123 - } 1.3124 - } 1.3125 - 1.3126 - MethodHandles::init_DirectMethodHandle(mh, m, (do_dispatch != JNI_FALSE), CHECK); 1.3127 - stress_method_handle_walk(mh, CHECK); 1.3128 -} 1.3129 -JVM_END 1.3130 - 1.3131 -// bound method handles 1.3132 -JVM_ENTRY(void, MHN_init_BMH(JNIEnv *env, jobject igcls, jobject mh_jh, 1.3133 - jobject target_jh, int argnum)) { 1.3134 - ResourceMark rm; // for error messages 1.3135 - 1.3136 - // This is the guy we are initializing: 1.3137 - if (mh_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "self is null"); } 1.3138 - Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh)); 1.3139 - 1.3140 - // Early returns out of this method leave the BMH in an unfinished state. 1.3141 - assert(java_lang_invoke_MethodHandle::vmentry(mh()) == NULL, "must be safely null"); 1.3142 - 1.3143 - if (target_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); } 1.3144 - Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); 1.3145 - 1.3146 - if (!java_lang_invoke_MethodHandle::is_instance(target())) { 1.3147 - // Target object is a reflective method. (%%% Do we need this alternate path?) 1.3148 - Untested("init_BMH of non-MH"); 1.3149 - if (argnum != 0) { THROW(vmSymbols::java_lang_InternalError()); } 1.3150 - KlassHandle receiver_limit; int decode_flags = 0; 1.3151 - methodHandle m = MethodHandles::decode_method(target(), receiver_limit, decode_flags); 1.3152 - MethodHandles::init_BoundMethodHandle_with_receiver(mh, m, 1.3153 - receiver_limit, 1.3154 - decode_flags, 1.3155 - CHECK); 1.3156 - } else { 1.3157 - // Build a BMH on top of a DMH or another BMH: 1.3158 - MethodHandles::init_BoundMethodHandle(mh, target, argnum, CHECK); 1.3159 - } 1.3160 - 1.3161 - if (StressMethodHandleWalk) { 1.3162 - if (mh->klass() == SystemDictionary::BoundMethodHandle_klass()) 1.3163 - stress_method_handle_walk(mh, CHECK); 1.3164 - // else don't, since the subclass has not yet initialized its own fields 1.3165 - } 1.3166 -} 1.3167 -JVM_END 1.3168 - 1.3169 -// adapter method handles 1.3170 -JVM_ENTRY(void, MHN_init_AMH(JNIEnv *env, jobject igcls, jobject mh_jh, 1.3171 - jobject target_jh, int argnum)) { 1.3172 - // This is the guy we are initializing: 1.3173 - if (mh_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "self is null"); } 1.3174 - if (target_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); } 1.3175 - Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh)); 1.3176 - Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); 1.3177 - 1.3178 - // Early returns out of this method leave the AMH in an unfinished state. 1.3179 - assert(java_lang_invoke_MethodHandle::vmentry(mh()) == NULL, "must be safely null"); 1.3180 - 1.3181 - MethodHandles::init_AdapterMethodHandle(mh, target, argnum, CHECK); 1.3182 - stress_method_handle_walk(mh, CHECK); 1.3183 -} 1.3184 -JVM_END 1.3185 - 1.3186 -// method type forms 1.3187 -JVM_ENTRY(void, MHN_init_MT(JNIEnv *env, jobject igcls, jobject erased_jh)) { 1.3188 - if (erased_jh == NULL) return; 1.3189 - if (TraceMethodHandles) { 1.3190 - tty->print("creating MethodType form "); 1.3191 - if (WizardMode || Verbose) { // Warning: this calls Java code on the MH! 1.3192 - // call Object.toString() 1.3193 - Symbol* name = vmSymbols::toString_name(); 1.3194 - Symbol* sig = vmSymbols::void_string_signature(); 1.3195 - JavaCallArguments args(Handle(THREAD, JNIHandles::resolve_non_null(erased_jh))); 1.3196 - JavaValue result(T_OBJECT); 1.3197 - JavaCalls::call_virtual(&result, SystemDictionary::Object_klass(), name, sig, 1.3198 - &args, CHECK); 1.3199 - Handle str(THREAD, (oop)result.get_jobject()); 1.3200 - java_lang_String::print(str, tty); 1.3201 - } 1.3202 - tty->cr(); 1.3203 - } 1.3204 -} 1.3205 -JVM_END 1.3206 - 1.3207 -// debugging and reflection 1.3208 -JVM_ENTRY(jobject, MHN_getTarget(JNIEnv *env, jobject igcls, jobject mh_jh, jint format)) { 1.3209 - Handle mh(THREAD, JNIHandles::resolve(mh_jh)); 1.3210 - if (!java_lang_invoke_MethodHandle::is_instance(mh())) { 1.3211 - THROW_NULL(vmSymbols::java_lang_IllegalArgumentException()); 1.3212 - } 1.3213 - oop target = MethodHandles::encode_target(mh, format, CHECK_NULL); 1.3214 - return JNIHandles::make_local(THREAD, target); 1.3215 -} 1.3216 -JVM_END 1.3217 - 1.3218 JVM_ENTRY(jint, MHN_getConstant(JNIEnv *env, jobject igcls, jint which)) { 1.3219 switch (which) { 1.3220 - case MethodHandles::GC_JVM_PUSH_LIMIT: 1.3221 - guarantee(MethodHandlePushLimit >= 2 && MethodHandlePushLimit <= 0xFF, 1.3222 - "MethodHandlePushLimit parameter must be in valid range"); 1.3223 - return MethodHandlePushLimit; 1.3224 - case MethodHandles::GC_JVM_STACK_MOVE_UNIT: 1.3225 - // return number of words per slot, signed according to stack direction 1.3226 - return MethodHandles::stack_move_unit(); 1.3227 - case MethodHandles::GC_CONV_OP_IMPLEMENTED_MASK: 1.3228 - return MethodHandles::adapter_conversion_ops_supported_mask(); 1.3229 case MethodHandles::GC_COUNT_GWT: 1.3230 #ifdef COMPILER2 1.3231 return true; 1.3232 @@ -2872,64 +942,54 @@ 1.3233 JVM_END 1.3234 1.3235 #ifndef PRODUCT 1.3236 -#define EACH_NAMED_CON(template) \ 1.3237 - /* hold back this one until JDK stabilizes */ \ 1.3238 - /* template(MethodHandles,GC_JVM_PUSH_LIMIT) */ \ 1.3239 - /* hold back this one until JDK stabilizes */ \ 1.3240 - /* template(MethodHandles,GC_JVM_STACK_MOVE_UNIT) */ \ 1.3241 - /* hold back this one until JDK stabilizes */ \ 1.3242 - /* template(MethodHandles,GC_OP_ROT_ARGS_DOWN_LIMIT_BIAS) */ \ 1.3243 - template(MethodHandles,ETF_HANDLE_OR_METHOD_NAME) \ 1.3244 - template(MethodHandles,ETF_DIRECT_HANDLE) \ 1.3245 - template(MethodHandles,ETF_METHOD_NAME) \ 1.3246 - template(MethodHandles,ETF_REFLECT_METHOD) \ 1.3247 +#define EACH_NAMED_CON(template, requirement) \ 1.3248 + template(MethodHandles,GC_COUNT_GWT) \ 1.3249 template(java_lang_invoke_MemberName,MN_IS_METHOD) \ 1.3250 template(java_lang_invoke_MemberName,MN_IS_CONSTRUCTOR) \ 1.3251 template(java_lang_invoke_MemberName,MN_IS_FIELD) \ 1.3252 template(java_lang_invoke_MemberName,MN_IS_TYPE) \ 1.3253 template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \ 1.3254 template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \ 1.3255 - template(java_lang_invoke_MemberName,VM_INDEX_UNINITIALIZED) \ 1.3256 - template(java_lang_invoke_AdapterMethodHandle,OP_RETYPE_ONLY) \ 1.3257 - template(java_lang_invoke_AdapterMethodHandle,OP_RETYPE_RAW) \ 1.3258 - template(java_lang_invoke_AdapterMethodHandle,OP_CHECK_CAST) \ 1.3259 - template(java_lang_invoke_AdapterMethodHandle,OP_PRIM_TO_PRIM) \ 1.3260 - template(java_lang_invoke_AdapterMethodHandle,OP_REF_TO_PRIM) \ 1.3261 - template(java_lang_invoke_AdapterMethodHandle,OP_PRIM_TO_REF) \ 1.3262 - template(java_lang_invoke_AdapterMethodHandle,OP_SWAP_ARGS) \ 1.3263 - template(java_lang_invoke_AdapterMethodHandle,OP_ROT_ARGS) \ 1.3264 - template(java_lang_invoke_AdapterMethodHandle,OP_DUP_ARGS) \ 1.3265 - template(java_lang_invoke_AdapterMethodHandle,OP_DROP_ARGS) \ 1.3266 - template(java_lang_invoke_AdapterMethodHandle,OP_COLLECT_ARGS) \ 1.3267 - template(java_lang_invoke_AdapterMethodHandle,OP_SPREAD_ARGS) \ 1.3268 - /* hold back this one until JDK stabilizes */ \ 1.3269 - /*template(java_lang_invoke_AdapterMethodHandle,CONV_OP_LIMIT)*/ \ 1.3270 - template(java_lang_invoke_AdapterMethodHandle,CONV_OP_MASK) \ 1.3271 - template(java_lang_invoke_AdapterMethodHandle,CONV_VMINFO_MASK) \ 1.3272 - template(java_lang_invoke_AdapterMethodHandle,CONV_VMINFO_SHIFT) \ 1.3273 - template(java_lang_invoke_AdapterMethodHandle,CONV_OP_SHIFT) \ 1.3274 - template(java_lang_invoke_AdapterMethodHandle,CONV_DEST_TYPE_SHIFT) \ 1.3275 - template(java_lang_invoke_AdapterMethodHandle,CONV_SRC_TYPE_SHIFT) \ 1.3276 - template(java_lang_invoke_AdapterMethodHandle,CONV_STACK_MOVE_SHIFT) \ 1.3277 - template(java_lang_invoke_AdapterMethodHandle,CONV_STACK_MOVE_MASK) \ 1.3278 + template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_SHIFT) \ 1.3279 + template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_MASK) \ 1.3280 + template(MethodHandles,GC_LAMBDA_SUPPORT) \ 1.3281 /*end*/ 1.3282 1.3283 +#define IGNORE_REQ(req_expr) /* req_expr */ 1.3284 #define ONE_PLUS(scope,value) 1+ 1.3285 -static const int con_value_count = EACH_NAMED_CON(ONE_PLUS) 0; 1.3286 +static const int con_value_count = EACH_NAMED_CON(ONE_PLUS, IGNORE_REQ) 0; 1.3287 #define VALUE_COMMA(scope,value) scope::value, 1.3288 -static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA) 0 }; 1.3289 +static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA, IGNORE_REQ) 0 }; 1.3290 #define STRING_NULL(scope,value) #value "\0" 1.3291 -static const char con_names[] = { EACH_NAMED_CON(STRING_NULL) }; 1.3292 +static const char con_names[] = { EACH_NAMED_CON(STRING_NULL, IGNORE_REQ) }; 1.3293 + 1.3294 +static bool advertise_con_value(int which) { 1.3295 + if (which < 0) return false; 1.3296 + bool ok = true; 1.3297 + int count = 0; 1.3298 +#define INC_COUNT(scope,value) \ 1.3299 + ++count; 1.3300 +#define CHECK_REQ(req_expr) \ 1.3301 + if (which < count) return ok; \ 1.3302 + ok = (req_expr); 1.3303 + EACH_NAMED_CON(INC_COUNT, CHECK_REQ); 1.3304 +#undef INC_COUNT 1.3305 +#undef CHECK_REQ 1.3306 + assert(count == con_value_count, ""); 1.3307 + if (which < count) return ok; 1.3308 + return false; 1.3309 +} 1.3310 1.3311 #undef ONE_PLUS 1.3312 #undef VALUE_COMMA 1.3313 #undef STRING_NULL 1.3314 #undef EACH_NAMED_CON 1.3315 -#endif 1.3316 +#endif // PRODUCT 1.3317 1.3318 JVM_ENTRY(jint, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) { 1.3319 #ifndef PRODUCT 1.3320 - if (which >= 0 && which < con_value_count) { 1.3321 + if (advertise_con_value(which)) { 1.3322 + assert(which >= 0 && which < con_value_count, ""); 1.3323 int con = con_values[which]; 1.3324 objArrayHandle box(THREAD, (objArrayOop) JNIHandles::resolve(box_jh)); 1.3325 if (box.not_null() && box->klass() == Universe::objectArrayKlassObj() && box->length() > 0) { 1.3326 @@ -2965,13 +1025,14 @@ 1.3327 JVM_END 1.3328 1.3329 // void resolve(MemberName self, Class<?> caller) 1.3330 -JVM_ENTRY(void, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh)) { 1.3331 - if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); } 1.3332 +JVM_ENTRY(jobject, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh)) { 1.3333 + if (mname_jh == NULL) { THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "mname is null"); } 1.3334 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1.3335 1.3336 // The trusted Java code that calls this method should already have performed 1.3337 // access checks on behalf of the given caller. But, we can verify this. 1.3338 - if (VerifyMethodHandles && caller_jh != NULL) { 1.3339 + if (VerifyMethodHandles && caller_jh != NULL && 1.3340 + java_lang_invoke_MemberName::clazz(mname()) != NULL) { 1.3341 klassOop reference_klass = java_lang_Class::as_klassOop(java_lang_invoke_MemberName::clazz(mname())); 1.3342 if (reference_klass != NULL) { 1.3343 // Emulate LinkResolver::check_klass_accessability. 1.3344 @@ -2979,15 +1040,97 @@ 1.3345 if (!Reflection::verify_class_access(caller, 1.3346 reference_klass, 1.3347 true)) { 1.3348 - THROW_MSG(vmSymbols::java_lang_InternalError(), Klass::cast(reference_klass)->external_name()); 1.3349 + THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), Klass::cast(reference_klass)->external_name()); 1.3350 } 1.3351 } 1.3352 } 1.3353 1.3354 - MethodHandles::resolve_MemberName(mname, CHECK); 1.3355 + Handle resolved = MethodHandles::resolve_MemberName(mname, CHECK_NULL); 1.3356 + if (resolved.is_null()) { 1.3357 + int flags = java_lang_invoke_MemberName::flags(mname()); 1.3358 + int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; 1.3359 + if (!MethodHandles::ref_kind_is_valid(ref_kind)) { 1.3360 + THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "obsolete MemberName format"); 1.3361 + } 1.3362 + if ((flags & ALL_KINDS) == IS_FIELD) { 1.3363 + THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), "field resolution failed"); 1.3364 + } else if ((flags & ALL_KINDS) == IS_METHOD || 1.3365 + (flags & ALL_KINDS) == IS_CONSTRUCTOR) { 1.3366 + THROW_MSG_NULL(vmSymbols::java_lang_NoSuchFieldError(), "method resolution failed"); 1.3367 + } else { 1.3368 + THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "resolution failed"); 1.3369 + } 1.3370 + } 1.3371 + 1.3372 + return JNIHandles::make_local(THREAD, resolved()); 1.3373 } 1.3374 JVM_END 1.3375 1.3376 +static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) { 1.3377 + if (mname == NULL || 1.3378 + java_lang_invoke_MemberName::vmtarget(mname) == NULL) { 1.3379 + THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved"); 1.3380 + } else { 1.3381 + int flags = java_lang_invoke_MemberName::flags(mname); 1.3382 + if ((flags & IS_FIELD) != 0 && 1.3383 + (must_be_static 1.3384 + ? (flags & JVM_ACC_STATIC) != 0 1.3385 + : (flags & JVM_ACC_STATIC) == 0)) { 1.3386 + int vmindex = java_lang_invoke_MemberName::vmindex(mname); 1.3387 + return (jlong) vmindex; 1.3388 + } 1.3389 + } 1.3390 + const char* msg = (must_be_static ? "static field required" : "non-static field required"); 1.3391 + THROW_MSG_0(vmSymbols::java_lang_InternalError(), msg); 1.3392 + return 0; 1.3393 +} 1.3394 + 1.3395 +JVM_ENTRY(jlong, MHN_objectFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1.3396 + return find_member_field_offset(JNIHandles::resolve(mname_jh), false, THREAD); 1.3397 +} 1.3398 +JVM_END 1.3399 + 1.3400 +JVM_ENTRY(jlong, MHN_staticFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1.3401 + return find_member_field_offset(JNIHandles::resolve(mname_jh), true, THREAD); 1.3402 +} 1.3403 +JVM_END 1.3404 + 1.3405 +JVM_ENTRY(jobject, MHN_staticFieldBase(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1.3406 + // use the other function to perform sanity checks: 1.3407 + jlong ignore = find_member_field_offset(JNIHandles::resolve(mname_jh), true, CHECK_NULL); 1.3408 + oop clazz = java_lang_invoke_MemberName::clazz(JNIHandles::resolve_non_null(mname_jh)); 1.3409 + return JNIHandles::make_local(THREAD, clazz); 1.3410 +} 1.3411 +JVM_END 1.3412 + 1.3413 +JVM_ENTRY(jobject, MHN_getMemberVMInfo(JNIEnv *env, jobject igcls, jobject mname_jh)) { 1.3414 + if (mname_jh == NULL) return NULL; 1.3415 + Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); 1.3416 + intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname()); 1.3417 + Handle vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); 1.3418 + objArrayHandle result = oopFactory::new_objArray(SystemDictionary::Object_klass(), 2, CHECK_NULL); 1.3419 + jvalue vmindex_value; vmindex_value.j = (long)vmindex; 1.3420 + oop x = java_lang_boxing_object::create(T_LONG, &vmindex_value, CHECK_NULL); 1.3421 + result->obj_at_put(0, x); 1.3422 + x = NULL; 1.3423 + if (vmtarget.is_null() || vmtarget->is_instance()) { 1.3424 + x = vmtarget(); 1.3425 + } else if (vmtarget->is_klass()) { 1.3426 + x = Klass::cast((klassOop) vmtarget())->java_mirror(); 1.3427 + } else { 1.3428 + Handle mname2 = MethodHandles::new_MemberName(CHECK_NULL); 1.3429 + if (vmtarget->is_method()) 1.3430 + x = MethodHandles::init_method_MemberName(mname2(), methodOop(vmtarget()), false, NULL); 1.3431 + else 1.3432 + x = MethodHandles::init_MemberName(mname2(), vmtarget()); 1.3433 + } 1.3434 + result->obj_at_put(1, x); 1.3435 + return JNIHandles::make_local(env, result()); 1.3436 +} 1.3437 +JVM_END 1.3438 + 1.3439 + 1.3440 + 1.3441 // static native int getMembers(Class<?> defc, String matchName, String matchSig, 1.3442 // int matchFlags, Class<?> caller, int skip, MemberName[] results); 1.3443 JVM_ENTRY(jint, MHN_getMembers(JNIEnv *env, jobject igcls, 1.3444 @@ -3053,45 +1196,6 @@ 1.3445 } 1.3446 JVM_END 1.3447 1.3448 -methodOop MethodHandles::resolve_raise_exception_method(TRAPS) { 1.3449 - if (_raise_exception_method != NULL) { 1.3450 - // no need to do it twice 1.3451 - return raise_exception_method(); 1.3452 - } 1.3453 - // LinkResolver::resolve_invokedynamic can reach this point 1.3454 - // because an invokedynamic has failed very early (7049415) 1.3455 - KlassHandle MHN_klass = SystemDictionaryHandles::MethodHandleNatives_klass(); 1.3456 - if (MHN_klass.not_null()) { 1.3457 - TempNewSymbol raiseException_name = SymbolTable::new_symbol("raiseException", CHECK_NULL); 1.3458 - TempNewSymbol raiseException_sig = SymbolTable::new_symbol("(ILjava/lang/Object;Ljava/lang/Object;)V", CHECK_NULL); 1.3459 - methodOop raiseException_method = instanceKlass::cast(MHN_klass->as_klassOop()) 1.3460 - ->find_method(raiseException_name, raiseException_sig); 1.3461 - if (raiseException_method != NULL && raiseException_method->is_static()) { 1.3462 - return raiseException_method; 1.3463 - } 1.3464 - } 1.3465 - // not found; let the caller deal with it 1.3466 - return NULL; 1.3467 -} 1.3468 -void MethodHandles::raise_exception(int code, oop actual, oop required, TRAPS) { 1.3469 - methodOop raiseException_method = resolve_raise_exception_method(CHECK); 1.3470 - if (raiseException_method != NULL && 1.3471 - instanceKlass::cast(raiseException_method->method_holder())->is_not_initialized()) { 1.3472 - instanceKlass::cast(raiseException_method->method_holder())->initialize(CHECK); 1.3473 - // it had better be resolved by now, or maybe JSR 292 failed to load 1.3474 - raiseException_method = raise_exception_method(); 1.3475 - } 1.3476 - if (raiseException_method == NULL) { 1.3477 - THROW_MSG(vmSymbols::java_lang_InternalError(), "no raiseException method"); 1.3478 - } 1.3479 - JavaCallArguments args; 1.3480 - args.push_int(code); 1.3481 - args.push_oop(actual); 1.3482 - args.push_oop(required); 1.3483 - JavaValue result(T_VOID); 1.3484 - JavaCalls::call(&result, raiseException_method, &args, CHECK); 1.3485 -} 1.3486 - 1.3487 JVM_ENTRY(jobject, MH_invoke_UOE(JNIEnv *env, jobject igmh, jobjectArray igargs)) { 1.3488 TempNewSymbol UOE_name = SymbolTable::new_symbol("java/lang/UnsupportedOperationException", CHECK_NULL); 1.3489 THROW_MSG_NULL(UOE_name, "MethodHandle.invoke cannot be invoked reflectively"); 1.3490 @@ -3121,39 +1225,30 @@ 1.3491 #define MT JLINV"MethodType;" 1.3492 #define MH JLINV"MethodHandle;" 1.3493 #define MEM JLINV"MemberName;" 1.3494 -#define AMH JLINV"AdapterMethodHandle;" 1.3495 -#define BMH JLINV"BoundMethodHandle;" 1.3496 -#define DMH JLINV"DirectMethodHandle;" 1.3497 1.3498 #define CC (char*) /*cast a literal from (const char*)*/ 1.3499 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) 1.3500 1.3501 // These are the native methods on java.lang.invoke.MethodHandleNatives. 1.3502 -static JNINativeMethod methods[] = { 1.3503 - // void init(MemberName self, AccessibleObject ref) 1.3504 - {CC"init", CC"("AMH""MH"I)V", FN_PTR(MHN_init_AMH)}, 1.3505 - {CC"init", CC"("BMH""OBJ"I)V", FN_PTR(MHN_init_BMH)}, 1.3506 - {CC"init", CC"("DMH""OBJ"Z"CLS")V", FN_PTR(MHN_init_DMH)}, 1.3507 - {CC"init", CC"("MT")V", FN_PTR(MHN_init_MT)}, 1.3508 +static JNINativeMethod required_methods_JDK8[] = { 1.3509 {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHN_init_Mem)}, 1.3510 {CC"expand", CC"("MEM")V", FN_PTR(MHN_expand_Mem)}, 1.3511 - {CC"resolve", CC"("MEM""CLS")V", FN_PTR(MHN_resolve_Mem)}, 1.3512 - {CC"getTarget", CC"("MH"I)"OBJ, FN_PTR(MHN_getTarget)}, 1.3513 + {CC"resolve", CC"("MEM""CLS")"MEM, FN_PTR(MHN_resolve_Mem)}, 1.3514 {CC"getConstant", CC"(I)I", FN_PTR(MHN_getConstant)}, 1.3515 // static native int getNamedCon(int which, Object[] name) 1.3516 {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHN_getNamedCon)}, 1.3517 // static native int getMembers(Class<?> defc, String matchName, String matchSig, 1.3518 // int matchFlags, Class<?> caller, int skip, MemberName[] results); 1.3519 - {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)} 1.3520 -}; 1.3521 - 1.3522 -static JNINativeMethod call_site_methods[] = { 1.3523 + {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)}, 1.3524 + {CC"objectFieldOffset", CC"("MEM")J", FN_PTR(MHN_objectFieldOffset)}, 1.3525 {CC"setCallSiteTargetNormal", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetNormal)}, 1.3526 - {CC"setCallSiteTargetVolatile", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetVolatile)} 1.3527 + {CC"setCallSiteTargetVolatile", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetVolatile)}, 1.3528 + {CC"staticFieldOffset", CC"("MEM")J", FN_PTR(MHN_staticFieldOffset)}, 1.3529 + {CC"staticFieldBase", CC"("MEM")"OBJ, FN_PTR(MHN_staticFieldBase)}, 1.3530 + {CC"getMemberVMInfo", CC"("MEM")"OBJ, FN_PTR(MHN_getMemberVMInfo)} 1.3531 }; 1.3532 1.3533 static JNINativeMethod invoke_methods[] = { 1.3534 - // void init(MemberName self, AccessibleObject ref) 1.3535 {CC"invoke", CC"(["OBJ")"OBJ, FN_PTR(MH_invoke_UOE)}, 1.3536 {CC"invokeExact", CC"(["OBJ")"OBJ, FN_PTR(MH_invokeExact_UOE)} 1.3537 }; 1.3538 @@ -3161,8 +1256,6 @@ 1.3539 // This one function is exported, used by NativeLookup. 1.3540 1.3541 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { 1.3542 - assert(MethodHandles::spot_check_entry_names(), "entry enum is OK"); 1.3543 - 1.3544 if (!EnableInvokeDynamic) { 1.3545 warning("JSR 292 is disabled in this JVM. Use -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic to enable."); 1.3546 return; // bind nothing 1.3547 @@ -3171,34 +1264,32 @@ 1.3548 assert(!MethodHandles::enabled(), "must not be enabled"); 1.3549 bool enable_MH = true; 1.3550 1.3551 - { 1.3552 + jclass MH_class = NULL; 1.3553 + if (SystemDictionary::MethodHandle_klass() == NULL) { 1.3554 + enable_MH = false; 1.3555 + } else { 1.3556 + oop mirror = Klass::cast(SystemDictionary::MethodHandle_klass())->java_mirror(); 1.3557 + MH_class = (jclass) JNIHandles::make_local(env, mirror); 1.3558 + } 1.3559 + 1.3560 + int status; 1.3561 + 1.3562 + if (enable_MH) { 1.3563 ThreadToNativeFromVM ttnfv(thread); 1.3564 - int status = env->RegisterNatives(MHN_class, methods, sizeof(methods)/sizeof(JNINativeMethod)); 1.3565 - if (!env->ExceptionOccurred()) { 1.3566 - const char* L_MH_name = (JLINV "MethodHandle"); 1.3567 - const char* MH_name = L_MH_name+1; 1.3568 - jclass MH_class = env->FindClass(MH_name); 1.3569 + 1.3570 + status = env->RegisterNatives(MHN_class, required_methods_JDK8, sizeof(required_methods_JDK8)/sizeof(JNINativeMethod)); 1.3571 + if (status == JNI_OK && !env->ExceptionOccurred()) { 1.3572 status = env->RegisterNatives(MH_class, invoke_methods, sizeof(invoke_methods)/sizeof(JNINativeMethod)); 1.3573 } 1.3574 - if (!env->ExceptionOccurred()) { 1.3575 - status = env->RegisterNatives(MHN_class, call_site_methods, sizeof(call_site_methods)/sizeof(JNINativeMethod)); 1.3576 - } 1.3577 - if (env->ExceptionOccurred()) { 1.3578 + if (status != JNI_OK || env->ExceptionOccurred()) { 1.3579 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); 1.3580 enable_MH = false; 1.3581 env->ExceptionClear(); 1.3582 } 1.3583 - 1.3584 } 1.3585 1.3586 - if (enable_MH) { 1.3587 - methodOop raiseException_method = MethodHandles::resolve_raise_exception_method(CHECK); 1.3588 - if (raiseException_method != NULL) { 1.3589 - MethodHandles::set_raise_exception_method(raiseException_method); 1.3590 - } else { 1.3591 - warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); 1.3592 - enable_MH = false; 1.3593 - } 1.3594 + if (TraceInvokeDynamic) { 1.3595 + tty->print_cr("MethodHandle support loaded (using LambdaForms)"); 1.3596 } 1.3597 1.3598 if (enable_MH) {