twisti@1568: /* twisti@2437: * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. twisti@1568: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. twisti@1568: * twisti@1568: * This code is free software; you can redistribute it and/or modify it twisti@1568: * under the terms of the GNU General Public License version 2 only, as twisti@1568: * published by the Free Software Foundation. twisti@1568: * twisti@1568: * This code is distributed in the hope that it will be useful, but WITHOUT twisti@1568: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or twisti@1568: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License twisti@1568: * version 2 for more details (a copy is included in the LICENSE file that twisti@1568: * accompanied this code). twisti@1568: * twisti@1568: * You should have received a copy of the GNU General Public License version twisti@1568: * 2 along with this work; if not, write to the Free Software Foundation, twisti@1568: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. twisti@1568: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. twisti@1568: * twisti@1568: */ twisti@1568: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "interpreter/rewriter.hpp" stefank@2314: #include "memory/oopFactory.hpp" stefank@2314: #include "prims/methodHandleWalk.hpp" stefank@2314: twisti@1568: /* twisti@1568: * JSR 292 reference implementation: method handle structure analysis twisti@1568: */ twisti@1568: twisti@2903: #ifdef PRODUCT twisti@2903: #define print_method_handle(mh) {} twisti@2903: #else //PRODUCT twisti@2903: extern "C" void print_method_handle(oop mh); twisti@2903: #endif //PRODUCT twisti@1573: twisti@1573: // ----------------------------------------------------------------------------- twisti@1573: // MethodHandleChain twisti@1573: twisti@1568: void MethodHandleChain::set_method_handle(Handle mh, TRAPS) { jrose@2639: if (!java_lang_invoke_MethodHandle::is_instance(mh())) lose("bad method handle", CHECK); twisti@1568: twisti@1568: // set current method handle and unpack partially twisti@1568: _method_handle = mh; twisti@1568: _is_last = false; twisti@1568: _is_bound = false; twisti@1568: _arg_slot = -1; twisti@1568: _arg_type = T_VOID; twisti@1568: _conversion = -1; twisti@1568: _last_invoke = Bytecodes::_nop; //arbitrary non-garbage twisti@1568: jrose@2639: if (java_lang_invoke_DirectMethodHandle::is_instance(mh())) { twisti@1568: set_last_method(mh(), THREAD); twisti@1568: return; twisti@1568: } jrose@2639: if (java_lang_invoke_AdapterMethodHandle::is_instance(mh())) { twisti@1568: _conversion = AdapterMethodHandle_conversion(); twisti@1568: assert(_conversion != -1, "bad conv value"); jrose@2639: assert(java_lang_invoke_BoundMethodHandle::is_instance(mh()), "also BMH"); twisti@1568: } jrose@2639: if (java_lang_invoke_BoundMethodHandle::is_instance(mh())) { twisti@1568: if (!is_adapter()) // keep AMH and BMH separate in this model twisti@1568: _is_bound = true; twisti@1568: _arg_slot = BoundMethodHandle_vmargslot(); twisti@1568: oop target = MethodHandle_vmtarget_oop(); jrose@2639: if (!is_bound() || java_lang_invoke_MethodHandle::is_instance(target)) { twisti@1568: _arg_type = compute_bound_arg_type(target, NULL, _arg_slot, CHECK); twisti@1568: } else if (target != NULL && target->is_method()) { twisti@1573: methodOop m = (methodOop) target; twisti@1573: _arg_type = compute_bound_arg_type(NULL, m, _arg_slot, CHECK); twisti@1568: set_last_method(mh(), CHECK); twisti@1568: } else { twisti@1568: _is_bound = false; // lose! twisti@1568: } twisti@1568: } twisti@1568: if (is_bound() && _arg_type == T_VOID) { twisti@1568: lose("bad vmargslot", CHECK); twisti@1568: } twisti@1568: if (!is_bound() && !is_adapter()) { twisti@1568: lose("unrecognized MH type", CHECK); twisti@1568: } twisti@1568: } twisti@1568: twisti@1573: twisti@1568: void MethodHandleChain::set_last_method(oop target, TRAPS) { twisti@1568: _is_last = true; twisti@2806: KlassHandle receiver_limit; int flags = 0; twisti@2806: _last_method = MethodHandles::decode_method(target, receiver_limit, flags); twisti@1568: if ((flags & MethodHandles::_dmf_has_receiver) == 0) twisti@1568: _last_invoke = Bytecodes::_invokestatic; twisti@1568: else if ((flags & MethodHandles::_dmf_does_dispatch) == 0) twisti@1568: _last_invoke = Bytecodes::_invokespecial; twisti@1568: else if ((flags & MethodHandles::_dmf_from_interface) != 0) twisti@1568: _last_invoke = Bytecodes::_invokeinterface; twisti@1568: else twisti@1568: _last_invoke = Bytecodes::_invokevirtual; twisti@1568: } twisti@1568: twisti@1573: twisti@1568: BasicType MethodHandleChain::compute_bound_arg_type(oop target, methodOop m, int arg_slot, TRAPS) { twisti@1568: // There is no direct indication of whether the argument is primitive or not. twisti@1568: // It is implied by the _vmentry code, and by the MethodType of the target. twisti@1568: BasicType arg_type = T_VOID; twisti@1568: if (target != NULL) { jrose@2639: oop mtype = java_lang_invoke_MethodHandle::type(target); twisti@1568: int arg_num = MethodHandles::argument_slot_to_argnum(mtype, arg_slot); twisti@1568: if (arg_num >= 0) { jrose@2639: oop ptype = java_lang_invoke_MethodType::ptype(mtype, arg_num); twisti@1568: arg_type = java_lang_Class::as_BasicType(ptype); twisti@1568: } twisti@1568: } else if (m != NULL) { twisti@1568: // figure out the argument type from the slot twisti@1568: // FIXME: make this explicit in the MH twisti@1568: int cur_slot = m->size_of_parameters(); twisti@1568: if (arg_slot >= cur_slot) twisti@1568: return T_VOID; twisti@1568: if (!m->is_static()) { twisti@1568: cur_slot -= type2size[T_OBJECT]; twisti@1568: if (cur_slot == arg_slot) twisti@1568: return T_OBJECT; twisti@1568: } coleenp@2497: ResourceMark rm(THREAD); twisti@1568: for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) { twisti@1568: BasicType bt = ss.type(); twisti@1568: cur_slot -= type2size[bt]; twisti@1568: if (cur_slot <= arg_slot) { twisti@1568: if (cur_slot == arg_slot) twisti@1568: arg_type = bt; twisti@1568: break; twisti@1568: } twisti@1568: } twisti@1568: } twisti@1568: if (arg_type == T_ARRAY) twisti@1568: arg_type = T_OBJECT; twisti@1568: return arg_type; twisti@1568: } twisti@1568: twisti@1573: twisti@1568: void MethodHandleChain::lose(const char* msg, TRAPS) { twisti@1568: _lose_message = msg; never@2937: #ifdef ASSERT never@2937: if (Verbose) { never@2937: tty->print_cr(INTPTR_FORMAT " lose: %s", _method_handle(), msg); never@2937: print(); never@2937: } never@2937: #endif twisti@1568: if (!THREAD->is_Java_thread() || ((JavaThread*)THREAD)->thread_state() != _thread_in_vm) { twisti@1568: // throw a preallocated exception twisti@1568: THROW_OOP(Universe::virtual_machine_error_instance()); twisti@1568: } twisti@1568: THROW_MSG(vmSymbols::java_lang_InternalError(), msg); twisti@1568: } twisti@1568: twisti@1573: never@2937: #ifdef ASSERT never@2937: static const char* adapter_ops[] = { never@2937: "retype_only" , never@2937: "retype_raw" , never@2937: "check_cast" , never@2937: "prim_to_prim" , never@2937: "ref_to_prim" , never@2937: "prim_to_ref" , never@2937: "swap_args" , never@2937: "rot_args" , never@2937: "dup_args" , never@2937: "drop_args" , never@2937: "collect_args" , never@2937: "spread_args" , never@2937: "fold_args" never@2937: }; never@2937: never@2937: static const char* adapter_op_to_string(int op) { never@2937: if (op >= 0 && op < (int)ARRAY_SIZE(adapter_ops)) never@2937: return adapter_ops[op]; never@2937: return "unknown_op"; never@2937: } never@2937: never@2950: void MethodHandleChain::print(oopDesc* m) { never@2950: HandleMark hm; never@2950: ResourceMark rm; never@2950: Handle mh(m); never@2937: EXCEPTION_MARK; never@2937: MethodHandleChain mhc(mh, THREAD); never@2937: if (HAS_PENDING_EXCEPTION) { never@2937: oop ex = THREAD->pending_exception(); never@2937: CLEAR_PENDING_EXCEPTION; never@2937: ex->print(); never@2937: return; never@2937: } never@2937: mhc.print(); never@2937: } never@2937: never@2937: never@2937: void MethodHandleChain::print() { never@2937: EXCEPTION_MARK; never@2937: print_impl(THREAD); never@2937: if (HAS_PENDING_EXCEPTION) { never@2937: oop ex = THREAD->pending_exception(); never@2937: CLEAR_PENDING_EXCEPTION; never@2937: ex->print(); never@2937: } never@2937: } never@2937: never@2937: void MethodHandleChain::print_impl(TRAPS) { never@2937: ResourceMark rm; never@2937: never@2937: MethodHandleChain chain(_root, CHECK); never@2937: for (;;) { never@2937: tty->print(INTPTR_FORMAT ": ", chain.method_handle()()); never@2937: if (chain.is_bound()) { never@2937: tty->print("bound: arg_type %s arg_slot %d", never@2937: type2name(chain.bound_arg_type()), never@2937: chain.bound_arg_slot()); never@2937: oop o = chain.bound_arg_oop(); never@2937: if (o != NULL) { never@2937: if (o->is_instance()) { never@2937: tty->print(" instance %s", o->klass()->klass_part()->internal_name()); never@3105: if (java_lang_invoke_CountingMethodHandle::is_instance(o)) { never@3105: tty->print(" vmcount: %d", java_lang_invoke_CountingMethodHandle::vmcount(o)); never@3105: } never@2937: } else { never@2937: o->print(); never@2937: } never@2937: } never@3105: oop vmt = chain.vmtarget_oop(); never@3105: if (vmt != NULL) { never@3105: if (vmt->is_method()) { never@3105: tty->print(" "); never@3105: methodOop(vmt)->print_short_name(tty); never@3105: } else if (java_lang_invoke_MethodHandle::is_instance(vmt)) { never@3105: tty->print(" method handle " INTPTR_FORMAT, vmt); never@3105: } else { never@3105: ShouldNotReachHere(); never@3105: } never@3105: } never@2937: } else if (chain.is_adapter()) { never@2937: tty->print("adapter: arg_slot %d conversion op %s", never@2937: chain.adapter_arg_slot(), never@2937: adapter_op_to_string(chain.adapter_conversion_op())); never@2937: switch (chain.adapter_conversion_op()) { never@2937: case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY: never@3105: if (java_lang_invoke_CountingMethodHandle::is_instance(chain.method_handle_oop())) { never@3105: tty->print(" vmcount: %d", java_lang_invoke_CountingMethodHandle::vmcount(chain.method_handle_oop())); never@3105: } never@2937: case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW: never@2937: case java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST: never@2937: case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM: never@2937: case java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM: never@2937: break; never@2937: never@2954: case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF: { never@2954: tty->print(" src_type = %s", type2name(chain.adapter_conversion_src_type())); never@2954: break; never@2954: } never@2954: never@2937: case java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS: never@2937: case java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS: { never@2937: int dest_arg_slot = chain.adapter_conversion_vminfo(); never@2937: tty->print(" dest_arg_slot %d type %s", dest_arg_slot, type2name(chain.adapter_conversion_src_type())); never@2937: break; never@2937: } never@2937: never@2937: case java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS: never@2937: case java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS: { never@2937: int dup_slots = chain.adapter_conversion_stack_pushes(); never@2937: tty->print(" pushes %d", dup_slots); never@2937: break; never@2937: } never@2937: never@2937: case java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS: never@2937: case java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS: { never@2937: int coll_slots = chain.MethodHandle_vmslots(); never@2937: tty->print(" coll_slots %d", coll_slots); never@2937: break; never@2937: } never@2937: never@2937: case java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS: { never@2937: // Check the required length. never@2937: int spread_slots = 1 + chain.adapter_conversion_stack_pushes(); never@2937: tty->print(" spread_slots %d", spread_slots); never@2937: break; never@2937: } never@2937: never@2937: default: never@2937: tty->print_cr("bad adapter conversion"); never@2937: break; never@2937: } never@2937: } else { never@2937: // DMH never@2937: tty->print("direct: "); never@2937: chain.last_method_oop()->print_short_name(tty); never@2937: } never@2937: never@2937: tty->print(" ("); never@2937: objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(chain.method_type_oop()); never@2937: for (int i = ptypes->length() - 1; i >= 0; i--) { never@2937: BasicType t = java_lang_Class::as_BasicType(ptypes->obj_at(i)); never@2937: if (t == T_ARRAY) t = T_OBJECT; never@2937: tty->print("%c", type2char(t)); never@2937: if (t == T_LONG || t == T_DOUBLE) tty->print("_"); never@2937: } never@2937: tty->print(")"); never@2937: BasicType rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(chain.method_type_oop())); never@2937: if (rtype == T_ARRAY) rtype = T_OBJECT; never@2937: tty->print("%c", type2char(rtype)); never@2937: tty->cr(); never@2937: if (!chain.is_last()) { never@2937: chain.next(CHECK); never@2937: } else { never@2937: break; never@2937: } never@2937: } never@2937: } never@2937: #endif never@2937: never@2937: twisti@1573: // ----------------------------------------------------------------------------- twisti@1573: // MethodHandleWalker twisti@1573: twisti@1568: Bytecodes::Code MethodHandleWalker::conversion_code(BasicType src, BasicType dest) { twisti@1568: if (is_subword_type(src)) { twisti@1568: src = T_INT; // all subword src types act like int twisti@1568: } twisti@1568: if (src == dest) { twisti@1568: return Bytecodes::_nop; twisti@1568: } twisti@1568: twisti@1568: #define SRC_DEST(s,d) (((int)(s) << 4) + (int)(d)) twisti@1568: switch (SRC_DEST(src, dest)) { twisti@1568: case SRC_DEST(T_INT, T_LONG): return Bytecodes::_i2l; twisti@1568: case SRC_DEST(T_INT, T_FLOAT): return Bytecodes::_i2f; twisti@1568: case SRC_DEST(T_INT, T_DOUBLE): return Bytecodes::_i2d; twisti@1568: case SRC_DEST(T_INT, T_BYTE): return Bytecodes::_i2b; twisti@1568: case SRC_DEST(T_INT, T_CHAR): return Bytecodes::_i2c; twisti@1568: case SRC_DEST(T_INT, T_SHORT): return Bytecodes::_i2s; twisti@1568: twisti@1568: case SRC_DEST(T_LONG, T_INT): return Bytecodes::_l2i; twisti@1568: case SRC_DEST(T_LONG, T_FLOAT): return Bytecodes::_l2f; twisti@1568: case SRC_DEST(T_LONG, T_DOUBLE): return Bytecodes::_l2d; twisti@1568: twisti@1568: case SRC_DEST(T_FLOAT, T_INT): return Bytecodes::_f2i; twisti@1568: case SRC_DEST(T_FLOAT, T_LONG): return Bytecodes::_f2l; twisti@1568: case SRC_DEST(T_FLOAT, T_DOUBLE): return Bytecodes::_f2d; twisti@1568: twisti@1568: case SRC_DEST(T_DOUBLE, T_INT): return Bytecodes::_d2i; twisti@1568: case SRC_DEST(T_DOUBLE, T_LONG): return Bytecodes::_d2l; twisti@1568: case SRC_DEST(T_DOUBLE, T_FLOAT): return Bytecodes::_d2f; twisti@1568: } twisti@1568: #undef SRC_DEST twisti@1568: twisti@1568: // cannot do it in one step, or at all twisti@1568: return Bytecodes::_illegal; twisti@1568: } twisti@1568: twisti@1573: twisti@1573: // ----------------------------------------------------------------------------- twisti@1573: // MethodHandleWalker::walk twisti@1573: // twisti@1568: MethodHandleWalker::ArgToken twisti@1568: MethodHandleWalker::walk(TRAPS) { twisti@1573: ArgToken empty = ArgToken(); // Empty return value. twisti@1573: twisti@1573: walk_incoming_state(CHECK_(empty)); twisti@1568: twisti@1568: for (;;) { twisti@1568: set_method_handle(chain().method_handle_oop()); twisti@1568: twisti@1568: assert(_outgoing_argc == argument_count_slow(), "empty slots under control"); twisti@1568: twisti@1568: if (chain().is_adapter()) { twisti@1568: int conv_op = chain().adapter_conversion_op(); twisti@1568: int arg_slot = chain().adapter_arg_slot(); never@2937: never@2937: // Check that the arg_slot is valid. In most cases it must be never@2937: // within range of the current arguments but there are some never@2937: // exceptions. Those are sanity checked in their implemention never@2937: // below. never@2937: if ((arg_slot < 0 || arg_slot >= _outgoing.length()) && never@2937: conv_op > java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW && never@2937: conv_op != java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS && never@2937: conv_op != java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS) { never@2937: lose(err_msg("bad argument index %d", arg_slot), CHECK_(empty)); twisti@1568: } twisti@1568: twisti@2903: bool retain_original_args = false; // used by fold/collect logic twisti@2903: twisti@1568: // perform the adapter action twisti@2903: switch (conv_op) { jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY: twisti@1568: // No changes to arguments; pass the bits through. twisti@1568: break; twisti@1568: jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW: { twisti@1573: // To keep the verifier happy, emit bitwise ("raw") conversions as needed. twisti@1573: // See MethodHandles::same_basic_type_for_arguments for allowed conversions. twisti@1573: Handle incoming_mtype(THREAD, chain().method_type_oop()); twisti@2903: Handle outgoing_mtype; twisti@2903: { twisti@2903: oop outgoing_mh_oop = chain().vmtarget_oop(); twisti@2903: if (!java_lang_invoke_MethodHandle::is_instance(outgoing_mh_oop)) twisti@2903: lose("outgoing target not a MethodHandle", CHECK_(empty)); twisti@2903: outgoing_mtype = Handle(THREAD, java_lang_invoke_MethodHandle::type(outgoing_mh_oop)); twisti@2903: } twisti@1573: jrose@2639: int nptypes = java_lang_invoke_MethodType::ptype_count(outgoing_mtype()); jrose@2639: if (nptypes != java_lang_invoke_MethodType::ptype_count(incoming_mtype())) twisti@1573: lose("incoming and outgoing parameter count do not agree", CHECK_(empty)); twisti@1573: twisti@2903: // Argument types. twisti@1573: for (int i = 0, slot = _outgoing.length() - 1; slot >= 0; slot--) { never@2937: if (arg_type(slot) == T_VOID) continue; twisti@1573: twisti@2903: klassOop src_klass = NULL; twisti@2903: klassOop dst_klass = NULL; twisti@2903: BasicType src = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(incoming_mtype(), i), &src_klass); twisti@2903: BasicType dst = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(outgoing_mtype(), i), &dst_klass); twisti@2903: retype_raw_argument_type(src, dst, slot, CHECK_(empty)); twisti@1573: i++; // We need to skip void slots at the top of the loop. twisti@1573: } twisti@1573: twisti@2903: // Return type. twisti@2903: { twisti@2903: BasicType src = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(incoming_mtype())); twisti@2903: BasicType dst = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(outgoing_mtype())); twisti@2903: retype_raw_return_type(src, dst, CHECK_(empty)); twisti@1573: } twisti@1573: break; twisti@1573: } twisti@1573: jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST: { twisti@1568: // checkcast the Nth outgoing argument in place twisti@1568: klassOop dest_klass = NULL; twisti@1568: BasicType dest = java_lang_Class::as_BasicType(chain().adapter_arg_oop(), &dest_klass); twisti@1568: assert(dest == T_OBJECT, ""); never@2937: ArgToken arg = _outgoing.at(arg_slot); never@2937: assert(dest == arg.basic_type(), ""); never@2950: arg = make_conversion(T_OBJECT, dest_klass, Bytecodes::_checkcast, arg, CHECK_(empty)); jrose@2982: // replace the object by the result of the cast, to make the compiler happy: jrose@2982: change_argument(T_OBJECT, arg_slot, T_OBJECT, arg); twisti@1568: debug_only(dest_klass = (klassOop)badOop); twisti@1568: break; twisti@1568: } twisti@1568: jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM: { twisti@1568: // i2l, etc., on the Nth outgoing argument in place twisti@1568: BasicType src = chain().adapter_conversion_src_type(), twisti@1568: dest = chain().adapter_conversion_dest_type(); never@2937: ArgToken arg = _outgoing.at(arg_slot); twisti@1568: Bytecodes::Code bc = conversion_code(src, dest); twisti@1568: if (bc == Bytecodes::_nop) { twisti@1568: break; twisti@1568: } else if (bc != Bytecodes::_illegal) { twisti@1573: arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty)); twisti@1568: } else if (is_subword_type(dest)) { twisti@1568: bc = conversion_code(src, T_INT); twisti@1568: if (bc != Bytecodes::_illegal) { twisti@1573: arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty)); twisti@1568: bc = conversion_code(T_INT, dest); twisti@1573: arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty)); twisti@1568: } twisti@1568: } twisti@1568: if (bc == Bytecodes::_illegal) { never@2937: lose(err_msg("bad primitive conversion for %s -> %s", type2name(src), type2name(dest)), CHECK_(empty)); twisti@1568: } twisti@1568: change_argument(src, arg_slot, dest, arg); twisti@1568: break; twisti@1568: } twisti@1568: jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM: { twisti@1568: // checkcast to wrapper type & call intValue, etc. twisti@1568: BasicType dest = chain().adapter_conversion_dest_type(); never@2937: ArgToken arg = _outgoing.at(arg_slot); twisti@1568: arg = make_conversion(T_OBJECT, SystemDictionary::box_klass(dest), twisti@1573: Bytecodes::_checkcast, arg, CHECK_(empty)); twisti@1568: vmIntrinsics::ID unboxer = vmIntrinsics::for_unboxing(dest); twisti@1568: if (unboxer == vmIntrinsics::_none) { twisti@1573: lose("no unboxing method", CHECK_(empty)); twisti@1568: } twisti@1568: ArgToken arglist[2]; twisti@1573: arglist[0] = arg; // outgoing 'this' twisti@1573: arglist[1] = ArgToken(); // sentinel jrose@2982: arg = make_invoke(methodHandle(), unboxer, Bytecodes::_invokevirtual, false, 1, &arglist[0], CHECK_(empty)); twisti@1568: change_argument(T_OBJECT, arg_slot, dest, arg); twisti@1568: break; twisti@1568: } twisti@1568: jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF: { twisti@1568: // call wrapper type.valueOf twisti@1568: BasicType src = chain().adapter_conversion_src_type(); twisti@1568: vmIntrinsics::ID boxer = vmIntrinsics::for_boxing(src); twisti@1568: if (boxer == vmIntrinsics::_none) { twisti@1573: lose("no boxing method", CHECK_(empty)); twisti@1568: } never@2937: ArgToken arg = _outgoing.at(arg_slot); twisti@1568: ArgToken arglist[2]; twisti@1573: arglist[0] = arg; // outgoing value twisti@1573: arglist[1] = ArgToken(); // sentinel jrose@2982: arg = make_invoke(methodHandle(), boxer, Bytecodes::_invokestatic, false, 1, &arglist[0], CHECK_(empty)); twisti@1568: change_argument(src, arg_slot, T_OBJECT, arg); twisti@1568: break; twisti@1568: } twisti@1568: jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS: { twisti@1568: int dest_arg_slot = chain().adapter_conversion_vminfo(); never@2937: if (!has_argument(dest_arg_slot)) { twisti@1573: lose("bad swap index", CHECK_(empty)); twisti@1568: } twisti@1568: // a simple swap between two arguments never@2937: if (arg_slot > dest_arg_slot) { never@2937: int tmp = arg_slot; never@2937: arg_slot = dest_arg_slot; never@2937: dest_arg_slot = tmp; never@2937: } never@2937: ArgToken a1 = _outgoing.at(arg_slot); never@2937: ArgToken a2 = _outgoing.at(dest_arg_slot); never@2937: change_argument(a2.basic_type(), dest_arg_slot, a1); never@2937: change_argument(a1.basic_type(), arg_slot, a2); twisti@1568: break; twisti@1568: } twisti@1568: jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS: { never@2954: int limit_raw = chain().adapter_conversion_vminfo(); never@2954: bool rot_down = (arg_slot < limit_raw); never@2954: int limit_bias = (rot_down ? MethodHandles::OP_ROT_ARGS_DOWN_LIMIT_BIAS : 0); never@2954: int limit_slot = limit_raw - limit_bias; never@2954: if ((uint)limit_slot > (uint)_outgoing.length()) { twisti@1573: lose("bad rotate index", CHECK_(empty)); twisti@1568: } twisti@1568: // Rotate the source argument (plus following N slots) into the twisti@1568: // position occupied by the dest argument (plus following N slots). never@2937: int rotate_count = type2size[chain().adapter_conversion_src_type()]; twisti@1568: // (no other rotate counts are currently supported) never@2954: if (rot_down) { twisti@1568: for (int i = 0; i < rotate_count; i++) { never@2937: ArgToken temp = _outgoing.at(arg_slot); twisti@1568: _outgoing.remove_at(arg_slot); never@2954: _outgoing.insert_before(limit_slot - 1, temp); twisti@1568: } never@2954: } else { // arg_slot > limit_slot => rotate_up twisti@1568: for (int i = 0; i < rotate_count; i++) { never@2937: ArgToken temp = _outgoing.at(arg_slot + rotate_count - 1); twisti@1568: _outgoing.remove_at(arg_slot + rotate_count - 1); never@2954: _outgoing.insert_before(limit_slot, temp); twisti@1568: } twisti@1568: } never@2937: assert(_outgoing_argc == argument_count_slow(), "empty slots under control"); twisti@1568: break; twisti@1568: } twisti@1568: jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS: { twisti@1568: int dup_slots = chain().adapter_conversion_stack_pushes(); twisti@1568: if (dup_slots <= 0) { twisti@1573: lose("bad dup count", CHECK_(empty)); twisti@1568: } twisti@1568: for (int i = 0; i < dup_slots; i++) { never@2937: ArgToken dup = _outgoing.at(arg_slot + 2*i); never@2937: if (dup.basic_type() != T_VOID) _outgoing_argc += 1; never@2937: _outgoing.insert_before(i, dup); twisti@1568: } never@2937: assert(_outgoing_argc == argument_count_slow(), "empty slots under control"); twisti@1568: break; twisti@1568: } twisti@1568: jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS: { twisti@1568: int drop_slots = -chain().adapter_conversion_stack_pushes(); twisti@1568: if (drop_slots <= 0) { twisti@1573: lose("bad drop count", CHECK_(empty)); twisti@1568: } twisti@1568: for (int i = 0; i < drop_slots; i++) { never@2937: ArgToken drop = _outgoing.at(arg_slot); never@2937: if (drop.basic_type() != T_VOID) _outgoing_argc -= 1; twisti@1568: _outgoing.remove_at(arg_slot); twisti@1568: } never@2937: assert(_outgoing_argc == argument_count_slow(), "empty slots under control"); twisti@1568: break; twisti@1568: } twisti@1568: twisti@2903: case java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS: twisti@2903: retain_original_args = true; // and fall through: twisti@2903: case java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS: { twisti@2903: // call argument MH recursively twisti@2903: //{static int x; if (!x++) print_method_handle(chain().method_handle_oop()); --x;} twisti@2903: Handle recursive_mh(THREAD, chain().adapter_arg_oop()); twisti@2903: if (!java_lang_invoke_MethodHandle::is_instance(recursive_mh())) { twisti@2903: lose("recursive target not a MethodHandle", CHECK_(empty)); twisti@2903: } twisti@2903: Handle recursive_mtype(THREAD, java_lang_invoke_MethodHandle::type(recursive_mh())); twisti@2903: int argc = java_lang_invoke_MethodType::ptype_count(recursive_mtype()); twisti@2903: int coll_slots = java_lang_invoke_MethodHandle::vmslots(recursive_mh()); twisti@2903: BasicType rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(recursive_mtype())); twisti@2903: ArgToken* arglist = NEW_RESOURCE_ARRAY(ArgToken, 1 + argc + 1); // 1+: mh, +1: sentinel twisti@2903: arglist[0] = make_oop_constant(recursive_mh(), CHECK_(empty)); twisti@2903: if (arg_slot < 0 || coll_slots < 0 || arg_slot + coll_slots > _outgoing.length()) { twisti@2903: lose("bad fold/collect arg slot", CHECK_(empty)); twisti@2903: } twisti@2903: for (int i = 0, slot = arg_slot + coll_slots - 1; slot >= arg_slot; slot--) { never@2937: ArgToken arg_state = _outgoing.at(slot); never@2937: BasicType arg_type = arg_state.basic_type(); twisti@2903: if (arg_type == T_VOID) continue; never@2937: ArgToken arg = _outgoing.at(slot); twisti@2903: if (i >= argc) { lose("bad fold/collect arg", CHECK_(empty)); } twisti@2903: arglist[1+i] = arg; twisti@2903: if (!retain_original_args) twisti@2903: change_argument(arg_type, slot, T_VOID, ArgToken(tt_void)); bdelsart@2917: i++; twisti@2903: } twisti@2903: arglist[1+argc] = ArgToken(); // sentinel twisti@2903: oop invoker = java_lang_invoke_MethodTypeForm::vmlayout( twisti@2903: java_lang_invoke_MethodType::form(recursive_mtype()) ); twisti@2903: if (invoker == NULL || !invoker->is_method()) { twisti@2903: lose("bad vmlayout slot", CHECK_(empty)); twisti@2903: } twisti@2903: // FIXME: consider inlining the invokee at the bytecode level jrose@2982: ArgToken ret = make_invoke(methodHandle(THREAD, methodOop(invoker)), vmIntrinsics::_invokeGeneric, twisti@2903: Bytecodes::_invokevirtual, false, 1+argc, &arglist[0], CHECK_(empty)); jrose@2982: // The iid = _invokeGeneric really means to adjust reference types as needed. twisti@2903: DEBUG_ONLY(invoker = NULL); twisti@2903: if (rtype == T_OBJECT) { twisti@2903: klassOop rklass = java_lang_Class::as_klassOop( java_lang_invoke_MethodType::rtype(recursive_mtype()) ); twisti@2903: if (rklass != SystemDictionary::Object_klass() && twisti@2903: !Klass::cast(rklass)->is_interface()) { twisti@2903: // preserve type safety twisti@2903: ret = make_conversion(T_OBJECT, rklass, Bytecodes::_checkcast, ret, CHECK_(empty)); twisti@2903: } twisti@2903: } never@2920: if (rtype != T_VOID) { never@2920: int ret_slot = arg_slot + (retain_original_args ? coll_slots : 0); never@2920: change_argument(T_VOID, ret_slot, rtype, ret); never@2920: } never@2895: break; never@2895: } never@2895: jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS: { twisti@1568: klassOop array_klass_oop = NULL; twisti@1568: BasicType array_type = java_lang_Class::as_BasicType(chain().adapter_arg_oop(), twisti@1568: &array_klass_oop); twisti@1568: assert(array_type == T_OBJECT, ""); twisti@1568: assert(Klass::cast(array_klass_oop)->oop_is_array(), ""); twisti@1568: arrayKlassHandle array_klass(THREAD, array_klass_oop); twisti@1568: debug_only(array_klass_oop = (klassOop)badOop); twisti@1568: twisti@1568: klassOop element_klass_oop = NULL; twisti@1568: BasicType element_type = java_lang_Class::as_BasicType(array_klass->component_mirror(), twisti@1568: &element_klass_oop); twisti@1568: KlassHandle element_klass(THREAD, element_klass_oop); twisti@1568: debug_only(element_klass_oop = (klassOop)badOop); twisti@1568: twisti@1568: // Fetch the argument, which we will cast to the required array type. never@2937: ArgToken arg = _outgoing.at(arg_slot); never@2937: assert(arg.basic_type() == T_OBJECT, ""); never@2937: ArgToken array_arg = arg; twisti@1573: array_arg = make_conversion(T_OBJECT, array_klass(), Bytecodes::_checkcast, array_arg, CHECK_(empty)); twisti@1573: change_argument(T_OBJECT, arg_slot, T_VOID, ArgToken(tt_void)); twisti@1568: twisti@1568: // Check the required length. twisti@1568: int spread_slots = 1 + chain().adapter_conversion_stack_pushes(); twisti@1568: int spread_length = spread_slots; twisti@1568: if (type2size[element_type] == 2) { twisti@1568: if (spread_slots % 2 != 0) spread_slots = -1; // force error twisti@1568: spread_length = spread_slots / 2; twisti@1568: } twisti@1568: if (spread_slots < 0) { twisti@1573: lose("bad spread length", CHECK_(empty)); twisti@1568: } twisti@1568: twisti@1568: jvalue length_jvalue; length_jvalue.i = spread_length; twisti@1573: ArgToken length_arg = make_prim_constant(T_INT, &length_jvalue, CHECK_(empty)); twisti@1568: // Call a built-in method known to the JVM to validate the length. twisti@1568: ArgToken arglist[3]; twisti@1573: arglist[0] = array_arg; // value to check twisti@1573: arglist[1] = length_arg; // length to check twisti@1573: arglist[2] = ArgToken(); // sentinel jrose@2982: make_invoke(methodHandle(), vmIntrinsics::_checkSpreadArgument, bdelsart@2917: Bytecodes::_invokestatic, false, 2, &arglist[0], CHECK_(empty)); twisti@1568: twisti@1568: // Spread out the array elements. never@2895: Bytecodes::Code aload_op = Bytecodes::_nop; never@2895: switch (element_type) { never@2895: case T_INT: aload_op = Bytecodes::_iaload; break; never@2895: case T_LONG: aload_op = Bytecodes::_laload; break; never@2895: case T_FLOAT: aload_op = Bytecodes::_faload; break; never@2895: case T_DOUBLE: aload_op = Bytecodes::_daload; break; never@2895: case T_OBJECT: aload_op = Bytecodes::_aaload; break; never@2895: case T_BOOLEAN: // fall through: never@2895: case T_BYTE: aload_op = Bytecodes::_baload; break; never@2895: case T_CHAR: aload_op = Bytecodes::_caload; break; never@2895: case T_SHORT: aload_op = Bytecodes::_saload; break; never@2895: default: lose("primitive array NYI", CHECK_(empty)); twisti@1568: } twisti@1568: int ap = arg_slot; twisti@1568: for (int i = 0; i < spread_length; i++) { twisti@1568: jvalue offset_jvalue; offset_jvalue.i = i; twisti@1573: ArgToken offset_arg = make_prim_constant(T_INT, &offset_jvalue, CHECK_(empty)); twisti@1573: ArgToken element_arg = make_fetch(element_type, element_klass(), aload_op, array_arg, offset_arg, CHECK_(empty)); twisti@1568: change_argument(T_VOID, ap, element_type, element_arg); jrose@2982: //ap += type2size[element_type]; // don't do this; insert next arg to *right* of previous twisti@1568: } twisti@1568: break; twisti@1568: } twisti@1568: twisti@1568: default: twisti@1573: lose("bad adapter conversion", CHECK_(empty)); twisti@1568: break; twisti@1568: } twisti@1568: } twisti@1568: twisti@1568: if (chain().is_bound()) { twisti@1568: // push a new argument twisti@1568: BasicType arg_type = chain().bound_arg_type(); twisti@1568: jint arg_slot = chain().bound_arg_slot(); twisti@1568: oop arg_oop = chain().bound_arg_oop(); twisti@1573: ArgToken arg; twisti@1568: if (arg_type == T_OBJECT) { twisti@1573: arg = make_oop_constant(arg_oop, CHECK_(empty)); twisti@1568: } else { twisti@1568: jvalue arg_value; twisti@1568: BasicType bt = java_lang_boxing_object::get_value(arg_oop, &arg_value); never@2937: if (bt == arg_type || (bt == T_INT && is_subword_type(arg_type))) { twisti@1573: arg = make_prim_constant(arg_type, &arg_value, CHECK_(empty)); twisti@1568: } else { never@2937: lose(err_msg("bad bound value: arg_type %s boxing %s", type2name(arg_type), type2name(bt)), CHECK_(empty)); twisti@1568: } twisti@1568: } twisti@2903: DEBUG_ONLY(arg_oop = badOop); twisti@1568: change_argument(T_VOID, arg_slot, arg_type, arg); twisti@1568: } twisti@1568: twisti@1568: // this test must come after the body of the loop twisti@1568: if (!chain().is_last()) { twisti@1573: chain().next(CHECK_(empty)); twisti@1568: } else { twisti@1568: break; twisti@1568: } twisti@1568: } twisti@1568: twisti@1568: // finish the sequence with a tail-call to the ultimate target twisti@1568: // parameters are passed in logical order (recv 1st), not slot order twisti@1568: ArgToken* arglist = NEW_RESOURCE_ARRAY(ArgToken, _outgoing.length() + 1); twisti@1568: int ap = 0; twisti@1568: for (int i = _outgoing.length() - 1; i >= 0; i--) { never@2937: ArgToken arg_state = _outgoing.at(i); never@2937: if (arg_state.basic_type() == T_VOID) continue; never@2937: arglist[ap++] = _outgoing.at(i); twisti@1568: } twisti@1568: assert(ap == _outgoing_argc, ""); twisti@1573: arglist[ap] = ArgToken(); // add a sentinel, for the sake of asserts jrose@2982: return make_invoke(chain().last_method(), twisti@1568: vmIntrinsics::_none, twisti@1568: chain().last_invoke_code(), true, twisti@1568: ap, arglist, THREAD); twisti@1568: } twisti@1568: twisti@1573: twisti@1573: // ----------------------------------------------------------------------------- twisti@1573: // MethodHandleWalker::walk_incoming_state twisti@1573: // twisti@1568: void MethodHandleWalker::walk_incoming_state(TRAPS) { twisti@1568: Handle mtype(THREAD, chain().method_type_oop()); jrose@2639: int nptypes = java_lang_invoke_MethodType::ptype_count(mtype()); twisti@1568: _outgoing_argc = nptypes; twisti@1568: int argp = nptypes - 1; twisti@1568: if (argp >= 0) { never@2937: _outgoing.at_grow(argp, ArgToken(tt_void)); // presize twisti@1568: } twisti@1568: for (int i = 0; i < nptypes; i++) { twisti@1568: klassOop arg_type_klass = NULL; twisti@2903: BasicType arg_type = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(mtype(), i), &arg_type_klass); twisti@1573: int index = new_local_index(arg_type); twisti@1573: ArgToken arg = make_parameter(arg_type, arg_type_klass, index, CHECK); twisti@2903: DEBUG_ONLY(arg_type_klass = (klassOop) NULL); never@2937: _outgoing.at_put(argp, arg); twisti@1568: if (type2size[arg_type] == 2) { twisti@1568: // add the extra slot, so we can model the JVM stack never@2937: _outgoing.insert_before(argp+1, ArgToken(tt_void)); twisti@1568: } twisti@1568: --argp; twisti@1568: } twisti@1568: // call make_parameter at the end of the list for the return type twisti@1568: klassOop ret_type_klass = NULL; twisti@2903: BasicType ret_type = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(mtype()), &ret_type_klass); twisti@1568: ArgToken ret = make_parameter(ret_type, ret_type_klass, -1, CHECK); twisti@1568: // ignore ret; client can catch it if needed never@2937: never@2937: assert(_outgoing_argc == argument_count_slow(), "empty slots under control"); never@2937: never@2937: verify_args_and_signature(CHECK); twisti@1568: } twisti@1568: twisti@1573: never@2937: #ifdef ASSERT never@2937: void MethodHandleWalker::verify_args_and_signature(TRAPS) { never@2937: int index = _outgoing.length() - 1; never@2937: objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(chain().method_type_oop()); never@2937: for (int i = 0, limit = ptypes->length(); i < limit; i++) { never@2937: BasicType t = java_lang_Class::as_BasicType(ptypes->obj_at(i)); never@2937: if (t == T_ARRAY) t = T_OBJECT; never@2937: if (t == T_LONG || t == T_DOUBLE) { never@2937: assert(T_VOID == _outgoing.at(index).basic_type(), "types must match"); never@2937: index--; never@2937: } never@2937: assert(t == _outgoing.at(index).basic_type(), "types must match"); never@2937: index--; never@2937: } never@2937: } never@2937: #endif never@2937: never@2937: twisti@1573: // ----------------------------------------------------------------------------- twisti@1573: // MethodHandleWalker::change_argument twisti@1573: // twisti@1573: // This is messy because some kinds of arguments are paired with twisti@1573: // companion slots containing an empty value. never@2937: void MethodHandleWalker::change_argument(BasicType old_type, int slot, const ArgToken& new_arg) { never@2937: BasicType new_type = new_arg.basic_type(); twisti@1568: int old_size = type2size[old_type]; twisti@1568: int new_size = type2size[new_type]; twisti@1568: if (old_size == new_size) { twisti@1568: // simple case first never@2937: _outgoing.at_put(slot, new_arg); twisti@1568: } else if (old_size > new_size) { twisti@1573: for (int i = old_size - 1; i >= new_size; i--) { never@2937: assert((i != 0) == (_outgoing.at(slot + i).basic_type() == T_VOID), ""); twisti@1568: _outgoing.remove_at(slot + i); twisti@1568: } twisti@1568: if (new_size > 0) never@2937: _outgoing.at_put(slot, new_arg); twisti@1568: else twisti@1568: _outgoing_argc -= 1; // deleted a real argument twisti@1568: } else { twisti@1568: for (int i = old_size; i < new_size; i++) { never@2937: _outgoing.insert_before(slot + i, ArgToken(tt_void)); twisti@1568: } never@2937: _outgoing.at_put(slot, new_arg); twisti@1568: if (old_size == 0) twisti@1568: _outgoing_argc += 1; // inserted a real argument twisti@1568: } never@2937: assert(_outgoing_argc == argument_count_slow(), "empty slots under control"); twisti@1568: } twisti@1568: twisti@1568: twisti@1568: #ifdef ASSERT twisti@1568: int MethodHandleWalker::argument_count_slow() { twisti@1568: int args_seen = 0; twisti@1568: for (int i = _outgoing.length() - 1; i >= 0; i--) { never@2937: if (_outgoing.at(i).basic_type() != T_VOID) { twisti@1568: ++args_seen; never@2937: if (_outgoing.at(i).basic_type() == T_LONG || never@2937: _outgoing.at(i).basic_type() == T_DOUBLE) { never@2937: assert(_outgoing.at(i + 1).basic_type() == T_VOID, "should only follow two word"); never@2937: } never@2937: } else { never@2937: assert(_outgoing.at(i - 1).basic_type() == T_LONG || never@2937: _outgoing.at(i - 1).basic_type() == T_DOUBLE, "should only follow two word"); twisti@1568: } twisti@1568: } twisti@1568: return args_seen; twisti@1568: } twisti@1568: #endif twisti@1568: twisti@1568: twisti@1573: // ----------------------------------------------------------------------------- twisti@2903: // MethodHandleWalker::retype_raw_conversion twisti@2903: // twisti@2903: // Do the raw retype conversions for OP_RETYPE_RAW. twisti@2903: void MethodHandleWalker::retype_raw_conversion(BasicType src, BasicType dst, bool for_return, int slot, TRAPS) { twisti@2903: if (src != dst) { twisti@2903: if (MethodHandles::same_basic_type_for_returns(src, dst, /*raw*/ true)) { twisti@2903: if (MethodHandles::is_float_fixed_reinterpretation_cast(src, dst)) { twisti@2903: vmIntrinsics::ID iid = vmIntrinsics::for_raw_conversion(src, dst); twisti@2903: if (iid == vmIntrinsics::_none) { twisti@2903: lose("no raw conversion method", CHECK); twisti@2903: } twisti@2903: ArgToken arglist[2]; twisti@2903: if (!for_return) { twisti@2903: // argument type conversion never@2937: ArgToken arg = _outgoing.at(slot); twisti@2903: assert(arg.token_type() >= tt_symbolic || src == arg.basic_type(), "sanity"); twisti@2903: arglist[0] = arg; // outgoing 'this' twisti@2903: arglist[1] = ArgToken(); // sentinel jrose@2982: arg = make_invoke(methodHandle(), iid, Bytecodes::_invokestatic, false, 1, &arglist[0], CHECK); twisti@2903: change_argument(src, slot, dst, arg); twisti@2903: } else { twisti@2903: // return type conversion jrose@2982: if (_return_conv == vmIntrinsics::_none) { jrose@2982: _return_conv = iid; jrose@2982: } else if (_return_conv == vmIntrinsics::for_raw_conversion(dst, src)) { jrose@2982: _return_conv = vmIntrinsics::_none; jrose@2982: } else if (_return_conv != zero_return_conv()) { jrose@2982: lose(err_msg("requested raw return conversion not allowed: %s -> %s (before %s)", type2name(src), type2name(dst), vmIntrinsics::name_at(_return_conv)), CHECK); jrose@2982: } twisti@2903: } twisti@2903: } else { twisti@2903: // Nothing to do. twisti@2903: } jrose@2982: } else if (for_return && (!is_subword_type(src) || !is_subword_type(dst))) { jrose@2982: // This can occur in exception-throwing MHs, which have a fictitious return value encoded as Void or Empty. jrose@2982: _return_conv = zero_return_conv(); twisti@2903: } else if (src == T_OBJECT && is_java_primitive(dst)) { twisti@2903: // ref-to-prim: discard ref, push zero twisti@2903: lose("requested ref-to-prim conversion not expected", CHECK); twisti@2903: } else { never@2937: lose(err_msg("requested raw conversion not allowed: %s -> %s", type2name(src), type2name(dst)), CHECK); twisti@2903: } twisti@2903: } twisti@2903: } twisti@2903: twisti@2903: twisti@2903: // ----------------------------------------------------------------------------- twisti@1573: // MethodHandleCompiler twisti@1573: never@2920: MethodHandleCompiler::MethodHandleCompiler(Handle root, Symbol* name, Symbol* signature, int invoke_count, bool is_invokedynamic, TRAPS) twisti@1573: : MethodHandleWalker(root, is_invokedynamic, THREAD), twisti@2898: _invoke_count(invoke_count), twisti@1573: _thread(THREAD), twisti@1573: _bytecode(THREAD, 50), twisti@1573: _constants(THREAD, 10), jrose@2982: _non_bcp_klasses(THREAD, 5), twisti@1573: _cur_stack(0), twisti@1573: _max_stack(0), never@3105: _rtype(T_ILLEGAL), never@3105: _selectAlternative_bci(-1), never@3105: _taken_count(0), never@3105: _not_taken_count(0) twisti@1573: { twisti@1573: twisti@1573: // Element zero is always the null constant. twisti@1573: (void) _constants.append(NULL); twisti@1573: twisti@1573: // Set name and signature index. never@2920: _name_index = cpool_symbol_put(name); never@2920: _signature_index = cpool_symbol_put(signature); twisti@1573: jrose@2982: // To make the resulting methods more recognizable by jrose@2982: // stack walkers and compiler heuristics, jrose@2982: // we put them in holder class MethodHandle. jrose@2982: // See klass_is_method_handle_adapter_holder jrose@2982: // and methodOopDesc::is_method_handle_adapter. jrose@2982: _target_klass = SystemDictionaryHandles::MethodHandle_klass(); jrose@2982: jrose@2982: check_non_bcp_klasses(java_lang_invoke_MethodHandle::type(root()), CHECK); jrose@2982: twisti@1573: // Get return type klass. twisti@1573: Handle first_mtype(THREAD, chain().method_type_oop()); twisti@1573: // _rklass is NULL for primitives. jrose@2639: _rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(first_mtype()), &_rklass); twisti@1587: if (_rtype == T_ARRAY) _rtype = T_OBJECT; twisti@1573: never@2920: ArgumentSizeComputer args(signature); never@2920: int params = args.size() + 1; // Incoming arguments plus receiver. twisti@1573: _num_params = for_invokedynamic() ? params - 1 : params; // XXX Check if callee is static? twisti@1573: } twisti@1573: twisti@1573: twisti@1573: // ----------------------------------------------------------------------------- twisti@1573: // MethodHandleCompiler::compile twisti@1573: // twisti@1573: // Compile this MethodHandle into a bytecode adapter and return a twisti@1573: // methodOop. twisti@1573: methodHandle MethodHandleCompiler::compile(TRAPS) { twisti@1568: assert(_thread == THREAD, "must be same thread"); twisti@1573: methodHandle nullHandle; twisti@1573: (void) walk(CHECK_(nullHandle)); jrose@2982: record_non_bcp_klasses(); twisti@1573: return get_method_oop(CHECK_(nullHandle)); twisti@1573: } twisti@1568: twisti@1573: never@2920: void MethodHandleCompiler::emit_bc(Bytecodes::Code op, int index, int args_size) { twisti@1573: Bytecodes::check(op); // Are we legal? twisti@1573: twisti@1573: switch (op) { twisti@1573: // b twisti@1573: case Bytecodes::_aconst_null: twisti@1573: case Bytecodes::_iconst_m1: twisti@1573: case Bytecodes::_iconst_0: twisti@1573: case Bytecodes::_iconst_1: twisti@1573: case Bytecodes::_iconst_2: twisti@1573: case Bytecodes::_iconst_3: twisti@1573: case Bytecodes::_iconst_4: twisti@1573: case Bytecodes::_iconst_5: twisti@1573: case Bytecodes::_lconst_0: twisti@1573: case Bytecodes::_lconst_1: twisti@1573: case Bytecodes::_fconst_0: twisti@1573: case Bytecodes::_fconst_1: twisti@1573: case Bytecodes::_fconst_2: twisti@1573: case Bytecodes::_dconst_0: twisti@1573: case Bytecodes::_dconst_1: twisti@1573: case Bytecodes::_iload_0: twisti@1573: case Bytecodes::_iload_1: twisti@1573: case Bytecodes::_iload_2: twisti@1573: case Bytecodes::_iload_3: twisti@1573: case Bytecodes::_lload_0: twisti@1573: case Bytecodes::_lload_1: twisti@1573: case Bytecodes::_lload_2: twisti@1573: case Bytecodes::_lload_3: twisti@1573: case Bytecodes::_fload_0: twisti@1573: case Bytecodes::_fload_1: twisti@1573: case Bytecodes::_fload_2: twisti@1573: case Bytecodes::_fload_3: twisti@1573: case Bytecodes::_dload_0: twisti@1573: case Bytecodes::_dload_1: twisti@1573: case Bytecodes::_dload_2: twisti@1573: case Bytecodes::_dload_3: twisti@1573: case Bytecodes::_aload_0: twisti@1573: case Bytecodes::_aload_1: twisti@1573: case Bytecodes::_aload_2: twisti@1573: case Bytecodes::_aload_3: twisti@1573: case Bytecodes::_istore_0: twisti@1573: case Bytecodes::_istore_1: twisti@1573: case Bytecodes::_istore_2: twisti@1573: case Bytecodes::_istore_3: twisti@1573: case Bytecodes::_lstore_0: twisti@1573: case Bytecodes::_lstore_1: twisti@1573: case Bytecodes::_lstore_2: twisti@1573: case Bytecodes::_lstore_3: twisti@1573: case Bytecodes::_fstore_0: twisti@1573: case Bytecodes::_fstore_1: twisti@1573: case Bytecodes::_fstore_2: twisti@1573: case Bytecodes::_fstore_3: twisti@1573: case Bytecodes::_dstore_0: twisti@1573: case Bytecodes::_dstore_1: twisti@1573: case Bytecodes::_dstore_2: twisti@1573: case Bytecodes::_dstore_3: twisti@1573: case Bytecodes::_astore_0: twisti@1573: case Bytecodes::_astore_1: twisti@1573: case Bytecodes::_astore_2: twisti@1573: case Bytecodes::_astore_3: twisti@2903: case Bytecodes::_iand: twisti@1573: case Bytecodes::_i2l: twisti@1573: case Bytecodes::_i2f: twisti@1573: case Bytecodes::_i2d: twisti@1573: case Bytecodes::_i2b: twisti@1573: case Bytecodes::_i2c: twisti@1573: case Bytecodes::_i2s: twisti@1573: case Bytecodes::_l2i: twisti@1573: case Bytecodes::_l2f: twisti@1573: case Bytecodes::_l2d: twisti@1573: case Bytecodes::_f2i: twisti@1573: case Bytecodes::_f2l: twisti@1573: case Bytecodes::_f2d: twisti@1573: case Bytecodes::_d2i: twisti@1573: case Bytecodes::_d2l: twisti@1573: case Bytecodes::_d2f: never@2920: case Bytecodes::_iaload: never@2920: case Bytecodes::_laload: never@2920: case Bytecodes::_faload: never@2920: case Bytecodes::_daload: never@2920: case Bytecodes::_aaload: never@2920: case Bytecodes::_baload: never@2920: case Bytecodes::_caload: never@2920: case Bytecodes::_saload: twisti@1573: case Bytecodes::_ireturn: twisti@1573: case Bytecodes::_lreturn: twisti@1573: case Bytecodes::_freturn: twisti@1573: case Bytecodes::_dreturn: twisti@1573: case Bytecodes::_areturn: twisti@1573: case Bytecodes::_return: jrose@1920: assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_b, "wrong bytecode format"); twisti@1573: _bytecode.push(op); twisti@1573: break; twisti@1573: twisti@1573: // bi twisti@1573: case Bytecodes::_ldc: jrose@2017: assert(Bytecodes::format_bits(op, false) == (Bytecodes::_fmt_b|Bytecodes::_fmt_has_k), "wrong bytecode format"); never@2920: if (index == (index & 0xff)) { never@2920: _bytecode.push(op); never@2920: _bytecode.push(index); never@2920: } else { never@2920: _bytecode.push(Bytecodes::_ldc_w); never@2920: _bytecode.push(index >> 8); never@2920: _bytecode.push(index); never@2920: } jrose@2017: break; jrose@2017: twisti@1573: case Bytecodes::_iload: twisti@1573: case Bytecodes::_lload: twisti@1573: case Bytecodes::_fload: twisti@1573: case Bytecodes::_dload: twisti@1573: case Bytecodes::_aload: twisti@1573: case Bytecodes::_istore: twisti@1573: case Bytecodes::_lstore: twisti@1573: case Bytecodes::_fstore: twisti@1573: case Bytecodes::_dstore: twisti@1573: case Bytecodes::_astore: jrose@1920: assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bi, "wrong bytecode format"); never@2920: if (index == (index & 0xff)) { never@2920: _bytecode.push(op); never@2920: _bytecode.push(index); never@2920: } else { never@2920: // doesn't fit in a u2 never@2920: _bytecode.push(Bytecodes::_wide); never@2920: _bytecode.push(op); never@2920: _bytecode.push(index >> 8); never@2920: _bytecode.push(index); never@2920: } twisti@1573: break; twisti@1573: jrose@2017: // bkk jrose@2017: case Bytecodes::_ldc_w: twisti@1573: case Bytecodes::_ldc2_w: twisti@1573: case Bytecodes::_checkcast: jrose@1920: assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bkk, "wrong bytecode format"); never@2920: assert((unsigned short) index == index, "index does not fit in 16-bit"); twisti@1573: _bytecode.push(op); twisti@1573: _bytecode.push(index >> 8); twisti@1573: _bytecode.push(index); twisti@1573: break; twisti@1573: jrose@1920: // bJJ twisti@1573: case Bytecodes::_invokestatic: twisti@1573: case Bytecodes::_invokespecial: twisti@1573: case Bytecodes::_invokevirtual: jrose@1920: assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format"); never@2920: assert((unsigned short) index == index, "index does not fit in 16-bit"); twisti@1573: _bytecode.push(op); twisti@1573: _bytecode.push(index >> 8); twisti@1573: _bytecode.push(index); twisti@1573: break; twisti@1573: never@2920: case Bytecodes::_invokeinterface: never@2920: assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format"); never@2920: assert((unsigned short) index == index, "index does not fit in 16-bit"); never@2920: assert(args_size > 0, "valid args_size"); never@2920: _bytecode.push(op); never@2920: _bytecode.push(index >> 8); never@2920: _bytecode.push(index); never@2920: _bytecode.push(args_size); never@2920: _bytecode.push(0); never@2920: break; never@2920: never@3105: case Bytecodes::_ifeq: never@3105: assert((unsigned short) index == index, "index does not fit in 16-bit"); never@3105: _bytecode.push(op); never@3105: _bytecode.push(index >> 8); never@3105: _bytecode.push(index); never@3105: break; never@3105: twisti@1573: default: twisti@1573: ShouldNotReachHere(); twisti@1568: } twisti@1573: } twisti@1568: never@3105: void MethodHandleCompiler::update_branch_dest(int src, int dst) { never@3105: switch (_bytecode.at(src)) { never@3105: case Bytecodes::_ifeq: never@3105: dst -= src; // compute the offset never@3105: assert((unsigned short) dst == dst, "index does not fit in 16-bit"); never@3105: _bytecode.at_put(src + 1, dst >> 8); never@3105: _bytecode.at_put(src + 2, dst); never@3105: break; never@3105: default: never@3105: ShouldNotReachHere(); never@3105: } never@3105: } never@3105: never@3105: void MethodHandleCompiler::emit_load(ArgToken arg) { never@3105: TokenType tt = arg.token_type(); never@3105: BasicType bt = arg.basic_type(); never@3105: never@3105: switch (tt) { never@3105: case tt_parameter: never@3105: case tt_temporary: never@3105: emit_load(bt, arg.index()); never@3105: break; never@3105: case tt_constant: never@3105: emit_load_constant(arg); never@3105: break; never@3105: case tt_illegal: never@3105: case tt_void: never@3105: default: never@3105: ShouldNotReachHere(); never@3105: } never@3105: } never@3105: twisti@1573: twisti@1573: void MethodHandleCompiler::emit_load(BasicType bt, int index) { twisti@1573: if (index <= 3) { twisti@1573: switch (bt) { twisti@1573: case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT: twisti@1573: case T_INT: emit_bc(Bytecodes::cast(Bytecodes::_iload_0 + index)); break; twisti@1573: case T_LONG: emit_bc(Bytecodes::cast(Bytecodes::_lload_0 + index)); break; twisti@1573: case T_FLOAT: emit_bc(Bytecodes::cast(Bytecodes::_fload_0 + index)); break; twisti@1573: case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dload_0 + index)); break; twisti@1573: case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_aload_0 + index)); break; twisti@1573: default: twisti@1573: ShouldNotReachHere(); twisti@1573: } twisti@1573: } twisti@1573: else { twisti@1573: switch (bt) { twisti@1573: case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT: twisti@1573: case T_INT: emit_bc(Bytecodes::_iload, index); break; twisti@1573: case T_LONG: emit_bc(Bytecodes::_lload, index); break; twisti@1573: case T_FLOAT: emit_bc(Bytecodes::_fload, index); break; twisti@1573: case T_DOUBLE: emit_bc(Bytecodes::_dload, index); break; twisti@1573: case T_OBJECT: emit_bc(Bytecodes::_aload, index); break; twisti@1573: default: twisti@1573: ShouldNotReachHere(); twisti@1573: } twisti@1573: } twisti@1573: stack_push(bt); twisti@1568: } twisti@1568: twisti@1573: void MethodHandleCompiler::emit_store(BasicType bt, int index) { twisti@1573: if (index <= 3) { twisti@1573: switch (bt) { twisti@1573: case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT: twisti@1573: case T_INT: emit_bc(Bytecodes::cast(Bytecodes::_istore_0 + index)); break; twisti@1573: case T_LONG: emit_bc(Bytecodes::cast(Bytecodes::_lstore_0 + index)); break; twisti@1573: case T_FLOAT: emit_bc(Bytecodes::cast(Bytecodes::_fstore_0 + index)); break; twisti@1573: case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dstore_0 + index)); break; twisti@1573: case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_astore_0 + index)); break; twisti@1573: default: twisti@1573: ShouldNotReachHere(); twisti@1573: } twisti@1573: } twisti@1573: else { twisti@1573: switch (bt) { twisti@1573: case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT: twisti@1573: case T_INT: emit_bc(Bytecodes::_istore, index); break; twisti@1573: case T_LONG: emit_bc(Bytecodes::_lstore, index); break; twisti@1573: case T_FLOAT: emit_bc(Bytecodes::_fstore, index); break; twisti@1573: case T_DOUBLE: emit_bc(Bytecodes::_dstore, index); break; twisti@1573: case T_OBJECT: emit_bc(Bytecodes::_astore, index); break; twisti@1573: default: twisti@1573: ShouldNotReachHere(); twisti@1573: } twisti@1573: } twisti@1573: stack_pop(bt); twisti@1573: } twisti@1573: twisti@1573: twisti@1573: void MethodHandleCompiler::emit_load_constant(ArgToken arg) { twisti@1573: BasicType bt = arg.basic_type(); never@2937: if (is_subword_type(bt)) bt = T_INT; twisti@1573: switch (bt) { twisti@1573: case T_INT: { twisti@1573: jint value = arg.get_jint(); twisti@1573: if (-1 <= value && value <= 5) twisti@1573: emit_bc(Bytecodes::cast(Bytecodes::_iconst_0 + value)); twisti@1573: else twisti@1573: emit_bc(Bytecodes::_ldc, cpool_int_put(value)); twisti@1573: break; twisti@1573: } twisti@1573: case T_LONG: { twisti@1573: jlong value = arg.get_jlong(); twisti@1573: if (0 <= value && value <= 1) twisti@1573: emit_bc(Bytecodes::cast(Bytecodes::_lconst_0 + (int) value)); twisti@1573: else twisti@1573: emit_bc(Bytecodes::_ldc2_w, cpool_long_put(value)); twisti@1573: break; twisti@1573: } twisti@1573: case T_FLOAT: { twisti@1573: jfloat value = arg.get_jfloat(); twisti@1573: if (value == 0.0 || value == 1.0 || value == 2.0) twisti@1573: emit_bc(Bytecodes::cast(Bytecodes::_fconst_0 + (int) value)); twisti@1573: else twisti@1573: emit_bc(Bytecodes::_ldc, cpool_float_put(value)); twisti@1573: break; twisti@1573: } twisti@1573: case T_DOUBLE: { twisti@1573: jdouble value = arg.get_jdouble(); twisti@1573: if (value == 0.0 || value == 1.0) twisti@1573: emit_bc(Bytecodes::cast(Bytecodes::_dconst_0 + (int) value)); twisti@1573: else twisti@1573: emit_bc(Bytecodes::_ldc2_w, cpool_double_put(value)); twisti@1573: break; twisti@1573: } twisti@1573: case T_OBJECT: { twisti@1573: Handle value = arg.object(); jrose@2982: if (value.is_null()) { twisti@1573: emit_bc(Bytecodes::_aconst_null); jrose@2982: break; jrose@2982: } jrose@2982: if (java_lang_Class::is_instance(value())) { jrose@2982: klassOop k = java_lang_Class::as_klassOop(value()); jrose@2982: if (k != NULL) { jrose@2982: emit_bc(Bytecodes::_ldc, cpool_klass_put(k)); jrose@2982: break; jrose@2982: } jrose@2982: } jrose@2982: emit_bc(Bytecodes::_ldc, cpool_object_put(value)); twisti@1573: break; twisti@1573: } twisti@1573: default: twisti@1573: ShouldNotReachHere(); twisti@1573: } twisti@1573: stack_push(bt); twisti@1573: } twisti@1573: twisti@1573: twisti@1568: MethodHandleWalker::ArgToken twisti@1568: MethodHandleCompiler::make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, twisti@1573: const ArgToken& src, TRAPS) { twisti@1573: twisti@1573: BasicType srctype = src.basic_type(); never@2920: TokenType tt = src.token_type(); never@2920: int index = -1; twisti@1573: twisti@1573: switch (op) { twisti@1573: case Bytecodes::_i2l: twisti@1573: case Bytecodes::_i2f: twisti@1573: case Bytecodes::_i2d: twisti@1573: case Bytecodes::_i2b: twisti@1573: case Bytecodes::_i2c: twisti@1573: case Bytecodes::_i2s: twisti@1573: twisti@1573: case Bytecodes::_l2i: twisti@1573: case Bytecodes::_l2f: twisti@1573: case Bytecodes::_l2d: twisti@1573: twisti@1573: case Bytecodes::_f2i: twisti@1573: case Bytecodes::_f2l: twisti@1573: case Bytecodes::_f2d: twisti@1573: twisti@1573: case Bytecodes::_d2i: twisti@1573: case Bytecodes::_d2l: twisti@1573: case Bytecodes::_d2f: never@2920: if (tt == tt_constant) { never@2920: emit_load_constant(src); never@2920: } else { never@2920: emit_load(srctype, src.index()); never@2920: } twisti@1573: stack_pop(srctype); // pop the src type twisti@1573: emit_bc(op); twisti@1573: stack_push(type); // push the dest value never@2920: if (tt != tt_constant) never@2920: index = src.index(); never@2920: if (srctype != type || index == -1) twisti@1573: index = new_local_index(type); twisti@1573: emit_store(type, index); twisti@1573: break; twisti@1573: twisti@1573: case Bytecodes::_checkcast: never@2920: if (tt == tt_constant) { never@2920: emit_load_constant(src); never@2920: } else { never@2920: emit_load(srctype, src.index()); never@2920: index = src.index(); never@2920: } twisti@1573: emit_bc(op, cpool_klass_put(tk)); jrose@2982: check_non_bcp_klass(tk, CHECK_(src)); never@2950: // Allocate a new local for the type so that we don't hide the never@2950: // previous type from the verifier. never@2950: index = new_local_index(type); twisti@1573: emit_store(srctype, index); twisti@1573: break; twisti@1573: never@2937: case Bytecodes::_nop: never@2937: // nothing to do never@2937: return src; never@2937: twisti@1573: default: twisti@2903: if (op == Bytecodes::_illegal) never@2937: lose(err_msg("no such primitive conversion: %s -> %s", type2name(src.basic_type()), type2name(type)), THREAD); twisti@2903: else never@2937: lose(err_msg("bad primitive conversion op: %s", Bytecodes::name(op)), THREAD); twisti@2903: return make_prim_constant(type, &zero_jvalue, THREAD); twisti@1573: } twisti@1573: twisti@1573: return make_parameter(type, tk, index, THREAD); twisti@1568: } twisti@1568: twisti@1573: twisti@1573: // ----------------------------------------------------------------------------- twisti@1573: // MethodHandleCompiler twisti@1573: // twisti@1573: twisti@2903: // Values used by the compiler. twisti@2903: jvalue MethodHandleCompiler::zero_jvalue = { 0 }; twisti@2903: jvalue MethodHandleCompiler::one_jvalue = { 1 }; twisti@1573: never@3105: // Fetch any values from CountingMethodHandles and capture them for profiles never@3105: bool MethodHandleCompiler::fetch_counts(ArgToken arg1, ArgToken arg2) { never@3105: int count1 = -1, count2 = -1; never@3105: if (arg1.token_type() == tt_constant && arg1.basic_type() == T_OBJECT && never@3105: java_lang_invoke_CountingMethodHandle::is_instance(arg1.object()())) { never@3105: count1 = java_lang_invoke_CountingMethodHandle::vmcount(arg1.object()()); never@3105: } never@3105: if (arg2.token_type() == tt_constant && arg2.basic_type() == T_OBJECT && never@3105: java_lang_invoke_CountingMethodHandle::is_instance(arg2.object()())) { never@3105: count2 = java_lang_invoke_CountingMethodHandle::vmcount(arg2.object()()); never@3105: } never@3105: int total = count1 + count2; never@3105: if (count1 != -1 && count2 != -1 && total != 0) { never@3105: // Normalize the collect counts to the invoke_count never@3105: if (count1 != 0) _not_taken_count = (int)(_invoke_count * count1 / (double)total); never@3105: if (count2 != 0) _taken_count = (int)(_invoke_count * count2 / (double)total); never@3105: return true; never@3105: } never@3105: return false; never@3105: } never@3105: twisti@1573: // Emit bytecodes for the given invoke instruction. twisti@1568: MethodHandleWalker::ArgToken jrose@2982: MethodHandleCompiler::make_invoke(methodHandle m, vmIntrinsics::ID iid, twisti@1568: Bytecodes::Code op, bool tailcall, twisti@1568: int argc, MethodHandleWalker::ArgToken* argv, twisti@1568: TRAPS) { twisti@2903: ArgToken zero; jrose@2982: if (m.is_null()) { twisti@1573: // Get the intrinsic methodOop. jrose@2982: m = methodHandle(THREAD, vmIntrinsics::method_for(iid)); jrose@2982: if (m.is_null()) { jrose@2638: lose(vmIntrinsics::name_at(iid), CHECK_(zero)); jrose@2638: } twisti@1573: } twisti@1573: twisti@2903: klassOop klass = m->method_holder(); twisti@2903: Symbol* name = m->name(); twisti@2903: Symbol* signature = m->signature(); twisti@1573: jrose@2982: if (iid == vmIntrinsics::_invokeGeneric && jrose@2982: argc >= 1 && argv[0].token_type() == tt_constant) { jrose@2982: assert(m->intrinsic_id() == vmIntrinsics::_invokeExact, ""); jrose@2982: Handle receiver = argv[0].object(); jrose@2982: Handle rtype(THREAD, java_lang_invoke_MethodHandle::type(receiver())); jrose@2982: Handle mtype(THREAD, m->method_handle_type()); jrose@2982: if (rtype() != mtype()) { jrose@2982: assert(java_lang_invoke_MethodType::form(rtype()) == jrose@2982: java_lang_invoke_MethodType::form(mtype()), jrose@2982: "must be the same shape"); jrose@2982: // customize m to the exact required rtype jrose@2982: bool has_non_bcp_klass = check_non_bcp_klasses(rtype(), CHECK_(zero)); jrose@2982: TempNewSymbol sig2 = java_lang_invoke_MethodType::as_signature(rtype(), true, CHECK_(zero)); jrose@2982: methodHandle m2; jrose@2982: if (!has_non_bcp_klass) { jrose@2982: methodOop m2_oop = SystemDictionary::find_method_handle_invoke(m->name(), sig2, jrose@2982: KlassHandle(), CHECK_(zero)); jrose@2982: m2 = methodHandle(THREAD, m2_oop); jrose@2982: } jrose@2982: if (m2.is_null()) { jrose@2982: // just build it fresh jrose@2982: m2 = methodOopDesc::make_invoke_method(klass, m->name(), sig2, rtype, CHECK_(zero)); jrose@2982: if (m2.is_null()) jrose@2982: lose(err_msg("no customized invoker %s", sig2->as_utf8()), CHECK_(zero)); jrose@2982: } jrose@2982: m = m2; jrose@2982: signature = m->signature(); jrose@2982: } jrose@2982: } jrose@2982: never@3105: if (m->intrinsic_id() == vmIntrinsics::_selectAlternative && never@3105: fetch_counts(argv[1], argv[2])) { never@3105: assert(argc == 3, "three arguments"); never@3105: assert(tailcall, "only"); never@3105: never@3105: // do inline bytecodes so we can drop profile data into it, never@3105: // 0: iload_0 never@3105: emit_load(argv[0]); never@3105: // 1: ifeq 8 never@3105: _selectAlternative_bci = _bytecode.length(); never@3105: emit_bc(Bytecodes::_ifeq, 0); // emit placeholder offset never@3105: // 4: aload_1 never@3105: emit_load(argv[1]); never@3105: // 5: areturn; never@3105: emit_bc(Bytecodes::_areturn); never@3105: // 8: aload_2 never@3105: update_branch_dest(_selectAlternative_bci, cur_bci()); never@3105: emit_load(argv[2]); never@3105: // 9: areturn never@3105: emit_bc(Bytecodes::_areturn); never@3105: return ArgToken(); // Dummy return value. never@3105: } never@3105: jrose@2982: check_non_bcp_klass(klass, CHECK_(zero)); jrose@2982: if (m->is_method_handle_invoke()) { jrose@2982: check_non_bcp_klasses(m->method_handle_type(), CHECK_(zero)); jrose@2982: } jrose@2982: never@2920: // Count the number of arguments, not the size never@2920: ArgumentCount asc(signature); never@2920: assert(argc == asc.size() + ((op == Bytecodes::_invokestatic || op == Bytecodes::_invokedynamic) ? 0 : 1), never@2920: "argc mismatch"); never@2920: twisti@1573: for (int i = 0; i < argc; i++) { twisti@1573: ArgToken arg = argv[i]; twisti@1573: TokenType tt = arg.token_type(); twisti@1573: BasicType bt = arg.basic_type(); twisti@1573: twisti@1573: switch (tt) { twisti@1573: case tt_parameter: twisti@1573: case tt_temporary: twisti@1573: emit_load(bt, arg.index()); twisti@1573: break; twisti@1573: case tt_constant: twisti@1573: emit_load_constant(arg); twisti@1573: break; twisti@1573: case tt_illegal: twisti@1573: // Sentinel. twisti@1573: assert(i == (argc - 1), "sentinel must be last entry"); twisti@1573: break; twisti@1573: case tt_void: twisti@1573: default: twisti@1573: ShouldNotReachHere(); twisti@1573: } twisti@1573: } twisti@1573: twisti@1573: // Populate constant pool. twisti@1573: int name_index = cpool_symbol_put(name); twisti@1573: int signature_index = cpool_symbol_put(signature); twisti@1573: int name_and_type_index = cpool_name_and_type_put(name_index, signature_index); twisti@1573: int klass_index = cpool_klass_put(klass); jrose@2982: int methodref_index = cpool_methodref_put(op, klass_index, name_and_type_index, m); twisti@1573: twisti@1573: // Generate invoke. twisti@1568: switch (op) { twisti@1573: case Bytecodes::_invokestatic: twisti@1573: case Bytecodes::_invokespecial: twisti@1568: case Bytecodes::_invokevirtual: twisti@1573: emit_bc(op, methodref_index); twisti@1573: break; never@2920: never@2920: case Bytecodes::_invokeinterface: { never@2920: ArgumentSizeComputer asc(signature); never@2920: emit_bc(op, methodref_index, asc.size() + 1); twisti@1568: break; never@2920: } never@2920: twisti@1568: default: twisti@1568: ShouldNotReachHere(); twisti@1568: } twisti@1568: twisti@1573: // If tailcall, we have walked all the way to a direct method handle. twisti@1573: // Otherwise, make a recursive call to some helper routine. twisti@1573: BasicType rbt = m->result_type(); twisti@1587: if (rbt == T_ARRAY) rbt = T_OBJECT; never@2920: stack_push(rbt); // The return value is already pushed onto the stack. twisti@1573: ArgToken ret; twisti@1573: if (tailcall) { jrose@2982: if (return_conv() == zero_return_conv()) { jrose@2982: rbt = T_VOID; // discard value jrose@2982: } else if (return_conv() != vmIntrinsics::_none) { jrose@2982: // return value conversion jrose@2982: int index = new_local_index(rbt); jrose@2982: emit_store(rbt, index); jrose@2982: ArgToken arglist[2]; jrose@2982: arglist[0] = ArgToken(tt_temporary, rbt, index); jrose@2982: arglist[1] = ArgToken(); // sentinel jrose@2982: ret = make_invoke(methodHandle(), return_conv(), Bytecodes::_invokestatic, false, 1, &arglist[0], CHECK_(zero)); jrose@2982: set_return_conv(vmIntrinsics::_none); jrose@2982: rbt = ret.basic_type(); jrose@2982: emit_load(rbt, ret.index()); jrose@2982: } twisti@1573: if (rbt != _rtype) { twisti@1573: if (rbt == T_VOID) { twisti@1573: // push a zero of the right sort twisti@1573: if (_rtype == T_OBJECT) { twisti@1573: zero = make_oop_constant(NULL, CHECK_(zero)); twisti@1573: } else { twisti@1573: zero = make_prim_constant(_rtype, &zero_jvalue, CHECK_(zero)); twisti@1573: } twisti@1573: emit_load_constant(zero); twisti@1573: } else if (_rtype == T_VOID) { twisti@1573: // We'll emit a _return with something on the stack. twisti@1573: // It's OK to ignore what's on the stack. twisti@2903: } else if (rbt == T_INT && is_subword_type(_rtype)) { twisti@2903: // Convert value to match return type. twisti@2903: switch (_rtype) { twisti@2903: case T_BOOLEAN: { twisti@2903: // boolean is treated as a one-bit unsigned integer. twisti@2903: // Cf. API documentation: java/lang/invoke/MethodHandles.html#explicitCastArguments twisti@2903: ArgToken one = make_prim_constant(T_INT, &one_jvalue, CHECK_(zero)); twisti@2903: emit_load_constant(one); twisti@2903: emit_bc(Bytecodes::_iand); twisti@2903: break; twisti@2903: } twisti@2903: case T_BYTE: emit_bc(Bytecodes::_i2b); break; twisti@2903: case T_CHAR: emit_bc(Bytecodes::_i2c); break; twisti@2903: case T_SHORT: emit_bc(Bytecodes::_i2s); break; twisti@2903: default: ShouldNotReachHere(); twisti@2903: } twisti@2903: } else if (is_subword_type(rbt) && (is_subword_type(_rtype) || (_rtype == T_INT))) { twisti@2903: // The subword type was returned as an int and will be passed twisti@2903: // on as an int. twisti@1573: } else { twisti@2903: lose("unknown conversion", CHECK_(zero)); twisti@1573: } twisti@1573: } twisti@1573: switch (_rtype) { twisti@1573: case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT: twisti@1573: case T_INT: emit_bc(Bytecodes::_ireturn); break; twisti@1573: case T_LONG: emit_bc(Bytecodes::_lreturn); break; twisti@1573: case T_FLOAT: emit_bc(Bytecodes::_freturn); break; twisti@1573: case T_DOUBLE: emit_bc(Bytecodes::_dreturn); break; twisti@1573: case T_VOID: emit_bc(Bytecodes::_return); break; twisti@1573: case T_OBJECT: never@2954: if (_rklass.not_null() && _rklass() != SystemDictionary::Object_klass() && !Klass::cast(_rklass())->is_interface()) { twisti@1573: emit_bc(Bytecodes::_checkcast, cpool_klass_put(_rklass())); jrose@2982: check_non_bcp_klass(_rklass(), CHECK_(zero)); never@2954: } twisti@1573: emit_bc(Bytecodes::_areturn); twisti@1573: break; twisti@1573: default: ShouldNotReachHere(); twisti@1573: } twisti@1573: ret = ArgToken(); // Dummy return value. twisti@1573: } twisti@1573: else { twisti@1573: int index = new_local_index(rbt); twisti@1573: switch (rbt) { twisti@1573: case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT: twisti@1573: case T_INT: case T_LONG: case T_FLOAT: case T_DOUBLE: twisti@1573: case T_OBJECT: twisti@1573: emit_store(rbt, index); twisti@1573: ret = ArgToken(tt_temporary, rbt, index); twisti@1573: break; twisti@1573: case T_VOID: twisti@1573: ret = ArgToken(tt_void); twisti@1573: break; twisti@1573: default: twisti@1573: ShouldNotReachHere(); twisti@1573: } twisti@1573: } twisti@1573: twisti@1573: return ret; twisti@1568: } twisti@1568: twisti@1568: MethodHandleWalker::ArgToken twisti@1568: MethodHandleCompiler::make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, twisti@1573: const MethodHandleWalker::ArgToken& base, twisti@1573: const MethodHandleWalker::ArgToken& offset, twisti@1568: TRAPS) { never@2920: switch (base.token_type()) { never@2920: case tt_parameter: never@2920: case tt_temporary: never@2920: emit_load(base.basic_type(), base.index()); never@2920: break; never@2920: case tt_constant: never@2920: emit_load_constant(base); never@2920: break; never@2920: default: never@2920: ShouldNotReachHere(); never@2920: } never@2920: switch (offset.token_type()) { never@2920: case tt_parameter: never@2920: case tt_temporary: never@2920: emit_load(offset.basic_type(), offset.index()); never@2920: break; never@2920: case tt_constant: never@2920: emit_load_constant(offset); never@2920: break; never@2920: default: never@2920: ShouldNotReachHere(); never@2920: } never@2920: emit_bc(op); never@2920: int index = new_local_index(type); never@2920: emit_store(type, index); never@2920: return ArgToken(tt_temporary, type, index); twisti@1568: } twisti@1568: twisti@1568: twisti@1573: int MethodHandleCompiler::cpool_primitive_put(BasicType bt, jvalue* con) { twisti@1568: jvalue con_copy; twisti@1568: assert(bt < T_OBJECT, ""); twisti@1568: if (type2aelembytes(bt) < jintSize) { twisti@1568: // widen to int twisti@1568: con_copy = (*con); twisti@1568: con = &con_copy; twisti@1568: switch (bt) { twisti@1568: case T_BOOLEAN: con->i = (con->z ? 1 : 0); break; twisti@1568: case T_BYTE: con->i = con->b; break; twisti@1568: case T_CHAR: con->i = con->c; break; twisti@1568: case T_SHORT: con->i = con->s; break; twisti@1568: default: ShouldNotReachHere(); twisti@1568: } twisti@1568: bt = T_INT; twisti@1568: } twisti@1573: twisti@1573: // for (int i = 1, imax = _constants.length(); i < imax; i++) { twisti@1573: // ConstantValue* con = _constants.at(i); never@2937: // if (con != NULL && con->is_primitive() && con.basic_type() == bt) { twisti@1573: // bool match = false; twisti@1573: // switch (type2size[bt]) { twisti@1573: // case 1: if (pcon->_value.i == con->i) match = true; break; twisti@1573: // case 2: if (pcon->_value.j == con->j) match = true; break; twisti@1573: // } twisti@1573: // if (match) twisti@1573: // return i; twisti@1573: // } twisti@1573: // } twisti@1573: ConstantValue* cv = new ConstantValue(bt, *con); twisti@1573: int index = _constants.append(cv); twisti@1573: twisti@1573: // long and double entries take 2 slots, we add another empty entry. twisti@1573: if (type2size[bt] == 2) twisti@1573: (void) _constants.append(NULL); twisti@1573: twisti@1573: return index; twisti@1573: } twisti@1573: jrose@2982: bool MethodHandleCompiler::check_non_bcp_klasses(Handle method_type, TRAPS) { jrose@2982: bool res = false; jrose@2982: for (int i = -1, len = java_lang_invoke_MethodType::ptype_count(method_type()); i < len; i++) { jrose@2982: oop ptype = (i == -1 jrose@2982: ? java_lang_invoke_MethodType::rtype(method_type()) jrose@2982: : java_lang_invoke_MethodType::ptype(method_type(), i)); jrose@2982: res |= check_non_bcp_klass(java_lang_Class::as_klassOop(ptype), CHECK_(false)); jrose@2982: } jrose@2982: return res; jrose@2982: } jrose@2982: jrose@2982: bool MethodHandleCompiler::check_non_bcp_klass(klassOop klass, TRAPS) { jrose@2982: klass = methodOopDesc::check_non_bcp_klass(klass); jrose@2982: if (klass != NULL) { jrose@2982: Symbol* name = Klass::cast(klass)->name(); jrose@2982: for (int i = _non_bcp_klasses.length() - 1; i >= 0; i--) { jrose@2982: klassOop k2 = _non_bcp_klasses.at(i)(); jrose@2982: if (Klass::cast(k2)->name() == name) { jrose@2982: if (k2 != klass) { jrose@2982: lose(err_msg("unsupported klass name alias %s", name->as_utf8()), THREAD); jrose@2982: } jrose@2982: return true; jrose@2982: } jrose@2982: } jrose@2982: _non_bcp_klasses.append(KlassHandle(THREAD, klass)); jrose@2982: return true; jrose@2982: } jrose@2982: return false; jrose@2982: } jrose@2982: jrose@2982: void MethodHandleCompiler::record_non_bcp_klasses() { jrose@2982: // Append extra klasses to constant pool, to guide klass lookup. jrose@2982: for (int k = 0; k < _non_bcp_klasses.length(); k++) { jrose@2982: klassOop non_bcp_klass = _non_bcp_klasses.at(k)(); jrose@2982: bool add_to_cp = true; jrose@2982: for (int j = 1; j < _constants.length(); j++) { jrose@2982: ConstantValue* cv = _constants.at(j); jrose@2982: if (cv != NULL && cv->tag() == JVM_CONSTANT_Class jrose@2982: && cv->klass_oop() == non_bcp_klass) { jrose@2982: add_to_cp = false; jrose@2982: break; jrose@2982: } jrose@2982: } jrose@2982: if (add_to_cp) cpool_klass_put(non_bcp_klass); jrose@2982: } jrose@2982: } twisti@1573: twisti@1573: constantPoolHandle MethodHandleCompiler::get_constant_pool(TRAPS) const { twisti@1573: constantPoolHandle nullHandle; ysr@2533: constantPoolOop cpool_oop = oopFactory::new_constantPool(_constants.length(), ysr@2533: oopDesc::IsSafeConc, ysr@2533: CHECK_(nullHandle)); twisti@1573: constantPoolHandle cpool(THREAD, cpool_oop); twisti@1573: twisti@1573: // Fill the real constant pool skipping the zero element. twisti@1573: for (int i = 1; i < _constants.length(); i++) { twisti@1573: ConstantValue* cv = _constants.at(i); twisti@1573: switch (cv->tag()) { coleenp@2497: case JVM_CONSTANT_Utf8: cpool->symbol_at_put( i, cv->symbol() ); break; twisti@1573: case JVM_CONSTANT_Integer: cpool->int_at_put( i, cv->get_jint() ); break; twisti@1573: case JVM_CONSTANT_Float: cpool->float_at_put( i, cv->get_jfloat() ); break; twisti@1573: case JVM_CONSTANT_Long: cpool->long_at_put( i, cv->get_jlong() ); break; twisti@1573: case JVM_CONSTANT_Double: cpool->double_at_put( i, cv->get_jdouble() ); break; twisti@1573: case JVM_CONSTANT_Class: cpool->klass_at_put( i, cv->klass_oop() ); break; twisti@1573: case JVM_CONSTANT_Methodref: cpool->method_at_put( i, cv->first_index(), cv->second_index()); break; jrose@2982: case JVM_CONSTANT_InterfaceMethodref: jrose@2982: cpool->interface_method_at_put(i, cv->first_index(), cv->second_index()); break; twisti@1573: case JVM_CONSTANT_NameAndType: cpool->name_and_type_at_put(i, cv->first_index(), cv->second_index()); break; twisti@1573: case JVM_CONSTANT_Object: cpool->object_at_put( i, cv->object_oop() ); break; twisti@1573: default: ShouldNotReachHere(); twisti@1573: } twisti@1573: twisti@1573: switch (cv->tag()) { twisti@1573: case JVM_CONSTANT_Long: twisti@1573: case JVM_CONSTANT_Double: twisti@1573: i++; // Skip empty entry. twisti@1573: assert(_constants.at(i) == NULL, "empty entry"); twisti@1573: break; twisti@1568: } twisti@1568: } twisti@1573: jrose@2982: cpool->set_preresolution(); jrose@2982: twisti@1573: // Set the constant pool holder to the target method's class. twisti@1573: cpool->set_pool_holder(_target_klass()); twisti@1573: twisti@1573: return cpool; twisti@1573: } twisti@1573: twisti@1573: never@3105: methodHandle MethodHandleCompiler::get_method_oop(TRAPS) { twisti@2898: methodHandle empty; twisti@1573: // Create a method that holds the generated bytecode. invokedynamic twisti@1573: // has no receiver, normal MH calls do. twisti@1573: int flags_bits; twisti@1573: if (for_invokedynamic()) jrose@1862: flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC | JVM_ACC_STATIC); twisti@1573: else jrose@1862: flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC); twisti@1573: twisti@2898: // Create a new method twisti@2898: methodHandle m; twisti@2898: { twisti@2898: methodOop m_oop = oopFactory::new_method(bytecode_length(), twisti@2898: accessFlags_from(flags_bits), twisti@2898: 0, 0, 0, oopDesc::IsSafeConc, CHECK_(empty)); twisti@2898: m = methodHandle(THREAD, m_oop); twisti@2898: } twisti@1573: twisti@2898: constantPoolHandle cpool = get_constant_pool(CHECK_(empty)); twisti@1573: m->set_constants(cpool()); twisti@1573: twisti@1573: m->set_name_index(_name_index); twisti@1573: m->set_signature_index(_signature_index); twisti@1573: twisti@1573: m->set_code((address) bytecode()); twisti@1573: twisti@1573: m->set_max_stack(_max_stack); twisti@1573: m->set_max_locals(max_locals()); twisti@1573: m->set_size_of_parameters(_num_params); twisti@1573: twisti@1573: typeArrayHandle exception_handlers(THREAD, Universe::the_empty_int_array()); twisti@1573: m->set_exception_table(exception_handlers()); twisti@1573: twisti@1573: // Rewrite the method and set up the constant pool cache. twisti@2898: objArrayOop m_array = oopFactory::new_system_objArray(1, CHECK_(empty)); twisti@1573: objArrayHandle methods(THREAD, m_array); twisti@1573: methods->obj_at_put(0, m()); twisti@2898: Rewriter::rewrite(_target_klass(), cpool, methods, CHECK_(empty)); // Use fake class. coleenp@2945: Rewriter::relocate_and_link(_target_klass(), methods, CHECK_(empty)); // Use fake class. twisti@2898: jrose@2982: // Pre-resolve selected CP cache entries, to avoid problems with class loader scoping. jrose@2982: constantPoolCacheHandle cpc(THREAD, cpool->cache()); jrose@2982: for (int i = 0; i < cpc->length(); i++) { jrose@2982: ConstantPoolCacheEntry* e = cpc->entry_at(i); jrose@2982: assert(!e->is_secondary_entry(), "no indy instructions in here, yet"); jrose@2982: int constant_pool_index = e->constant_pool_index(); jrose@2982: ConstantValue* cv = _constants.at(constant_pool_index); jrose@2982: if (!cv->has_linkage()) continue; jrose@2982: methodHandle m = cv->linkage(); jrose@2982: int index; jrose@2982: switch (cv->tag()) { jrose@2982: case JVM_CONSTANT_Methodref: jrose@2982: index = m->vtable_index(); jrose@2982: if (m->is_static()) { jrose@2982: e->set_method(Bytecodes::_invokestatic, m, index); jrose@2982: } else { jrose@2982: e->set_method(Bytecodes::_invokespecial, m, index); jrose@2982: e->set_method(Bytecodes::_invokevirtual, m, index); jrose@2982: } jrose@2982: break; jrose@2982: case JVM_CONSTANT_InterfaceMethodref: jrose@2982: index = klassItable::compute_itable_index(m()); jrose@2982: e->set_interface_call(m, index); jrose@2982: break; jrose@2982: } jrose@2982: } jrose@2982: twisti@2903: // Set the invocation counter's count to the invoke count of the twisti@2903: // original call site. twisti@2903: InvocationCounter* ic = m->invocation_counter(); twisti@2903: ic->set(InvocationCounter::wait_for_compile, _invoke_count); twisti@2903: twisti@2898: // Create a new MDO twisti@2898: { twisti@2898: methodDataOop mdo = oopFactory::new_methodData(m, CHECK_(empty)); twisti@2898: assert(m->method_data() == NULL, "there should not be an MDO yet"); twisti@2898: m->set_method_data(mdo); twisti@2898: never@3105: bool found_selectAlternative = false; twisti@2898: // Iterate over all profile data and set the count of the counter twisti@2898: // data entries to the original call site counter. twisti@2903: for (ProfileData* profile_data = mdo->first_data(); twisti@2903: mdo->is_valid(profile_data); twisti@2903: profile_data = mdo->next_data(profile_data)) { twisti@2903: if (profile_data->is_CounterData()) { twisti@2903: CounterData* counter_data = profile_data->as_CounterData(); twisti@2903: counter_data->set_count(_invoke_count); twisti@2898: } never@3105: if (profile_data->is_BranchData() && never@3105: profile_data->bci() == _selectAlternative_bci) { never@3105: BranchData* bd = profile_data->as_BranchData(); never@3105: bd->set_taken(_taken_count); never@3105: bd->set_not_taken(_not_taken_count); never@3105: found_selectAlternative = true; never@3105: } twisti@2898: } never@3105: assert(_selectAlternative_bci == -1 || found_selectAlternative, "must have found profile entry"); twisti@2898: } twisti@1573: twisti@1573: #ifndef PRODUCT twisti@1573: if (TraceMethodHandles) { twisti@1573: m->print(); twisti@1573: m->print_codes(); twisti@1573: } twisti@1573: #endif //PRODUCT twisti@1573: jrose@1862: assert(m->is_method_handle_adapter(), "must be recognized as an adapter"); twisti@1573: return m; twisti@1568: } twisti@1568: twisti@1568: twisti@1568: #ifndef PRODUCT twisti@1568: twisti@1568: // MH printer for debugging. twisti@1568: twisti@1568: class MethodHandlePrinter : public MethodHandleWalker { twisti@1568: private: twisti@1568: outputStream* _out; twisti@1568: bool _verbose; twisti@1568: int _temp_num; twisti@2903: int _param_state; twisti@1568: stringStream _strbuf; twisti@1568: const char* strbuf() { twisti@1568: const char* s = _strbuf.as_string(); twisti@1568: _strbuf.reset(); twisti@1568: return s; twisti@1568: } never@2937: ArgToken token(const char* str, BasicType type) { never@2937: return ArgToken(str, type); twisti@2903: } twisti@2903: const char* string(ArgToken token) { never@2920: return token.str(); twisti@1568: } twisti@1568: void start_params() { twisti@2903: _param_state <<= 1; twisti@1568: _out->print("("); twisti@1568: } twisti@1568: void end_params() { twisti@1568: if (_verbose) _out->print("\n"); twisti@1568: _out->print(") => {"); twisti@2903: _param_state >>= 1; twisti@1568: } twisti@1568: void put_type_name(BasicType type, klassOop tk, outputStream* s) { twisti@1568: const char* kname = NULL; twisti@1568: if (tk != NULL) twisti@1568: kname = Klass::cast(tk)->external_name(); twisti@1568: s->print("%s", (kname != NULL) ? kname : type2name(type)); twisti@1568: } twisti@1568: ArgToken maybe_make_temp(const char* statement_op, BasicType type, const char* temp_name) { twisti@1568: const char* value = strbuf(); never@2937: if (!_verbose) return token(value, type); twisti@1568: // make an explicit binding for each separate value twisti@1568: _strbuf.print("%s%d", temp_name, ++_temp_num); twisti@1568: const char* temp = strbuf(); twisti@1568: _out->print("\n %s %s %s = %s;", statement_op, type2name(type), temp, value); never@2937: return token(temp, type); twisti@1568: } twisti@1568: twisti@1568: public: twisti@1568: MethodHandlePrinter(Handle root, bool verbose, outputStream* out, TRAPS) twisti@2903: : MethodHandleWalker(root, false, THREAD), twisti@1568: _out(out), twisti@1568: _verbose(verbose), twisti@2903: _param_state(0), twisti@1568: _temp_num(0) twisti@1568: { jrose@2982: out->print("MethodHandle:"); jrose@2982: java_lang_invoke_MethodType::print_signature(java_lang_invoke_MethodHandle::type(root()), out); jrose@2982: out->print(" : #"); twisti@1568: start_params(); twisti@1568: } twisti@1568: virtual ArgToken make_parameter(BasicType type, klassOop tk, int argnum, TRAPS) { twisti@1568: if (argnum < 0) { twisti@1568: end_params(); never@2937: return token("return", type); twisti@1568: } twisti@2903: if ((_param_state & 1) == 0) { twisti@2903: _param_state |= 1; twisti@1568: _out->print(_verbose ? "\n " : ""); twisti@1568: } else { twisti@1568: _out->print(_verbose ? ",\n " : ", "); twisti@1568: } twisti@1568: if (argnum >= _temp_num) twisti@1568: _temp_num = argnum; twisti@1568: // generate an argument name twisti@1568: _strbuf.print("a%d", argnum); twisti@1568: const char* arg = strbuf(); twisti@1568: put_type_name(type, tk, _out); twisti@1568: _out->print(" %s", arg); never@2937: return token(arg, type); twisti@1568: } twisti@1568: virtual ArgToken make_oop_constant(oop con, TRAPS) { twisti@1568: if (con == NULL) twisti@1568: _strbuf.print("null"); twisti@1568: else twisti@1568: con->print_value_on(&_strbuf); twisti@1568: if (_strbuf.size() == 0) { // yuck twisti@1568: _strbuf.print("(a "); twisti@1568: put_type_name(T_OBJECT, con->klass(), &_strbuf); twisti@1568: _strbuf.print(")"); twisti@1568: } twisti@1568: return maybe_make_temp("constant", T_OBJECT, "k"); twisti@1568: } twisti@1568: virtual ArgToken make_prim_constant(BasicType type, jvalue* con, TRAPS) { twisti@1568: java_lang_boxing_object::print(type, con, &_strbuf); twisti@1568: return maybe_make_temp("constant", type, "k"); twisti@1568: } twisti@2903: void print_bytecode_name(Bytecodes::Code op) { twisti@2903: if (Bytecodes::is_defined(op)) twisti@2903: _strbuf.print("%s", Bytecodes::name(op)); twisti@2903: else twisti@2903: _strbuf.print("bytecode_%d", (int) op); twisti@2903: } twisti@2903: virtual ArgToken make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& src, TRAPS) { twisti@2903: print_bytecode_name(op); twisti@2903: _strbuf.print("(%s", string(src)); twisti@1568: if (tk != NULL) { twisti@1568: _strbuf.print(", "); twisti@1568: put_type_name(type, tk, &_strbuf); twisti@1568: } twisti@1568: _strbuf.print(")"); twisti@1568: return maybe_make_temp("convert", type, "v"); twisti@1568: } twisti@2903: virtual ArgToken make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& base, const ArgToken& offset, TRAPS) { twisti@2903: _strbuf.print("%s(%s, %s", Bytecodes::name(op), string(base), string(offset)); twisti@1568: if (tk != NULL) { twisti@1568: _strbuf.print(", "); twisti@1568: put_type_name(type, tk, &_strbuf); twisti@1568: } twisti@1568: _strbuf.print(")"); twisti@1568: return maybe_make_temp("fetch", type, "x"); twisti@1568: } jrose@2982: virtual ArgToken make_invoke(methodHandle m, vmIntrinsics::ID iid, twisti@1568: Bytecodes::Code op, bool tailcall, twisti@1568: int argc, ArgToken* argv, TRAPS) { twisti@2903: Symbol* name; twisti@2903: Symbol* sig; jrose@2982: if (m.not_null()) { twisti@1568: name = m->name(); twisti@1568: sig = m->signature(); twisti@1568: } else { twisti@1568: name = vmSymbols::symbol_at(vmIntrinsics::name_for(iid)); twisti@1568: sig = vmSymbols::symbol_at(vmIntrinsics::signature_for(iid)); twisti@1568: } twisti@1568: _strbuf.print("%s %s%s(", Bytecodes::name(op), name->as_C_string(), sig->as_C_string()); twisti@1568: for (int i = 0; i < argc; i++) { twisti@2903: _strbuf.print("%s%s", (i > 0 ? ", " : ""), string(argv[i])); twisti@1568: } twisti@1568: _strbuf.print(")"); twisti@1568: if (!tailcall) { twisti@1568: BasicType rt = char2type(sig->byte_at(sig->utf8_length()-1)); twisti@1568: if (rt == T_ILLEGAL) rt = T_OBJECT; // ';' at the end of '(...)L...;' twisti@1568: return maybe_make_temp("invoke", rt, "x"); twisti@1568: } else { twisti@1568: const char* ret = strbuf(); twisti@1568: _out->print(_verbose ? "\n return " : " "); twisti@1568: _out->print("%s", ret); twisti@1568: _out->print(_verbose ? "\n}\n" : " }"); twisti@1568: } twisti@1568: return ArgToken(); twisti@1568: } twisti@1568: twisti@1568: virtual void set_method_handle(oop mh) { twisti@1568: if (WizardMode && Verbose) { twisti@1568: tty->print("\n--- next target: "); twisti@1568: mh->print(); twisti@1568: } twisti@1568: } twisti@1568: twisti@1568: static void print(Handle root, bool verbose, outputStream* out, TRAPS) { twisti@1568: ResourceMark rm; twisti@1568: MethodHandlePrinter printer(root, verbose, out, CHECK); twisti@1568: printer.walk(CHECK); twisti@1568: out->print("\n"); twisti@1568: } twisti@1568: static void print(Handle root, bool verbose = Verbose, outputStream* out = tty) { never@2937: Thread* THREAD = Thread::current(); twisti@1568: ResourceMark rm; twisti@1568: MethodHandlePrinter printer(root, verbose, out, THREAD); twisti@1568: if (!HAS_PENDING_EXCEPTION) twisti@1568: printer.walk(THREAD); twisti@1568: if (HAS_PENDING_EXCEPTION) { twisti@1568: oop ex = PENDING_EXCEPTION; twisti@1568: CLEAR_PENDING_EXCEPTION; twisti@2903: out->print(" *** "); twisti@2903: if (printer.lose_message() != NULL) out->print("%s ", printer.lose_message()); twisti@2903: out->print("}"); twisti@1568: } twisti@1568: out->print("\n"); twisti@1568: } twisti@1568: }; twisti@1568: twisti@1568: extern "C" twisti@1568: void print_method_handle(oop mh) { jrose@2148: if (!mh->is_oop()) { twisti@2903: tty->print_cr("*** not a method handle: "PTR_FORMAT, (intptr_t)mh); jrose@2639: } else if (java_lang_invoke_MethodHandle::is_instance(mh)) { twisti@2903: MethodHandlePrinter::print(mh); twisti@1568: } else { twisti@1568: tty->print("*** not a method handle: "); twisti@1568: mh->print(); twisti@1568: } twisti@1568: } twisti@1568: twisti@1568: #endif // PRODUCT