Tue, 05 Jan 2010 15:21:25 +0100
6893268: additional dynamic language related optimizations in C2
Summary: C2 needs some additional optimizations to be able to handle MethodHandle invokes and invokedynamic instructions at the best performance.
Reviewed-by: kvn, never
jrose@1145 | 1 | /* |
jrose@1145 | 2 | * Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved. |
jrose@1145 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jrose@1145 | 4 | * |
jrose@1145 | 5 | * This code is free software; you can redistribute it and/or modify it |
jrose@1145 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jrose@1145 | 7 | * published by the Free Software Foundation. |
jrose@1145 | 8 | * |
jrose@1145 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jrose@1145 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jrose@1145 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jrose@1145 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jrose@1145 | 13 | * accompanied this code). |
jrose@1145 | 14 | * |
jrose@1145 | 15 | * You should have received a copy of the GNU General Public License version |
jrose@1145 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jrose@1145 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jrose@1145 | 18 | * |
jrose@1145 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jrose@1145 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jrose@1145 | 21 | * have any questions. |
jrose@1145 | 22 | * |
jrose@1145 | 23 | */ |
jrose@1145 | 24 | |
jrose@1145 | 25 | /* |
jrose@1145 | 26 | * JSR 292 reference implementation: method handles |
jrose@1145 | 27 | */ |
jrose@1145 | 28 | |
jrose@1145 | 29 | #include "incls/_precompiled.incl" |
jrose@1145 | 30 | #include "incls/_methodHandles.cpp.incl" |
jrose@1145 | 31 | |
jrose@1145 | 32 | bool MethodHandles::_enabled = false; // set true after successful native linkage |
jrose@1145 | 33 | |
jrose@1145 | 34 | MethodHandleEntry* MethodHandles::_entries[MethodHandles::_EK_LIMIT] = {NULL}; |
jrose@1145 | 35 | const char* MethodHandles::_entry_names[_EK_LIMIT+1] = { |
jrose@1474 | 36 | "raise_exception", |
jrose@1145 | 37 | "invokestatic", // how a MH emulates invokestatic |
jrose@1145 | 38 | "invokespecial", // ditto for the other invokes... |
jrose@1145 | 39 | "invokevirtual", |
jrose@1145 | 40 | "invokeinterface", |
jrose@1145 | 41 | "bound_ref", // these are for BMH... |
jrose@1145 | 42 | "bound_int", |
jrose@1145 | 43 | "bound_long", |
jrose@1145 | 44 | "bound_ref_direct", // (direct versions have a direct methodOop) |
jrose@1145 | 45 | "bound_int_direct", |
jrose@1145 | 46 | "bound_long_direct", |
jrose@1145 | 47 | |
jrose@1145 | 48 | // starting at _adapter_mh_first: |
jrose@1145 | 49 | "adapter_retype_only", // these are for AMH... |
jrose@1474 | 50 | "adapter_retype_raw", |
jrose@1145 | 51 | "adapter_check_cast", |
jrose@1145 | 52 | "adapter_prim_to_prim", |
jrose@1145 | 53 | "adapter_ref_to_prim", |
jrose@1145 | 54 | "adapter_prim_to_ref", |
jrose@1145 | 55 | "adapter_swap_args", |
jrose@1145 | 56 | "adapter_rot_args", |
jrose@1145 | 57 | "adapter_dup_args", |
jrose@1145 | 58 | "adapter_drop_args", |
jrose@1145 | 59 | "adapter_collect_args", |
jrose@1145 | 60 | "adapter_spread_args", |
jrose@1145 | 61 | "adapter_flyby", |
jrose@1145 | 62 | "adapter_ricochet", |
jrose@1145 | 63 | |
jrose@1145 | 64 | // optimized adapter types: |
jrose@1145 | 65 | "adapter_swap_args/1", |
jrose@1145 | 66 | "adapter_swap_args/2", |
jrose@1145 | 67 | "adapter_rot_args/1,up", |
jrose@1145 | 68 | "adapter_rot_args/1,down", |
jrose@1145 | 69 | "adapter_rot_args/2,up", |
jrose@1145 | 70 | "adapter_rot_args/2,down", |
jrose@1145 | 71 | "adapter_prim_to_prim/i2i", |
jrose@1145 | 72 | "adapter_prim_to_prim/l2i", |
jrose@1145 | 73 | "adapter_prim_to_prim/d2f", |
jrose@1145 | 74 | "adapter_prim_to_prim/i2l", |
jrose@1145 | 75 | "adapter_prim_to_prim/f2d", |
jrose@1145 | 76 | "adapter_ref_to_prim/unboxi", |
jrose@1145 | 77 | "adapter_ref_to_prim/unboxl", |
jrose@1145 | 78 | "adapter_spread_args/0", |
jrose@1145 | 79 | "adapter_spread_args/1", |
jrose@1145 | 80 | "adapter_spread_args/more", |
jrose@1145 | 81 | |
jrose@1145 | 82 | NULL |
jrose@1145 | 83 | }; |
jrose@1145 | 84 | |
jrose@1474 | 85 | jobject MethodHandles::_raise_exception_method; |
jrose@1474 | 86 | |
jrose@1145 | 87 | #ifdef ASSERT |
jrose@1145 | 88 | bool MethodHandles::spot_check_entry_names() { |
jrose@1145 | 89 | assert(!strcmp(entry_name(_invokestatic_mh), "invokestatic"), ""); |
jrose@1145 | 90 | assert(!strcmp(entry_name(_bound_ref_mh), "bound_ref"), ""); |
jrose@1145 | 91 | assert(!strcmp(entry_name(_adapter_retype_only), "adapter_retype_only"), ""); |
jrose@1145 | 92 | assert(!strcmp(entry_name(_adapter_ricochet), "adapter_ricochet"), ""); |
jrose@1145 | 93 | assert(!strcmp(entry_name(_adapter_opt_unboxi), "adapter_ref_to_prim/unboxi"), ""); |
jrose@1145 | 94 | return true; |
jrose@1145 | 95 | } |
jrose@1145 | 96 | #endif |
jrose@1145 | 97 | |
jrose@1145 | 98 | void MethodHandles::set_enabled(bool z) { |
jrose@1145 | 99 | if (_enabled != z) { |
jrose@1145 | 100 | guarantee(z && EnableMethodHandles, "can only enable once, and only if -XX:+EnableMethodHandles"); |
jrose@1145 | 101 | _enabled = z; |
jrose@1145 | 102 | } |
jrose@1145 | 103 | } |
jrose@1145 | 104 | |
jrose@1145 | 105 | // Note: A method which does not have a TRAPS argument cannot block in the GC |
jrose@1145 | 106 | // or throw exceptions. Such methods are used in this file to do something quick |
jrose@1145 | 107 | // and local, like parse a data structure. For speed, such methods work on plain |
jrose@1145 | 108 | // oops, not handles. Trapping methods uniformly operate on handles. |
jrose@1145 | 109 | |
jrose@1145 | 110 | methodOop MethodHandles::decode_vmtarget(oop vmtarget, int vmindex, oop mtype, |
jrose@1145 | 111 | klassOop& receiver_limit_result, int& decode_flags_result) { |
jrose@1145 | 112 | if (vmtarget == NULL) return NULL; |
jrose@1145 | 113 | assert(methodOopDesc::nonvirtual_vtable_index < 0, "encoding"); |
jrose@1145 | 114 | if (vmindex < 0) { |
jrose@1145 | 115 | // this DMH performs no dispatch; it is directly bound to a methodOop |
jrose@1145 | 116 | // A MemberName may either be directly bound to a methodOop, |
jrose@1145 | 117 | // or it may use the klass/index form; both forms mean the same thing. |
jrose@1145 | 118 | methodOop m = decode_methodOop(methodOop(vmtarget), decode_flags_result); |
jrose@1145 | 119 | if ((decode_flags_result & _dmf_has_receiver) != 0 |
jrose@1145 | 120 | && java_dyn_MethodType::is_instance(mtype)) { |
jrose@1145 | 121 | // Extract receiver type restriction from mtype.ptypes[0]. |
jrose@1145 | 122 | objArrayOop ptypes = java_dyn_MethodType::ptypes(mtype); |
jrose@1145 | 123 | oop ptype0 = (ptypes == NULL || ptypes->length() < 1) ? oop(NULL) : ptypes->obj_at(0); |
jrose@1145 | 124 | if (java_lang_Class::is_instance(ptype0)) |
jrose@1145 | 125 | receiver_limit_result = java_lang_Class::as_klassOop(ptype0); |
jrose@1145 | 126 | } |
jrose@1145 | 127 | if (vmindex == methodOopDesc::nonvirtual_vtable_index) { |
jrose@1145 | 128 | // this DMH can be an "invokespecial" version |
jrose@1145 | 129 | decode_flags_result &= ~_dmf_does_dispatch; |
jrose@1145 | 130 | } else { |
jrose@1145 | 131 | assert(vmindex == methodOopDesc::invalid_vtable_index, "random vmindex?"); |
jrose@1145 | 132 | } |
jrose@1145 | 133 | return m; |
jrose@1145 | 134 | } else { |
twisti@1573 | 135 | assert(vmtarget->is_klass(), "must be class or interface"); |
jrose@1145 | 136 | decode_flags_result |= MethodHandles::_dmf_does_dispatch; |
twisti@1573 | 137 | decode_flags_result |= MethodHandles::_dmf_has_receiver; |
jrose@1145 | 138 | receiver_limit_result = (klassOop)vmtarget; |
jrose@1145 | 139 | Klass* tk = Klass::cast((klassOop)vmtarget); |
jrose@1145 | 140 | if (tk->is_interface()) { |
jrose@1145 | 141 | // an itable linkage is <interface, itable index> |
jrose@1145 | 142 | decode_flags_result |= MethodHandles::_dmf_from_interface; |
jrose@1145 | 143 | return klassItable::method_for_itable_index((klassOop)vmtarget, vmindex); |
jrose@1145 | 144 | } else { |
jrose@1145 | 145 | if (!tk->oop_is_instance()) |
jrose@1145 | 146 | tk = instanceKlass::cast(SystemDictionary::object_klass()); |
jrose@1145 | 147 | return ((instanceKlass*)tk)->method_at_vtable(vmindex); |
jrose@1145 | 148 | } |
jrose@1145 | 149 | } |
jrose@1145 | 150 | } |
jrose@1145 | 151 | |
jrose@1145 | 152 | // MemberName and DirectMethodHandle have the same linkage to the JVM internals. |
jrose@1145 | 153 | // (MemberName is the non-operational name used for queries and setup.) |
jrose@1145 | 154 | |
jrose@1145 | 155 | methodOop MethodHandles::decode_DirectMethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) { |
jrose@1145 | 156 | oop vmtarget = sun_dyn_DirectMethodHandle::vmtarget(mh); |
jrose@1145 | 157 | int vmindex = sun_dyn_DirectMethodHandle::vmindex(mh); |
jrose@1145 | 158 | oop mtype = sun_dyn_DirectMethodHandle::type(mh); |
jrose@1145 | 159 | return decode_vmtarget(vmtarget, vmindex, mtype, receiver_limit_result, decode_flags_result); |
jrose@1145 | 160 | } |
jrose@1145 | 161 | |
jrose@1145 | 162 | methodOop MethodHandles::decode_BoundMethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) { |
jrose@1474 | 163 | assert(sun_dyn_BoundMethodHandle::is_instance(mh), ""); |
jrose@1474 | 164 | assert(mh->klass() != SystemDictionary::AdapterMethodHandle_klass(), ""); |
jrose@1145 | 165 | for (oop bmh = mh;;) { |
jrose@1145 | 166 | // Bound MHs can be stacked to bind several arguments. |
jrose@1145 | 167 | oop target = java_dyn_MethodHandle::vmtarget(bmh); |
jrose@1145 | 168 | if (target == NULL) return NULL; |
jrose@1145 | 169 | decode_flags_result |= MethodHandles::_dmf_binds_argument; |
jrose@1145 | 170 | klassOop tk = target->klass(); |
jrose@1145 | 171 | if (tk == SystemDictionary::BoundMethodHandle_klass()) { |
jrose@1145 | 172 | bmh = target; |
jrose@1145 | 173 | continue; |
jrose@1145 | 174 | } else { |
jrose@1145 | 175 | if (java_dyn_MethodHandle::is_subclass(tk)) { |
jrose@1145 | 176 | //assert(tk == SystemDictionary::DirectMethodHandle_klass(), "end of BMH chain must be DMH"); |
jrose@1145 | 177 | return decode_MethodHandle(target, receiver_limit_result, decode_flags_result); |
jrose@1145 | 178 | } else { |
jrose@1145 | 179 | // Optimized case: binding a receiver to a non-dispatched DMH |
jrose@1145 | 180 | // short-circuits directly to the methodOop. |
jrose@1474 | 181 | // (It might be another argument besides a receiver also.) |
jrose@1145 | 182 | assert(target->is_method(), "must be a simple method"); |
twisti@1573 | 183 | decode_flags_result |= MethodHandles::_dmf_binds_method; |
jrose@1145 | 184 | methodOop m = (methodOop) target; |
twisti@1573 | 185 | if (!m->is_static()) |
twisti@1573 | 186 | decode_flags_result |= MethodHandles::_dmf_has_receiver; |
jrose@1145 | 187 | return m; |
jrose@1145 | 188 | } |
jrose@1145 | 189 | } |
jrose@1145 | 190 | } |
jrose@1145 | 191 | } |
jrose@1145 | 192 | |
jrose@1145 | 193 | methodOop MethodHandles::decode_AdapterMethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) { |
jrose@1145 | 194 | assert(mh->klass() == SystemDictionary::AdapterMethodHandle_klass(), ""); |
jrose@1145 | 195 | for (oop amh = mh;;) { |
jrose@1145 | 196 | // Adapter MHs can be stacked to convert several arguments. |
jrose@1145 | 197 | int conv_op = adapter_conversion_op(sun_dyn_AdapterMethodHandle::conversion(amh)); |
jrose@1145 | 198 | decode_flags_result |= (_dmf_adapter_lsb << conv_op) & _DMF_ADAPTER_MASK; |
jrose@1145 | 199 | oop target = java_dyn_MethodHandle::vmtarget(amh); |
jrose@1145 | 200 | if (target == NULL) return NULL; |
jrose@1145 | 201 | klassOop tk = target->klass(); |
jrose@1145 | 202 | if (tk == SystemDictionary::AdapterMethodHandle_klass()) { |
jrose@1145 | 203 | amh = target; |
jrose@1145 | 204 | continue; |
jrose@1145 | 205 | } else { |
jrose@1145 | 206 | // must be a BMH (which will bind some more arguments) or a DMH (for the final call) |
jrose@1145 | 207 | return MethodHandles::decode_MethodHandle(target, receiver_limit_result, decode_flags_result); |
jrose@1145 | 208 | } |
jrose@1145 | 209 | } |
jrose@1145 | 210 | } |
jrose@1145 | 211 | |
jrose@1145 | 212 | methodOop MethodHandles::decode_MethodHandle(oop mh, klassOop& receiver_limit_result, int& decode_flags_result) { |
jrose@1145 | 213 | if (mh == NULL) return NULL; |
jrose@1145 | 214 | klassOop mhk = mh->klass(); |
jrose@1145 | 215 | assert(java_dyn_MethodHandle::is_subclass(mhk), "must be a MethodHandle"); |
jrose@1145 | 216 | if (mhk == SystemDictionary::DirectMethodHandle_klass()) { |
jrose@1145 | 217 | return decode_DirectMethodHandle(mh, receiver_limit_result, decode_flags_result); |
jrose@1145 | 218 | } else if (mhk == SystemDictionary::BoundMethodHandle_klass()) { |
jrose@1145 | 219 | return decode_BoundMethodHandle(mh, receiver_limit_result, decode_flags_result); |
jrose@1145 | 220 | } else if (mhk == SystemDictionary::AdapterMethodHandle_klass()) { |
jrose@1145 | 221 | return decode_AdapterMethodHandle(mh, receiver_limit_result, decode_flags_result); |
jrose@1474 | 222 | } else if (sun_dyn_BoundMethodHandle::is_subclass(mhk)) { |
jrose@1474 | 223 | // could be a JavaMethodHandle (but not an adapter MH) |
jrose@1474 | 224 | return decode_BoundMethodHandle(mh, receiver_limit_result, decode_flags_result); |
jrose@1145 | 225 | } else { |
jrose@1145 | 226 | assert(false, "cannot parse this MH"); |
jrose@1145 | 227 | return NULL; // random MH? |
jrose@1145 | 228 | } |
jrose@1145 | 229 | } |
jrose@1145 | 230 | |
jrose@1145 | 231 | methodOop MethodHandles::decode_methodOop(methodOop m, int& decode_flags_result) { |
jrose@1145 | 232 | assert(m->is_method(), ""); |
jrose@1145 | 233 | if (m->is_static()) { |
jrose@1145 | 234 | // check that signature begins '(L' or '([' (not '(I', '()', etc.) |
jrose@1145 | 235 | symbolOop sig = m->signature(); |
jrose@1145 | 236 | BasicType recv_bt = char2type(sig->byte_at(1)); |
jrose@1145 | 237 | // Note: recv_bt might be T_ILLEGAL if byte_at(2) is ')' |
jrose@1145 | 238 | assert(sig->byte_at(0) == '(', "must be method sig"); |
twisti@1573 | 239 | // if (recv_bt == T_OBJECT || recv_bt == T_ARRAY) |
twisti@1573 | 240 | // decode_flags_result |= _dmf_has_receiver; |
jrose@1145 | 241 | } else { |
jrose@1145 | 242 | // non-static method |
jrose@1145 | 243 | decode_flags_result |= _dmf_has_receiver; |
jrose@1145 | 244 | if (!m->can_be_statically_bound() && !m->is_initializer()) { |
jrose@1145 | 245 | decode_flags_result |= _dmf_does_dispatch; |
jrose@1145 | 246 | if (Klass::cast(m->method_holder())->is_interface()) |
jrose@1145 | 247 | decode_flags_result |= _dmf_from_interface; |
jrose@1145 | 248 | } |
jrose@1145 | 249 | } |
jrose@1145 | 250 | return m; |
jrose@1145 | 251 | } |
jrose@1145 | 252 | |
jrose@1145 | 253 | |
jrose@1145 | 254 | // A trusted party is handing us a cookie to determine a method. |
jrose@1145 | 255 | // Let's boil it down to the method oop they really want. |
jrose@1145 | 256 | methodOop MethodHandles::decode_method(oop x, klassOop& receiver_limit_result, int& decode_flags_result) { |
jrose@1145 | 257 | decode_flags_result = 0; |
jrose@1145 | 258 | receiver_limit_result = NULL; |
jrose@1145 | 259 | klassOop xk = x->klass(); |
jrose@1145 | 260 | if (xk == Universe::methodKlassObj()) { |
jrose@1145 | 261 | return decode_methodOop((methodOop) x, decode_flags_result); |
jrose@1145 | 262 | } else if (xk == SystemDictionary::MemberName_klass()) { |
jrose@1145 | 263 | // Note: This only works if the MemberName has already been resolved. |
jrose@1145 | 264 | return decode_MemberName(x, receiver_limit_result, decode_flags_result); |
jrose@1145 | 265 | } else if (java_dyn_MethodHandle::is_subclass(xk)) { |
jrose@1145 | 266 | return decode_MethodHandle(x, receiver_limit_result, decode_flags_result); |
jrose@1145 | 267 | } else if (xk == SystemDictionary::reflect_method_klass()) { |
jrose@1145 | 268 | oop clazz = java_lang_reflect_Method::clazz(x); |
jrose@1145 | 269 | int slot = java_lang_reflect_Method::slot(x); |
jrose@1145 | 270 | klassOop k = java_lang_Class::as_klassOop(clazz); |
jrose@1145 | 271 | if (k != NULL && Klass::cast(k)->oop_is_instance()) |
jrose@1145 | 272 | return decode_methodOop(instanceKlass::cast(k)->method_with_idnum(slot), |
jrose@1145 | 273 | decode_flags_result); |
jrose@1145 | 274 | } else if (xk == SystemDictionary::reflect_constructor_klass()) { |
jrose@1145 | 275 | oop clazz = java_lang_reflect_Constructor::clazz(x); |
jrose@1145 | 276 | int slot = java_lang_reflect_Constructor::slot(x); |
jrose@1145 | 277 | klassOop k = java_lang_Class::as_klassOop(clazz); |
jrose@1145 | 278 | if (k != NULL && Klass::cast(k)->oop_is_instance()) |
jrose@1145 | 279 | return decode_methodOop(instanceKlass::cast(k)->method_with_idnum(slot), |
jrose@1145 | 280 | decode_flags_result); |
jrose@1145 | 281 | } else { |
jrose@1145 | 282 | // unrecognized object |
jrose@1145 | 283 | assert(!x->is_method(), "already checked"); |
jrose@1145 | 284 | assert(!sun_dyn_MemberName::is_instance(x), "already checked"); |
jrose@1145 | 285 | } |
jrose@1145 | 286 | return NULL; |
jrose@1145 | 287 | } |
jrose@1145 | 288 | |
jrose@1145 | 289 | |
jrose@1145 | 290 | int MethodHandles::decode_MethodHandle_stack_pushes(oop mh) { |
jrose@1145 | 291 | if (mh->klass() == SystemDictionary::DirectMethodHandle_klass()) |
jrose@1145 | 292 | return 0; // no push/pop |
jrose@1145 | 293 | int this_vmslots = java_dyn_MethodHandle::vmslots(mh); |
jrose@1145 | 294 | int last_vmslots = 0; |
jrose@1145 | 295 | oop last_mh = mh; |
jrose@1145 | 296 | for (;;) { |
jrose@1145 | 297 | oop target = java_dyn_MethodHandle::vmtarget(last_mh); |
jrose@1145 | 298 | if (target->klass() == SystemDictionary::DirectMethodHandle_klass()) { |
jrose@1145 | 299 | last_vmslots = java_dyn_MethodHandle::vmslots(target); |
jrose@1145 | 300 | break; |
jrose@1145 | 301 | } else if (!java_dyn_MethodHandle::is_instance(target)) { |
jrose@1145 | 302 | // might be klass or method |
jrose@1145 | 303 | assert(target->is_method(), "must get here with a direct ref to method"); |
jrose@1145 | 304 | last_vmslots = methodOop(target)->size_of_parameters(); |
jrose@1145 | 305 | break; |
jrose@1145 | 306 | } |
jrose@1145 | 307 | last_mh = target; |
jrose@1145 | 308 | } |
jrose@1145 | 309 | // If I am called with fewer VM slots than my ultimate callee, |
jrose@1145 | 310 | // it must be that I push the additionally needed slots. |
jrose@1145 | 311 | // Likewise if am called with more VM slots, I will pop them. |
jrose@1145 | 312 | return (last_vmslots - this_vmslots); |
jrose@1145 | 313 | } |
jrose@1145 | 314 | |
jrose@1145 | 315 | |
jrose@1145 | 316 | // MemberName support |
jrose@1145 | 317 | |
jrose@1145 | 318 | // import sun_dyn_MemberName.* |
jrose@1145 | 319 | enum { |
jrose@1145 | 320 | IS_METHOD = sun_dyn_MemberName::MN_IS_METHOD, |
jrose@1145 | 321 | IS_CONSTRUCTOR = sun_dyn_MemberName::MN_IS_CONSTRUCTOR, |
jrose@1145 | 322 | IS_FIELD = sun_dyn_MemberName::MN_IS_FIELD, |
jrose@1145 | 323 | IS_TYPE = sun_dyn_MemberName::MN_IS_TYPE, |
jrose@1145 | 324 | SEARCH_SUPERCLASSES = sun_dyn_MemberName::MN_SEARCH_SUPERCLASSES, |
jrose@1145 | 325 | SEARCH_INTERFACES = sun_dyn_MemberName::MN_SEARCH_INTERFACES, |
jrose@1145 | 326 | ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE, |
jrose@1145 | 327 | VM_INDEX_UNINITIALIZED = sun_dyn_MemberName::VM_INDEX_UNINITIALIZED |
jrose@1145 | 328 | }; |
jrose@1145 | 329 | |
jrose@1145 | 330 | void MethodHandles::init_MemberName(oop mname_oop, oop target_oop) { |
jrose@1145 | 331 | if (target_oop->klass() == SystemDictionary::reflect_field_klass()) { |
jrose@1145 | 332 | oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder() |
jrose@1145 | 333 | int slot = java_lang_reflect_Field::slot(target_oop); // fd.index() |
jrose@1145 | 334 | int mods = java_lang_reflect_Field::modifiers(target_oop); |
jrose@1145 | 335 | klassOop k = java_lang_Class::as_klassOop(clazz); |
jrose@1145 | 336 | int offset = instanceKlass::cast(k)->offset_from_fields(slot); |
jrose@1145 | 337 | init_MemberName(mname_oop, k, accessFlags_from(mods), offset); |
jrose@1145 | 338 | } else { |
jrose@1145 | 339 | int decode_flags = 0; klassOop receiver_limit = NULL; |
jrose@1145 | 340 | methodOop m = MethodHandles::decode_method(target_oop, |
jrose@1145 | 341 | receiver_limit, decode_flags); |
jrose@1145 | 342 | bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0); |
jrose@1145 | 343 | init_MemberName(mname_oop, m, do_dispatch); |
jrose@1145 | 344 | } |
jrose@1145 | 345 | } |
jrose@1145 | 346 | |
jrose@1145 | 347 | void MethodHandles::init_MemberName(oop mname_oop, methodOop m, bool do_dispatch) { |
jrose@1145 | 348 | int flags = ((m->is_initializer() ? IS_CONSTRUCTOR : IS_METHOD) |
jrose@1145 | 349 | | (jushort)( m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS )); |
jrose@1145 | 350 | oop vmtarget = m; |
jrose@1145 | 351 | int vmindex = methodOopDesc::invalid_vtable_index; // implies no info yet |
jrose@1145 | 352 | if (!do_dispatch || (flags & IS_CONSTRUCTOR) || m->can_be_statically_bound()) |
jrose@1145 | 353 | vmindex = methodOopDesc::nonvirtual_vtable_index; // implies never any dispatch |
jrose@1145 | 354 | assert(vmindex != VM_INDEX_UNINITIALIZED, "Java sentinel value"); |
jrose@1145 | 355 | sun_dyn_MemberName::set_vmtarget(mname_oop, vmtarget); |
jrose@1145 | 356 | sun_dyn_MemberName::set_vmindex(mname_oop, vmindex); |
jrose@1145 | 357 | sun_dyn_MemberName::set_flags(mname_oop, flags); |
jrose@1145 | 358 | } |
jrose@1145 | 359 | |
jrose@1145 | 360 | void MethodHandles::init_MemberName(oop mname_oop, klassOop field_holder, AccessFlags mods, int offset) { |
jrose@1145 | 361 | int flags = (IS_FIELD | (jushort)( mods.as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS )); |
jrose@1145 | 362 | oop vmtarget = field_holder; |
jrose@1145 | 363 | int vmindex = offset; // implies no info yet |
jrose@1145 | 364 | assert(vmindex != VM_INDEX_UNINITIALIZED, "bad alias on vmindex"); |
jrose@1145 | 365 | sun_dyn_MemberName::set_vmtarget(mname_oop, vmtarget); |
jrose@1145 | 366 | sun_dyn_MemberName::set_vmindex(mname_oop, vmindex); |
jrose@1145 | 367 | sun_dyn_MemberName::set_flags(mname_oop, flags); |
jrose@1145 | 368 | } |
jrose@1145 | 369 | |
jrose@1145 | 370 | |
jrose@1145 | 371 | methodOop MethodHandles::decode_MemberName(oop mname, klassOop& receiver_limit_result, int& decode_flags_result) { |
jrose@1145 | 372 | int flags = sun_dyn_MemberName::flags(mname); |
jrose@1145 | 373 | if ((flags & (IS_METHOD | IS_CONSTRUCTOR)) == 0) return NULL; // not invocable |
jrose@1145 | 374 | oop vmtarget = sun_dyn_MemberName::vmtarget(mname); |
jrose@1145 | 375 | int vmindex = sun_dyn_MemberName::vmindex(mname); |
jrose@1145 | 376 | if (vmindex == VM_INDEX_UNINITIALIZED) return NULL; // not resolved |
jrose@1474 | 377 | methodOop m = decode_vmtarget(vmtarget, vmindex, NULL, receiver_limit_result, decode_flags_result); |
jrose@1474 | 378 | oop clazz = sun_dyn_MemberName::clazz(mname); |
jrose@1474 | 379 | if (clazz != NULL && java_lang_Class::is_instance(clazz)) { |
jrose@1474 | 380 | klassOop klass = java_lang_Class::as_klassOop(clazz); |
jrose@1474 | 381 | if (klass != NULL) receiver_limit_result = klass; |
jrose@1474 | 382 | } |
jrose@1474 | 383 | return m; |
jrose@1145 | 384 | } |
jrose@1145 | 385 | |
jrose@1145 | 386 | // An unresolved member name is a mere symbolic reference. |
jrose@1145 | 387 | // Resolving it plants a vmtarget/vmindex in it, |
jrose@1145 | 388 | // which refers dirctly to JVM internals. |
jrose@1145 | 389 | void MethodHandles::resolve_MemberName(Handle mname, TRAPS) { |
jrose@1145 | 390 | assert(sun_dyn_MemberName::is_instance(mname()), ""); |
jrose@1145 | 391 | #ifdef ASSERT |
jrose@1145 | 392 | // If this assert throws, renegotiate the sentinel value used by the Java code, |
jrose@1145 | 393 | // so that it is distinct from any valid vtable index value, and any special |
jrose@1145 | 394 | // values defined in methodOopDesc::VtableIndexFlag. |
jrose@1145 | 395 | // The point of the slop is to give the Java code and the JVM some room |
jrose@1145 | 396 | // to independently specify sentinel values. |
jrose@1145 | 397 | const int sentinel_slop = 10; |
jrose@1145 | 398 | const int sentinel_limit = methodOopDesc::highest_unused_vtable_index_value - sentinel_slop; |
jrose@1145 | 399 | assert(VM_INDEX_UNINITIALIZED < sentinel_limit, "Java sentinel != JVM sentinels"); |
jrose@1145 | 400 | #endif |
jrose@1145 | 401 | if (sun_dyn_MemberName::vmindex(mname()) != VM_INDEX_UNINITIALIZED) |
jrose@1145 | 402 | return; // already resolved |
jrose@1145 | 403 | oop defc_oop = sun_dyn_MemberName::clazz(mname()); |
jrose@1145 | 404 | oop name_str = sun_dyn_MemberName::name(mname()); |
jrose@1145 | 405 | oop type_str = sun_dyn_MemberName::type(mname()); |
jrose@1145 | 406 | int flags = sun_dyn_MemberName::flags(mname()); |
jrose@1145 | 407 | |
jrose@1145 | 408 | if (defc_oop == NULL || name_str == NULL || type_str == NULL) { |
jrose@1145 | 409 | THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to resolve"); |
jrose@1145 | 410 | } |
jrose@1145 | 411 | klassOop defc_klassOop = java_lang_Class::as_klassOop(defc_oop); |
jrose@1145 | 412 | defc_oop = NULL; // safety |
jrose@1145 | 413 | if (defc_klassOop == NULL) return; // a primitive; no resolution possible |
jrose@1145 | 414 | if (!Klass::cast(defc_klassOop)->oop_is_instance()) { |
jrose@1145 | 415 | if (!Klass::cast(defc_klassOop)->oop_is_array()) return; |
jrose@1145 | 416 | defc_klassOop = SystemDictionary::object_klass(); |
jrose@1145 | 417 | } |
jrose@1145 | 418 | instanceKlassHandle defc(THREAD, defc_klassOop); |
jrose@1145 | 419 | defc_klassOop = NULL; // safety |
jrose@1145 | 420 | if (defc.is_null()) { |
jrose@1145 | 421 | THROW_MSG(vmSymbols::java_lang_InternalError(), "primitive class"); |
jrose@1145 | 422 | } |
jrose@1145 | 423 | defc->link_class(CHECK); |
jrose@1145 | 424 | |
jrose@1145 | 425 | // convert the external string name to an internal symbol |
jrose@1145 | 426 | symbolHandle name(THREAD, java_lang_String::as_symbol_or_null(name_str)); |
jrose@1145 | 427 | if (name.is_null()) return; // no such name |
jrose@1145 | 428 | name_str = NULL; // safety |
jrose@1145 | 429 | |
jrose@1145 | 430 | // convert the external string or reflective type to an internal signature |
jrose@1145 | 431 | bool force_signature = (name() == vmSymbols::invoke_name()); |
jrose@1145 | 432 | symbolHandle type; { |
jrose@1145 | 433 | symbolOop type_sym = NULL; |
jrose@1145 | 434 | if (java_dyn_MethodType::is_instance(type_str)) { |
jrose@1145 | 435 | type_sym = java_dyn_MethodType::as_signature(type_str, force_signature, CHECK); |
jrose@1145 | 436 | } else if (java_lang_Class::is_instance(type_str)) { |
jrose@1145 | 437 | type_sym = java_lang_Class::as_signature(type_str, force_signature, CHECK); |
jrose@1145 | 438 | } else if (java_lang_String::is_instance(type_str)) { |
jrose@1145 | 439 | if (force_signature) { |
jrose@1145 | 440 | type = java_lang_String::as_symbol(type_str, CHECK); |
jrose@1145 | 441 | } else { |
jrose@1145 | 442 | type_sym = java_lang_String::as_symbol_or_null(type_str); |
jrose@1145 | 443 | } |
jrose@1145 | 444 | } else { |
jrose@1145 | 445 | THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized type"); |
jrose@1145 | 446 | } |
jrose@1145 | 447 | if (type_sym != NULL) |
jrose@1145 | 448 | type = symbolHandle(THREAD, type_sym); |
jrose@1145 | 449 | } |
jrose@1145 | 450 | if (type.is_null()) return; // no such signature exists in the VM |
jrose@1145 | 451 | type_str = NULL; // safety |
jrose@1145 | 452 | |
jrose@1145 | 453 | // Time to do the lookup. |
jrose@1145 | 454 | switch (flags & ALL_KINDS) { |
jrose@1145 | 455 | case IS_METHOD: |
jrose@1145 | 456 | { |
jrose@1145 | 457 | CallInfo result; |
jrose@1145 | 458 | { |
jrose@1145 | 459 | EXCEPTION_MARK; |
jrose@1145 | 460 | if ((flags & JVM_ACC_STATIC) != 0) { |
jrose@1145 | 461 | LinkResolver::resolve_static_call(result, |
jrose@1145 | 462 | defc, name, type, KlassHandle(), false, false, THREAD); |
jrose@1145 | 463 | } else if (defc->is_interface()) { |
jrose@1145 | 464 | LinkResolver::resolve_interface_call(result, Handle(), defc, |
jrose@1145 | 465 | defc, name, type, KlassHandle(), false, false, THREAD); |
jrose@1145 | 466 | } else { |
jrose@1145 | 467 | LinkResolver::resolve_virtual_call(result, Handle(), defc, |
jrose@1145 | 468 | defc, name, type, KlassHandle(), false, false, THREAD); |
jrose@1145 | 469 | } |
jrose@1145 | 470 | if (HAS_PENDING_EXCEPTION) { |
jrose@1145 | 471 | CLEAR_PENDING_EXCEPTION; |
jrose@1145 | 472 | return; |
jrose@1145 | 473 | } |
jrose@1145 | 474 | } |
jrose@1145 | 475 | methodHandle m = result.resolved_method(); |
jrose@1145 | 476 | oop vmtarget = NULL; |
jrose@1145 | 477 | int vmindex = methodOopDesc::nonvirtual_vtable_index; |
jrose@1145 | 478 | if (defc->is_interface()) { |
jrose@1145 | 479 | vmindex = klassItable::compute_itable_index(m()); |
jrose@1145 | 480 | assert(vmindex >= 0, ""); |
jrose@1145 | 481 | } else if (result.has_vtable_index()) { |
jrose@1145 | 482 | vmindex = result.vtable_index(); |
jrose@1145 | 483 | assert(vmindex >= 0, ""); |
jrose@1145 | 484 | } |
jrose@1145 | 485 | assert(vmindex != VM_INDEX_UNINITIALIZED, ""); |
jrose@1145 | 486 | if (vmindex < 0) { |
jrose@1145 | 487 | assert(result.is_statically_bound(), ""); |
jrose@1145 | 488 | vmtarget = m(); |
jrose@1145 | 489 | } else { |
jrose@1145 | 490 | vmtarget = result.resolved_klass()->as_klassOop(); |
jrose@1145 | 491 | } |
jrose@1145 | 492 | int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS); |
jrose@1145 | 493 | sun_dyn_MemberName::set_vmtarget(mname(), vmtarget); |
jrose@1145 | 494 | sun_dyn_MemberName::set_vmindex(mname(), vmindex); |
jrose@1145 | 495 | sun_dyn_MemberName::set_modifiers(mname(), mods); |
jrose@1145 | 496 | DEBUG_ONLY(int junk; klassOop junk2); |
jrose@1145 | 497 | assert(decode_MemberName(mname(), junk2, junk) == result.resolved_method()(), |
jrose@1145 | 498 | "properly stored for later decoding"); |
jrose@1145 | 499 | return; |
jrose@1145 | 500 | } |
jrose@1145 | 501 | case IS_CONSTRUCTOR: |
jrose@1145 | 502 | { |
jrose@1145 | 503 | CallInfo result; |
jrose@1145 | 504 | { |
jrose@1145 | 505 | EXCEPTION_MARK; |
jrose@1145 | 506 | if (name() == vmSymbols::object_initializer_name()) { |
jrose@1145 | 507 | LinkResolver::resolve_special_call(result, |
jrose@1145 | 508 | defc, name, type, KlassHandle(), false, THREAD); |
jrose@1145 | 509 | } else { |
jrose@1145 | 510 | break; // will throw after end of switch |
jrose@1145 | 511 | } |
jrose@1145 | 512 | if (HAS_PENDING_EXCEPTION) { |
jrose@1145 | 513 | CLEAR_PENDING_EXCEPTION; |
jrose@1145 | 514 | return; |
jrose@1145 | 515 | } |
jrose@1145 | 516 | } |
jrose@1145 | 517 | assert(result.is_statically_bound(), ""); |
jrose@1145 | 518 | methodHandle m = result.resolved_method(); |
jrose@1145 | 519 | oop vmtarget = m(); |
jrose@1145 | 520 | int vmindex = methodOopDesc::nonvirtual_vtable_index; |
jrose@1145 | 521 | int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS); |
jrose@1145 | 522 | sun_dyn_MemberName::set_vmtarget(mname(), vmtarget); |
jrose@1145 | 523 | sun_dyn_MemberName::set_vmindex(mname(), vmindex); |
jrose@1145 | 524 | sun_dyn_MemberName::set_modifiers(mname(), mods); |
jrose@1145 | 525 | DEBUG_ONLY(int junk; klassOop junk2); |
jrose@1145 | 526 | assert(decode_MemberName(mname(), junk2, junk) == result.resolved_method()(), |
jrose@1145 | 527 | "properly stored for later decoding"); |
jrose@1145 | 528 | return; |
jrose@1145 | 529 | } |
jrose@1145 | 530 | case IS_FIELD: |
jrose@1145 | 531 | { |
jrose@1145 | 532 | // This is taken from LinkResolver::resolve_field, sans access checks. |
jrose@1145 | 533 | fieldDescriptor fd; // find_field initializes fd if found |
jrose@1145 | 534 | KlassHandle sel_klass(THREAD, instanceKlass::cast(defc())->find_field(name(), type(), &fd)); |
jrose@1145 | 535 | // check if field exists; i.e., if a klass containing the field def has been selected |
jrose@1145 | 536 | if (sel_klass.is_null()) return; |
jrose@1145 | 537 | oop vmtarget = sel_klass->as_klassOop(); |
jrose@1145 | 538 | int vmindex = fd.offset(); |
jrose@1145 | 539 | int mods = (fd.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS); |
jrose@1145 | 540 | if (vmindex == VM_INDEX_UNINITIALIZED) break; // should not happen |
jrose@1145 | 541 | sun_dyn_MemberName::set_vmtarget(mname(), vmtarget); |
jrose@1145 | 542 | sun_dyn_MemberName::set_vmindex(mname(), vmindex); |
jrose@1145 | 543 | sun_dyn_MemberName::set_modifiers(mname(), mods); |
jrose@1145 | 544 | return; |
jrose@1145 | 545 | } |
jrose@1145 | 546 | } |
jrose@1145 | 547 | THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); |
jrose@1145 | 548 | } |
jrose@1145 | 549 | |
jrose@1145 | 550 | // Conversely, a member name which is only initialized from JVM internals |
jrose@1145 | 551 | // may have null defc, name, and type fields. |
jrose@1145 | 552 | // Resolving it plants a vmtarget/vmindex in it, |
jrose@1145 | 553 | // which refers directly to JVM internals. |
jrose@1145 | 554 | void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) { |
jrose@1145 | 555 | assert(sun_dyn_MemberName::is_instance(mname()), ""); |
jrose@1145 | 556 | oop vmtarget = sun_dyn_MemberName::vmtarget(mname()); |
jrose@1145 | 557 | int vmindex = sun_dyn_MemberName::vmindex(mname()); |
jrose@1145 | 558 | if (vmtarget == NULL || vmindex == VM_INDEX_UNINITIALIZED) { |
jrose@1145 | 559 | THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand"); |
jrose@1145 | 560 | } |
jrose@1145 | 561 | |
jrose@1145 | 562 | bool have_defc = (sun_dyn_MemberName::clazz(mname()) != NULL); |
jrose@1145 | 563 | bool have_name = (sun_dyn_MemberName::name(mname()) != NULL); |
jrose@1145 | 564 | bool have_type = (sun_dyn_MemberName::type(mname()) != NULL); |
jrose@1145 | 565 | int flags = sun_dyn_MemberName::flags(mname()); |
jrose@1145 | 566 | |
jrose@1145 | 567 | if (suppress != 0) { |
jrose@1145 | 568 | if (suppress & _suppress_defc) have_defc = true; |
jrose@1145 | 569 | if (suppress & _suppress_name) have_name = true; |
jrose@1145 | 570 | if (suppress & _suppress_type) have_type = true; |
jrose@1145 | 571 | } |
jrose@1145 | 572 | |
jrose@1145 | 573 | if (have_defc && have_name && have_type) return; // nothing needed |
jrose@1145 | 574 | |
jrose@1145 | 575 | switch (flags & ALL_KINDS) { |
jrose@1145 | 576 | case IS_METHOD: |
jrose@1145 | 577 | case IS_CONSTRUCTOR: |
jrose@1145 | 578 | { |
jrose@1145 | 579 | klassOop receiver_limit = NULL; |
jrose@1145 | 580 | int decode_flags = 0; |
jrose@1145 | 581 | methodHandle m(THREAD, decode_vmtarget(vmtarget, vmindex, NULL, |
jrose@1145 | 582 | receiver_limit, decode_flags)); |
jrose@1145 | 583 | if (m.is_null()) break; |
jrose@1145 | 584 | if (!have_defc) { |
jrose@1145 | 585 | klassOop defc = m->method_holder(); |
jrose@1145 | 586 | if (receiver_limit != NULL && receiver_limit != defc |
jrose@1145 | 587 | && Klass::cast(receiver_limit)->is_subtype_of(defc)) |
jrose@1145 | 588 | defc = receiver_limit; |
jrose@1145 | 589 | sun_dyn_MemberName::set_clazz(mname(), Klass::cast(defc)->java_mirror()); |
jrose@1145 | 590 | } |
jrose@1145 | 591 | if (!have_name) { |
jrose@1145 | 592 | //not java_lang_String::create_from_symbol; let's intern member names |
jrose@1145 | 593 | Handle name = StringTable::intern(m->name(), CHECK); |
jrose@1145 | 594 | sun_dyn_MemberName::set_name(mname(), name()); |
jrose@1145 | 595 | } |
jrose@1145 | 596 | if (!have_type) { |
jrose@1145 | 597 | Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK); |
jrose@1145 | 598 | sun_dyn_MemberName::set_type(mname(), type()); |
jrose@1145 | 599 | } |
jrose@1145 | 600 | return; |
jrose@1145 | 601 | } |
jrose@1145 | 602 | case IS_FIELD: |
jrose@1145 | 603 | { |
jrose@1145 | 604 | // This is taken from LinkResolver::resolve_field, sans access checks. |
jrose@1145 | 605 | if (!vmtarget->is_klass()) break; |
jrose@1145 | 606 | if (!Klass::cast((klassOop) vmtarget)->oop_is_instance()) break; |
jrose@1145 | 607 | instanceKlassHandle defc(THREAD, (klassOop) vmtarget); |
jrose@1145 | 608 | bool is_static = ((flags & JVM_ACC_STATIC) != 0); |
jrose@1145 | 609 | fieldDescriptor fd; // find_field initializes fd if found |
jrose@1145 | 610 | if (!defc->find_field_from_offset(vmindex, is_static, &fd)) |
jrose@1145 | 611 | break; // cannot expand |
jrose@1145 | 612 | if (!have_defc) { |
jrose@1145 | 613 | sun_dyn_MemberName::set_clazz(mname(), defc->java_mirror()); |
jrose@1145 | 614 | } |
jrose@1145 | 615 | if (!have_name) { |
jrose@1145 | 616 | //not java_lang_String::create_from_symbol; let's intern member names |
jrose@1145 | 617 | Handle name = StringTable::intern(fd.name(), CHECK); |
jrose@1145 | 618 | sun_dyn_MemberName::set_name(mname(), name()); |
jrose@1145 | 619 | } |
jrose@1145 | 620 | if (!have_type) { |
jrose@1145 | 621 | Handle type = java_lang_String::create_from_symbol(fd.signature(), CHECK); |
jrose@1145 | 622 | sun_dyn_MemberName::set_type(mname(), type()); |
jrose@1145 | 623 | } |
jrose@1145 | 624 | return; |
jrose@1145 | 625 | } |
jrose@1145 | 626 | } |
jrose@1145 | 627 | THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); |
jrose@1145 | 628 | } |
jrose@1145 | 629 | |
jrose@1145 | 630 | int MethodHandles::find_MemberNames(klassOop k, |
jrose@1145 | 631 | symbolOop name, symbolOop sig, |
jrose@1145 | 632 | int mflags, klassOop caller, |
jrose@1145 | 633 | int skip, objArrayOop results) { |
jrose@1145 | 634 | DEBUG_ONLY(No_Safepoint_Verifier nsv); |
jrose@1145 | 635 | // this code contains no safepoints! |
jrose@1145 | 636 | |
jrose@1145 | 637 | // %%% take caller into account! |
jrose@1145 | 638 | |
jrose@1145 | 639 | if (k == NULL || !Klass::cast(k)->oop_is_instance()) return -1; |
jrose@1145 | 640 | |
jrose@1145 | 641 | int rfill = 0, rlimit = results->length(), rskip = skip; |
jrose@1145 | 642 | // overflow measurement: |
jrose@1145 | 643 | int overflow = 0, overflow_limit = MAX2(1000, rlimit); |
jrose@1145 | 644 | |
jrose@1145 | 645 | int match_flags = mflags; |
jrose@1145 | 646 | bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0); |
jrose@1145 | 647 | bool search_intfc = ((match_flags & SEARCH_INTERFACES) != 0); |
jrose@1145 | 648 | bool local_only = !(search_superc | search_intfc); |
jrose@1145 | 649 | bool classes_only = false; |
jrose@1145 | 650 | |
jrose@1145 | 651 | if (name != NULL) { |
jrose@1145 | 652 | if (name->utf8_length() == 0) return 0; // a match is not possible |
jrose@1145 | 653 | } |
jrose@1145 | 654 | if (sig != NULL) { |
jrose@1145 | 655 | if (sig->utf8_length() == 0) return 0; // a match is not possible |
jrose@1145 | 656 | if (sig->byte_at(0) == '(') |
jrose@1145 | 657 | match_flags &= ~(IS_FIELD | IS_TYPE); |
jrose@1145 | 658 | else |
jrose@1145 | 659 | match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD); |
jrose@1145 | 660 | } |
jrose@1145 | 661 | |
jrose@1145 | 662 | if ((match_flags & IS_TYPE) != 0) { |
jrose@1145 | 663 | // NYI, and Core Reflection works quite well for this query |
jrose@1145 | 664 | } |
jrose@1145 | 665 | |
jrose@1145 | 666 | if ((match_flags & IS_FIELD) != 0) { |
jrose@1145 | 667 | for (FieldStream st(k, local_only, !search_intfc); !st.eos(); st.next()) { |
jrose@1145 | 668 | if (name != NULL && st.name() != name) |
jrose@1145 | 669 | continue; |
jrose@1145 | 670 | if (sig != NULL && st.signature() != sig) |
jrose@1145 | 671 | continue; |
jrose@1145 | 672 | // passed the filters |
jrose@1145 | 673 | if (rskip > 0) { |
jrose@1145 | 674 | --rskip; |
jrose@1145 | 675 | } else if (rfill < rlimit) { |
jrose@1145 | 676 | oop result = results->obj_at(rfill++); |
jrose@1145 | 677 | if (!sun_dyn_MemberName::is_instance(result)) |
jrose@1145 | 678 | return -99; // caller bug! |
jrose@1145 | 679 | MethodHandles::init_MemberName(result, st.klass()->as_klassOop(), st.access_flags(), st.offset()); |
jrose@1145 | 680 | } else if (++overflow >= overflow_limit) { |
jrose@1145 | 681 | match_flags = 0; break; // got tired of looking at overflow |
jrose@1145 | 682 | } |
jrose@1145 | 683 | } |
jrose@1145 | 684 | } |
jrose@1145 | 685 | |
jrose@1145 | 686 | if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) { |
jrose@1145 | 687 | // watch out for these guys: |
jrose@1145 | 688 | symbolOop init_name = vmSymbols::object_initializer_name(); |
jrose@1145 | 689 | symbolOop clinit_name = vmSymbols::class_initializer_name(); |
jrose@1145 | 690 | if (name == clinit_name) clinit_name = NULL; // hack for exposing <clinit> |
jrose@1145 | 691 | bool negate_name_test = false; |
jrose@1145 | 692 | // fix name so that it captures the intention of IS_CONSTRUCTOR |
jrose@1145 | 693 | if (!(match_flags & IS_METHOD)) { |
jrose@1145 | 694 | // constructors only |
jrose@1145 | 695 | if (name == NULL) { |
jrose@1145 | 696 | name = init_name; |
jrose@1145 | 697 | } else if (name != init_name) { |
jrose@1145 | 698 | return 0; // no constructors of this method name |
jrose@1145 | 699 | } |
jrose@1145 | 700 | } else if (!(match_flags & IS_CONSTRUCTOR)) { |
jrose@1145 | 701 | // methods only |
jrose@1145 | 702 | if (name == NULL) { |
jrose@1145 | 703 | name = init_name; |
jrose@1145 | 704 | negate_name_test = true; // if we see the name, we *omit* the entry |
jrose@1145 | 705 | } else if (name == init_name) { |
jrose@1145 | 706 | return 0; // no methods of this constructor name |
jrose@1145 | 707 | } |
jrose@1145 | 708 | } else { |
jrose@1145 | 709 | // caller will accept either sort; no need to adjust name |
jrose@1145 | 710 | } |
jrose@1145 | 711 | for (MethodStream st(k, local_only, !search_intfc); !st.eos(); st.next()) { |
jrose@1145 | 712 | methodOop m = st.method(); |
jrose@1145 | 713 | symbolOop m_name = m->name(); |
jrose@1145 | 714 | if (m_name == clinit_name) |
jrose@1145 | 715 | continue; |
jrose@1145 | 716 | if (name != NULL && ((m_name != name) ^ negate_name_test)) |
jrose@1145 | 717 | continue; |
jrose@1145 | 718 | if (sig != NULL && m->signature() != sig) |
jrose@1145 | 719 | continue; |
jrose@1145 | 720 | // passed the filters |
jrose@1145 | 721 | if (rskip > 0) { |
jrose@1145 | 722 | --rskip; |
jrose@1145 | 723 | } else if (rfill < rlimit) { |
jrose@1145 | 724 | oop result = results->obj_at(rfill++); |
jrose@1145 | 725 | if (!sun_dyn_MemberName::is_instance(result)) |
jrose@1145 | 726 | return -99; // caller bug! |
jrose@1145 | 727 | MethodHandles::init_MemberName(result, m, true); |
jrose@1145 | 728 | } else if (++overflow >= overflow_limit) { |
jrose@1145 | 729 | match_flags = 0; break; // got tired of looking at overflow |
jrose@1145 | 730 | } |
jrose@1145 | 731 | } |
jrose@1145 | 732 | } |
jrose@1145 | 733 | |
jrose@1145 | 734 | // return number of elements we at leasted wanted to initialize |
jrose@1145 | 735 | return rfill + overflow; |
jrose@1145 | 736 | } |
jrose@1145 | 737 | |
jrose@1145 | 738 | |
jrose@1145 | 739 | |
jrose@1145 | 740 | |
jrose@1145 | 741 | // Decode the vmtarget field of a method handle. |
jrose@1145 | 742 | // Sanitize out methodOops, klassOops, and any other non-Java data. |
jrose@1145 | 743 | // This is for debugging and reflection. |
jrose@1145 | 744 | oop MethodHandles::encode_target(Handle mh, int format, TRAPS) { |
jrose@1145 | 745 | assert(java_dyn_MethodHandle::is_instance(mh()), "must be a MH"); |
jrose@1145 | 746 | if (format == ETF_HANDLE_OR_METHOD_NAME) { |
jrose@1145 | 747 | oop target = java_dyn_MethodHandle::vmtarget(mh()); |
jrose@1145 | 748 | if (target == NULL) { |
jrose@1145 | 749 | return NULL; // unformed MH |
jrose@1145 | 750 | } |
jrose@1145 | 751 | klassOop tklass = target->klass(); |
jrose@1145 | 752 | if (Klass::cast(tklass)->is_subclass_of(SystemDictionary::object_klass())) { |
jrose@1145 | 753 | return target; // target is another MH (or something else?) |
jrose@1145 | 754 | } |
jrose@1145 | 755 | } |
jrose@1145 | 756 | if (format == ETF_DIRECT_HANDLE) { |
jrose@1145 | 757 | oop target = mh(); |
jrose@1145 | 758 | for (;;) { |
jrose@1145 | 759 | if (target->klass() == SystemDictionary::DirectMethodHandle_klass()) { |
jrose@1145 | 760 | return target; |
jrose@1145 | 761 | } |
jrose@1145 | 762 | if (!java_dyn_MethodHandle::is_instance(target)){ |
jrose@1145 | 763 | return NULL; // unformed MH |
jrose@1145 | 764 | } |
jrose@1145 | 765 | target = java_dyn_MethodHandle::vmtarget(target); |
jrose@1145 | 766 | } |
jrose@1145 | 767 | } |
jrose@1145 | 768 | // cases of metadata in MH.vmtarget: |
jrose@1145 | 769 | // - AMH can have methodOop for static invoke with bound receiver |
jrose@1145 | 770 | // - DMH can have methodOop for static invoke (on variable receiver) |
jrose@1145 | 771 | // - DMH can have klassOop for dispatched (non-static) invoke |
jrose@1145 | 772 | klassOop receiver_limit = NULL; |
jrose@1145 | 773 | int decode_flags = 0; |
jrose@1145 | 774 | methodOop m = decode_MethodHandle(mh(), receiver_limit, decode_flags); |
jrose@1145 | 775 | if (m == NULL) return NULL; |
jrose@1145 | 776 | switch (format) { |
jrose@1145 | 777 | case ETF_REFLECT_METHOD: |
jrose@1145 | 778 | // same as jni_ToReflectedMethod: |
jrose@1145 | 779 | if (m->is_initializer()) { |
jrose@1145 | 780 | return Reflection::new_constructor(m, THREAD); |
jrose@1145 | 781 | } else { |
jrose@1145 | 782 | return Reflection::new_method(m, UseNewReflection, false, THREAD); |
jrose@1145 | 783 | } |
jrose@1145 | 784 | |
jrose@1145 | 785 | case ETF_HANDLE_OR_METHOD_NAME: // method, not handle |
jrose@1145 | 786 | case ETF_METHOD_NAME: |
jrose@1145 | 787 | { |
jrose@1145 | 788 | if (SystemDictionary::MemberName_klass() == NULL) break; |
jrose@1145 | 789 | instanceKlassHandle mname_klass(THREAD, SystemDictionary::MemberName_klass()); |
jrose@1145 | 790 | mname_klass->initialize(CHECK_NULL); |
jrose@1145 | 791 | Handle mname = mname_klass->allocate_instance_handle(CHECK_NULL); |
jrose@1145 | 792 | sun_dyn_MemberName::set_vmindex(mname(), VM_INDEX_UNINITIALIZED); |
jrose@1145 | 793 | bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0); |
jrose@1145 | 794 | init_MemberName(mname(), m, do_dispatch); |
jrose@1145 | 795 | expand_MemberName(mname, 0, CHECK_NULL); |
jrose@1145 | 796 | return mname(); |
jrose@1145 | 797 | } |
jrose@1145 | 798 | } |
jrose@1145 | 799 | |
jrose@1145 | 800 | // Unknown format code. |
jrose@1145 | 801 | char msg[50]; |
jrose@1145 | 802 | jio_snprintf(msg, sizeof(msg), "unknown getTarget format=%d", format); |
jrose@1145 | 803 | THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), msg); |
jrose@1145 | 804 | } |
jrose@1145 | 805 | |
jrose@1474 | 806 | static const char* always_null_names[] = { |
jrose@1474 | 807 | "java/lang/Void", |
jrose@1474 | 808 | "java/lang/Null", |
jrose@1474 | 809 | //"java/lang/Nothing", |
jrose@1474 | 810 | "sun/dyn/empty/Empty", |
jrose@1474 | 811 | NULL |
jrose@1474 | 812 | }; |
jrose@1474 | 813 | |
jrose@1474 | 814 | static bool is_always_null_type(klassOop klass) { |
jrose@1474 | 815 | if (!Klass::cast(klass)->oop_is_instance()) return false; |
jrose@1474 | 816 | instanceKlass* ik = instanceKlass::cast(klass); |
jrose@1474 | 817 | // Must be on the boot class path: |
jrose@1474 | 818 | if (ik->class_loader() != NULL) return false; |
jrose@1474 | 819 | // Check the name. |
jrose@1474 | 820 | symbolOop name = ik->name(); |
jrose@1474 | 821 | for (int i = 0; ; i++) { |
jrose@1474 | 822 | const char* test_name = always_null_names[i]; |
jrose@1474 | 823 | if (test_name == NULL) break; |
twisti@1573 | 824 | if (name->equals(test_name)) |
jrose@1474 | 825 | return true; |
jrose@1474 | 826 | } |
jrose@1474 | 827 | return false; |
jrose@1474 | 828 | } |
jrose@1474 | 829 | |
jrose@1145 | 830 | bool MethodHandles::class_cast_needed(klassOop src, klassOop dst) { |
jrose@1145 | 831 | if (src == dst || dst == SystemDictionary::object_klass()) |
jrose@1145 | 832 | return false; // quickest checks |
jrose@1145 | 833 | Klass* srck = Klass::cast(src); |
jrose@1145 | 834 | Klass* dstk = Klass::cast(dst); |
jrose@1145 | 835 | if (dstk->is_interface()) { |
jrose@1145 | 836 | // interface receivers can safely be viewed as untyped, |
jrose@1145 | 837 | // because interface calls always include a dynamic check |
jrose@1145 | 838 | //dstk = Klass::cast(SystemDictionary::object_klass()); |
jrose@1145 | 839 | return false; |
jrose@1145 | 840 | } |
jrose@1145 | 841 | if (srck->is_interface()) { |
jrose@1145 | 842 | // interface arguments must be viewed as untyped |
jrose@1145 | 843 | //srck = Klass::cast(SystemDictionary::object_klass()); |
jrose@1145 | 844 | return true; |
jrose@1145 | 845 | } |
jrose@1474 | 846 | if (is_always_null_type(src)) { |
jrose@1474 | 847 | // some source types are known to be never instantiated; |
jrose@1474 | 848 | // they represent references which are always null |
jrose@1474 | 849 | // such null references never fail to convert safely |
jrose@1474 | 850 | return false; |
jrose@1474 | 851 | } |
jrose@1145 | 852 | return !srck->is_subclass_of(dstk->as_klassOop()); |
jrose@1145 | 853 | } |
jrose@1145 | 854 | |
jrose@1145 | 855 | static oop object_java_mirror() { |
jrose@1145 | 856 | return Klass::cast(SystemDictionary::object_klass())->java_mirror(); |
jrose@1145 | 857 | } |
jrose@1145 | 858 | |
jrose@1145 | 859 | bool MethodHandles::same_basic_type_for_arguments(BasicType src, |
jrose@1145 | 860 | BasicType dst, |
jrose@1474 | 861 | bool raw, |
jrose@1145 | 862 | bool for_return) { |
jrose@1474 | 863 | if (for_return) { |
jrose@1474 | 864 | // return values can always be forgotten: |
jrose@1474 | 865 | if (dst == T_VOID) return true; |
jrose@1474 | 866 | if (src == T_VOID) return raw && (dst == T_INT); |
jrose@1474 | 867 | // We allow caller to receive a garbage int, which is harmless. |
jrose@1474 | 868 | // This trick is pulled by trusted code (see VerifyType.canPassRaw). |
jrose@1474 | 869 | } |
jrose@1145 | 870 | assert(src != T_VOID && dst != T_VOID, "should not be here"); |
jrose@1145 | 871 | if (src == dst) return true; |
jrose@1145 | 872 | if (type2size[src] != type2size[dst]) return false; |
jrose@1145 | 873 | // allow reinterpretation casts for integral widening |
jrose@1145 | 874 | if (is_subword_type(src)) { // subwords can fit in int or other subwords |
jrose@1145 | 875 | if (dst == T_INT) // any subword fits in an int |
jrose@1145 | 876 | return true; |
jrose@1145 | 877 | if (src == T_BOOLEAN) // boolean fits in any subword |
jrose@1145 | 878 | return is_subword_type(dst); |
jrose@1145 | 879 | if (src == T_BYTE && dst == T_SHORT) |
jrose@1145 | 880 | return true; // remaining case: byte fits in short |
jrose@1145 | 881 | } |
jrose@1145 | 882 | // allow float/fixed reinterpretation casts |
jrose@1145 | 883 | if (src == T_FLOAT) return dst == T_INT; |
jrose@1145 | 884 | if (src == T_INT) return dst == T_FLOAT; |
jrose@1145 | 885 | if (src == T_DOUBLE) return dst == T_LONG; |
jrose@1145 | 886 | if (src == T_LONG) return dst == T_DOUBLE; |
jrose@1145 | 887 | return false; |
jrose@1145 | 888 | } |
jrose@1145 | 889 | |
jrose@1145 | 890 | const char* MethodHandles::check_method_receiver(methodOop m, |
jrose@1145 | 891 | klassOop passed_recv_type) { |
jrose@1145 | 892 | assert(!m->is_static(), "caller resp."); |
jrose@1145 | 893 | if (passed_recv_type == NULL) |
jrose@1145 | 894 | return "receiver type is primitive"; |
jrose@1145 | 895 | if (class_cast_needed(passed_recv_type, m->method_holder())) { |
jrose@1145 | 896 | Klass* formal = Klass::cast(m->method_holder()); |
jrose@1145 | 897 | return SharedRuntime::generate_class_cast_message("receiver type", |
jrose@1145 | 898 | formal->external_name()); |
jrose@1145 | 899 | } |
jrose@1145 | 900 | return NULL; // checks passed |
jrose@1145 | 901 | } |
jrose@1145 | 902 | |
jrose@1145 | 903 | // Verify that m's signature can be called type-safely by a method handle |
jrose@1145 | 904 | // of the given method type 'mtype'. |
jrose@1145 | 905 | // It takes a TRAPS argument because it must perform symbol lookups. |
jrose@1145 | 906 | void MethodHandles::verify_method_signature(methodHandle m, |
jrose@1145 | 907 | Handle mtype, |
jrose@1145 | 908 | int first_ptype_pos, |
jrose@1145 | 909 | KlassHandle insert_ptype, |
jrose@1145 | 910 | TRAPS) { |
jrose@1145 | 911 | objArrayHandle ptypes(THREAD, java_dyn_MethodType::ptypes(mtype())); |
jrose@1145 | 912 | int pnum = first_ptype_pos; |
jrose@1145 | 913 | int pmax = ptypes->length(); |
jrose@1145 | 914 | int mnum = 0; // method argument |
jrose@1145 | 915 | const char* err = NULL; |
jrose@1145 | 916 | for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) { |
jrose@1145 | 917 | oop ptype_oop = NULL; |
jrose@1145 | 918 | if (ss.at_return_type()) { |
jrose@1145 | 919 | if (pnum != pmax) |
jrose@1145 | 920 | { err = "too many arguments"; break; } |
jrose@1145 | 921 | ptype_oop = java_dyn_MethodType::rtype(mtype()); |
jrose@1145 | 922 | } else { |
jrose@1145 | 923 | if (pnum >= pmax) |
jrose@1145 | 924 | { err = "not enough arguments"; break; } |
jrose@1145 | 925 | if (pnum >= 0) |
jrose@1145 | 926 | ptype_oop = ptypes->obj_at(pnum); |
jrose@1145 | 927 | else if (insert_ptype.is_null()) |
jrose@1145 | 928 | ptype_oop = NULL; |
jrose@1145 | 929 | else |
jrose@1145 | 930 | ptype_oop = insert_ptype->java_mirror(); |
jrose@1145 | 931 | pnum += 1; |
jrose@1145 | 932 | mnum += 1; |
jrose@1145 | 933 | } |
jrose@1145 | 934 | klassOop mklass = NULL; |
jrose@1145 | 935 | BasicType mtype = ss.type(); |
jrose@1145 | 936 | if (mtype == T_ARRAY) mtype = T_OBJECT; // fold all refs to T_OBJECT |
jrose@1145 | 937 | if (mtype == T_OBJECT) { |
jrose@1145 | 938 | if (ptype_oop == NULL) { |
jrose@1145 | 939 | // null matches any reference |
jrose@1145 | 940 | continue; |
jrose@1145 | 941 | } |
jrose@1145 | 942 | // If we fail to resolve types at this point, we will throw an error. |
jrose@1145 | 943 | symbolOop name_oop = ss.as_symbol(CHECK); |
jrose@1145 | 944 | symbolHandle name(THREAD, name_oop); |
jrose@1145 | 945 | instanceKlass* mk = instanceKlass::cast(m->method_holder()); |
jrose@1145 | 946 | Handle loader(THREAD, mk->class_loader()); |
jrose@1145 | 947 | Handle domain(THREAD, mk->protection_domain()); |
jrose@1145 | 948 | mklass = SystemDictionary::resolve_or_fail(name, loader, domain, |
jrose@1145 | 949 | true, CHECK); |
jrose@1145 | 950 | } |
jrose@1145 | 951 | if (ptype_oop == NULL) { |
jrose@1145 | 952 | // null does not match any non-reference; use Object to report the error |
jrose@1145 | 953 | ptype_oop = object_java_mirror(); |
jrose@1145 | 954 | } |
jrose@1145 | 955 | klassOop pklass = NULL; |
jrose@1145 | 956 | BasicType ptype = java_lang_Class::as_BasicType(ptype_oop, &pklass); |
jrose@1145 | 957 | if (!ss.at_return_type()) { |
jrose@1145 | 958 | err = check_argument_type_change(ptype, pklass, mtype, mklass, mnum); |
jrose@1145 | 959 | } else { |
jrose@1145 | 960 | err = check_return_type_change(mtype, mklass, ptype, pklass); // note reversal! |
jrose@1145 | 961 | } |
jrose@1145 | 962 | if (err != NULL) break; |
jrose@1145 | 963 | } |
jrose@1145 | 964 | |
jrose@1145 | 965 | if (err != NULL) { |
jrose@1145 | 966 | THROW_MSG(vmSymbols::java_lang_InternalError(), err); |
jrose@1145 | 967 | } |
jrose@1145 | 968 | } |
jrose@1145 | 969 | |
jrose@1145 | 970 | // Main routine for verifying the MethodHandle.type of a proposed |
jrose@1145 | 971 | // direct or bound-direct method handle. |
jrose@1145 | 972 | void MethodHandles::verify_method_type(methodHandle m, |
jrose@1145 | 973 | Handle mtype, |
jrose@1145 | 974 | bool has_bound_recv, |
jrose@1145 | 975 | KlassHandle bound_recv_type, |
jrose@1145 | 976 | TRAPS) { |
jrose@1145 | 977 | bool m_needs_receiver = !m->is_static(); |
jrose@1145 | 978 | |
jrose@1145 | 979 | const char* err = NULL; |
jrose@1145 | 980 | |
jrose@1145 | 981 | int first_ptype_pos = m_needs_receiver ? 1 : 0; |
jrose@1474 | 982 | if (has_bound_recv) { |
jrose@1474 | 983 | first_ptype_pos -= 1; // ptypes do not include the bound argument; start earlier in them |
jrose@1145 | 984 | if (m_needs_receiver && bound_recv_type.is_null()) |
jrose@1145 | 985 | { err = "bound receiver is not an object"; goto die; } |
jrose@1145 | 986 | } |
jrose@1145 | 987 | |
jrose@1145 | 988 | if (m_needs_receiver && err == NULL) { |
jrose@1145 | 989 | objArrayOop ptypes = java_dyn_MethodType::ptypes(mtype()); |
jrose@1145 | 990 | if (ptypes->length() < first_ptype_pos) |
jrose@1145 | 991 | { err = "receiver argument is missing"; goto die; } |
jrose@1474 | 992 | if (has_bound_recv) |
jrose@1145 | 993 | err = check_method_receiver(m(), bound_recv_type->as_klassOop()); |
jrose@1145 | 994 | else |
jrose@1474 | 995 | err = check_method_receiver(m(), java_lang_Class::as_klassOop(ptypes->obj_at(first_ptype_pos-1))); |
jrose@1145 | 996 | if (err != NULL) goto die; |
jrose@1145 | 997 | } |
jrose@1145 | 998 | |
jrose@1145 | 999 | // Check the other arguments for mistypes. |
jrose@1145 | 1000 | verify_method_signature(m, mtype, first_ptype_pos, bound_recv_type, CHECK); |
jrose@1145 | 1001 | return; |
jrose@1145 | 1002 | |
jrose@1145 | 1003 | die: |
jrose@1145 | 1004 | THROW_MSG(vmSymbols::java_lang_InternalError(), err); |
jrose@1145 | 1005 | } |
jrose@1145 | 1006 | |
jrose@1145 | 1007 | void MethodHandles::verify_vmslots(Handle mh, TRAPS) { |
jrose@1145 | 1008 | // Verify vmslots. |
jrose@1145 | 1009 | int check_slots = argument_slot_count(java_dyn_MethodHandle::type(mh())); |
jrose@1145 | 1010 | if (java_dyn_MethodHandle::vmslots(mh()) != check_slots) { |
jrose@1145 | 1011 | THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in BMH"); |
jrose@1145 | 1012 | } |
jrose@1145 | 1013 | } |
jrose@1145 | 1014 | |
jrose@1145 | 1015 | void MethodHandles::verify_vmargslot(Handle mh, int argnum, int argslot, TRAPS) { |
jrose@1145 | 1016 | // Verify that argslot points at the given argnum. |
jrose@1145 | 1017 | int check_slot = argument_slot(java_dyn_MethodHandle::type(mh()), argnum); |
jrose@1145 | 1018 | if (argslot != check_slot || argslot < 0) { |
jrose@1145 | 1019 | const char* fmt = "for argnum of %d, vmargslot is %d, should be %d"; |
jrose@1145 | 1020 | size_t msglen = strlen(fmt) + 3*11 + 1; |
jrose@1145 | 1021 | char* msg = NEW_RESOURCE_ARRAY(char, msglen); |
jrose@1145 | 1022 | jio_snprintf(msg, msglen, fmt, argnum, argslot, check_slot); |
jrose@1145 | 1023 | THROW_MSG(vmSymbols::java_lang_InternalError(), msg); |
jrose@1145 | 1024 | } |
jrose@1145 | 1025 | } |
jrose@1145 | 1026 | |
jrose@1145 | 1027 | // Verify the correspondence between two method types. |
jrose@1145 | 1028 | // Apart from the advertised changes, caller method type X must |
jrose@1145 | 1029 | // be able to invoke the callee method Y type with no violations |
jrose@1145 | 1030 | // of type integrity. |
jrose@1145 | 1031 | // Return NULL if all is well, else a short error message. |
jrose@1145 | 1032 | const char* MethodHandles::check_method_type_change(oop src_mtype, int src_beg, int src_end, |
jrose@1145 | 1033 | int insert_argnum, oop insert_type, |
jrose@1145 | 1034 | int change_argnum, oop change_type, |
jrose@1145 | 1035 | int delete_argnum, |
jrose@1474 | 1036 | oop dst_mtype, int dst_beg, int dst_end, |
jrose@1474 | 1037 | bool raw) { |
jrose@1145 | 1038 | objArrayOop src_ptypes = java_dyn_MethodType::ptypes(src_mtype); |
jrose@1145 | 1039 | objArrayOop dst_ptypes = java_dyn_MethodType::ptypes(dst_mtype); |
jrose@1145 | 1040 | |
jrose@1145 | 1041 | int src_max = src_ptypes->length(); |
jrose@1145 | 1042 | int dst_max = dst_ptypes->length(); |
jrose@1145 | 1043 | |
jrose@1145 | 1044 | if (src_end == -1) src_end = src_max; |
jrose@1145 | 1045 | if (dst_end == -1) dst_end = dst_max; |
jrose@1145 | 1046 | |
jrose@1145 | 1047 | assert(0 <= src_beg && src_beg <= src_end && src_end <= src_max, "oob"); |
jrose@1145 | 1048 | assert(0 <= dst_beg && dst_beg <= dst_end && dst_end <= dst_max, "oob"); |
jrose@1145 | 1049 | |
jrose@1145 | 1050 | // pending actions; set to -1 when done: |
jrose@1145 | 1051 | int ins_idx = insert_argnum, chg_idx = change_argnum, del_idx = delete_argnum; |
jrose@1145 | 1052 | |
jrose@1145 | 1053 | const char* err = NULL; |
jrose@1145 | 1054 | |
jrose@1145 | 1055 | // Walk along each array of parameter types, including a virtual |
jrose@1145 | 1056 | // NULL end marker at the end of each. |
jrose@1145 | 1057 | for (int src_idx = src_beg, dst_idx = dst_beg; |
jrose@1145 | 1058 | (src_idx <= src_end && dst_idx <= dst_end); |
jrose@1145 | 1059 | src_idx++, dst_idx++) { |
jrose@1145 | 1060 | oop src_type = (src_idx == src_end) ? oop(NULL) : src_ptypes->obj_at(src_idx); |
jrose@1145 | 1061 | oop dst_type = (dst_idx == dst_end) ? oop(NULL) : dst_ptypes->obj_at(dst_idx); |
jrose@1145 | 1062 | bool fix_null_src_type = false; |
jrose@1145 | 1063 | |
jrose@1145 | 1064 | // Perform requested edits. |
jrose@1145 | 1065 | if (ins_idx == src_idx) { |
jrose@1145 | 1066 | // note that the inserted guy is never affected by a change or deletion |
jrose@1145 | 1067 | ins_idx = -1; |
jrose@1145 | 1068 | src_type = insert_type; |
jrose@1145 | 1069 | fix_null_src_type = true; |
jrose@1145 | 1070 | --src_idx; // back up to process src type on next loop |
jrose@1145 | 1071 | src_idx = src_end; |
jrose@1145 | 1072 | } else { |
jrose@1145 | 1073 | // note that the changed guy can be immediately deleted |
jrose@1145 | 1074 | if (chg_idx == src_idx) { |
jrose@1145 | 1075 | chg_idx = -1; |
jrose@1145 | 1076 | assert(src_idx < src_end, "oob"); |
jrose@1145 | 1077 | src_type = change_type; |
jrose@1145 | 1078 | fix_null_src_type = true; |
jrose@1145 | 1079 | } |
jrose@1145 | 1080 | if (del_idx == src_idx) { |
jrose@1145 | 1081 | del_idx = -1; |
jrose@1145 | 1082 | assert(src_idx < src_end, "oob"); |
jrose@1145 | 1083 | --dst_idx; |
jrose@1145 | 1084 | continue; // rerun loop after skipping this position |
jrose@1145 | 1085 | } |
jrose@1145 | 1086 | } |
jrose@1145 | 1087 | |
jrose@1145 | 1088 | if (src_type == NULL && fix_null_src_type) |
jrose@1145 | 1089 | // explicit null in this case matches any dest reference |
jrose@1145 | 1090 | src_type = (java_lang_Class::is_primitive(dst_type) ? object_java_mirror() : dst_type); |
jrose@1145 | 1091 | |
jrose@1145 | 1092 | // Compare the two argument types. |
jrose@1145 | 1093 | if (src_type != dst_type) { |
jrose@1145 | 1094 | if (src_type == NULL) return "not enough arguments"; |
jrose@1145 | 1095 | if (dst_type == NULL) return "too many arguments"; |
jrose@1474 | 1096 | err = check_argument_type_change(src_type, dst_type, dst_idx, raw); |
jrose@1145 | 1097 | if (err != NULL) return err; |
jrose@1145 | 1098 | } |
jrose@1145 | 1099 | } |
jrose@1145 | 1100 | |
jrose@1145 | 1101 | // Now compare return types also. |
jrose@1145 | 1102 | oop src_rtype = java_dyn_MethodType::rtype(src_mtype); |
jrose@1145 | 1103 | oop dst_rtype = java_dyn_MethodType::rtype(dst_mtype); |
jrose@1145 | 1104 | if (src_rtype != dst_rtype) { |
jrose@1474 | 1105 | err = check_return_type_change(dst_rtype, src_rtype, raw); // note reversal! |
jrose@1145 | 1106 | if (err != NULL) return err; |
jrose@1145 | 1107 | } |
jrose@1145 | 1108 | |
jrose@1145 | 1109 | assert(err == NULL, ""); |
jrose@1145 | 1110 | return NULL; // all is well |
jrose@1145 | 1111 | } |
jrose@1145 | 1112 | |
jrose@1145 | 1113 | |
jrose@1145 | 1114 | const char* MethodHandles::check_argument_type_change(BasicType src_type, |
jrose@1474 | 1115 | klassOop src_klass, |
jrose@1474 | 1116 | BasicType dst_type, |
jrose@1474 | 1117 | klassOop dst_klass, |
jrose@1474 | 1118 | int argnum, |
jrose@1474 | 1119 | bool raw) { |
jrose@1145 | 1120 | const char* err = NULL; |
jrose@1474 | 1121 | bool for_return = (argnum < 0); |
jrose@1145 | 1122 | |
jrose@1145 | 1123 | // just in case: |
jrose@1145 | 1124 | if (src_type == T_ARRAY) src_type = T_OBJECT; |
jrose@1145 | 1125 | if (dst_type == T_ARRAY) dst_type = T_OBJECT; |
jrose@1145 | 1126 | |
jrose@1145 | 1127 | // Produce some nice messages if VerifyMethodHandles is turned on: |
jrose@1474 | 1128 | if (!same_basic_type_for_arguments(src_type, dst_type, raw, for_return)) { |
jrose@1145 | 1129 | if (src_type == T_OBJECT) { |
jrose@1474 | 1130 | if (raw && dst_type == T_INT && is_always_null_type(src_klass)) |
jrose@1474 | 1131 | return NULL; // OK to convert a null pointer to a garbage int |
jrose@1145 | 1132 | err = ((argnum >= 0) |
jrose@1145 | 1133 | ? "type mismatch: passing a %s for method argument #%d, which expects primitive %s" |
jrose@1145 | 1134 | : "type mismatch: returning a %s, but caller expects primitive %s"); |
jrose@1145 | 1135 | } else if (dst_type == T_OBJECT) { |
jrose@1474 | 1136 | err = ((argnum >= 0) |
jrose@1145 | 1137 | ? "type mismatch: passing a primitive %s for method argument #%d, which expects %s" |
jrose@1145 | 1138 | : "type mismatch: returning a primitive %s, but caller expects %s"); |
jrose@1145 | 1139 | } else { |
jrose@1474 | 1140 | err = ((argnum >= 0) |
jrose@1145 | 1141 | ? "type mismatch: passing a %s for method argument #%d, which expects %s" |
jrose@1145 | 1142 | : "type mismatch: returning a %s, but caller expects %s"); |
jrose@1145 | 1143 | } |
jrose@1474 | 1144 | } else if (src_type == T_OBJECT && dst_type == T_OBJECT && |
jrose@1474 | 1145 | class_cast_needed(src_klass, dst_klass)) { |
jrose@1145 | 1146 | if (!class_cast_needed(dst_klass, src_klass)) { |
jrose@1474 | 1147 | if (raw) |
jrose@1474 | 1148 | return NULL; // reverse cast is OK; the MH target is trusted to enforce it |
jrose@1474 | 1149 | err = ((argnum >= 0) |
jrose@1145 | 1150 | ? "cast required: passing a %s for method argument #%d, which expects %s" |
jrose@1145 | 1151 | : "cast required: returning a %s, but caller expects %s"); |
jrose@1145 | 1152 | } else { |
jrose@1474 | 1153 | err = ((argnum >= 0) |
jrose@1145 | 1154 | ? "reference mismatch: passing a %s for method argument #%d, which expects %s" |
jrose@1145 | 1155 | : "reference mismatch: returning a %s, but caller expects %s"); |
jrose@1145 | 1156 | } |
jrose@1145 | 1157 | } else { |
jrose@1145 | 1158 | // passed the obstacle course |
jrose@1145 | 1159 | return NULL; |
jrose@1145 | 1160 | } |
jrose@1145 | 1161 | |
jrose@1145 | 1162 | // format, format, format |
jrose@1145 | 1163 | const char* src_name = type2name(src_type); |
jrose@1145 | 1164 | const char* dst_name = type2name(dst_type); |
jrose@1145 | 1165 | if (src_type == T_OBJECT) src_name = Klass::cast(src_klass)->external_name(); |
jrose@1145 | 1166 | if (dst_type == T_OBJECT) dst_name = Klass::cast(dst_klass)->external_name(); |
jrose@1145 | 1167 | if (src_name == NULL) src_name = "unknown type"; |
jrose@1145 | 1168 | if (dst_name == NULL) dst_name = "unknown type"; |
jrose@1145 | 1169 | |
jrose@1145 | 1170 | size_t msglen = strlen(err) + strlen(src_name) + strlen(dst_name) + (argnum < 10 ? 1 : 11); |
jrose@1145 | 1171 | char* msg = NEW_RESOURCE_ARRAY(char, msglen + 1); |
jrose@1145 | 1172 | if (argnum >= 0) { |
jrose@1145 | 1173 | assert(strstr(err, "%d") != NULL, ""); |
jrose@1145 | 1174 | jio_snprintf(msg, msglen, err, src_name, argnum, dst_name); |
jrose@1145 | 1175 | } else { |
jrose@1145 | 1176 | assert(strstr(err, "%d") == NULL, ""); |
jrose@1145 | 1177 | jio_snprintf(msg, msglen, err, src_name, dst_name); |
jrose@1145 | 1178 | } |
jrose@1145 | 1179 | return msg; |
jrose@1145 | 1180 | } |
jrose@1145 | 1181 | |
jrose@1145 | 1182 | // Compute the depth within the stack of the given argument, i.e., |
jrose@1145 | 1183 | // the combined size of arguments to the right of the given argument. |
jrose@1145 | 1184 | // For the last argument (ptypes.length-1) this will be zero. |
jrose@1145 | 1185 | // For the first argument (0) this will be the size of all |
jrose@1145 | 1186 | // arguments but that one. For the special number -1, this |
jrose@1145 | 1187 | // will be the size of all arguments, including the first. |
jrose@1145 | 1188 | // If the argument is neither -1 nor a valid argument index, |
jrose@1145 | 1189 | // then return a negative number. Otherwise, the result |
jrose@1145 | 1190 | // is in the range [0..vmslots] inclusive. |
jrose@1145 | 1191 | int MethodHandles::argument_slot(oop method_type, int arg) { |
jrose@1145 | 1192 | objArrayOop ptypes = java_dyn_MethodType::ptypes(method_type); |
jrose@1145 | 1193 | int argslot = 0; |
jrose@1145 | 1194 | int len = ptypes->length(); |
jrose@1145 | 1195 | if (arg < -1 || arg >= len) return -99; |
jrose@1145 | 1196 | for (int i = len-1; i > arg; i--) { |
jrose@1145 | 1197 | BasicType bt = java_lang_Class::as_BasicType(ptypes->obj_at(i)); |
jrose@1145 | 1198 | argslot += type2size[bt]; |
jrose@1145 | 1199 | } |
jrose@1145 | 1200 | assert(argument_slot_to_argnum(method_type, argslot) == arg, "inverse works"); |
jrose@1145 | 1201 | return argslot; |
jrose@1145 | 1202 | } |
jrose@1145 | 1203 | |
jrose@1145 | 1204 | // Given a slot number, return the argument number. |
jrose@1145 | 1205 | int MethodHandles::argument_slot_to_argnum(oop method_type, int query_argslot) { |
jrose@1145 | 1206 | objArrayOop ptypes = java_dyn_MethodType::ptypes(method_type); |
jrose@1145 | 1207 | int argslot = 0; |
jrose@1145 | 1208 | int len = ptypes->length(); |
jrose@1145 | 1209 | for (int i = len-1; i >= 0; i--) { |
jrose@1145 | 1210 | if (query_argslot == argslot) return i; |
jrose@1145 | 1211 | BasicType bt = java_lang_Class::as_BasicType(ptypes->obj_at(i)); |
jrose@1145 | 1212 | argslot += type2size[bt]; |
jrose@1145 | 1213 | } |
jrose@1145 | 1214 | // return pseudo-arg deepest in stack: |
jrose@1145 | 1215 | if (query_argslot == argslot) return -1; |
jrose@1145 | 1216 | return -99; // oob slot, or splitting a double-slot arg |
jrose@1145 | 1217 | } |
jrose@1145 | 1218 | |
jrose@1145 | 1219 | methodHandle MethodHandles::dispatch_decoded_method(methodHandle m, |
jrose@1145 | 1220 | KlassHandle receiver_limit, |
jrose@1145 | 1221 | int decode_flags, |
jrose@1145 | 1222 | KlassHandle receiver_klass, |
jrose@1145 | 1223 | TRAPS) { |
jrose@1145 | 1224 | assert((decode_flags & ~_DMF_DIRECT_MASK) == 0, "must be direct method reference"); |
jrose@1145 | 1225 | assert((decode_flags & _dmf_has_receiver) != 0, "must have a receiver or first reference argument"); |
jrose@1145 | 1226 | |
jrose@1145 | 1227 | if (!m->is_static() && |
jrose@1145 | 1228 | (receiver_klass.is_null() || !receiver_klass->is_subtype_of(m->method_holder()))) |
jrose@1145 | 1229 | // given type does not match class of method, or receiver is null! |
jrose@1145 | 1230 | // caller should have checked this, but let's be extra careful... |
jrose@1145 | 1231 | return methodHandle(); |
jrose@1145 | 1232 | |
jrose@1145 | 1233 | if (receiver_limit.not_null() && |
jrose@1145 | 1234 | (receiver_klass.not_null() && !receiver_klass->is_subtype_of(receiver_limit()))) |
jrose@1145 | 1235 | // given type is not limited to the receiver type |
jrose@1145 | 1236 | // note that a null receiver can match any reference value, for a static method |
jrose@1145 | 1237 | return methodHandle(); |
jrose@1145 | 1238 | |
jrose@1145 | 1239 | if (!(decode_flags & MethodHandles::_dmf_does_dispatch)) { |
jrose@1145 | 1240 | // pre-dispatched or static method (null receiver is OK for static) |
jrose@1145 | 1241 | return m; |
jrose@1145 | 1242 | |
jrose@1145 | 1243 | } else if (receiver_klass.is_null()) { |
jrose@1145 | 1244 | // null receiver value; cannot dispatch |
jrose@1145 | 1245 | return methodHandle(); |
jrose@1145 | 1246 | |
jrose@1145 | 1247 | } else if (!(decode_flags & MethodHandles::_dmf_from_interface)) { |
jrose@1145 | 1248 | // perform virtual dispatch |
jrose@1145 | 1249 | int vtable_index = m->vtable_index(); |
jrose@1145 | 1250 | guarantee(vtable_index >= 0, "valid vtable index"); |
jrose@1145 | 1251 | |
jrose@1145 | 1252 | // receiver_klass might be an arrayKlassOop but all vtables start at |
jrose@1145 | 1253 | // the same place. The cast is to avoid virtual call and assertion. |
jrose@1145 | 1254 | // See also LinkResolver::runtime_resolve_virtual_method. |
jrose@1145 | 1255 | instanceKlass* inst = (instanceKlass*)Klass::cast(receiver_klass()); |
jrose@1145 | 1256 | DEBUG_ONLY(inst->verify_vtable_index(vtable_index)); |
jrose@1145 | 1257 | methodOop m_oop = inst->method_at_vtable(vtable_index); |
jrose@1145 | 1258 | return methodHandle(THREAD, m_oop); |
jrose@1145 | 1259 | |
jrose@1145 | 1260 | } else { |
jrose@1145 | 1261 | // perform interface dispatch |
jrose@1145 | 1262 | int itable_index = klassItable::compute_itable_index(m()); |
jrose@1145 | 1263 | guarantee(itable_index >= 0, "valid itable index"); |
jrose@1145 | 1264 | instanceKlass* inst = instanceKlass::cast(receiver_klass()); |
jrose@1145 | 1265 | methodOop m_oop = inst->method_at_itable(m->method_holder(), itable_index, THREAD); |
jrose@1145 | 1266 | return methodHandle(THREAD, m_oop); |
jrose@1145 | 1267 | } |
jrose@1145 | 1268 | } |
jrose@1145 | 1269 | |
jrose@1145 | 1270 | void MethodHandles::verify_DirectMethodHandle(Handle mh, methodHandle m, TRAPS) { |
jrose@1145 | 1271 | // Verify type. |
jrose@1145 | 1272 | Handle mtype(THREAD, java_dyn_MethodHandle::type(mh())); |
jrose@1145 | 1273 | verify_method_type(m, mtype, false, KlassHandle(), CHECK); |
jrose@1145 | 1274 | |
jrose@1145 | 1275 | // Verify vmslots. |
jrose@1145 | 1276 | if (java_dyn_MethodHandle::vmslots(mh()) != m->size_of_parameters()) { |
jrose@1145 | 1277 | THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in DMH"); |
jrose@1145 | 1278 | } |
jrose@1145 | 1279 | } |
jrose@1145 | 1280 | |
jrose@1145 | 1281 | void MethodHandles::init_DirectMethodHandle(Handle mh, methodHandle m, bool do_dispatch, TRAPS) { |
jrose@1145 | 1282 | // Check arguments. |
jrose@1145 | 1283 | if (mh.is_null() || m.is_null() || |
jrose@1145 | 1284 | (!do_dispatch && m->is_abstract())) { |
jrose@1145 | 1285 | THROW(vmSymbols::java_lang_InternalError()); |
jrose@1145 | 1286 | } |
jrose@1145 | 1287 | |
jrose@1145 | 1288 | java_dyn_MethodHandle::init_vmslots(mh()); |
jrose@1145 | 1289 | |
jrose@1145 | 1290 | if (VerifyMethodHandles) { |
jrose@1145 | 1291 | // The privileged code which invokes this routine should not make |
jrose@1145 | 1292 | // a mistake about types, but it's better to verify. |
jrose@1145 | 1293 | verify_DirectMethodHandle(mh, m, CHECK); |
jrose@1145 | 1294 | } |
jrose@1145 | 1295 | |
jrose@1145 | 1296 | // Finally, after safety checks are done, link to the target method. |
jrose@1145 | 1297 | // We will follow the same path as the latter part of |
jrose@1145 | 1298 | // InterpreterRuntime::resolve_invoke(), which first finds the method |
jrose@1145 | 1299 | // and then decides how to populate the constant pool cache entry |
jrose@1145 | 1300 | // that links the interpreter calls to the method. We need the same |
jrose@1145 | 1301 | // bits, and will use the same calling sequence code. |
jrose@1145 | 1302 | |
jrose@1145 | 1303 | int vmindex = methodOopDesc::garbage_vtable_index; |
jrose@1145 | 1304 | oop vmtarget = NULL; |
jrose@1145 | 1305 | |
jrose@1145 | 1306 | instanceKlass::cast(m->method_holder())->link_class(CHECK); |
jrose@1145 | 1307 | |
jrose@1145 | 1308 | MethodHandleEntry* me = NULL; |
jrose@1145 | 1309 | if (do_dispatch && Klass::cast(m->method_holder())->is_interface()) { |
jrose@1145 | 1310 | // We are simulating an invokeinterface instruction. |
jrose@1145 | 1311 | // (We might also be simulating an invokevirtual on a miranda method, |
jrose@1145 | 1312 | // but it is safe to treat it as an invokeinterface.) |
jrose@1145 | 1313 | assert(!m->can_be_statically_bound(), "no final methods on interfaces"); |
jrose@1145 | 1314 | vmindex = klassItable::compute_itable_index(m()); |
jrose@1145 | 1315 | assert(vmindex >= 0, "(>=0) == do_dispatch"); |
jrose@1145 | 1316 | // Set up same bits as ConstantPoolCacheEntry::set_interface_call(). |
jrose@1145 | 1317 | vmtarget = m->method_holder(); // the interface |
jrose@1145 | 1318 | me = MethodHandles::entry(MethodHandles::_invokeinterface_mh); |
jrose@1145 | 1319 | } else if (!do_dispatch || m->can_be_statically_bound()) { |
jrose@1145 | 1320 | // We are simulating an invokestatic or invokespecial instruction. |
jrose@1145 | 1321 | // Set up the method pointer, just like ConstantPoolCacheEntry::set_method(). |
jrose@1145 | 1322 | vmtarget = m(); |
jrose@1145 | 1323 | // this does not help dispatch, but it will make it possible to parse this MH: |
jrose@1145 | 1324 | vmindex = methodOopDesc::nonvirtual_vtable_index; |
jrose@1145 | 1325 | assert(vmindex < 0, "(>=0) == do_dispatch"); |
jrose@1145 | 1326 | if (!m->is_static()) { |
jrose@1145 | 1327 | me = MethodHandles::entry(MethodHandles::_invokespecial_mh); |
jrose@1145 | 1328 | } else { |
jrose@1145 | 1329 | me = MethodHandles::entry(MethodHandles::_invokestatic_mh); |
jrose@1145 | 1330 | // Part of the semantics of a static call is an initialization barrier. |
jrose@1145 | 1331 | // For a DMH, it is done now, when the handle is created. |
jrose@1145 | 1332 | Klass* k = Klass::cast(m->method_holder()); |
jrose@1145 | 1333 | if (k->should_be_initialized()) { |
jrose@1145 | 1334 | k->initialize(CHECK); |
jrose@1145 | 1335 | } |
jrose@1145 | 1336 | } |
jrose@1145 | 1337 | } else { |
jrose@1145 | 1338 | // We are simulating an invokevirtual instruction. |
jrose@1145 | 1339 | // Set up the vtable index, just like ConstantPoolCacheEntry::set_method(). |
jrose@1145 | 1340 | // The key logic is LinkResolver::runtime_resolve_virtual_method. |
jrose@1145 | 1341 | vmindex = m->vtable_index(); |
jrose@1145 | 1342 | vmtarget = m->method_holder(); |
jrose@1145 | 1343 | me = MethodHandles::entry(MethodHandles::_invokevirtual_mh); |
jrose@1145 | 1344 | } |
jrose@1145 | 1345 | |
jrose@1145 | 1346 | if (me == NULL) { THROW(vmSymbols::java_lang_InternalError()); } |
jrose@1145 | 1347 | |
jrose@1145 | 1348 | sun_dyn_DirectMethodHandle::set_vmtarget(mh(), vmtarget); |
jrose@1145 | 1349 | sun_dyn_DirectMethodHandle::set_vmindex(mh(), vmindex); |
jrose@1145 | 1350 | DEBUG_ONLY(int flags; klassOop rlimit); |
jrose@1145 | 1351 | assert(MethodHandles::decode_method(mh(), rlimit, flags) == m(), |
jrose@1145 | 1352 | "properly stored for later decoding"); |
jrose@1145 | 1353 | DEBUG_ONLY(bool actual_do_dispatch = ((flags & _dmf_does_dispatch) != 0)); |
jrose@1145 | 1354 | assert(!(actual_do_dispatch && !do_dispatch), |
jrose@1145 | 1355 | "do not perform dispatch if !do_dispatch specified"); |
jrose@1145 | 1356 | assert(actual_do_dispatch == (vmindex >= 0), "proper later decoding of do_dispatch"); |
jrose@1145 | 1357 | assert(decode_MethodHandle_stack_pushes(mh()) == 0, "DMH does not move stack"); |
jrose@1145 | 1358 | |
jrose@1145 | 1359 | // Done! |
jrose@1145 | 1360 | java_dyn_MethodHandle::set_vmentry(mh(), me); |
jrose@1145 | 1361 | } |
jrose@1145 | 1362 | |
jrose@1145 | 1363 | void MethodHandles::verify_BoundMethodHandle_with_receiver(Handle mh, |
jrose@1145 | 1364 | methodHandle m, |
jrose@1145 | 1365 | TRAPS) { |
jrose@1145 | 1366 | // Verify type. |
jrose@1145 | 1367 | oop receiver = sun_dyn_BoundMethodHandle::argument(mh()); |
jrose@1145 | 1368 | Handle mtype(THREAD, java_dyn_MethodHandle::type(mh())); |
jrose@1145 | 1369 | KlassHandle bound_recv_type; |
jrose@1145 | 1370 | if (receiver != NULL) bound_recv_type = KlassHandle(THREAD, receiver->klass()); |
jrose@1145 | 1371 | verify_method_type(m, mtype, true, bound_recv_type, CHECK); |
jrose@1145 | 1372 | |
jrose@1145 | 1373 | int receiver_pos = m->size_of_parameters() - 1; |
jrose@1145 | 1374 | |
jrose@1145 | 1375 | // Verify MH.vmargslot, which should point at the bound receiver. |
jrose@1145 | 1376 | verify_vmargslot(mh, -1, sun_dyn_BoundMethodHandle::vmargslot(mh()), CHECK); |
jrose@1145 | 1377 | //verify_vmslots(mh, CHECK); |
jrose@1145 | 1378 | |
jrose@1145 | 1379 | // Verify vmslots. |
jrose@1145 | 1380 | if (java_dyn_MethodHandle::vmslots(mh()) != receiver_pos) { |
jrose@1145 | 1381 | THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in BMH (receiver)"); |
jrose@1145 | 1382 | } |
jrose@1145 | 1383 | } |
jrose@1145 | 1384 | |
jrose@1145 | 1385 | // Initialize a BMH with a receiver bound directly to a methodOop. |
jrose@1145 | 1386 | void MethodHandles::init_BoundMethodHandle_with_receiver(Handle mh, |
jrose@1145 | 1387 | methodHandle original_m, |
jrose@1145 | 1388 | KlassHandle receiver_limit, |
jrose@1145 | 1389 | int decode_flags, |
jrose@1145 | 1390 | TRAPS) { |
jrose@1145 | 1391 | // Check arguments. |
jrose@1145 | 1392 | if (mh.is_null() || original_m.is_null()) { |
jrose@1145 | 1393 | THROW(vmSymbols::java_lang_InternalError()); |
jrose@1145 | 1394 | } |
jrose@1145 | 1395 | |
jrose@1145 | 1396 | KlassHandle receiver_klass; |
jrose@1145 | 1397 | { |
jrose@1145 | 1398 | oop receiver_oop = sun_dyn_BoundMethodHandle::argument(mh()); |
jrose@1145 | 1399 | if (receiver_oop != NULL) |
jrose@1145 | 1400 | receiver_klass = KlassHandle(THREAD, receiver_oop->klass()); |
jrose@1145 | 1401 | } |
jrose@1145 | 1402 | methodHandle m = dispatch_decoded_method(original_m, |
jrose@1145 | 1403 | receiver_limit, decode_flags, |
jrose@1145 | 1404 | receiver_klass, |
jrose@1145 | 1405 | CHECK); |
jrose@1145 | 1406 | if (m.is_null()) { THROW(vmSymbols::java_lang_InternalError()); } |
jrose@1145 | 1407 | if (m->is_abstract()) { THROW(vmSymbols::java_lang_AbstractMethodError()); } |
jrose@1145 | 1408 | |
jrose@1145 | 1409 | java_dyn_MethodHandle::init_vmslots(mh()); |
jrose@1145 | 1410 | |
jrose@1145 | 1411 | if (VerifyMethodHandles) { |
jrose@1145 | 1412 | verify_BoundMethodHandle_with_receiver(mh, m, CHECK); |
jrose@1145 | 1413 | } |
jrose@1145 | 1414 | |
jrose@1145 | 1415 | sun_dyn_BoundMethodHandle::set_vmtarget(mh(), m()); |
jrose@1145 | 1416 | |
jrose@1145 | 1417 | DEBUG_ONLY(int junk; klassOop junk2); |
jrose@1145 | 1418 | assert(MethodHandles::decode_method(mh(), junk2, junk) == m(), "properly stored for later decoding"); |
jrose@1145 | 1419 | assert(decode_MethodHandle_stack_pushes(mh()) == 1, "BMH pushes one stack slot"); |
jrose@1145 | 1420 | |
jrose@1145 | 1421 | // Done! |
jrose@1145 | 1422 | java_dyn_MethodHandle::set_vmentry(mh(), MethodHandles::entry(MethodHandles::_bound_ref_direct_mh)); |
jrose@1145 | 1423 | } |
jrose@1145 | 1424 | |
jrose@1145 | 1425 | void MethodHandles::verify_BoundMethodHandle(Handle mh, Handle target, int argnum, |
jrose@1145 | 1426 | bool direct_to_method, TRAPS) { |
jrose@1145 | 1427 | Handle ptype_handle(THREAD, |
jrose@1145 | 1428 | java_dyn_MethodType::ptype(java_dyn_MethodHandle::type(target()), argnum)); |
jrose@1145 | 1429 | KlassHandle ptype_klass; |
jrose@1145 | 1430 | BasicType ptype = java_lang_Class::as_BasicType(ptype_handle(), &ptype_klass); |
jrose@1145 | 1431 | int slots_pushed = type2size[ptype]; |
jrose@1145 | 1432 | |
jrose@1145 | 1433 | oop argument = sun_dyn_BoundMethodHandle::argument(mh()); |
jrose@1145 | 1434 | |
jrose@1145 | 1435 | const char* err = NULL; |
jrose@1145 | 1436 | |
jrose@1145 | 1437 | switch (ptype) { |
jrose@1145 | 1438 | case T_OBJECT: |
jrose@1145 | 1439 | if (argument != NULL) |
jrose@1145 | 1440 | // we must implicitly convert from the arg type to the outgoing ptype |
jrose@1145 | 1441 | err = check_argument_type_change(T_OBJECT, argument->klass(), ptype, ptype_klass(), argnum); |
jrose@1145 | 1442 | break; |
jrose@1145 | 1443 | |
jrose@1145 | 1444 | case T_ARRAY: case T_VOID: |
jrose@1145 | 1445 | assert(false, "array, void do not appear here"); |
jrose@1145 | 1446 | default: |
jrose@1145 | 1447 | if (ptype != T_INT && !is_subword_type(ptype)) { |
jrose@1145 | 1448 | err = "unexpected parameter type"; |
jrose@1145 | 1449 | break; |
jrose@1145 | 1450 | } |
jrose@1145 | 1451 | // check subrange of Integer.value, if necessary |
jrose@1145 | 1452 | if (argument == NULL || argument->klass() != SystemDictionary::int_klass()) { |
jrose@1145 | 1453 | err = "bound integer argument must be of type java.lang.Integer"; |
jrose@1145 | 1454 | break; |
jrose@1145 | 1455 | } |
jrose@1145 | 1456 | if (ptype != T_INT) { |
jrose@1145 | 1457 | int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_INT); |
jrose@1145 | 1458 | jint value = argument->int_field(value_offset); |
jrose@1145 | 1459 | int vminfo = adapter_subword_vminfo(ptype); |
jrose@1145 | 1460 | jint subword = truncate_subword_from_vminfo(value, vminfo); |
jrose@1145 | 1461 | if (value != subword) { |
jrose@1145 | 1462 | err = "bound subword value does not fit into the subword type"; |
jrose@1145 | 1463 | break; |
jrose@1145 | 1464 | } |
jrose@1145 | 1465 | } |
jrose@1145 | 1466 | break; |
jrose@1145 | 1467 | case T_FLOAT: |
jrose@1145 | 1468 | case T_DOUBLE: |
jrose@1145 | 1469 | case T_LONG: |
jrose@1145 | 1470 | { |
jrose@1145 | 1471 | // we must implicitly convert from the unboxed arg type to the outgoing ptype |
jrose@1145 | 1472 | BasicType argbox = java_lang_boxing_object::basic_type(argument); |
jrose@1145 | 1473 | if (argbox != ptype) { |
jrose@1145 | 1474 | err = check_argument_type_change(T_OBJECT, (argument == NULL |
jrose@1145 | 1475 | ? SystemDictionary::object_klass() |
jrose@1145 | 1476 | : argument->klass()), |
jrose@1145 | 1477 | ptype, ptype_klass(), argnum); |
jrose@1145 | 1478 | assert(err != NULL, "this must be an error"); |
jrose@1145 | 1479 | } |
jrose@1145 | 1480 | break; |
jrose@1145 | 1481 | } |
jrose@1145 | 1482 | } |
jrose@1145 | 1483 | |
jrose@1145 | 1484 | if (err == NULL) { |
jrose@1145 | 1485 | DEBUG_ONLY(int this_pushes = decode_MethodHandle_stack_pushes(mh())); |
jrose@1145 | 1486 | if (direct_to_method) { |
jrose@1145 | 1487 | assert(this_pushes == slots_pushed, "BMH pushes one or two stack slots"); |
jrose@1145 | 1488 | assert(slots_pushed <= MethodHandlePushLimit, ""); |
jrose@1145 | 1489 | } else { |
jrose@1474 | 1490 | int target_pushes = decode_MethodHandle_stack_pushes(target()); |
jrose@1474 | 1491 | assert(this_pushes == slots_pushed + target_pushes, "BMH stack motion must be correct"); |
jrose@1145 | 1492 | // do not blow the stack; use a Java-based adapter if this limit is exceeded |
twisti@1573 | 1493 | // FIXME |
twisti@1573 | 1494 | // if (slots_pushed + target_pushes > MethodHandlePushLimit) |
twisti@1573 | 1495 | // err = "too many bound parameters"; |
jrose@1145 | 1496 | } |
jrose@1145 | 1497 | } |
jrose@1145 | 1498 | |
jrose@1145 | 1499 | if (err == NULL) { |
jrose@1145 | 1500 | // Verify the rest of the method type. |
jrose@1145 | 1501 | err = check_method_type_insertion(java_dyn_MethodHandle::type(mh()), |
jrose@1145 | 1502 | argnum, ptype_handle(), |
jrose@1145 | 1503 | java_dyn_MethodHandle::type(target())); |
jrose@1145 | 1504 | } |
jrose@1145 | 1505 | |
jrose@1145 | 1506 | if (err != NULL) { |
jrose@1145 | 1507 | THROW_MSG(vmSymbols::java_lang_InternalError(), err); |
jrose@1145 | 1508 | } |
jrose@1145 | 1509 | } |
jrose@1145 | 1510 | |
jrose@1145 | 1511 | void MethodHandles::init_BoundMethodHandle(Handle mh, Handle target, int argnum, TRAPS) { |
jrose@1145 | 1512 | // Check arguments. |
jrose@1145 | 1513 | if (mh.is_null() || target.is_null() || !java_dyn_MethodHandle::is_instance(target())) { |
jrose@1145 | 1514 | THROW(vmSymbols::java_lang_InternalError()); |
jrose@1145 | 1515 | } |
jrose@1145 | 1516 | |
jrose@1145 | 1517 | java_dyn_MethodHandle::init_vmslots(mh()); |
jrose@1145 | 1518 | |
jrose@1145 | 1519 | if (VerifyMethodHandles) { |
jrose@1145 | 1520 | int insert_after = argnum - 1; |
jrose@1145 | 1521 | verify_vmargslot(mh, insert_after, sun_dyn_BoundMethodHandle::vmargslot(mh()), CHECK); |
jrose@1145 | 1522 | verify_vmslots(mh, CHECK); |
jrose@1145 | 1523 | } |
jrose@1145 | 1524 | |
twisti@1573 | 1525 | // Get bound type and required slots. |
twisti@1573 | 1526 | oop ptype_oop = java_dyn_MethodType::ptype(java_dyn_MethodHandle::type(target()), argnum); |
twisti@1573 | 1527 | BasicType ptype = java_lang_Class::as_BasicType(ptype_oop); |
twisti@1573 | 1528 | int slots_pushed = type2size[ptype]; |
twisti@1573 | 1529 | |
jrose@1145 | 1530 | // If (a) the target is a direct non-dispatched method handle, |
jrose@1145 | 1531 | // or (b) the target is a dispatched direct method handle and we |
jrose@1145 | 1532 | // are binding the receiver, cut out the middle-man. |
jrose@1145 | 1533 | // Do this by decoding the DMH and using its methodOop directly as vmtarget. |
jrose@1145 | 1534 | bool direct_to_method = false; |
jrose@1145 | 1535 | if (OptimizeMethodHandles && |
jrose@1145 | 1536 | target->klass() == SystemDictionary::DirectMethodHandle_klass() && |
jrose@1145 | 1537 | (argnum == 0 || sun_dyn_DirectMethodHandle::vmindex(target()) < 0)) { |
jrose@1145 | 1538 | int decode_flags = 0; klassOop receiver_limit_oop = NULL; |
jrose@1145 | 1539 | methodHandle m(THREAD, decode_method(target(), receiver_limit_oop, decode_flags)); |
jrose@1145 | 1540 | if (m.is_null()) { THROW_MSG(vmSymbols::java_lang_InternalError(), "DMH failed to decode"); } |
twisti@1573 | 1541 | DEBUG_ONLY(int m_vmslots = m->size_of_parameters() - slots_pushed); // pos. of 1st arg. |
jrose@1145 | 1542 | assert(sun_dyn_BoundMethodHandle::vmslots(mh()) == m_vmslots, "type w/ m sig"); |
jrose@1145 | 1543 | if (argnum == 0 && (decode_flags & _dmf_has_receiver) != 0) { |
jrose@1145 | 1544 | KlassHandle receiver_limit(THREAD, receiver_limit_oop); |
jrose@1145 | 1545 | init_BoundMethodHandle_with_receiver(mh, m, |
jrose@1145 | 1546 | receiver_limit, decode_flags, |
jrose@1145 | 1547 | CHECK); |
jrose@1145 | 1548 | return; |
jrose@1145 | 1549 | } |
jrose@1145 | 1550 | |
jrose@1145 | 1551 | // Even if it is not a bound receiver, we still might be able |
jrose@1145 | 1552 | // to bind another argument and still invoke the methodOop directly. |
jrose@1145 | 1553 | if (!(decode_flags & _dmf_does_dispatch)) { |
jrose@1145 | 1554 | direct_to_method = true; |
jrose@1145 | 1555 | sun_dyn_BoundMethodHandle::set_vmtarget(mh(), m()); |
jrose@1145 | 1556 | } |
jrose@1145 | 1557 | } |
jrose@1145 | 1558 | if (!direct_to_method) |
jrose@1145 | 1559 | sun_dyn_BoundMethodHandle::set_vmtarget(mh(), target()); |
jrose@1145 | 1560 | |
jrose@1145 | 1561 | if (VerifyMethodHandles) { |
jrose@1145 | 1562 | verify_BoundMethodHandle(mh, target, argnum, direct_to_method, CHECK); |
jrose@1145 | 1563 | } |
jrose@1145 | 1564 | |
jrose@1145 | 1565 | // Next question: Is this a ref, int, or long bound value? |
jrose@1145 | 1566 | MethodHandleEntry* me = NULL; |
jrose@1145 | 1567 | if (ptype == T_OBJECT) { |
jrose@1145 | 1568 | if (direct_to_method) me = MethodHandles::entry(_bound_ref_direct_mh); |
jrose@1145 | 1569 | else me = MethodHandles::entry(_bound_ref_mh); |
jrose@1145 | 1570 | } else if (slots_pushed == 2) { |
jrose@1145 | 1571 | if (direct_to_method) me = MethodHandles::entry(_bound_long_direct_mh); |
jrose@1145 | 1572 | else me = MethodHandles::entry(_bound_long_mh); |
jrose@1145 | 1573 | } else if (slots_pushed == 1) { |
jrose@1145 | 1574 | if (direct_to_method) me = MethodHandles::entry(_bound_int_direct_mh); |
jrose@1145 | 1575 | else me = MethodHandles::entry(_bound_int_mh); |
jrose@1145 | 1576 | } else { |
jrose@1145 | 1577 | assert(false, ""); |
jrose@1145 | 1578 | } |
jrose@1145 | 1579 | |
jrose@1145 | 1580 | // Done! |
jrose@1145 | 1581 | java_dyn_MethodHandle::set_vmentry(mh(), me); |
jrose@1145 | 1582 | } |
jrose@1145 | 1583 | |
jrose@1145 | 1584 | static void throw_InternalError_for_bad_conversion(int conversion, const char* err, TRAPS) { |
jrose@1145 | 1585 | char msg[200]; |
jrose@1145 | 1586 | jio_snprintf(msg, sizeof(msg), "bad adapter (conversion=0x%08x): %s", conversion, err); |
jrose@1145 | 1587 | THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), msg); |
jrose@1145 | 1588 | } |
jrose@1145 | 1589 | |
jrose@1145 | 1590 | void MethodHandles::verify_AdapterMethodHandle(Handle mh, int argnum, TRAPS) { |
jrose@1145 | 1591 | jint conversion = sun_dyn_AdapterMethodHandle::conversion(mh()); |
jrose@1145 | 1592 | int argslot = sun_dyn_AdapterMethodHandle::vmargslot(mh()); |
jrose@1145 | 1593 | |
jrose@1145 | 1594 | verify_vmargslot(mh, argnum, argslot, CHECK); |
jrose@1145 | 1595 | verify_vmslots(mh, CHECK); |
jrose@1145 | 1596 | |
jrose@1145 | 1597 | jint conv_op = adapter_conversion_op(conversion); |
jrose@1145 | 1598 | if (!conv_op_valid(conv_op)) { |
jrose@1145 | 1599 | throw_InternalError_for_bad_conversion(conversion, "unknown conversion op", THREAD); |
jrose@1145 | 1600 | return; |
jrose@1145 | 1601 | } |
jrose@1145 | 1602 | EntryKind ek = adapter_entry_kind(conv_op); |
jrose@1145 | 1603 | |
jrose@1145 | 1604 | int stack_move = adapter_conversion_stack_move(conversion); |
jrose@1145 | 1605 | BasicType src = adapter_conversion_src_type(conversion); |
jrose@1145 | 1606 | BasicType dest = adapter_conversion_dest_type(conversion); |
jrose@1145 | 1607 | int vminfo = adapter_conversion_vminfo(conversion); // should be zero |
jrose@1145 | 1608 | |
jrose@1145 | 1609 | Handle argument(THREAD, sun_dyn_AdapterMethodHandle::argument(mh())); |
jrose@1145 | 1610 | Handle target(THREAD, sun_dyn_AdapterMethodHandle::vmtarget(mh())); |
jrose@1145 | 1611 | Handle src_mtype(THREAD, java_dyn_MethodHandle::type(mh())); |
jrose@1145 | 1612 | Handle dst_mtype(THREAD, java_dyn_MethodHandle::type(target())); |
jrose@1145 | 1613 | |
jrose@1145 | 1614 | const char* err = NULL; |
jrose@1145 | 1615 | |
jrose@1145 | 1616 | if (err == NULL) { |
jrose@1145 | 1617 | // Check that the correct argument is supplied, but only if it is required. |
jrose@1145 | 1618 | switch (ek) { |
jrose@1145 | 1619 | case _adapter_check_cast: // target type of cast |
jrose@1145 | 1620 | case _adapter_ref_to_prim: // wrapper type from which to unbox |
jrose@1145 | 1621 | case _adapter_prim_to_ref: // wrapper type to box into |
jrose@1145 | 1622 | case _adapter_collect_args: // array type to collect into |
jrose@1145 | 1623 | case _adapter_spread_args: // array type to spread from |
jrose@1145 | 1624 | if (!java_lang_Class::is_instance(argument()) |
jrose@1145 | 1625 | || java_lang_Class::is_primitive(argument())) |
jrose@1145 | 1626 | { err = "adapter requires argument of type java.lang.Class"; break; } |
jrose@1145 | 1627 | if (ek == _adapter_collect_args || |
jrose@1145 | 1628 | ek == _adapter_spread_args) { |
jrose@1145 | 1629 | // Make sure it is a suitable collection type. (Array, for now.) |
jrose@1145 | 1630 | Klass* ak = Klass::cast(java_lang_Class::as_klassOop(argument())); |
jrose@1145 | 1631 | if (!ak->oop_is_objArray()) { |
jrose@1145 | 1632 | { err = "adapter requires argument of type java.lang.Class<Object[]>"; break; } |
jrose@1145 | 1633 | } |
jrose@1145 | 1634 | } |
jrose@1145 | 1635 | break; |
jrose@1145 | 1636 | case _adapter_flyby: |
jrose@1145 | 1637 | case _adapter_ricochet: |
jrose@1145 | 1638 | if (!java_dyn_MethodHandle::is_instance(argument())) |
jrose@1145 | 1639 | { err = "MethodHandle adapter argument required"; break; } |
jrose@1145 | 1640 | break; |
jrose@1145 | 1641 | default: |
jrose@1145 | 1642 | if (argument.not_null()) |
jrose@1145 | 1643 | { err = "adapter has spurious argument"; break; } |
jrose@1145 | 1644 | break; |
jrose@1145 | 1645 | } |
jrose@1145 | 1646 | } |
jrose@1145 | 1647 | |
jrose@1145 | 1648 | if (err == NULL) { |
jrose@1145 | 1649 | // Check that the src/dest types are supplied if needed. |
jrose@1145 | 1650 | switch (ek) { |
jrose@1474 | 1651 | case _adapter_check_cast: |
jrose@1474 | 1652 | if (src != T_OBJECT || dest != T_OBJECT) { |
jrose@1474 | 1653 | err = "adapter requires object src/dest conversion subfields"; |
jrose@1474 | 1654 | } |
jrose@1474 | 1655 | break; |
jrose@1145 | 1656 | case _adapter_prim_to_prim: |
jrose@1145 | 1657 | if (!is_java_primitive(src) || !is_java_primitive(dest) || src == dest) { |
jrose@1145 | 1658 | err = "adapter requires primitive src/dest conversion subfields"; break; |
jrose@1145 | 1659 | } |
jrose@1145 | 1660 | if ( (src == T_FLOAT || src == T_DOUBLE) && !(dest == T_FLOAT || dest == T_DOUBLE) || |
jrose@1145 | 1661 | !(src == T_FLOAT || src == T_DOUBLE) && (dest == T_FLOAT || dest == T_DOUBLE)) { |
jrose@1145 | 1662 | err = "adapter cannot convert beween floating and fixed-point"; break; |
jrose@1145 | 1663 | } |
jrose@1145 | 1664 | break; |
jrose@1145 | 1665 | case _adapter_ref_to_prim: |
jrose@1145 | 1666 | if (src != T_OBJECT || !is_java_primitive(dest) |
jrose@1145 | 1667 | || argument() != Klass::cast(SystemDictionary::box_klass(dest))->java_mirror()) { |
jrose@1145 | 1668 | err = "adapter requires primitive dest conversion subfield"; break; |
jrose@1145 | 1669 | } |
jrose@1145 | 1670 | break; |
jrose@1145 | 1671 | case _adapter_prim_to_ref: |
jrose@1145 | 1672 | if (!is_java_primitive(src) || dest != T_OBJECT |
jrose@1145 | 1673 | || argument() != Klass::cast(SystemDictionary::box_klass(src))->java_mirror()) { |
jrose@1145 | 1674 | err = "adapter requires primitive src conversion subfield"; break; |
jrose@1145 | 1675 | } |
jrose@1145 | 1676 | break; |
jrose@1145 | 1677 | case _adapter_swap_args: |
jrose@1145 | 1678 | case _adapter_rot_args: |
jrose@1145 | 1679 | { |
jrose@1145 | 1680 | if (!src || src != dest) { |
jrose@1145 | 1681 | err = "adapter requires src/dest conversion subfields for swap"; break; |
jrose@1145 | 1682 | } |
jrose@1145 | 1683 | int swap_size = type2size[src]; |
jrose@1474 | 1684 | oop src_mtype = sun_dyn_AdapterMethodHandle::type(mh()); |
jrose@1474 | 1685 | oop dest_mtype = sun_dyn_AdapterMethodHandle::type(target()); |
jrose@1474 | 1686 | int slot_limit = sun_dyn_AdapterMethodHandle::vmslots(target()); |
jrose@1145 | 1687 | int src_slot = argslot; |
jrose@1145 | 1688 | int dest_slot = vminfo; |
jrose@1145 | 1689 | bool rotate_up = (src_slot > dest_slot); // upward rotation |
jrose@1145 | 1690 | int src_arg = argnum; |
jrose@1145 | 1691 | int dest_arg = argument_slot_to_argnum(dest_mtype, dest_slot); |
jrose@1145 | 1692 | verify_vmargslot(mh, dest_arg, dest_slot, CHECK); |
jrose@1145 | 1693 | if (!(dest_slot >= src_slot + swap_size) && |
jrose@1145 | 1694 | !(src_slot >= dest_slot + swap_size)) { |
jrose@1145 | 1695 | err = "source, destination slots must be distinct"; |
jrose@1145 | 1696 | } else if (ek == _adapter_swap_args && !(src_slot > dest_slot)) { |
jrose@1145 | 1697 | err = "source of swap must be deeper in stack"; |
jrose@1145 | 1698 | } else if (ek == _adapter_swap_args) { |
jrose@1145 | 1699 | err = check_argument_type_change(java_dyn_MethodType::ptype(src_mtype, dest_arg), |
jrose@1145 | 1700 | java_dyn_MethodType::ptype(dest_mtype, src_arg), |
jrose@1145 | 1701 | dest_arg); |
jrose@1145 | 1702 | } else if (ek == _adapter_rot_args) { |
jrose@1145 | 1703 | if (rotate_up) { |
jrose@1145 | 1704 | assert((src_slot > dest_slot) && (src_arg < dest_arg), ""); |
jrose@1145 | 1705 | // rotate up: [dest_slot..src_slot-ss] --> [dest_slot+ss..src_slot] |
jrose@1145 | 1706 | // that is: [src_arg+1..dest_arg] --> [src_arg..dest_arg-1] |
jrose@1145 | 1707 | for (int i = src_arg+1; i <= dest_arg && err == NULL; i++) { |
jrose@1145 | 1708 | err = check_argument_type_change(java_dyn_MethodType::ptype(src_mtype, i), |
jrose@1145 | 1709 | java_dyn_MethodType::ptype(dest_mtype, i-1), |
jrose@1145 | 1710 | i); |
jrose@1145 | 1711 | } |
jrose@1145 | 1712 | } else { // rotate down |
jrose@1145 | 1713 | assert((src_slot < dest_slot) && (src_arg > dest_arg), ""); |
jrose@1145 | 1714 | // rotate down: [src_slot+ss..dest_slot] --> [src_slot..dest_slot-ss] |
jrose@1145 | 1715 | // that is: [dest_arg..src_arg-1] --> [dst_arg+1..src_arg] |
jrose@1145 | 1716 | for (int i = dest_arg; i <= src_arg-1 && err == NULL; i++) { |
jrose@1145 | 1717 | err = check_argument_type_change(java_dyn_MethodType::ptype(src_mtype, i), |
jrose@1145 | 1718 | java_dyn_MethodType::ptype(dest_mtype, i+1), |
jrose@1145 | 1719 | i); |
jrose@1145 | 1720 | } |
jrose@1145 | 1721 | } |
jrose@1145 | 1722 | } |
jrose@1145 | 1723 | if (err == NULL) |
jrose@1145 | 1724 | err = check_argument_type_change(java_dyn_MethodType::ptype(src_mtype, src_arg), |
jrose@1145 | 1725 | java_dyn_MethodType::ptype(dest_mtype, dest_arg), |
jrose@1145 | 1726 | src_arg); |
jrose@1145 | 1727 | } |
jrose@1145 | 1728 | break; |
jrose@1145 | 1729 | case _adapter_collect_args: |
jrose@1145 | 1730 | case _adapter_spread_args: |
jrose@1145 | 1731 | { |
jrose@1145 | 1732 | BasicType coll_type = (ek == _adapter_collect_args) ? dest : src; |
jrose@1145 | 1733 | BasicType elem_type = (ek == _adapter_collect_args) ? src : dest; |
jrose@1145 | 1734 | if (coll_type != T_OBJECT || elem_type != T_OBJECT) { |
jrose@1145 | 1735 | err = "adapter requires src/dest subfields"; break; |
jrose@1145 | 1736 | // later: |
jrose@1145 | 1737 | // - consider making coll be a primitive array |
jrose@1145 | 1738 | // - consider making coll be a heterogeneous collection |
jrose@1145 | 1739 | } |
jrose@1145 | 1740 | } |
jrose@1145 | 1741 | break; |
jrose@1145 | 1742 | default: |
jrose@1145 | 1743 | if (src != 0 || dest != 0) { |
jrose@1145 | 1744 | err = "adapter has spurious src/dest conversion subfields"; break; |
jrose@1145 | 1745 | } |
jrose@1145 | 1746 | break; |
jrose@1145 | 1747 | } |
jrose@1145 | 1748 | } |
jrose@1145 | 1749 | |
jrose@1145 | 1750 | if (err == NULL) { |
jrose@1145 | 1751 | // Check the stack_move subfield. |
jrose@1145 | 1752 | // It must always report the net change in stack size, positive or negative. |
jrose@1145 | 1753 | int slots_pushed = stack_move / stack_move_unit(); |
jrose@1145 | 1754 | switch (ek) { |
jrose@1145 | 1755 | case _adapter_prim_to_prim: |
jrose@1145 | 1756 | case _adapter_ref_to_prim: |
jrose@1145 | 1757 | case _adapter_prim_to_ref: |
jrose@1145 | 1758 | if (slots_pushed != type2size[dest] - type2size[src]) { |
jrose@1145 | 1759 | err = "wrong stack motion for primitive conversion"; |
jrose@1145 | 1760 | } |
jrose@1145 | 1761 | break; |
jrose@1145 | 1762 | case _adapter_dup_args: |
jrose@1145 | 1763 | if (slots_pushed <= 0) { |
jrose@1145 | 1764 | err = "adapter requires conversion subfield slots_pushed > 0"; |
jrose@1145 | 1765 | } |
jrose@1145 | 1766 | break; |
jrose@1145 | 1767 | case _adapter_drop_args: |
jrose@1145 | 1768 | if (slots_pushed >= 0) { |
jrose@1145 | 1769 | err = "adapter requires conversion subfield slots_pushed < 0"; |
jrose@1145 | 1770 | } |
jrose@1145 | 1771 | break; |
jrose@1145 | 1772 | case _adapter_collect_args: |
jrose@1145 | 1773 | if (slots_pushed > 1) { |
jrose@1145 | 1774 | err = "adapter requires conversion subfield slots_pushed <= 1"; |
jrose@1145 | 1775 | } |
jrose@1145 | 1776 | break; |
jrose@1145 | 1777 | case _adapter_spread_args: |
jrose@1145 | 1778 | if (slots_pushed < -1) { |
jrose@1145 | 1779 | err = "adapter requires conversion subfield slots_pushed >= -1"; |
jrose@1145 | 1780 | } |
jrose@1145 | 1781 | break; |
jrose@1145 | 1782 | default: |
jrose@1145 | 1783 | if (stack_move != 0) { |
jrose@1145 | 1784 | err = "adapter has spurious stack_move conversion subfield"; |
jrose@1145 | 1785 | } |
jrose@1145 | 1786 | break; |
jrose@1145 | 1787 | } |
jrose@1145 | 1788 | if (err == NULL && stack_move != slots_pushed * stack_move_unit()) { |
jrose@1145 | 1789 | err = "stack_move conversion subfield must be multiple of stack_move_unit"; |
jrose@1145 | 1790 | } |
jrose@1145 | 1791 | } |
jrose@1145 | 1792 | |
jrose@1145 | 1793 | if (err == NULL) { |
jrose@1145 | 1794 | // Make sure this adapter does not push too deeply. |
jrose@1145 | 1795 | int slots_pushed = stack_move / stack_move_unit(); |
jrose@1145 | 1796 | int this_vmslots = java_dyn_MethodHandle::vmslots(mh()); |
jrose@1474 | 1797 | int target_vmslots = java_dyn_MethodHandle::vmslots(target()); |
jrose@1474 | 1798 | if (slots_pushed != (target_vmslots - this_vmslots)) { |
jrose@1145 | 1799 | err = "stack_move inconsistent with previous and current MethodType vmslots"; |
jrose@1145 | 1800 | } else if (slots_pushed > 0) { |
jrose@1145 | 1801 | // verify stack_move against MethodHandlePushLimit |
jrose@1474 | 1802 | int target_pushes = decode_MethodHandle_stack_pushes(target()); |
jrose@1145 | 1803 | // do not blow the stack; use a Java-based adapter if this limit is exceeded |
jrose@1474 | 1804 | if (slots_pushed + target_pushes > MethodHandlePushLimit) { |
jrose@1145 | 1805 | err = "adapter pushes too many parameters"; |
jrose@1145 | 1806 | } |
jrose@1145 | 1807 | } |
jrose@1145 | 1808 | |
jrose@1145 | 1809 | // While we're at it, check that the stack motion decoder works: |
jrose@1474 | 1810 | DEBUG_ONLY(int target_pushes = decode_MethodHandle_stack_pushes(target())); |
jrose@1145 | 1811 | DEBUG_ONLY(int this_pushes = decode_MethodHandle_stack_pushes(mh())); |
jrose@1474 | 1812 | assert(this_pushes == slots_pushed + target_pushes, "AMH stack motion must be correct"); |
jrose@1145 | 1813 | } |
jrose@1145 | 1814 | |
jrose@1145 | 1815 | if (err == NULL && vminfo != 0) { |
jrose@1145 | 1816 | switch (ek) { |
jrose@1145 | 1817 | case _adapter_swap_args: |
jrose@1145 | 1818 | case _adapter_rot_args: |
jrose@1145 | 1819 | break; // OK |
jrose@1145 | 1820 | default: |
jrose@1145 | 1821 | err = "vminfo subfield is reserved to the JVM"; |
jrose@1145 | 1822 | } |
jrose@1145 | 1823 | } |
jrose@1145 | 1824 | |
jrose@1145 | 1825 | // Do additional ad hoc checks. |
jrose@1145 | 1826 | if (err == NULL) { |
jrose@1145 | 1827 | switch (ek) { |
jrose@1145 | 1828 | case _adapter_retype_only: |
jrose@1474 | 1829 | err = check_method_type_passthrough(src_mtype(), dst_mtype(), false); |
jrose@1474 | 1830 | break; |
jrose@1474 | 1831 | |
jrose@1474 | 1832 | case _adapter_retype_raw: |
jrose@1474 | 1833 | err = check_method_type_passthrough(src_mtype(), dst_mtype(), true); |
jrose@1145 | 1834 | break; |
jrose@1145 | 1835 | |
jrose@1145 | 1836 | case _adapter_check_cast: |
jrose@1145 | 1837 | { |
jrose@1145 | 1838 | // The actual value being checked must be a reference: |
jrose@1145 | 1839 | err = check_argument_type_change(java_dyn_MethodType::ptype(src_mtype(), argnum), |
jrose@1145 | 1840 | object_java_mirror(), argnum); |
jrose@1145 | 1841 | if (err != NULL) break; |
jrose@1145 | 1842 | |
jrose@1145 | 1843 | // The output of the cast must fit with the destination argument: |
jrose@1145 | 1844 | Handle cast_class = argument; |
jrose@1145 | 1845 | err = check_method_type_conversion(src_mtype(), |
jrose@1145 | 1846 | argnum, cast_class(), |
jrose@1145 | 1847 | dst_mtype()); |
jrose@1145 | 1848 | } |
jrose@1145 | 1849 | break; |
jrose@1145 | 1850 | |
jrose@1145 | 1851 | // %%% TO DO: continue in remaining cases to verify src/dst_mtype if VerifyMethodHandles |
jrose@1145 | 1852 | } |
jrose@1145 | 1853 | } |
jrose@1145 | 1854 | |
jrose@1145 | 1855 | if (err != NULL) { |
jrose@1145 | 1856 | throw_InternalError_for_bad_conversion(conversion, err, THREAD); |
jrose@1145 | 1857 | return; |
jrose@1145 | 1858 | } |
jrose@1145 | 1859 | |
jrose@1145 | 1860 | } |
jrose@1145 | 1861 | |
jrose@1145 | 1862 | void MethodHandles::init_AdapterMethodHandle(Handle mh, Handle target, int argnum, TRAPS) { |
jrose@1145 | 1863 | oop argument = sun_dyn_AdapterMethodHandle::argument(mh()); |
jrose@1145 | 1864 | int argslot = sun_dyn_AdapterMethodHandle::vmargslot(mh()); |
jrose@1145 | 1865 | jint conversion = sun_dyn_AdapterMethodHandle::conversion(mh()); |
jrose@1145 | 1866 | jint conv_op = adapter_conversion_op(conversion); |
jrose@1145 | 1867 | |
jrose@1145 | 1868 | // adjust the adapter code to the internal EntryKind enumeration: |
jrose@1145 | 1869 | EntryKind ek_orig = adapter_entry_kind(conv_op); |
jrose@1145 | 1870 | EntryKind ek_opt = ek_orig; // may be optimized |
jrose@1145 | 1871 | |
jrose@1145 | 1872 | // Finalize the vmtarget field (Java initialized it to null). |
jrose@1145 | 1873 | if (!java_dyn_MethodHandle::is_instance(target())) { |
jrose@1145 | 1874 | throw_InternalError_for_bad_conversion(conversion, "bad target", THREAD); |
jrose@1145 | 1875 | return; |
jrose@1145 | 1876 | } |
jrose@1145 | 1877 | sun_dyn_AdapterMethodHandle::set_vmtarget(mh(), target()); |
jrose@1145 | 1878 | |
jrose@1145 | 1879 | if (VerifyMethodHandles) { |
jrose@1145 | 1880 | verify_AdapterMethodHandle(mh, argnum, CHECK); |
jrose@1145 | 1881 | } |
jrose@1145 | 1882 | |
jrose@1145 | 1883 | int stack_move = adapter_conversion_stack_move(conversion); |
jrose@1145 | 1884 | BasicType src = adapter_conversion_src_type(conversion); |
jrose@1145 | 1885 | BasicType dest = adapter_conversion_dest_type(conversion); |
jrose@1145 | 1886 | int vminfo = adapter_conversion_vminfo(conversion); // should be zero |
jrose@1145 | 1887 | |
jrose@1145 | 1888 | const char* err = NULL; |
jrose@1145 | 1889 | |
jrose@1145 | 1890 | // Now it's time to finish the case analysis and pick a MethodHandleEntry. |
jrose@1145 | 1891 | switch (ek_orig) { |
jrose@1145 | 1892 | case _adapter_retype_only: |
jrose@1474 | 1893 | case _adapter_retype_raw: |
jrose@1145 | 1894 | case _adapter_check_cast: |
jrose@1145 | 1895 | case _adapter_dup_args: |
jrose@1145 | 1896 | case _adapter_drop_args: |
jrose@1145 | 1897 | // these work fine via general case code |
jrose@1145 | 1898 | break; |
jrose@1145 | 1899 | |
jrose@1145 | 1900 | case _adapter_prim_to_prim: |
jrose@1145 | 1901 | { |
jrose@1145 | 1902 | // Non-subword cases are {int,float,long,double} -> {int,float,long,double}. |
jrose@1145 | 1903 | // And, the {float,double} -> {int,long} cases must be handled by Java. |
jrose@1145 | 1904 | switch (type2size[src] *4+ type2size[dest]) { |
jrose@1145 | 1905 | case 1 *4+ 1: |
jrose@1145 | 1906 | assert(src == T_INT || is_subword_type(src), "source is not float"); |
jrose@1145 | 1907 | // Subword-related cases are int -> {boolean,byte,char,short}. |
jrose@1145 | 1908 | ek_opt = _adapter_opt_i2i; |
jrose@1145 | 1909 | vminfo = adapter_subword_vminfo(dest); |
jrose@1145 | 1910 | break; |
jrose@1145 | 1911 | case 2 *4+ 1: |
jrose@1145 | 1912 | if (src == T_LONG && (dest == T_INT || is_subword_type(dest))) { |
jrose@1145 | 1913 | ek_opt = _adapter_opt_l2i; |
jrose@1145 | 1914 | vminfo = adapter_subword_vminfo(dest); |
jrose@1145 | 1915 | } else if (src == T_DOUBLE && dest == T_FLOAT) { |
jrose@1145 | 1916 | ek_opt = _adapter_opt_d2f; |
jrose@1145 | 1917 | } else { |
jrose@1145 | 1918 | assert(false, ""); |
jrose@1145 | 1919 | } |
jrose@1145 | 1920 | break; |
jrose@1145 | 1921 | case 1 *4+ 2: |
jrose@1145 | 1922 | if (src == T_INT && dest == T_LONG) { |
jrose@1145 | 1923 | ek_opt = _adapter_opt_i2l; |
jrose@1145 | 1924 | } else if (src == T_FLOAT && dest == T_DOUBLE) { |
jrose@1145 | 1925 | ek_opt = _adapter_opt_f2d; |
jrose@1145 | 1926 | } else { |
jrose@1145 | 1927 | assert(false, ""); |
jrose@1145 | 1928 | } |
jrose@1145 | 1929 | break; |
jrose@1145 | 1930 | default: |
jrose@1145 | 1931 | assert(false, ""); |
jrose@1145 | 1932 | break; |
jrose@1145 | 1933 | } |
jrose@1145 | 1934 | } |
jrose@1145 | 1935 | break; |
jrose@1145 | 1936 | |
jrose@1145 | 1937 | case _adapter_ref_to_prim: |
jrose@1145 | 1938 | { |
jrose@1145 | 1939 | switch (type2size[dest]) { |
jrose@1145 | 1940 | case 1: |
jrose@1145 | 1941 | ek_opt = _adapter_opt_unboxi; |
jrose@1145 | 1942 | vminfo = adapter_subword_vminfo(dest); |
jrose@1145 | 1943 | break; |
jrose@1145 | 1944 | case 2: |
jrose@1145 | 1945 | ek_opt = _adapter_opt_unboxl; |
jrose@1145 | 1946 | break; |
jrose@1145 | 1947 | default: |
jrose@1145 | 1948 | assert(false, ""); |
jrose@1145 | 1949 | break; |
jrose@1145 | 1950 | } |
jrose@1145 | 1951 | } |
jrose@1145 | 1952 | break; |
jrose@1145 | 1953 | |
jrose@1145 | 1954 | case _adapter_prim_to_ref: |
jrose@1145 | 1955 | goto throw_not_impl; // allocates, hence could block |
jrose@1145 | 1956 | |
jrose@1145 | 1957 | case _adapter_swap_args: |
jrose@1145 | 1958 | case _adapter_rot_args: |
jrose@1145 | 1959 | { |
jrose@1145 | 1960 | int swap_slots = type2size[src]; |
jrose@1474 | 1961 | int slot_limit = sun_dyn_AdapterMethodHandle::vmslots(mh()); |
jrose@1145 | 1962 | int src_slot = argslot; |
jrose@1145 | 1963 | int dest_slot = vminfo; |
jrose@1145 | 1964 | int rotate = (ek_orig == _adapter_swap_args) ? 0 : (src_slot > dest_slot) ? 1 : -1; |
jrose@1145 | 1965 | switch (swap_slots) { |
jrose@1145 | 1966 | case 1: |
jrose@1145 | 1967 | ek_opt = (!rotate ? _adapter_opt_swap_1 : |
jrose@1145 | 1968 | rotate > 0 ? _adapter_opt_rot_1_up : _adapter_opt_rot_1_down); |
jrose@1145 | 1969 | break; |
jrose@1145 | 1970 | case 2: |
jrose@1145 | 1971 | ek_opt = (!rotate ? _adapter_opt_swap_2 : |
jrose@1145 | 1972 | rotate > 0 ? _adapter_opt_rot_2_up : _adapter_opt_rot_2_down); |
jrose@1145 | 1973 | break; |
jrose@1145 | 1974 | default: |
jrose@1145 | 1975 | assert(false, ""); |
jrose@1145 | 1976 | break; |
jrose@1145 | 1977 | } |
jrose@1145 | 1978 | } |
jrose@1145 | 1979 | break; |
jrose@1145 | 1980 | |
jrose@1145 | 1981 | case _adapter_collect_args: |
jrose@1145 | 1982 | goto throw_not_impl; // allocates, hence could block |
jrose@1145 | 1983 | |
jrose@1145 | 1984 | case _adapter_spread_args: |
jrose@1145 | 1985 | { |
jrose@1145 | 1986 | // vminfo will be the required length of the array |
jrose@1145 | 1987 | int slots_pushed = stack_move / stack_move_unit(); |
jrose@1145 | 1988 | int array_size = slots_pushed + 1; |
jrose@1145 | 1989 | assert(array_size >= 0, ""); |
jrose@1145 | 1990 | vminfo = array_size; |
jrose@1145 | 1991 | switch (array_size) { |
jrose@1145 | 1992 | case 0: ek_opt = _adapter_opt_spread_0; break; |
jrose@1145 | 1993 | case 1: ek_opt = _adapter_opt_spread_1; break; |
jrose@1145 | 1994 | default: ek_opt = _adapter_opt_spread_more; break; |
jrose@1145 | 1995 | } |
jrose@1145 | 1996 | if ((vminfo & CONV_VMINFO_MASK) != vminfo) |
jrose@1145 | 1997 | goto throw_not_impl; // overflow |
jrose@1145 | 1998 | } |
jrose@1145 | 1999 | break; |
jrose@1145 | 2000 | |
jrose@1145 | 2001 | case _adapter_flyby: |
jrose@1145 | 2002 | case _adapter_ricochet: |
jrose@1145 | 2003 | goto throw_not_impl; // runs Java code, hence could block |
jrose@1145 | 2004 | |
jrose@1145 | 2005 | default: |
jrose@1145 | 2006 | // should have failed much earlier; must be a missing case here |
jrose@1145 | 2007 | assert(false, "incomplete switch"); |
jrose@1145 | 2008 | // and fall through: |
jrose@1145 | 2009 | |
jrose@1145 | 2010 | throw_not_impl: |
jrose@1145 | 2011 | // FIXME: these adapters are NYI |
jrose@1145 | 2012 | err = "adapter not yet implemented in the JVM"; |
jrose@1145 | 2013 | break; |
jrose@1145 | 2014 | } |
jrose@1145 | 2015 | |
jrose@1145 | 2016 | if (err != NULL) { |
jrose@1145 | 2017 | throw_InternalError_for_bad_conversion(conversion, err, THREAD); |
jrose@1145 | 2018 | return; |
jrose@1145 | 2019 | } |
jrose@1145 | 2020 | |
jrose@1145 | 2021 | // Rebuild the conversion value; maybe parts of it were changed. |
jrose@1145 | 2022 | jint new_conversion = adapter_conversion(conv_op, src, dest, stack_move, vminfo); |
jrose@1145 | 2023 | |
jrose@1145 | 2024 | // Finalize the conversion field. (Note that it is final to Java code.) |
jrose@1145 | 2025 | sun_dyn_AdapterMethodHandle::set_conversion(mh(), new_conversion); |
jrose@1145 | 2026 | |
jrose@1145 | 2027 | // Done! |
jrose@1145 | 2028 | java_dyn_MethodHandle::set_vmentry(mh(), entry(ek_opt)); |
jrose@1145 | 2029 | |
jrose@1145 | 2030 | // There should be enough memory barriers on exit from native methods |
jrose@1145 | 2031 | // to ensure that the MH is fully initialized to all threads before |
jrose@1145 | 2032 | // Java code can publish it in global data structures. |
jrose@1145 | 2033 | } |
jrose@1145 | 2034 | |
jrose@1145 | 2035 | // |
jrose@1145 | 2036 | // Here are the native methods on sun.dyn.MethodHandleImpl. |
jrose@1145 | 2037 | // They are the private interface between this JVM and the HotSpot-specific |
jrose@1145 | 2038 | // Java code that implements JSR 292 method handles. |
jrose@1145 | 2039 | // |
jrose@1145 | 2040 | // Note: We use a JVM_ENTRY macro to define each of these, for this is the way |
jrose@1145 | 2041 | // that intrinsic (non-JNI) native methods are defined in HotSpot. |
jrose@1145 | 2042 | // |
jrose@1145 | 2043 | |
jrose@1145 | 2044 | // direct method handles for invokestatic or invokespecial |
jrose@1145 | 2045 | // void init(DirectMethodHandle self, MemberName ref, boolean doDispatch, Class<?> caller); |
jrose@1145 | 2046 | JVM_ENTRY(void, MHI_init_DMH(JNIEnv *env, jobject igcls, jobject mh_jh, |
jrose@1145 | 2047 | jobject target_jh, jboolean do_dispatch, jobject caller_jh)) { |
jrose@1145 | 2048 | ResourceMark rm; // for error messages |
jrose@1145 | 2049 | |
jrose@1145 | 2050 | // This is the guy we are initializing: |
jrose@1145 | 2051 | if (mh_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } |
jrose@1145 | 2052 | Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh)); |
jrose@1145 | 2053 | |
jrose@1145 | 2054 | // Early returns out of this method leave the DMH in an unfinished state. |
jrose@1145 | 2055 | assert(java_dyn_MethodHandle::vmentry(mh()) == NULL, "must be safely null"); |
jrose@1145 | 2056 | |
jrose@1145 | 2057 | // which method are we really talking about? |
jrose@1145 | 2058 | if (target_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } |
jrose@1145 | 2059 | oop target_oop = JNIHandles::resolve_non_null(target_jh); |
jrose@1145 | 2060 | if (sun_dyn_MemberName::is_instance(target_oop) && |
jrose@1145 | 2061 | sun_dyn_MemberName::vmindex(target_oop) == VM_INDEX_UNINITIALIZED) { |
jrose@1145 | 2062 | Handle mname(THREAD, target_oop); |
jrose@1145 | 2063 | MethodHandles::resolve_MemberName(mname, CHECK); |
jrose@1145 | 2064 | target_oop = mname(); // in case of GC |
jrose@1145 | 2065 | } |
jrose@1145 | 2066 | |
jrose@1145 | 2067 | int decode_flags = 0; klassOop receiver_limit = NULL; |
jrose@1145 | 2068 | methodHandle m(THREAD, |
jrose@1145 | 2069 | MethodHandles::decode_method(target_oop, |
jrose@1145 | 2070 | receiver_limit, decode_flags)); |
jrose@1145 | 2071 | if (m.is_null()) { THROW_MSG(vmSymbols::java_lang_InternalError(), "no such method"); } |
jrose@1145 | 2072 | |
jrose@1145 | 2073 | // The trusted Java code that calls this method should already have performed |
jrose@1145 | 2074 | // access checks on behalf of the given caller. But, we can verify this. |
jrose@1145 | 2075 | if (VerifyMethodHandles && caller_jh != NULL) { |
jrose@1145 | 2076 | KlassHandle caller(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(caller_jh))); |
jrose@1145 | 2077 | // If this were a bytecode, the first access check would be against |
jrose@1145 | 2078 | // the "reference class" mentioned in the CONSTANT_Methodref. |
jrose@1145 | 2079 | // For that class, we use the defining class of m, |
jrose@1145 | 2080 | // or a more specific receiver limit if available. |
jrose@1145 | 2081 | klassOop reference_klass = m->method_holder(); // OK approximation |
jrose@1145 | 2082 | if (receiver_limit != NULL && receiver_limit != reference_klass) { |
jrose@1145 | 2083 | if (!Klass::cast(receiver_limit)->is_subtype_of(reference_klass)) |
jrose@1145 | 2084 | THROW_MSG(vmSymbols::java_lang_InternalError(), "receiver limit out of bounds"); // Java code bug |
jrose@1145 | 2085 | reference_klass = receiver_limit; |
jrose@1145 | 2086 | } |
jrose@1145 | 2087 | // Emulate LinkResolver::check_klass_accessability. |
jrose@1145 | 2088 | if (!Reflection::verify_class_access(caller->as_klassOop(), |
jrose@1145 | 2089 | reference_klass, |
jrose@1145 | 2090 | true)) { |
jrose@1145 | 2091 | THROW_MSG(vmSymbols::java_lang_InternalError(), Klass::cast(m->method_holder())->external_name()); |
jrose@1145 | 2092 | } |
jrose@1145 | 2093 | // If there were a bytecode, the next step would be to lookup the method |
jrose@1145 | 2094 | // in the reference class, then then check the method's access bits. |
jrose@1145 | 2095 | // Emulate LinkResolver::check_method_accessability. |
jrose@1145 | 2096 | klassOop resolved_klass = m->method_holder(); |
jrose@1145 | 2097 | if (!Reflection::verify_field_access(caller->as_klassOop(), |
jrose@1145 | 2098 | resolved_klass, reference_klass, |
jrose@1145 | 2099 | m->access_flags(), |
jrose@1145 | 2100 | true)) { |
jrose@1145 | 2101 | // %%% following cutout belongs in Reflection::verify_field_access? |
jrose@1145 | 2102 | bool same_pm = Reflection::is_same_package_member(caller->as_klassOop(), |
jrose@1145 | 2103 | reference_klass, THREAD); |
jrose@1145 | 2104 | if (!same_pm) { |
jrose@1145 | 2105 | THROW_MSG(vmSymbols::java_lang_InternalError(), m->name_and_sig_as_C_string()); |
jrose@1145 | 2106 | } |
jrose@1145 | 2107 | } |
jrose@1145 | 2108 | } |
jrose@1145 | 2109 | |
jrose@1145 | 2110 | MethodHandles::init_DirectMethodHandle(mh, m, (do_dispatch != JNI_FALSE), CHECK); |
jrose@1145 | 2111 | } |
jrose@1145 | 2112 | JVM_END |
jrose@1145 | 2113 | |
jrose@1145 | 2114 | // bound method handles |
jrose@1145 | 2115 | JVM_ENTRY(void, MHI_init_BMH(JNIEnv *env, jobject igcls, jobject mh_jh, |
jrose@1145 | 2116 | jobject target_jh, int argnum)) { |
jrose@1145 | 2117 | ResourceMark rm; // for error messages |
jrose@1145 | 2118 | |
jrose@1145 | 2119 | // This is the guy we are initializing: |
jrose@1145 | 2120 | if (mh_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } |
jrose@1145 | 2121 | Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh)); |
jrose@1145 | 2122 | |
jrose@1145 | 2123 | // Early returns out of this method leave the BMH in an unfinished state. |
jrose@1145 | 2124 | assert(java_dyn_MethodHandle::vmentry(mh()) == NULL, "must be safely null"); |
jrose@1145 | 2125 | |
jrose@1145 | 2126 | if (target_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } |
jrose@1145 | 2127 | Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); |
jrose@1145 | 2128 | |
jrose@1145 | 2129 | if (!java_dyn_MethodHandle::is_instance(target())) { |
jrose@1145 | 2130 | // Target object is a reflective method. (%%% Do we need this alternate path?) |
jrose@1145 | 2131 | Untested("init_BMH of non-MH"); |
jrose@1145 | 2132 | if (argnum != 0) { THROW(vmSymbols::java_lang_InternalError()); } |
jrose@1145 | 2133 | int decode_flags = 0; klassOop receiver_limit_oop = NULL; |
jrose@1145 | 2134 | methodHandle m(THREAD, |
jrose@1145 | 2135 | MethodHandles::decode_method(target(), |
jrose@1145 | 2136 | receiver_limit_oop, |
jrose@1145 | 2137 | decode_flags)); |
jrose@1145 | 2138 | KlassHandle receiver_limit(THREAD, receiver_limit_oop); |
jrose@1145 | 2139 | MethodHandles::init_BoundMethodHandle_with_receiver(mh, m, |
jrose@1145 | 2140 | receiver_limit, |
jrose@1145 | 2141 | decode_flags, |
jrose@1145 | 2142 | CHECK); |
jrose@1145 | 2143 | return; |
jrose@1145 | 2144 | } |
jrose@1145 | 2145 | |
jrose@1145 | 2146 | // Build a BMH on top of a DMH or another BMH: |
jrose@1145 | 2147 | MethodHandles::init_BoundMethodHandle(mh, target, argnum, CHECK); |
jrose@1145 | 2148 | } |
jrose@1145 | 2149 | JVM_END |
jrose@1145 | 2150 | |
jrose@1145 | 2151 | // adapter method handles |
jrose@1145 | 2152 | JVM_ENTRY(void, MHI_init_AMH(JNIEnv *env, jobject igcls, jobject mh_jh, |
jrose@1145 | 2153 | jobject target_jh, int argnum)) { |
jrose@1145 | 2154 | // This is the guy we are initializing: |
jrose@1145 | 2155 | if (mh_jh == NULL || target_jh == NULL) { |
jrose@1145 | 2156 | THROW(vmSymbols::java_lang_InternalError()); |
jrose@1145 | 2157 | } |
jrose@1145 | 2158 | Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh)); |
jrose@1145 | 2159 | Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); |
jrose@1145 | 2160 | |
jrose@1145 | 2161 | // Early returns out of this method leave the AMH in an unfinished state. |
jrose@1145 | 2162 | assert(java_dyn_MethodHandle::vmentry(mh()) == NULL, "must be safely null"); |
jrose@1145 | 2163 | |
jrose@1145 | 2164 | MethodHandles::init_AdapterMethodHandle(mh, target, argnum, CHECK); |
jrose@1145 | 2165 | } |
jrose@1145 | 2166 | JVM_END |
jrose@1145 | 2167 | |
jrose@1145 | 2168 | // method type forms |
jrose@1145 | 2169 | JVM_ENTRY(void, MHI_init_MT(JNIEnv *env, jobject igcls, jobject erased_jh)) { |
jrose@1145 | 2170 | if (erased_jh == NULL) return; |
jrose@1145 | 2171 | if (TraceMethodHandles) { |
jrose@1145 | 2172 | tty->print("creating MethodType form "); |
jrose@1145 | 2173 | if (WizardMode || Verbose) { // Warning: this calls Java code on the MH! |
jrose@1145 | 2174 | // call Object.toString() |
jrose@1145 | 2175 | symbolOop name = vmSymbols::toString_name(), sig = vmSymbols::void_string_signature(); |
jrose@1145 | 2176 | JavaCallArguments args(Handle(THREAD, JNIHandles::resolve_non_null(erased_jh))); |
jrose@1145 | 2177 | JavaValue result(T_OBJECT); |
jrose@1145 | 2178 | JavaCalls::call_virtual(&result, SystemDictionary::object_klass(), name, sig, |
jrose@1145 | 2179 | &args, CHECK); |
jrose@1145 | 2180 | Handle str(THREAD, (oop)result.get_jobject()); |
jrose@1145 | 2181 | java_lang_String::print(str, tty); |
jrose@1145 | 2182 | } |
jrose@1145 | 2183 | tty->cr(); |
jrose@1145 | 2184 | } |
jrose@1145 | 2185 | } |
jrose@1145 | 2186 | JVM_END |
jrose@1145 | 2187 | |
jrose@1145 | 2188 | // debugging and reflection |
jrose@1145 | 2189 | JVM_ENTRY(jobject, MHI_getTarget(JNIEnv *env, jobject igcls, jobject mh_jh, jint format)) { |
jrose@1145 | 2190 | Handle mh(THREAD, JNIHandles::resolve(mh_jh)); |
jrose@1145 | 2191 | if (!java_dyn_MethodHandle::is_instance(mh())) { |
jrose@1145 | 2192 | THROW_NULL(vmSymbols::java_lang_IllegalArgumentException()); |
jrose@1145 | 2193 | } |
jrose@1145 | 2194 | oop target = MethodHandles::encode_target(mh, format, CHECK_NULL); |
jrose@1145 | 2195 | return JNIHandles::make_local(THREAD, target); |
jrose@1145 | 2196 | } |
jrose@1145 | 2197 | JVM_END |
jrose@1145 | 2198 | |
jrose@1145 | 2199 | JVM_ENTRY(jint, MHI_getConstant(JNIEnv *env, jobject igcls, jint which)) { |
jrose@1145 | 2200 | switch (which) { |
jrose@1145 | 2201 | case MethodHandles::GC_JVM_PUSH_LIMIT: |
jrose@1145 | 2202 | guarantee(MethodHandlePushLimit >= 2 && MethodHandlePushLimit <= 0xFF, |
jrose@1145 | 2203 | "MethodHandlePushLimit parameter must be in valid range"); |
jrose@1145 | 2204 | return MethodHandlePushLimit; |
jrose@1474 | 2205 | case MethodHandles::GC_JVM_STACK_MOVE_UNIT: |
jrose@1145 | 2206 | // return number of words per slot, signed according to stack direction |
jrose@1145 | 2207 | return MethodHandles::stack_move_unit(); |
jrose@1145 | 2208 | } |
jrose@1145 | 2209 | return 0; |
jrose@1145 | 2210 | } |
jrose@1145 | 2211 | JVM_END |
jrose@1145 | 2212 | |
jrose@1145 | 2213 | #ifndef PRODUCT |
jrose@1145 | 2214 | #define EACH_NAMED_CON(template) \ |
jrose@1145 | 2215 | template(MethodHandles,GC_JVM_PUSH_LIMIT) \ |
jrose@1474 | 2216 | template(MethodHandles,GC_JVM_STACK_MOVE_UNIT) \ |
jrose@1145 | 2217 | template(MethodHandles,ETF_HANDLE_OR_METHOD_NAME) \ |
jrose@1145 | 2218 | template(MethodHandles,ETF_DIRECT_HANDLE) \ |
jrose@1145 | 2219 | template(MethodHandles,ETF_METHOD_NAME) \ |
jrose@1145 | 2220 | template(MethodHandles,ETF_REFLECT_METHOD) \ |
jrose@1145 | 2221 | template(sun_dyn_MemberName,MN_IS_METHOD) \ |
jrose@1145 | 2222 | template(sun_dyn_MemberName,MN_IS_CONSTRUCTOR) \ |
jrose@1145 | 2223 | template(sun_dyn_MemberName,MN_IS_FIELD) \ |
jrose@1145 | 2224 | template(sun_dyn_MemberName,MN_IS_TYPE) \ |
jrose@1145 | 2225 | template(sun_dyn_MemberName,MN_SEARCH_SUPERCLASSES) \ |
jrose@1145 | 2226 | template(sun_dyn_MemberName,MN_SEARCH_INTERFACES) \ |
jrose@1145 | 2227 | template(sun_dyn_MemberName,VM_INDEX_UNINITIALIZED) \ |
jrose@1145 | 2228 | template(sun_dyn_AdapterMethodHandle,OP_RETYPE_ONLY) \ |
jrose@1474 | 2229 | template(sun_dyn_AdapterMethodHandle,OP_RETYPE_RAW) \ |
jrose@1145 | 2230 | template(sun_dyn_AdapterMethodHandle,OP_CHECK_CAST) \ |
jrose@1145 | 2231 | template(sun_dyn_AdapterMethodHandle,OP_PRIM_TO_PRIM) \ |
jrose@1145 | 2232 | template(sun_dyn_AdapterMethodHandle,OP_REF_TO_PRIM) \ |
jrose@1145 | 2233 | template(sun_dyn_AdapterMethodHandle,OP_PRIM_TO_REF) \ |
jrose@1145 | 2234 | template(sun_dyn_AdapterMethodHandle,OP_SWAP_ARGS) \ |
jrose@1145 | 2235 | template(sun_dyn_AdapterMethodHandle,OP_ROT_ARGS) \ |
jrose@1145 | 2236 | template(sun_dyn_AdapterMethodHandle,OP_DUP_ARGS) \ |
jrose@1145 | 2237 | template(sun_dyn_AdapterMethodHandle,OP_DROP_ARGS) \ |
jrose@1145 | 2238 | template(sun_dyn_AdapterMethodHandle,OP_COLLECT_ARGS) \ |
jrose@1145 | 2239 | template(sun_dyn_AdapterMethodHandle,OP_SPREAD_ARGS) \ |
jrose@1145 | 2240 | template(sun_dyn_AdapterMethodHandle,OP_FLYBY) \ |
jrose@1145 | 2241 | template(sun_dyn_AdapterMethodHandle,OP_RICOCHET) \ |
jrose@1145 | 2242 | template(sun_dyn_AdapterMethodHandle,CONV_OP_LIMIT) \ |
jrose@1145 | 2243 | template(sun_dyn_AdapterMethodHandle,CONV_OP_MASK) \ |
jrose@1145 | 2244 | template(sun_dyn_AdapterMethodHandle,CONV_VMINFO_MASK) \ |
jrose@1145 | 2245 | template(sun_dyn_AdapterMethodHandle,CONV_VMINFO_SHIFT) \ |
jrose@1145 | 2246 | template(sun_dyn_AdapterMethodHandle,CONV_OP_SHIFT) \ |
jrose@1145 | 2247 | template(sun_dyn_AdapterMethodHandle,CONV_DEST_TYPE_SHIFT) \ |
jrose@1145 | 2248 | template(sun_dyn_AdapterMethodHandle,CONV_SRC_TYPE_SHIFT) \ |
jrose@1145 | 2249 | template(sun_dyn_AdapterMethodHandle,CONV_STACK_MOVE_SHIFT) \ |
jrose@1145 | 2250 | template(sun_dyn_AdapterMethodHandle,CONV_STACK_MOVE_MASK) \ |
jrose@1145 | 2251 | /*end*/ |
jrose@1145 | 2252 | |
jrose@1145 | 2253 | #define ONE_PLUS(scope,value) 1+ |
jrose@1145 | 2254 | static const int con_value_count = EACH_NAMED_CON(ONE_PLUS) 0; |
jrose@1145 | 2255 | #define VALUE_COMMA(scope,value) scope::value, |
jrose@1145 | 2256 | static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA) 0 }; |
jrose@1145 | 2257 | #define STRING_NULL(scope,value) #value "\0" |
jrose@1145 | 2258 | static const char con_names[] = { EACH_NAMED_CON(STRING_NULL) }; |
jrose@1145 | 2259 | |
jrose@1145 | 2260 | #undef ONE_PLUS |
jrose@1145 | 2261 | #undef VALUE_COMMA |
jrose@1145 | 2262 | #undef STRING_NULL |
jrose@1145 | 2263 | #undef EACH_NAMED_CON |
jrose@1145 | 2264 | #endif |
jrose@1145 | 2265 | |
jrose@1145 | 2266 | JVM_ENTRY(jint, MHI_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) { |
jrose@1145 | 2267 | #ifndef PRODUCT |
jrose@1145 | 2268 | if (which >= 0 && which < con_value_count) { |
jrose@1145 | 2269 | int con = con_values[which]; |
jrose@1145 | 2270 | objArrayOop box = (objArrayOop) JNIHandles::resolve(box_jh); |
jrose@1145 | 2271 | if (box != NULL && box->klass() == Universe::objectArrayKlassObj() && box->length() > 0) { |
jrose@1145 | 2272 | const char* str = &con_names[0]; |
jrose@1145 | 2273 | for (int i = 0; i < which; i++) |
jrose@1145 | 2274 | str += strlen(str) + 1; // skip name and null |
jrose@1145 | 2275 | oop name = java_lang_String::create_oop_from_str(str, CHECK_0); |
jrose@1145 | 2276 | box->obj_at_put(0, name); |
jrose@1145 | 2277 | } |
jrose@1145 | 2278 | return con; |
jrose@1145 | 2279 | } |
jrose@1145 | 2280 | #endif |
jrose@1145 | 2281 | return 0; |
jrose@1145 | 2282 | } |
jrose@1145 | 2283 | JVM_END |
jrose@1145 | 2284 | |
jrose@1145 | 2285 | // void init(MemberName self, AccessibleObject ref) |
jrose@1145 | 2286 | JVM_ENTRY(void, MHI_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) { |
jrose@1145 | 2287 | if (mname_jh == NULL || target_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } |
jrose@1145 | 2288 | Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); |
jrose@1145 | 2289 | oop target_oop = JNIHandles::resolve_non_null(target_jh); |
jrose@1145 | 2290 | MethodHandles::init_MemberName(mname(), target_oop); |
jrose@1145 | 2291 | } |
jrose@1145 | 2292 | JVM_END |
jrose@1145 | 2293 | |
jrose@1145 | 2294 | // void expand(MemberName self) |
jrose@1145 | 2295 | JVM_ENTRY(void, MHI_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) { |
jrose@1145 | 2296 | if (mname_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } |
jrose@1145 | 2297 | Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); |
jrose@1145 | 2298 | MethodHandles::expand_MemberName(mname, 0, CHECK); |
jrose@1145 | 2299 | } |
jrose@1145 | 2300 | JVM_END |
jrose@1145 | 2301 | |
jrose@1145 | 2302 | // void resolve(MemberName self, Class<?> caller) |
jrose@1145 | 2303 | JVM_ENTRY(void, MHI_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh)) { |
jrose@1145 | 2304 | if (mname_jh == NULL) { THROW(vmSymbols::java_lang_InternalError()); } |
jrose@1145 | 2305 | Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); |
jrose@1145 | 2306 | // %%% take caller into account! |
jrose@1145 | 2307 | MethodHandles::resolve_MemberName(mname, CHECK); |
jrose@1145 | 2308 | } |
jrose@1145 | 2309 | JVM_END |
jrose@1145 | 2310 | |
jrose@1145 | 2311 | // static native int getMembers(Class<?> defc, String matchName, String matchSig, |
jrose@1145 | 2312 | // int matchFlags, Class<?> caller, int skip, MemberName[] results); |
jrose@1145 | 2313 | JVM_ENTRY(jint, MHI_getMembers(JNIEnv *env, jobject igcls, |
jrose@1145 | 2314 | jclass clazz_jh, jstring name_jh, jstring sig_jh, |
jrose@1145 | 2315 | int mflags, jclass caller_jh, jint skip, jobjectArray results_jh)) { |
jrose@1145 | 2316 | if (clazz_jh == NULL || results_jh == NULL) return -1; |
jrose@1145 | 2317 | klassOop k_oop = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(clazz_jh)); |
jrose@1145 | 2318 | |
jrose@1145 | 2319 | objArrayOop results = (objArrayOop) JNIHandles::resolve(results_jh); |
jrose@1145 | 2320 | if (results == NULL || !results->is_objArray()) return -1; |
jrose@1145 | 2321 | |
jrose@1145 | 2322 | symbolOop name = NULL, sig = NULL; |
jrose@1145 | 2323 | if (name_jh != NULL) { |
jrose@1145 | 2324 | name = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(name_jh)); |
jrose@1145 | 2325 | if (name == NULL) return 0; // a match is not possible |
jrose@1145 | 2326 | } |
jrose@1145 | 2327 | if (sig_jh != NULL) { |
jrose@1145 | 2328 | sig = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(sig_jh)); |
jrose@1145 | 2329 | if (sig == NULL) return 0; // a match is not possible |
jrose@1145 | 2330 | } |
jrose@1145 | 2331 | |
jrose@1145 | 2332 | klassOop caller = NULL; |
jrose@1145 | 2333 | if (caller_jh != NULL) { |
jrose@1145 | 2334 | oop caller_oop = JNIHandles::resolve_non_null(caller_jh); |
jrose@1145 | 2335 | if (!java_lang_Class::is_instance(caller_oop)) return -1; |
jrose@1145 | 2336 | caller = java_lang_Class::as_klassOop(caller_oop); |
jrose@1145 | 2337 | } |
jrose@1145 | 2338 | |
jrose@1145 | 2339 | if (name != NULL && sig != NULL && results != NULL) { |
jrose@1145 | 2340 | // try a direct resolve |
jrose@1145 | 2341 | // %%% TO DO |
jrose@1145 | 2342 | } |
jrose@1145 | 2343 | |
jrose@1145 | 2344 | int res = MethodHandles::find_MemberNames(k_oop, name, sig, mflags, |
jrose@1145 | 2345 | caller, skip, results); |
jrose@1145 | 2346 | // TO DO: expand at least some of the MemberNames, to avoid massive callbacks |
jrose@1145 | 2347 | return res; |
jrose@1145 | 2348 | } |
jrose@1145 | 2349 | JVM_END |
jrose@1145 | 2350 | |
jrose@1145 | 2351 | |
jrose@1161 | 2352 | JVM_ENTRY(void, MH_linkCallSite(JNIEnv *env, jobject igcls, jobject site_jh, jobject target_jh)) { |
jrose@1161 | 2353 | // No special action required, yet. |
jrose@1161 | 2354 | oop site_oop = JNIHandles::resolve(site_jh); |
jrose@1494 | 2355 | if (site_oop == NULL || site_oop->klass() != SystemDictionary::CallSite_klass()) |
jrose@1161 | 2356 | THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "call site"); |
jrose@1494 | 2357 | java_dyn_CallSite::set_target(site_oop, JNIHandles::resolve(target_jh)); |
jrose@1161 | 2358 | } |
jrose@1161 | 2359 | JVM_END |
jrose@1161 | 2360 | |
jrose@1161 | 2361 | |
jrose@1145 | 2362 | /// JVM_RegisterMethodHandleMethods |
jrose@1145 | 2363 | |
jrose@1145 | 2364 | #define ADR "J" |
jrose@1145 | 2365 | |
jrose@1145 | 2366 | #define LANG "Ljava/lang/" |
jrose@1145 | 2367 | #define JDYN "Ljava/dyn/" |
jrose@1145 | 2368 | #define IDYN "Lsun/dyn/" |
jrose@1145 | 2369 | |
jrose@1145 | 2370 | #define OBJ LANG"Object;" |
jrose@1145 | 2371 | #define CLS LANG"Class;" |
jrose@1145 | 2372 | #define STRG LANG"String;" |
jrose@1494 | 2373 | #define CST JDYN"CallSite;" |
jrose@1145 | 2374 | #define MT JDYN"MethodType;" |
jrose@1145 | 2375 | #define MH JDYN"MethodHandle;" |
jrose@1145 | 2376 | #define MHI IDYN"MethodHandleImpl;" |
jrose@1145 | 2377 | #define MEM IDYN"MemberName;" |
jrose@1145 | 2378 | #define AMH IDYN"AdapterMethodHandle;" |
jrose@1145 | 2379 | #define BMH IDYN"BoundMethodHandle;" |
jrose@1145 | 2380 | #define DMH IDYN"DirectMethodHandle;" |
jrose@1145 | 2381 | |
jrose@1145 | 2382 | #define CC (char*) /*cast a literal from (const char*)*/ |
jrose@1145 | 2383 | #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) |
jrose@1145 | 2384 | |
jrose@1145 | 2385 | // These are the native methods on sun.dyn.MethodHandleNatives. |
jrose@1145 | 2386 | static JNINativeMethod methods[] = { |
jrose@1145 | 2387 | // void init(MemberName self, AccessibleObject ref) |
jrose@1145 | 2388 | {CC"init", CC"("AMH""MH"I)V", FN_PTR(MHI_init_AMH)}, |
jrose@1145 | 2389 | {CC"init", CC"("BMH""OBJ"I)V", FN_PTR(MHI_init_BMH)}, |
jrose@1145 | 2390 | {CC"init", CC"("DMH""OBJ"Z"CLS")V", FN_PTR(MHI_init_DMH)}, |
jrose@1145 | 2391 | {CC"init", CC"("MT")V", FN_PTR(MHI_init_MT)}, |
jrose@1145 | 2392 | {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHI_init_Mem)}, |
jrose@1145 | 2393 | {CC"expand", CC"("MEM")V", FN_PTR(MHI_expand_Mem)}, |
jrose@1145 | 2394 | {CC"resolve", CC"("MEM""CLS")V", FN_PTR(MHI_resolve_Mem)}, |
jrose@1145 | 2395 | {CC"getTarget", CC"("MH"I)"OBJ, FN_PTR(MHI_getTarget)}, |
jrose@1145 | 2396 | {CC"getConstant", CC"(I)I", FN_PTR(MHI_getConstant)}, |
jrose@1145 | 2397 | // static native int getNamedCon(int which, Object[] name) |
jrose@1145 | 2398 | {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHI_getNamedCon)}, |
jrose@1145 | 2399 | // static native int getMembers(Class<?> defc, String matchName, String matchSig, |
jrose@1145 | 2400 | // int matchFlags, Class<?> caller, int skip, MemberName[] results); |
jrose@1145 | 2401 | {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHI_getMembers)} |
jrose@1145 | 2402 | }; |
jrose@1145 | 2403 | |
jrose@1161 | 2404 | // More entry points specifically for EnableInvokeDynamic. |
jrose@1161 | 2405 | static JNINativeMethod methods2[] = { |
jrose@1494 | 2406 | {CC"linkCallSite", CC"("CST MH")V", FN_PTR(MH_linkCallSite)} |
jrose@1161 | 2407 | }; |
jrose@1161 | 2408 | |
jrose@1145 | 2409 | |
jrose@1145 | 2410 | // This one function is exported, used by NativeLookup. |
jrose@1145 | 2411 | |
jrose@1145 | 2412 | JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { |
jrose@1145 | 2413 | assert(MethodHandles::spot_check_entry_names(), "entry enum is OK"); |
jrose@1145 | 2414 | |
jrose@1161 | 2415 | // note: this explicit warning-producing stuff will be replaced by auto-detection of the JSR 292 classes |
jrose@1161 | 2416 | |
jrose@1145 | 2417 | if (!EnableMethodHandles) { |
jrose@1474 | 2418 | warning("JSR 292 method handles are disabled in this JVM. Use -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles to enable."); |
jrose@1145 | 2419 | return; // bind nothing |
jrose@1145 | 2420 | } |
jrose@1145 | 2421 | |
jrose@1474 | 2422 | bool enable_MH = true; |
jrose@1474 | 2423 | |
jrose@1145 | 2424 | { |
jrose@1145 | 2425 | ThreadToNativeFromVM ttnfv(thread); |
jrose@1145 | 2426 | |
jrose@1145 | 2427 | int status = env->RegisterNatives(MHN_class, methods, sizeof(methods)/sizeof(JNINativeMethod)); |
jrose@1145 | 2428 | if (env->ExceptionOccurred()) { |
jrose@1145 | 2429 | MethodHandles::set_enabled(false); |
jrose@1145 | 2430 | warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); |
jrose@1474 | 2431 | enable_MH = false; |
jrose@1145 | 2432 | env->ExceptionClear(); |
jrose@1145 | 2433 | } |
jrose@1145 | 2434 | } |
jrose@1161 | 2435 | |
jrose@1474 | 2436 | if (enable_MH) { |
jrose@1474 | 2437 | KlassHandle MHI_klass = SystemDictionaryHandles::MethodHandleImpl_klass(); |
jrose@1474 | 2438 | if (MHI_klass.not_null()) { |
jrose@1474 | 2439 | symbolHandle raiseException_name = oopFactory::new_symbol_handle("raiseException", CHECK); |
jrose@1474 | 2440 | symbolHandle raiseException_sig = oopFactory::new_symbol_handle("(ILjava/lang/Object;Ljava/lang/Object;)V", CHECK); |
jrose@1474 | 2441 | methodOop raiseException_method = instanceKlass::cast(MHI_klass->as_klassOop()) |
jrose@1474 | 2442 | ->find_method(raiseException_name(), raiseException_sig()); |
jrose@1474 | 2443 | if (raiseException_method != NULL && raiseException_method->is_static()) { |
jrose@1474 | 2444 | MethodHandles::set_raise_exception_method(raiseException_method); |
jrose@1474 | 2445 | } else { |
jrose@1474 | 2446 | warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); |
jrose@1474 | 2447 | enable_MH = false; |
jrose@1474 | 2448 | } |
jrose@1474 | 2449 | } |
jrose@1474 | 2450 | } |
jrose@1474 | 2451 | |
jrose@1474 | 2452 | if (enable_MH) { |
jrose@1474 | 2453 | MethodHandles::set_enabled(true); |
jrose@1474 | 2454 | } |
jrose@1474 | 2455 | |
jrose@1161 | 2456 | if (!EnableInvokeDynamic) { |
jrose@1474 | 2457 | warning("JSR 292 invokedynamic is disabled in this JVM. Use -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic to enable."); |
jrose@1161 | 2458 | return; // bind nothing |
jrose@1161 | 2459 | } |
jrose@1161 | 2460 | |
jrose@1161 | 2461 | { |
jrose@1161 | 2462 | ThreadToNativeFromVM ttnfv(thread); |
jrose@1161 | 2463 | |
jrose@1161 | 2464 | int status = env->RegisterNatives(MHN_class, methods2, sizeof(methods2)/sizeof(JNINativeMethod)); |
jrose@1161 | 2465 | if (env->ExceptionOccurred()) { |
jrose@1161 | 2466 | MethodHandles::set_enabled(false); |
jrose@1161 | 2467 | warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); |
jrose@1161 | 2468 | env->ExceptionClear(); |
jrose@1161 | 2469 | } else { |
jrose@1161 | 2470 | MethodHandles::set_enabled(true); |
jrose@1161 | 2471 | } |
jrose@1161 | 2472 | } |
jrose@1145 | 2473 | } |
jrose@1145 | 2474 | JVM_END |