annotate src/share/vm/prims/methodHandles.cpp @ 1983:8d0b933dda2d

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