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@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@1568: klassOop receiver_limit_oop = NULL; twisti@1568: int flags = 0; twisti@1568: methodOop m = MethodHandles::decode_method(target, receiver_limit_oop, flags); twisti@1568: _last_method = methodHandle(THREAD, m); 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; 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: 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(); twisti@1568: SlotState* arg_state = slot_state(arg_slot); twisti@1568: if (arg_state == NULL jrose@2639: && conv_op > java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW) { twisti@1573: lose("bad argument index", CHECK_(empty)); twisti@1568: } twisti@1568: twisti@1568: // perform the adapter action twisti@1568: switch (chain().adapter_conversion_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@1573: oop outgoing_mh_oop = chain().vmtarget_oop(); jrose@2639: if (!java_lang_invoke_MethodHandle::is_instance(outgoing_mh_oop)) twisti@1573: lose("outgoing target not a MethodHandle", CHECK_(empty)); jrose@2639: Handle outgoing_mtype(THREAD, java_lang_invoke_MethodHandle::type(outgoing_mh_oop)); twisti@1573: outgoing_mh_oop = NULL; // GC safety 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@1573: for (int i = 0, slot = _outgoing.length() - 1; slot >= 0; slot--) { twisti@1573: SlotState* arg_state = slot_state(slot); twisti@1573: if (arg_state->_type == T_VOID) continue; twisti@1573: ArgToken arg = _outgoing.at(slot)._arg; twisti@1573: twisti@1573: klassOop in_klass = NULL; twisti@1573: klassOop out_klass = NULL; jrose@2639: BasicType inpbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(incoming_mtype(), i), &in_klass); jrose@2639: BasicType outpbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(outgoing_mtype(), i), &out_klass); twisti@1573: assert(inpbt == arg.basic_type(), "sanity"); twisti@1573: twisti@1573: if (inpbt != outpbt) { twisti@1573: vmIntrinsics::ID iid = vmIntrinsics::for_raw_conversion(inpbt, outpbt); twisti@1573: if (iid == vmIntrinsics::_none) { twisti@1573: lose("no raw conversion method", CHECK_(empty)); twisti@1573: } twisti@1573: ArgToken arglist[2]; twisti@1573: arglist[0] = arg; // outgoing 'this' twisti@1573: arglist[1] = ArgToken(); // sentinel twisti@1573: arg = make_invoke(NULL, iid, Bytecodes::_invokestatic, false, 1, &arglist[0], CHECK_(empty)); twisti@1573: change_argument(inpbt, slot, outpbt, arg); twisti@1573: } twisti@1573: twisti@1573: i++; // We need to skip void slots at the top of the loop. twisti@1573: } twisti@1573: jrose@2639: BasicType inrbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(incoming_mtype())); jrose@2639: BasicType outrbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(outgoing_mtype())); twisti@1573: if (inrbt != outrbt) { twisti@1573: if (inrbt == T_INT && outrbt == T_VOID) { twisti@1573: // See comments in MethodHandles::same_basic_type_for_arguments. twisti@1573: } else { twisti@1573: assert(false, "IMPLEMENT ME"); twisti@1573: lose("no raw conversion method", CHECK_(empty)); twisti@1573: } 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, ""); twisti@1568: assert(dest == arg_state->_type, ""); twisti@1573: ArgToken arg = arg_state->_arg; twisti@1573: ArgToken new_arg = make_conversion(T_OBJECT, dest_klass, Bytecodes::_checkcast, arg, CHECK_(empty)); twisti@1573: assert(arg.index() == new_arg.index(), "should be the same index"); 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(); twisti@1568: Bytecodes::Code bc = conversion_code(src, dest); twisti@1568: ArgToken arg = arg_state->_arg; 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) { twisti@1573: lose("bad primitive conversion", 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(); twisti@1568: ArgToken arg = arg_state->_arg; 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 twisti@1573: arg = make_invoke(NULL, 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: ArgToken arg = arg_state->_arg; 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: } twisti@1568: ArgToken arglist[2]; twisti@1573: arglist[0] = arg; // outgoing value twisti@1573: arglist[1] = ArgToken(); // sentinel jrose@2148: arg = make_invoke(NULL, boxer, Bytecodes::_invokevirtual, 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(); twisti@1568: if (!slot_has_argument(dest_arg_slot)) { twisti@1573: lose("bad swap index", CHECK_(empty)); twisti@1568: } twisti@1568: // a simple swap between two arguments twisti@1568: SlotState* dest_arg_state = slot_state(dest_arg_slot); twisti@1568: SlotState temp = (*dest_arg_state); twisti@1568: (*dest_arg_state) = (*arg_state); twisti@1568: (*arg_state) = temp; twisti@1568: break; twisti@1568: } twisti@1568: jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS: { twisti@1568: int dest_arg_slot = chain().adapter_conversion_vminfo(); twisti@1568: if (!slot_has_argument(dest_arg_slot) || arg_slot == dest_arg_slot) { twisti@1573: lose("bad rotate index", CHECK_(empty)); twisti@1568: } twisti@1568: SlotState* dest_arg_state = slot_state(dest_arg_slot); twisti@1568: // Rotate the source argument (plus following N slots) into the twisti@1568: // position occupied by the dest argument (plus following N slots). twisti@1568: int rotate_count = type2size[dest_arg_state->_type]; twisti@1568: // (no other rotate counts are currently supported) twisti@1568: if (arg_slot < dest_arg_slot) { twisti@1568: for (int i = 0; i < rotate_count; i++) { twisti@1568: SlotState temp = _outgoing.at(arg_slot); twisti@1568: _outgoing.remove_at(arg_slot); twisti@1568: _outgoing.insert_before(dest_arg_slot + rotate_count - 1, temp); twisti@1568: } twisti@1568: } else { // arg_slot > dest_arg_slot twisti@1568: for (int i = 0; i < rotate_count; i++) { twisti@1568: SlotState temp = _outgoing.at(arg_slot + rotate_count - 1); twisti@1568: _outgoing.remove_at(arg_slot + rotate_count - 1); twisti@1568: _outgoing.insert_before(dest_arg_slot, temp); twisti@1568: } twisti@1568: } 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++) { twisti@1568: SlotState* dup = slot_state(arg_slot + 2*i); twisti@1568: if (dup == NULL) break; // safety net twisti@1568: if (dup->_type != T_VOID) _outgoing_argc += 1; twisti@1568: _outgoing.insert_before(i, (*dup)); twisti@1568: } 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++) { twisti@1568: SlotState* drop = slot_state(arg_slot); twisti@1568: if (drop == NULL) break; // safety net twisti@1568: if (drop->_type != T_VOID) _outgoing_argc -= 1; twisti@1568: _outgoing.remove_at(arg_slot); twisti@1568: } twisti@1568: break; twisti@1568: } twisti@1568: jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS: { //NYI, may GC twisti@1573: lose("unimplemented", CHECK_(empty)); twisti@1568: break; twisti@1568: } twisti@1568: 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. twisti@1568: assert(arg_state->_type == T_OBJECT, ""); twisti@1568: ArgToken array_arg = arg_state->_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 twisti@1568: make_invoke(NULL, vmIntrinsics::_checkSpreadArgument, twisti@1573: Bytecodes::_invokestatic, false, 3, &arglist[0], CHECK_(empty)); twisti@1568: twisti@1568: // Spread out the array elements. twisti@1568: Bytecodes::Code aload_op = Bytecodes::_aaload; twisti@1568: if (element_type != T_OBJECT) { twisti@1573: 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); twisti@1568: ap += type2size[element_type]; twisti@1568: } twisti@1568: break; twisti@1568: } twisti@1568: jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_FLYBY: //NYI, runs Java code jrose@2639: case java_lang_invoke_AdapterMethodHandle::OP_RICOCHET: //NYI, runs Java code twisti@1573: lose("unimplemented", CHECK_(empty)); twisti@1568: break; 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); twisti@1568: if (bt == arg_type) { twisti@1573: arg = make_prim_constant(arg_type, &arg_value, CHECK_(empty)); twisti@1568: } else { twisti@1573: lose("bad bound value", CHECK_(empty)); twisti@1568: } twisti@1568: } twisti@1568: 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--) { twisti@1568: SlotState* arg_state = slot_state(i); twisti@1568: if (arg_state->_type == T_VOID) continue; twisti@1568: arglist[ap++] = _outgoing.at(i)._arg; twisti@1568: } twisti@1568: assert(ap == _outgoing_argc, ""); twisti@1573: arglist[ap] = ArgToken(); // add a sentinel, for the sake of asserts twisti@1568: return make_invoke(chain().last_method_oop(), 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) { twisti@1573: _outgoing.at_grow(argp, make_state(T_VOID, ArgToken(tt_void))); // presize twisti@1568: } twisti@1568: for (int i = 0; i < nptypes; i++) { twisti@1568: klassOop arg_type_klass = NULL; twisti@1568: BasicType arg_type = java_lang_Class::as_BasicType( jrose@2639: 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@1573: debug_only(arg_type_klass = (klassOop) NULL); twisti@1568: _outgoing.at_put(argp, make_state(arg_type, arg)); twisti@1568: if (type2size[arg_type] == 2) { twisti@1568: // add the extra slot, so we can model the JVM stack twisti@1573: _outgoing.insert_before(argp+1, make_state(T_VOID, 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@1568: BasicType ret_type = java_lang_Class::as_BasicType( jrose@2639: 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 twisti@1568: } twisti@1568: twisti@1573: 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. twisti@1568: void MethodHandleWalker::change_argument(BasicType old_type, int slot, BasicType new_type, twisti@1573: const ArgToken& new_arg) { 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 twisti@1568: _outgoing.at_put(slot, make_state(new_type, new_arg)); twisti@1568: } else if (old_size > new_size) { twisti@1573: for (int i = old_size - 1; i >= new_size; i--) { twisti@1568: assert((i != 0) == (_outgoing.at(slot + i)._type == T_VOID), ""); twisti@1568: _outgoing.remove_at(slot + i); twisti@1568: } twisti@1568: if (new_size > 0) twisti@1568: _outgoing.at_put(slot, make_state(new_type, 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++) { twisti@1573: _outgoing.insert_before(slot + i, make_state(T_VOID, ArgToken(tt_void))); twisti@1568: } twisti@1568: _outgoing.at_put(slot, make_state(new_type, new_arg)); twisti@1568: if (old_size == 0) twisti@1568: _outgoing_argc += 1; // inserted a real argument twisti@1568: } 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--) { twisti@1568: if (_outgoing.at(i)._type != T_VOID) { twisti@1568: ++args_seen; twisti@1568: } twisti@1568: } twisti@1568: return args_seen; twisti@1568: } twisti@1568: #endif twisti@1568: twisti@1568: twisti@1573: // ----------------------------------------------------------------------------- twisti@1573: // MethodHandleCompiler twisti@1573: twisti@1573: MethodHandleCompiler::MethodHandleCompiler(Handle root, methodHandle callee, bool is_invokedynamic, TRAPS) twisti@1573: : MethodHandleWalker(root, is_invokedynamic, THREAD), twisti@1573: _callee(callee), twisti@1573: _thread(THREAD), twisti@1573: _bytecode(THREAD, 50), twisti@1573: _constants(THREAD, 10), twisti@1573: _cur_stack(0), twisti@1573: _max_stack(0), twisti@1573: _rtype(T_ILLEGAL) 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. twisti@1573: _name_index = cpool_symbol_put(_callee->name()); twisti@1573: _signature_index = cpool_symbol_put(_callee->signature()); twisti@1573: 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: twisti@1573: int params = _callee->size_of_parameters(); // 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)); twisti@1573: return get_method_oop(CHECK_(nullHandle)); twisti@1573: } twisti@1568: twisti@1573: twisti@1573: void MethodHandleCompiler::emit_bc(Bytecodes::Code op, int index) { 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@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: 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"); jrose@2017: assert((char) index == index, "index does not fit in 8-bit"); jrose@2017: _bytecode.push(op); jrose@2017: _bytecode.push(index); 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"); twisti@1573: assert((char) index == index, "index does not fit in 8-bit"); twisti@1573: _bytecode.push(op); twisti@1573: _bytecode.push(index); 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"); twisti@1573: assert((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"); twisti@1573: assert((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: twisti@1573: default: twisti@1573: ShouldNotReachHere(); twisti@1568: } twisti@1573: } twisti@1568: 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(); 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(); twisti@1573: if (value.is_null()) twisti@1573: emit_bc(Bytecodes::_aconst_null); twisti@1573: else twisti@1573: 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(); twisti@1573: int index = src.index(); 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: twisti@1573: emit_load(srctype, index); twisti@1573: stack_pop(srctype); // pop the src type twisti@1573: emit_bc(op); twisti@1573: stack_push(type); // push the dest value twisti@1573: if (srctype != type) twisti@1573: index = new_local_index(type); twisti@1573: emit_store(type, index); twisti@1573: break; twisti@1573: twisti@1573: case Bytecodes::_checkcast: twisti@1573: emit_load(srctype, index); twisti@1573: emit_bc(op, cpool_klass_put(tk)); twisti@1573: emit_store(srctype, index); twisti@1573: break; twisti@1573: twisti@1573: default: twisti@1573: ShouldNotReachHere(); 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@1573: static jvalue zero_jvalue; twisti@1573: twisti@1573: // Emit bytecodes for the given invoke instruction. twisti@1568: MethodHandleWalker::ArgToken twisti@1568: MethodHandleCompiler::make_invoke(methodOop m, vmIntrinsics::ID iid, twisti@1568: Bytecodes::Code op, bool tailcall, twisti@1568: int argc, MethodHandleWalker::ArgToken* argv, twisti@1568: TRAPS) { twisti@1573: if (m == NULL) { twisti@1573: // Get the intrinsic methodOop. twisti@1573: m = vmIntrinsics::method_for(iid); jrose@2638: if (m == NULL && iid == vmIntrinsics::_checkSpreadArgument && AllowTransitionalJSR292) { jrose@2638: m = vmIntrinsics::method_for(vmIntrinsics::_checkSpreadArgument_TRANS); jrose@2639: if (m == NULL) jrose@2639: // sun.dyn.MethodHandleImpl not found, look for java.dyn.MethodHandleNatives: jrose@2639: m = vmIntrinsics::method_for(vmIntrinsics::_checkSpreadArgument_TRANS2); jrose@2638: } jrose@2638: if (m == NULL) { jrose@2638: ArgToken zero; jrose@2638: lose(vmIntrinsics::name_at(iid), CHECK_(zero)); jrose@2638: } twisti@1573: } twisti@1573: coleenp@2497: klassOop klass = m->method_holder(); coleenp@2497: Symbol* name = m->name(); coleenp@2497: Symbol* signature = m->signature(); twisti@1573: twisti@1573: if (tailcall) { twisti@1587: // Actually, in order to make these methods more recognizable, twisti@2343: // let's put them in holder class MethodHandle. That way stack twisti@2343: // walkers and compiler heuristics can recognize them. twisti@2343: _target_klass = SystemDictionary::MethodHandle_klass(); twisti@1573: } twisti@1573: twisti@1573: // Inline the method. twisti@1573: InvocationCounter* ic = m->invocation_counter(); iveresov@2138: ic->set_carry_flag(); twisti@1573: 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); twisti@1573: int methodref_index = cpool_methodref_put(klass_index, name_and_type_index); 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; twisti@1568: case Bytecodes::_invokeinterface: twisti@1573: Unimplemented(); twisti@1568: break; 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; twisti@1573: ArgToken ret; twisti@1573: if (tailcall) { twisti@1573: if (rbt != _rtype) { twisti@1573: if (rbt == T_VOID) { twisti@1573: // push a zero of the right sort twisti@1573: ArgToken zero; 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@1573: } else { twisti@1573: tty->print_cr("*** rbt=%d != rtype=%d", rbt, _rtype); twisti@1573: assert(false, "IMPLEMENT ME"); 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@1577: if (_rklass.not_null() && _rklass() != SystemDictionary::Object_klass()) twisti@1573: emit_bc(Bytecodes::_checkcast, cpool_klass_put(_rklass())); 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: stack_push(rbt); // The return value is already pushed onto the stack. 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) { twisti@1568: Unimplemented(); twisti@1573: return ArgToken(); 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); twisti@1573: // if (con != NULL && con->is_primitive() && con->_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: 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; 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: 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: twisti@1573: methodHandle MethodHandleCompiler::get_method_oop(TRAPS) const { twisti@1573: methodHandle nullHandle; 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@1573: methodOop m_oop = oopFactory::new_method(bytecode_length(), twisti@1573: accessFlags_from(flags_bits), ysr@2533: 0, 0, 0, oopDesc::IsSafeConc, CHECK_(nullHandle)); twisti@1573: methodHandle m(THREAD, m_oop); twisti@1573: m_oop = NULL; // oop not GC safe twisti@1573: twisti@1573: constantPoolHandle cpool = get_constant_pool(CHECK_(nullHandle)); 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: // Set the carry bit of the invocation counter to force inlining of twisti@1573: // the adapter. twisti@1573: InvocationCounter* ic = m->invocation_counter(); iveresov@2138: ic->set_carry_flag(); twisti@1573: twisti@1573: // Rewrite the method and set up the constant pool cache. twisti@1573: objArrayOop m_array = oopFactory::new_system_objArray(1, CHECK_(nullHandle)); twisti@1573: objArrayHandle methods(THREAD, m_array); twisti@1573: methods->obj_at_put(0, m()); twisti@1573: Rewriter::rewrite(_target_klass(), cpool, methods, CHECK_(nullHandle)); // Use fake class. 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@1573: #if 0 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@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: } twisti@1568: ArgToken token(const char* str) { twisti@1568: return (ArgToken) str; twisti@1568: } twisti@1568: void start_params() { twisti@1568: _out->print("("); twisti@1568: } twisti@1568: void end_params() { twisti@1568: if (_verbose) _out->print("\n"); twisti@1568: _out->print(") => {"); 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(); twisti@1568: if (!_verbose) return token(value); 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); twisti@1568: return token(temp); twisti@1568: } twisti@1568: twisti@1568: public: twisti@1568: MethodHandlePrinter(Handle root, bool verbose, outputStream* out, TRAPS) twisti@1568: : MethodHandleWalker(root, THREAD), twisti@1568: _out(out), twisti@1568: _verbose(verbose), twisti@1568: _temp_num(0) twisti@1568: { 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(); twisti@1568: return NULL; twisti@1568: } twisti@1568: if (argnum == 0) { 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); twisti@1568: return token(arg); 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@1568: virtual ArgToken make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, ArgToken src, TRAPS) { twisti@1568: _strbuf.print("%s(%s", Bytecodes::name(op), (const char*)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@1568: virtual ArgToken make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, ArgToken base, ArgToken offset, TRAPS) { twisti@1568: _strbuf.print("%s(%s, %s", Bytecodes::name(op), (const char*)base, (const char*)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: } twisti@1568: virtual ArgToken make_invoke(methodOop m, vmIntrinsics::ID iid, twisti@1568: Bytecodes::Code op, bool tailcall, twisti@1568: int argc, ArgToken* argv, TRAPS) { coleenp@2497: Symbol* name, sig; twisti@1568: if (m != 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@1568: _strbuf.print("%s%s", (i > 0 ? ", " : ""), (const char*)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) { twisti@1568: EXCEPTION_MARK; 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@1568: out->print("\n*** "); twisti@1568: if (ex != Universe::virtual_machine_error_instance()) twisti@1568: ex->print_on(out); twisti@1568: else twisti@1568: out->print("lose: %s", printer.lose_message()); twisti@1568: out->print("\n}\n"); twisti@1568: } twisti@1568: out->print("\n"); twisti@1568: } twisti@1568: }; twisti@1573: #endif // 0 twisti@1568: twisti@1568: extern "C" twisti@1568: void print_method_handle(oop mh) { jrose@2148: if (!mh->is_oop()) { jrose@2148: tty->print_cr("*** not a method handle: "INTPTR_FORMAT, (intptr_t)mh); jrose@2639: } else if (java_lang_invoke_MethodHandle::is_instance(mh)) { twisti@1573: //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