twisti@1568: /* twisti@1568: * Copyright 2008-2010 Sun Microsystems, Inc. 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: * twisti@1568: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, twisti@1568: * CA 95054 USA or visit www.sun.com if you need additional information or twisti@1568: * have any questions. twisti@1568: * twisti@1568: */ twisti@1568: twisti@1568: /* twisti@1568: * JSR 292 reference implementation: method handle structure analysis twisti@1568: */ twisti@1568: twisti@1568: #include "incls/_precompiled.incl" twisti@1568: #include "incls/_methodHandleWalk.cpp.incl" twisti@1568: twisti@1568: void MethodHandleChain::set_method_handle(Handle mh, TRAPS) { twisti@1568: if (!java_dyn_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: twisti@1568: if (sun_dyn_DirectMethodHandle::is_instance(mh())) { twisti@1568: set_last_method(mh(), THREAD); twisti@1568: return; twisti@1568: } twisti@1568: if (sun_dyn_AdapterMethodHandle::is_instance(mh())) { twisti@1568: _conversion = AdapterMethodHandle_conversion(); twisti@1568: assert(_conversion != -1, "bad conv value"); twisti@1568: assert(sun_dyn_BoundMethodHandle::is_instance(mh()), "also BMH"); twisti@1568: } twisti@1568: if (sun_dyn_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(); twisti@1568: if (!is_bound() || java_dyn_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@1568: _arg_type = compute_bound_arg_type(NULL, (methodOop)target, _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@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@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: // FIXME: Make it explicit MethodHandleImpl refactors out from MethodHandle twisti@1568: BasicType arg_type = T_VOID; twisti@1568: if (target != NULL) { twisti@1568: oop mtype = java_dyn_MethodHandle::type(target); twisti@1568: int arg_num = MethodHandles::argument_slot_to_argnum(mtype, arg_slot); twisti@1568: if (arg_num >= 0) { twisti@1568: oop ptype = java_dyn_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: } 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@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@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@1568: MethodHandleWalker::ArgToken twisti@1568: MethodHandleWalker::walk(TRAPS) { twisti@1568: walk_incoming_state(CHECK_NULL); 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 twisti@1568: && conv_op > sun_dyn_AdapterMethodHandle::OP_RETYPE_RAW) { twisti@1568: lose("bad argument index", CHECK_NULL); twisti@1568: } twisti@1568: twisti@1568: // perform the adapter action twisti@1568: switch (chain().adapter_conversion_op()) { twisti@1568: case sun_dyn_AdapterMethodHandle::OP_RETYPE_ONLY: twisti@1568: case sun_dyn_AdapterMethodHandle::OP_RETYPE_RAW: twisti@1568: // No changes to arguments; pass the bits through. twisti@1568: // The only difference between the two ops is that the "only" version twisti@1568: // is fully compatible with the verifier, while the "raw" version twisti@1568: // performs a few extra bitwise conversions (like long <-> double). twisti@1568: break; twisti@1568: twisti@1568: case sun_dyn_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@1568: arg_state->_arg = make_conversion(T_OBJECT, dest_klass, Bytecodes::_checkcast, arg_state->_arg, CHECK_NULL); twisti@1568: debug_only(dest_klass = (klassOop)badOop); twisti@1568: break; twisti@1568: } twisti@1568: twisti@1568: case sun_dyn_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@1568: arg = make_conversion(dest, NULL, bc, arg, CHECK_NULL); twisti@1568: } else if (is_subword_type(dest)) { twisti@1568: bc = conversion_code(src, T_INT); twisti@1568: if (bc != Bytecodes::_illegal) { twisti@1568: arg = make_conversion(dest, NULL, bc, arg, CHECK_NULL); twisti@1568: bc = conversion_code(T_INT, dest); twisti@1568: arg = make_conversion(dest, NULL, bc, arg, CHECK_NULL); twisti@1568: } twisti@1568: } twisti@1568: if (bc == Bytecodes::_illegal) { twisti@1568: lose("bad primitive conversion", CHECK_NULL); twisti@1568: } twisti@1568: change_argument(src, arg_slot, dest, arg); twisti@1568: break; twisti@1568: } twisti@1568: twisti@1568: case sun_dyn_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@1568: Bytecodes::_checkcast, arg, CHECK_NULL); twisti@1568: vmIntrinsics::ID unboxer = vmIntrinsics::for_unboxing(dest); twisti@1568: if (unboxer == vmIntrinsics::_none) { twisti@1568: lose("no unboxing method", CHECK_NULL); twisti@1568: } twisti@1568: ArgToken arglist[2]; twisti@1568: arglist[0] = arg; // outgoing 'this' twisti@1568: arglist[1] = NULL; // sentinel twisti@1568: arg = make_invoke(NULL, unboxer, Bytecodes::_invokevirtual, false, 1, &arglist[0], CHECK_NULL); twisti@1568: change_argument(T_OBJECT, arg_slot, dest, arg); twisti@1568: break; twisti@1568: } twisti@1568: twisti@1568: case sun_dyn_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@1568: lose("no boxing method", CHECK_NULL); twisti@1568: } twisti@1568: ArgToken arglist[2]; twisti@1568: arglist[0] = arg; // outgoing value twisti@1568: arglist[1] = NULL; // sentinel twisti@1568: arg = make_invoke(NULL, boxer, Bytecodes::_invokevirtual, false, 1, &arglist[0], CHECK_NULL); twisti@1568: change_argument(src, arg_slot, T_OBJECT, arg); twisti@1568: break; twisti@1568: } twisti@1568: twisti@1568: case sun_dyn_AdapterMethodHandle::OP_SWAP_ARGS: { twisti@1568: int dest_arg_slot = chain().adapter_conversion_vminfo(); twisti@1568: if (!slot_has_argument(dest_arg_slot)) { twisti@1568: lose("bad swap index", CHECK_NULL); 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: twisti@1568: case sun_dyn_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@1568: lose("bad rotate index", CHECK_NULL); 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: twisti@1568: case sun_dyn_AdapterMethodHandle::OP_DUP_ARGS: { twisti@1568: int dup_slots = chain().adapter_conversion_stack_pushes(); twisti@1568: if (dup_slots <= 0) { twisti@1568: lose("bad dup count", CHECK_NULL); 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: twisti@1568: case sun_dyn_AdapterMethodHandle::OP_DROP_ARGS: { twisti@1568: int drop_slots = -chain().adapter_conversion_stack_pushes(); twisti@1568: if (drop_slots <= 0) { twisti@1568: lose("bad drop count", CHECK_NULL); 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: twisti@1568: case sun_dyn_AdapterMethodHandle::OP_COLLECT_ARGS: { //NYI, may GC twisti@1568: lose("unimplemented", CHECK_NULL); twisti@1568: break; twisti@1568: } twisti@1568: twisti@1568: case sun_dyn_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@1568: array_arg = make_conversion(T_OBJECT, array_klass(), Bytecodes::_checkcast, array_arg, CHECK_NULL); twisti@1568: change_argument(T_OBJECT, arg_slot, T_VOID, NULL); 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@1568: lose("bad spread length", CHECK_NULL); twisti@1568: } twisti@1568: twisti@1568: jvalue length_jvalue; length_jvalue.i = spread_length; twisti@1568: ArgToken length_arg = make_prim_constant(T_INT, &length_jvalue, CHECK_NULL); twisti@1568: // Call a built-in method known to the JVM to validate the length. twisti@1568: ArgToken arglist[3]; twisti@1568: arglist[0] = array_arg; // value to check twisti@1568: arglist[1] = length_arg; // length to check twisti@1568: arglist[2] = NULL; // sentinel twisti@1568: make_invoke(NULL, vmIntrinsics::_checkSpreadArgument, twisti@1568: Bytecodes::_invokestatic, false, 3, &arglist[0], CHECK_NULL); 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@1568: lose("primitive array NYI", CHECK_NULL); 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@1568: ArgToken offset_arg = make_prim_constant(T_INT, &offset_jvalue, CHECK_NULL); twisti@1568: ArgToken element_arg = make_fetch(element_type, element_klass(), aload_op, array_arg, offset_arg, CHECK_NULL); 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: twisti@1568: case sun_dyn_AdapterMethodHandle::OP_FLYBY: //NYI, runs Java code twisti@1568: case sun_dyn_AdapterMethodHandle::OP_RICOCHET: //NYI, runs Java code twisti@1568: lose("unimplemented", CHECK_NULL); twisti@1568: break; twisti@1568: twisti@1568: default: twisti@1568: lose("bad adapter conversion", CHECK_NULL); 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@1568: ArgToken arg = NULL; twisti@1568: if (arg_type == T_OBJECT) { twisti@1568: arg = make_oop_constant(arg_oop, CHECK_NULL); 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@1568: arg = make_prim_constant(arg_type, &arg_value, CHECK_NULL); twisti@1568: } else { twisti@1568: lose("bad bound value", CHECK_NULL); 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@1568: chain().next(CHECK_NULL); 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@1568: arglist[ap] = NULL; // 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@1568: void MethodHandleWalker::walk_incoming_state(TRAPS) { twisti@1568: Handle mtype(THREAD, chain().method_type_oop()); twisti@1568: int nptypes = java_dyn_MethodType::ptype_count(mtype()); twisti@1568: _outgoing_argc = nptypes; twisti@1568: int argp = nptypes - 1; twisti@1568: if (argp >= 0) { twisti@1568: _outgoing.at_grow(argp, make_state(T_VOID, NULL)); // 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( twisti@1568: java_dyn_MethodType::ptype(mtype(), i), &arg_type_klass); twisti@1568: ArgToken arg = make_parameter(arg_type, arg_type_klass, i, CHECK); twisti@1568: 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@1568: _outgoing.insert_before(argp+1, make_state(T_VOID, NULL)); 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( twisti@1568: java_dyn_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@1568: // this is messy because some kinds of arguments are paired with twisti@1568: // companion slots containing an empty value twisti@1568: void MethodHandleWalker::change_argument(BasicType old_type, int slot, BasicType new_type, twisti@1568: MethodHandleWalker::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@1568: 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@1568: _outgoing.insert_before(slot+i, make_state(T_VOID, NULL)); 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@1568: void MethodHandleCompiler::compile(TRAPS) { twisti@1568: assert(_thread == THREAD, "must be same thread"); twisti@1568: twisti@1568: _constant_oops.append(Handle()); // element zero is always the null constant twisti@1568: _constant_prims.append(NULL); twisti@1568: { twisti@1568: symbolOop sig twisti@1568: = java_dyn_MethodType::as_signature(chain().method_type_oop(), true, CHECK); twisti@1568: _signature_index = find_oop_constant(sig); twisti@1568: assert(signature() == sig, ""); twisti@1568: } twisti@1568: twisti@1568: walk(CHECK); twisti@1568: } twisti@1568: twisti@1568: MethodHandleWalker::ArgToken twisti@1568: MethodHandleCompiler::make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, twisti@1568: MethodHandleWalker::ArgToken src, TRAPS) { twisti@1568: Unimplemented(); twisti@1568: return NULL; twisti@1568: } twisti@1568: 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@1568: // If tailcall, we have walked all the way to a direct method handle. twisti@1568: // Otherwise, make a recursive call to some helper routine. twisti@1568: #ifdef ASSERT twisti@1568: switch (op) { twisti@1568: case Bytecodes::_invokevirtual: twisti@1568: case Bytecodes::_invokespecial: twisti@1568: case Bytecodes::_invokestatic: twisti@1568: case Bytecodes::_invokeinterface: twisti@1568: break; twisti@1568: default: twisti@1568: ShouldNotReachHere(); twisti@1568: } twisti@1568: #endif //ASSERT twisti@1568: _bytes.put((char) op); twisti@1568: twisti@1568: Unimplemented(); twisti@1568: return NULL; twisti@1568: } twisti@1568: twisti@1568: MethodHandleWalker::ArgToken twisti@1568: MethodHandleCompiler::make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, twisti@1568: MethodHandleWalker::ArgToken base, twisti@1568: MethodHandleWalker::ArgToken offset, twisti@1568: TRAPS) { twisti@1568: Unimplemented(); twisti@1568: return NULL; twisti@1568: } twisti@1568: twisti@1568: int MethodHandleCompiler::find_oop_constant(oop con) { twisti@1568: if (con == NULL) return 0; twisti@1568: for (int i = 1, imax = _constant_oops.length(); i < imax; i++) { twisti@1568: if (_constant_oops.at(i) == con) twisti@1568: return i; twisti@1568: } twisti@1568: _constant_prims.append(NULL); twisti@1568: return _constant_oops.append(con); twisti@1568: } twisti@1568: twisti@1568: int MethodHandleCompiler::find_prim_constant(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@1568: for (int i = 1, imax = _constant_prims.length(); i < imax; i++) { twisti@1568: PrimCon* pcon = _constant_prims.at(i); twisti@1568: if (pcon != NULL && pcon->_type == bt) { twisti@1568: bool match = false; twisti@1568: switch (type2size[bt]) { twisti@1568: case 1: if (pcon->_value.i == con->i) match = true; break; twisti@1568: case 2: if (pcon->_value.j == con->j) match = true; break; twisti@1568: } twisti@1568: if (match) twisti@1568: return i; twisti@1568: } twisti@1568: } twisti@1568: PrimCon* pcon = new PrimCon(); twisti@1568: pcon->_type = bt; twisti@1568: pcon->_value = (*con); twisti@1568: _constant_oops.append(Handle()); twisti@1568: return _constant_prims.append(pcon); 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@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) { twisti@1568: symbolOop 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@1568: twisti@1568: extern "C" twisti@1568: void print_method_handle(oop mh) { twisti@1568: if (java_dyn_MethodHandle::is_instance(mh)) { twisti@1568: 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