src/share/vm/prims/methodHandleWalk.cpp

Fri, 11 Mar 2011 22:34:57 -0800

author
jrose
date
Fri, 11 Mar 2011 22:34:57 -0800
changeset 2639
8033953d67ff
parent 2638
72dee110246f
child 2742
ed69575596ac
permissions
-rw-r--r--

7012648: move JSR 292 to package java.lang.invoke and adjust names
Summary: package and class renaming only; delete unused methods and classes
Reviewed-by: twisti

twisti@1568 1 /*
twisti@2437 2 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
twisti@1568 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
twisti@1568 4 *
twisti@1568 5 * This code is free software; you can redistribute it and/or modify it
twisti@1568 6 * under the terms of the GNU General Public License version 2 only, as
twisti@1568 7 * published by the Free Software Foundation.
twisti@1568 8 *
twisti@1568 9 * This code is distributed in the hope that it will be useful, but WITHOUT
twisti@1568 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
twisti@1568 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
twisti@1568 12 * version 2 for more details (a copy is included in the LICENSE file that
twisti@1568 13 * accompanied this code).
twisti@1568 14 *
twisti@1568 15 * You should have received a copy of the GNU General Public License version
twisti@1568 16 * 2 along with this work; if not, write to the Free Software Foundation,
twisti@1568 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
twisti@1568 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
twisti@1568 22 *
twisti@1568 23 */
twisti@1568 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "interpreter/rewriter.hpp"
stefank@2314 27 #include "memory/oopFactory.hpp"
stefank@2314 28 #include "prims/methodHandleWalk.hpp"
stefank@2314 29
twisti@1568 30 /*
twisti@1568 31 * JSR 292 reference implementation: method handle structure analysis
twisti@1568 32 */
twisti@1568 33
twisti@1573 34
twisti@1573 35 // -----------------------------------------------------------------------------
twisti@1573 36 // MethodHandleChain
twisti@1573 37
twisti@1568 38 void MethodHandleChain::set_method_handle(Handle mh, TRAPS) {
jrose@2639 39 if (!java_lang_invoke_MethodHandle::is_instance(mh())) lose("bad method handle", CHECK);
twisti@1568 40
twisti@1568 41 // set current method handle and unpack partially
twisti@1568 42 _method_handle = mh;
twisti@1568 43 _is_last = false;
twisti@1568 44 _is_bound = false;
twisti@1568 45 _arg_slot = -1;
twisti@1568 46 _arg_type = T_VOID;
twisti@1568 47 _conversion = -1;
twisti@1568 48 _last_invoke = Bytecodes::_nop; //arbitrary non-garbage
twisti@1568 49
jrose@2639 50 if (java_lang_invoke_DirectMethodHandle::is_instance(mh())) {
twisti@1568 51 set_last_method(mh(), THREAD);
twisti@1568 52 return;
twisti@1568 53 }
jrose@2639 54 if (java_lang_invoke_AdapterMethodHandle::is_instance(mh())) {
twisti@1568 55 _conversion = AdapterMethodHandle_conversion();
twisti@1568 56 assert(_conversion != -1, "bad conv value");
jrose@2639 57 assert(java_lang_invoke_BoundMethodHandle::is_instance(mh()), "also BMH");
twisti@1568 58 }
jrose@2639 59 if (java_lang_invoke_BoundMethodHandle::is_instance(mh())) {
twisti@1568 60 if (!is_adapter()) // keep AMH and BMH separate in this model
twisti@1568 61 _is_bound = true;
twisti@1568 62 _arg_slot = BoundMethodHandle_vmargslot();
twisti@1568 63 oop target = MethodHandle_vmtarget_oop();
jrose@2639 64 if (!is_bound() || java_lang_invoke_MethodHandle::is_instance(target)) {
twisti@1568 65 _arg_type = compute_bound_arg_type(target, NULL, _arg_slot, CHECK);
twisti@1568 66 } else if (target != NULL && target->is_method()) {
twisti@1573 67 methodOop m = (methodOop) target;
twisti@1573 68 _arg_type = compute_bound_arg_type(NULL, m, _arg_slot, CHECK);
twisti@1568 69 set_last_method(mh(), CHECK);
twisti@1568 70 } else {
twisti@1568 71 _is_bound = false; // lose!
twisti@1568 72 }
twisti@1568 73 }
twisti@1568 74 if (is_bound() && _arg_type == T_VOID) {
twisti@1568 75 lose("bad vmargslot", CHECK);
twisti@1568 76 }
twisti@1568 77 if (!is_bound() && !is_adapter()) {
twisti@1568 78 lose("unrecognized MH type", CHECK);
twisti@1568 79 }
twisti@1568 80 }
twisti@1568 81
twisti@1573 82
twisti@1568 83 void MethodHandleChain::set_last_method(oop target, TRAPS) {
twisti@1568 84 _is_last = true;
twisti@1568 85 klassOop receiver_limit_oop = NULL;
twisti@1568 86 int flags = 0;
twisti@1568 87 methodOop m = MethodHandles::decode_method(target, receiver_limit_oop, flags);
twisti@1568 88 _last_method = methodHandle(THREAD, m);
twisti@1568 89 if ((flags & MethodHandles::_dmf_has_receiver) == 0)
twisti@1568 90 _last_invoke = Bytecodes::_invokestatic;
twisti@1568 91 else if ((flags & MethodHandles::_dmf_does_dispatch) == 0)
twisti@1568 92 _last_invoke = Bytecodes::_invokespecial;
twisti@1568 93 else if ((flags & MethodHandles::_dmf_from_interface) != 0)
twisti@1568 94 _last_invoke = Bytecodes::_invokeinterface;
twisti@1568 95 else
twisti@1568 96 _last_invoke = Bytecodes::_invokevirtual;
twisti@1568 97 }
twisti@1568 98
twisti@1573 99
twisti@1568 100 BasicType MethodHandleChain::compute_bound_arg_type(oop target, methodOop m, int arg_slot, TRAPS) {
twisti@1568 101 // There is no direct indication of whether the argument is primitive or not.
twisti@1568 102 // It is implied by the _vmentry code, and by the MethodType of the target.
twisti@1568 103 BasicType arg_type = T_VOID;
twisti@1568 104 if (target != NULL) {
jrose@2639 105 oop mtype = java_lang_invoke_MethodHandle::type(target);
twisti@1568 106 int arg_num = MethodHandles::argument_slot_to_argnum(mtype, arg_slot);
twisti@1568 107 if (arg_num >= 0) {
jrose@2639 108 oop ptype = java_lang_invoke_MethodType::ptype(mtype, arg_num);
twisti@1568 109 arg_type = java_lang_Class::as_BasicType(ptype);
twisti@1568 110 }
twisti@1568 111 } else if (m != NULL) {
twisti@1568 112 // figure out the argument type from the slot
twisti@1568 113 // FIXME: make this explicit in the MH
twisti@1568 114 int cur_slot = m->size_of_parameters();
twisti@1568 115 if (arg_slot >= cur_slot)
twisti@1568 116 return T_VOID;
twisti@1568 117 if (!m->is_static()) {
twisti@1568 118 cur_slot -= type2size[T_OBJECT];
twisti@1568 119 if (cur_slot == arg_slot)
twisti@1568 120 return T_OBJECT;
twisti@1568 121 }
coleenp@2497 122 ResourceMark rm(THREAD);
twisti@1568 123 for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) {
twisti@1568 124 BasicType bt = ss.type();
twisti@1568 125 cur_slot -= type2size[bt];
twisti@1568 126 if (cur_slot <= arg_slot) {
twisti@1568 127 if (cur_slot == arg_slot)
twisti@1568 128 arg_type = bt;
twisti@1568 129 break;
twisti@1568 130 }
twisti@1568 131 }
twisti@1568 132 }
twisti@1568 133 if (arg_type == T_ARRAY)
twisti@1568 134 arg_type = T_OBJECT;
twisti@1568 135 return arg_type;
twisti@1568 136 }
twisti@1568 137
twisti@1573 138
twisti@1568 139 void MethodHandleChain::lose(const char* msg, TRAPS) {
twisti@1568 140 _lose_message = msg;
twisti@1568 141 if (!THREAD->is_Java_thread() || ((JavaThread*)THREAD)->thread_state() != _thread_in_vm) {
twisti@1568 142 // throw a preallocated exception
twisti@1568 143 THROW_OOP(Universe::virtual_machine_error_instance());
twisti@1568 144 }
twisti@1568 145 THROW_MSG(vmSymbols::java_lang_InternalError(), msg);
twisti@1568 146 }
twisti@1568 147
twisti@1573 148
twisti@1573 149 // -----------------------------------------------------------------------------
twisti@1573 150 // MethodHandleWalker
twisti@1573 151
twisti@1568 152 Bytecodes::Code MethodHandleWalker::conversion_code(BasicType src, BasicType dest) {
twisti@1568 153 if (is_subword_type(src)) {
twisti@1568 154 src = T_INT; // all subword src types act like int
twisti@1568 155 }
twisti@1568 156 if (src == dest) {
twisti@1568 157 return Bytecodes::_nop;
twisti@1568 158 }
twisti@1568 159
twisti@1568 160 #define SRC_DEST(s,d) (((int)(s) << 4) + (int)(d))
twisti@1568 161 switch (SRC_DEST(src, dest)) {
twisti@1568 162 case SRC_DEST(T_INT, T_LONG): return Bytecodes::_i2l;
twisti@1568 163 case SRC_DEST(T_INT, T_FLOAT): return Bytecodes::_i2f;
twisti@1568 164 case SRC_DEST(T_INT, T_DOUBLE): return Bytecodes::_i2d;
twisti@1568 165 case SRC_DEST(T_INT, T_BYTE): return Bytecodes::_i2b;
twisti@1568 166 case SRC_DEST(T_INT, T_CHAR): return Bytecodes::_i2c;
twisti@1568 167 case SRC_DEST(T_INT, T_SHORT): return Bytecodes::_i2s;
twisti@1568 168
twisti@1568 169 case SRC_DEST(T_LONG, T_INT): return Bytecodes::_l2i;
twisti@1568 170 case SRC_DEST(T_LONG, T_FLOAT): return Bytecodes::_l2f;
twisti@1568 171 case SRC_DEST(T_LONG, T_DOUBLE): return Bytecodes::_l2d;
twisti@1568 172
twisti@1568 173 case SRC_DEST(T_FLOAT, T_INT): return Bytecodes::_f2i;
twisti@1568 174 case SRC_DEST(T_FLOAT, T_LONG): return Bytecodes::_f2l;
twisti@1568 175 case SRC_DEST(T_FLOAT, T_DOUBLE): return Bytecodes::_f2d;
twisti@1568 176
twisti@1568 177 case SRC_DEST(T_DOUBLE, T_INT): return Bytecodes::_d2i;
twisti@1568 178 case SRC_DEST(T_DOUBLE, T_LONG): return Bytecodes::_d2l;
twisti@1568 179 case SRC_DEST(T_DOUBLE, T_FLOAT): return Bytecodes::_d2f;
twisti@1568 180 }
twisti@1568 181 #undef SRC_DEST
twisti@1568 182
twisti@1568 183 // cannot do it in one step, or at all
twisti@1568 184 return Bytecodes::_illegal;
twisti@1568 185 }
twisti@1568 186
twisti@1573 187
twisti@1573 188 // -----------------------------------------------------------------------------
twisti@1573 189 // MethodHandleWalker::walk
twisti@1573 190 //
twisti@1568 191 MethodHandleWalker::ArgToken
twisti@1568 192 MethodHandleWalker::walk(TRAPS) {
twisti@1573 193 ArgToken empty = ArgToken(); // Empty return value.
twisti@1573 194
twisti@1573 195 walk_incoming_state(CHECK_(empty));
twisti@1568 196
twisti@1568 197 for (;;) {
twisti@1568 198 set_method_handle(chain().method_handle_oop());
twisti@1568 199
twisti@1568 200 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
twisti@1568 201
twisti@1568 202 if (chain().is_adapter()) {
twisti@1568 203 int conv_op = chain().adapter_conversion_op();
twisti@1568 204 int arg_slot = chain().adapter_arg_slot();
twisti@1568 205 SlotState* arg_state = slot_state(arg_slot);
twisti@1568 206 if (arg_state == NULL
jrose@2639 207 && conv_op > java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW) {
twisti@1573 208 lose("bad argument index", CHECK_(empty));
twisti@1568 209 }
twisti@1568 210
twisti@1568 211 // perform the adapter action
twisti@1568 212 switch (chain().adapter_conversion_op()) {
jrose@2639 213 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY:
twisti@1568 214 // No changes to arguments; pass the bits through.
twisti@1568 215 break;
twisti@1568 216
jrose@2639 217 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW: {
twisti@1573 218 // To keep the verifier happy, emit bitwise ("raw") conversions as needed.
twisti@1573 219 // See MethodHandles::same_basic_type_for_arguments for allowed conversions.
twisti@1573 220 Handle incoming_mtype(THREAD, chain().method_type_oop());
twisti@1573 221 oop outgoing_mh_oop = chain().vmtarget_oop();
jrose@2639 222 if (!java_lang_invoke_MethodHandle::is_instance(outgoing_mh_oop))
twisti@1573 223 lose("outgoing target not a MethodHandle", CHECK_(empty));
jrose@2639 224 Handle outgoing_mtype(THREAD, java_lang_invoke_MethodHandle::type(outgoing_mh_oop));
twisti@1573 225 outgoing_mh_oop = NULL; // GC safety
twisti@1573 226
jrose@2639 227 int nptypes = java_lang_invoke_MethodType::ptype_count(outgoing_mtype());
jrose@2639 228 if (nptypes != java_lang_invoke_MethodType::ptype_count(incoming_mtype()))
twisti@1573 229 lose("incoming and outgoing parameter count do not agree", CHECK_(empty));
twisti@1573 230
twisti@1573 231 for (int i = 0, slot = _outgoing.length() - 1; slot >= 0; slot--) {
twisti@1573 232 SlotState* arg_state = slot_state(slot);
twisti@1573 233 if (arg_state->_type == T_VOID) continue;
twisti@1573 234 ArgToken arg = _outgoing.at(slot)._arg;
twisti@1573 235
twisti@1573 236 klassOop in_klass = NULL;
twisti@1573 237 klassOop out_klass = NULL;
jrose@2639 238 BasicType inpbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(incoming_mtype(), i), &in_klass);
jrose@2639 239 BasicType outpbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(outgoing_mtype(), i), &out_klass);
twisti@1573 240 assert(inpbt == arg.basic_type(), "sanity");
twisti@1573 241
twisti@1573 242 if (inpbt != outpbt) {
twisti@1573 243 vmIntrinsics::ID iid = vmIntrinsics::for_raw_conversion(inpbt, outpbt);
twisti@1573 244 if (iid == vmIntrinsics::_none) {
twisti@1573 245 lose("no raw conversion method", CHECK_(empty));
twisti@1573 246 }
twisti@1573 247 ArgToken arglist[2];
twisti@1573 248 arglist[0] = arg; // outgoing 'this'
twisti@1573 249 arglist[1] = ArgToken(); // sentinel
twisti@1573 250 arg = make_invoke(NULL, iid, Bytecodes::_invokestatic, false, 1, &arglist[0], CHECK_(empty));
twisti@1573 251 change_argument(inpbt, slot, outpbt, arg);
twisti@1573 252 }
twisti@1573 253
twisti@1573 254 i++; // We need to skip void slots at the top of the loop.
twisti@1573 255 }
twisti@1573 256
jrose@2639 257 BasicType inrbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(incoming_mtype()));
jrose@2639 258 BasicType outrbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(outgoing_mtype()));
twisti@1573 259 if (inrbt != outrbt) {
twisti@1573 260 if (inrbt == T_INT && outrbt == T_VOID) {
twisti@1573 261 // See comments in MethodHandles::same_basic_type_for_arguments.
twisti@1573 262 } else {
twisti@1573 263 assert(false, "IMPLEMENT ME");
twisti@1573 264 lose("no raw conversion method", CHECK_(empty));
twisti@1573 265 }
twisti@1573 266 }
twisti@1573 267 break;
twisti@1573 268 }
twisti@1573 269
jrose@2639 270 case java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST: {
twisti@1568 271 // checkcast the Nth outgoing argument in place
twisti@1568 272 klassOop dest_klass = NULL;
twisti@1568 273 BasicType dest = java_lang_Class::as_BasicType(chain().adapter_arg_oop(), &dest_klass);
twisti@1568 274 assert(dest == T_OBJECT, "");
twisti@1568 275 assert(dest == arg_state->_type, "");
twisti@1573 276 ArgToken arg = arg_state->_arg;
twisti@1573 277 ArgToken new_arg = make_conversion(T_OBJECT, dest_klass, Bytecodes::_checkcast, arg, CHECK_(empty));
twisti@1573 278 assert(arg.index() == new_arg.index(), "should be the same index");
twisti@1568 279 debug_only(dest_klass = (klassOop)badOop);
twisti@1568 280 break;
twisti@1568 281 }
twisti@1568 282
jrose@2639 283 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM: {
twisti@1568 284 // i2l, etc., on the Nth outgoing argument in place
twisti@1568 285 BasicType src = chain().adapter_conversion_src_type(),
twisti@1568 286 dest = chain().adapter_conversion_dest_type();
twisti@1568 287 Bytecodes::Code bc = conversion_code(src, dest);
twisti@1568 288 ArgToken arg = arg_state->_arg;
twisti@1568 289 if (bc == Bytecodes::_nop) {
twisti@1568 290 break;
twisti@1568 291 } else if (bc != Bytecodes::_illegal) {
twisti@1573 292 arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
twisti@1568 293 } else if (is_subword_type(dest)) {
twisti@1568 294 bc = conversion_code(src, T_INT);
twisti@1568 295 if (bc != Bytecodes::_illegal) {
twisti@1573 296 arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
twisti@1568 297 bc = conversion_code(T_INT, dest);
twisti@1573 298 arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
twisti@1568 299 }
twisti@1568 300 }
twisti@1568 301 if (bc == Bytecodes::_illegal) {
twisti@1573 302 lose("bad primitive conversion", CHECK_(empty));
twisti@1568 303 }
twisti@1568 304 change_argument(src, arg_slot, dest, arg);
twisti@1568 305 break;
twisti@1568 306 }
twisti@1568 307
jrose@2639 308 case java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM: {
twisti@1568 309 // checkcast to wrapper type & call intValue, etc.
twisti@1568 310 BasicType dest = chain().adapter_conversion_dest_type();
twisti@1568 311 ArgToken arg = arg_state->_arg;
twisti@1568 312 arg = make_conversion(T_OBJECT, SystemDictionary::box_klass(dest),
twisti@1573 313 Bytecodes::_checkcast, arg, CHECK_(empty));
twisti@1568 314 vmIntrinsics::ID unboxer = vmIntrinsics::for_unboxing(dest);
twisti@1568 315 if (unboxer == vmIntrinsics::_none) {
twisti@1573 316 lose("no unboxing method", CHECK_(empty));
twisti@1568 317 }
twisti@1568 318 ArgToken arglist[2];
twisti@1573 319 arglist[0] = arg; // outgoing 'this'
twisti@1573 320 arglist[1] = ArgToken(); // sentinel
twisti@1573 321 arg = make_invoke(NULL, unboxer, Bytecodes::_invokevirtual, false, 1, &arglist[0], CHECK_(empty));
twisti@1568 322 change_argument(T_OBJECT, arg_slot, dest, arg);
twisti@1568 323 break;
twisti@1568 324 }
twisti@1568 325
jrose@2639 326 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF: {
twisti@1568 327 // call wrapper type.valueOf
twisti@1568 328 BasicType src = chain().adapter_conversion_src_type();
twisti@1568 329 ArgToken arg = arg_state->_arg;
twisti@1568 330 vmIntrinsics::ID boxer = vmIntrinsics::for_boxing(src);
twisti@1568 331 if (boxer == vmIntrinsics::_none) {
twisti@1573 332 lose("no boxing method", CHECK_(empty));
twisti@1568 333 }
twisti@1568 334 ArgToken arglist[2];
twisti@1573 335 arglist[0] = arg; // outgoing value
twisti@1573 336 arglist[1] = ArgToken(); // sentinel
jrose@2148 337 arg = make_invoke(NULL, boxer, Bytecodes::_invokevirtual, false, 1, &arglist[0], CHECK_(empty));
twisti@1568 338 change_argument(src, arg_slot, T_OBJECT, arg);
twisti@1568 339 break;
twisti@1568 340 }
twisti@1568 341
jrose@2639 342 case java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS: {
twisti@1568 343 int dest_arg_slot = chain().adapter_conversion_vminfo();
twisti@1568 344 if (!slot_has_argument(dest_arg_slot)) {
twisti@1573 345 lose("bad swap index", CHECK_(empty));
twisti@1568 346 }
twisti@1568 347 // a simple swap between two arguments
twisti@1568 348 SlotState* dest_arg_state = slot_state(dest_arg_slot);
twisti@1568 349 SlotState temp = (*dest_arg_state);
twisti@1568 350 (*dest_arg_state) = (*arg_state);
twisti@1568 351 (*arg_state) = temp;
twisti@1568 352 break;
twisti@1568 353 }
twisti@1568 354
jrose@2639 355 case java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS: {
twisti@1568 356 int dest_arg_slot = chain().adapter_conversion_vminfo();
twisti@1568 357 if (!slot_has_argument(dest_arg_slot) || arg_slot == dest_arg_slot) {
twisti@1573 358 lose("bad rotate index", CHECK_(empty));
twisti@1568 359 }
twisti@1568 360 SlotState* dest_arg_state = slot_state(dest_arg_slot);
twisti@1568 361 // Rotate the source argument (plus following N slots) into the
twisti@1568 362 // position occupied by the dest argument (plus following N slots).
twisti@1568 363 int rotate_count = type2size[dest_arg_state->_type];
twisti@1568 364 // (no other rotate counts are currently supported)
twisti@1568 365 if (arg_slot < dest_arg_slot) {
twisti@1568 366 for (int i = 0; i < rotate_count; i++) {
twisti@1568 367 SlotState temp = _outgoing.at(arg_slot);
twisti@1568 368 _outgoing.remove_at(arg_slot);
twisti@1568 369 _outgoing.insert_before(dest_arg_slot + rotate_count - 1, temp);
twisti@1568 370 }
twisti@1568 371 } else { // arg_slot > dest_arg_slot
twisti@1568 372 for (int i = 0; i < rotate_count; i++) {
twisti@1568 373 SlotState temp = _outgoing.at(arg_slot + rotate_count - 1);
twisti@1568 374 _outgoing.remove_at(arg_slot + rotate_count - 1);
twisti@1568 375 _outgoing.insert_before(dest_arg_slot, temp);
twisti@1568 376 }
twisti@1568 377 }
twisti@1568 378 break;
twisti@1568 379 }
twisti@1568 380
jrose@2639 381 case java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS: {
twisti@1568 382 int dup_slots = chain().adapter_conversion_stack_pushes();
twisti@1568 383 if (dup_slots <= 0) {
twisti@1573 384 lose("bad dup count", CHECK_(empty));
twisti@1568 385 }
twisti@1568 386 for (int i = 0; i < dup_slots; i++) {
twisti@1568 387 SlotState* dup = slot_state(arg_slot + 2*i);
twisti@1568 388 if (dup == NULL) break; // safety net
twisti@1568 389 if (dup->_type != T_VOID) _outgoing_argc += 1;
twisti@1568 390 _outgoing.insert_before(i, (*dup));
twisti@1568 391 }
twisti@1568 392 break;
twisti@1568 393 }
twisti@1568 394
jrose@2639 395 case java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS: {
twisti@1568 396 int drop_slots = -chain().adapter_conversion_stack_pushes();
twisti@1568 397 if (drop_slots <= 0) {
twisti@1573 398 lose("bad drop count", CHECK_(empty));
twisti@1568 399 }
twisti@1568 400 for (int i = 0; i < drop_slots; i++) {
twisti@1568 401 SlotState* drop = slot_state(arg_slot);
twisti@1568 402 if (drop == NULL) break; // safety net
twisti@1568 403 if (drop->_type != T_VOID) _outgoing_argc -= 1;
twisti@1568 404 _outgoing.remove_at(arg_slot);
twisti@1568 405 }
twisti@1568 406 break;
twisti@1568 407 }
twisti@1568 408
jrose@2639 409 case java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS: { //NYI, may GC
twisti@1573 410 lose("unimplemented", CHECK_(empty));
twisti@1568 411 break;
twisti@1568 412 }
twisti@1568 413
jrose@2639 414 case java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS: {
twisti@1568 415 klassOop array_klass_oop = NULL;
twisti@1568 416 BasicType array_type = java_lang_Class::as_BasicType(chain().adapter_arg_oop(),
twisti@1568 417 &array_klass_oop);
twisti@1568 418 assert(array_type == T_OBJECT, "");
twisti@1568 419 assert(Klass::cast(array_klass_oop)->oop_is_array(), "");
twisti@1568 420 arrayKlassHandle array_klass(THREAD, array_klass_oop);
twisti@1568 421 debug_only(array_klass_oop = (klassOop)badOop);
twisti@1568 422
twisti@1568 423 klassOop element_klass_oop = NULL;
twisti@1568 424 BasicType element_type = java_lang_Class::as_BasicType(array_klass->component_mirror(),
twisti@1568 425 &element_klass_oop);
twisti@1568 426 KlassHandle element_klass(THREAD, element_klass_oop);
twisti@1568 427 debug_only(element_klass_oop = (klassOop)badOop);
twisti@1568 428
twisti@1568 429 // Fetch the argument, which we will cast to the required array type.
twisti@1568 430 assert(arg_state->_type == T_OBJECT, "");
twisti@1568 431 ArgToken array_arg = arg_state->_arg;
twisti@1573 432 array_arg = make_conversion(T_OBJECT, array_klass(), Bytecodes::_checkcast, array_arg, CHECK_(empty));
twisti@1573 433 change_argument(T_OBJECT, arg_slot, T_VOID, ArgToken(tt_void));
twisti@1568 434
twisti@1568 435 // Check the required length.
twisti@1568 436 int spread_slots = 1 + chain().adapter_conversion_stack_pushes();
twisti@1568 437 int spread_length = spread_slots;
twisti@1568 438 if (type2size[element_type] == 2) {
twisti@1568 439 if (spread_slots % 2 != 0) spread_slots = -1; // force error
twisti@1568 440 spread_length = spread_slots / 2;
twisti@1568 441 }
twisti@1568 442 if (spread_slots < 0) {
twisti@1573 443 lose("bad spread length", CHECK_(empty));
twisti@1568 444 }
twisti@1568 445
twisti@1568 446 jvalue length_jvalue; length_jvalue.i = spread_length;
twisti@1573 447 ArgToken length_arg = make_prim_constant(T_INT, &length_jvalue, CHECK_(empty));
twisti@1568 448 // Call a built-in method known to the JVM to validate the length.
twisti@1568 449 ArgToken arglist[3];
twisti@1573 450 arglist[0] = array_arg; // value to check
twisti@1573 451 arglist[1] = length_arg; // length to check
twisti@1573 452 arglist[2] = ArgToken(); // sentinel
twisti@1568 453 make_invoke(NULL, vmIntrinsics::_checkSpreadArgument,
twisti@1573 454 Bytecodes::_invokestatic, false, 3, &arglist[0], CHECK_(empty));
twisti@1568 455
twisti@1568 456 // Spread out the array elements.
twisti@1568 457 Bytecodes::Code aload_op = Bytecodes::_aaload;
twisti@1568 458 if (element_type != T_OBJECT) {
twisti@1573 459 lose("primitive array NYI", CHECK_(empty));
twisti@1568 460 }
twisti@1568 461 int ap = arg_slot;
twisti@1568 462 for (int i = 0; i < spread_length; i++) {
twisti@1568 463 jvalue offset_jvalue; offset_jvalue.i = i;
twisti@1573 464 ArgToken offset_arg = make_prim_constant(T_INT, &offset_jvalue, CHECK_(empty));
twisti@1573 465 ArgToken element_arg = make_fetch(element_type, element_klass(), aload_op, array_arg, offset_arg, CHECK_(empty));
twisti@1568 466 change_argument(T_VOID, ap, element_type, element_arg);
twisti@1568 467 ap += type2size[element_type];
twisti@1568 468 }
twisti@1568 469 break;
twisti@1568 470 }
twisti@1568 471
jrose@2639 472 case java_lang_invoke_AdapterMethodHandle::OP_FLYBY: //NYI, runs Java code
jrose@2639 473 case java_lang_invoke_AdapterMethodHandle::OP_RICOCHET: //NYI, runs Java code
twisti@1573 474 lose("unimplemented", CHECK_(empty));
twisti@1568 475 break;
twisti@1568 476
twisti@1568 477 default:
twisti@1573 478 lose("bad adapter conversion", CHECK_(empty));
twisti@1568 479 break;
twisti@1568 480 }
twisti@1568 481 }
twisti@1568 482
twisti@1568 483 if (chain().is_bound()) {
twisti@1568 484 // push a new argument
twisti@1568 485 BasicType arg_type = chain().bound_arg_type();
twisti@1568 486 jint arg_slot = chain().bound_arg_slot();
twisti@1568 487 oop arg_oop = chain().bound_arg_oop();
twisti@1573 488 ArgToken arg;
twisti@1568 489 if (arg_type == T_OBJECT) {
twisti@1573 490 arg = make_oop_constant(arg_oop, CHECK_(empty));
twisti@1568 491 } else {
twisti@1568 492 jvalue arg_value;
twisti@1568 493 BasicType bt = java_lang_boxing_object::get_value(arg_oop, &arg_value);
twisti@1568 494 if (bt == arg_type) {
twisti@1573 495 arg = make_prim_constant(arg_type, &arg_value, CHECK_(empty));
twisti@1568 496 } else {
twisti@1573 497 lose("bad bound value", CHECK_(empty));
twisti@1568 498 }
twisti@1568 499 }
twisti@1568 500 debug_only(arg_oop = badOop);
twisti@1568 501 change_argument(T_VOID, arg_slot, arg_type, arg);
twisti@1568 502 }
twisti@1568 503
twisti@1568 504 // this test must come after the body of the loop
twisti@1568 505 if (!chain().is_last()) {
twisti@1573 506 chain().next(CHECK_(empty));
twisti@1568 507 } else {
twisti@1568 508 break;
twisti@1568 509 }
twisti@1568 510 }
twisti@1568 511
twisti@1568 512 // finish the sequence with a tail-call to the ultimate target
twisti@1568 513 // parameters are passed in logical order (recv 1st), not slot order
twisti@1568 514 ArgToken* arglist = NEW_RESOURCE_ARRAY(ArgToken, _outgoing.length() + 1);
twisti@1568 515 int ap = 0;
twisti@1568 516 for (int i = _outgoing.length() - 1; i >= 0; i--) {
twisti@1568 517 SlotState* arg_state = slot_state(i);
twisti@1568 518 if (arg_state->_type == T_VOID) continue;
twisti@1568 519 arglist[ap++] = _outgoing.at(i)._arg;
twisti@1568 520 }
twisti@1568 521 assert(ap == _outgoing_argc, "");
twisti@1573 522 arglist[ap] = ArgToken(); // add a sentinel, for the sake of asserts
twisti@1568 523 return make_invoke(chain().last_method_oop(),
twisti@1568 524 vmIntrinsics::_none,
twisti@1568 525 chain().last_invoke_code(), true,
twisti@1568 526 ap, arglist, THREAD);
twisti@1568 527 }
twisti@1568 528
twisti@1573 529
twisti@1573 530 // -----------------------------------------------------------------------------
twisti@1573 531 // MethodHandleWalker::walk_incoming_state
twisti@1573 532 //
twisti@1568 533 void MethodHandleWalker::walk_incoming_state(TRAPS) {
twisti@1568 534 Handle mtype(THREAD, chain().method_type_oop());
jrose@2639 535 int nptypes = java_lang_invoke_MethodType::ptype_count(mtype());
twisti@1568 536 _outgoing_argc = nptypes;
twisti@1568 537 int argp = nptypes - 1;
twisti@1568 538 if (argp >= 0) {
twisti@1573 539 _outgoing.at_grow(argp, make_state(T_VOID, ArgToken(tt_void))); // presize
twisti@1568 540 }
twisti@1568 541 for (int i = 0; i < nptypes; i++) {
twisti@1568 542 klassOop arg_type_klass = NULL;
twisti@1568 543 BasicType arg_type = java_lang_Class::as_BasicType(
jrose@2639 544 java_lang_invoke_MethodType::ptype(mtype(), i), &arg_type_klass);
twisti@1573 545 int index = new_local_index(arg_type);
twisti@1573 546 ArgToken arg = make_parameter(arg_type, arg_type_klass, index, CHECK);
twisti@1573 547 debug_only(arg_type_klass = (klassOop) NULL);
twisti@1568 548 _outgoing.at_put(argp, make_state(arg_type, arg));
twisti@1568 549 if (type2size[arg_type] == 2) {
twisti@1568 550 // add the extra slot, so we can model the JVM stack
twisti@1573 551 _outgoing.insert_before(argp+1, make_state(T_VOID, ArgToken(tt_void)));
twisti@1568 552 }
twisti@1568 553 --argp;
twisti@1568 554 }
twisti@1568 555 // call make_parameter at the end of the list for the return type
twisti@1568 556 klassOop ret_type_klass = NULL;
twisti@1568 557 BasicType ret_type = java_lang_Class::as_BasicType(
jrose@2639 558 java_lang_invoke_MethodType::rtype(mtype()), &ret_type_klass);
twisti@1568 559 ArgToken ret = make_parameter(ret_type, ret_type_klass, -1, CHECK);
twisti@1568 560 // ignore ret; client can catch it if needed
twisti@1568 561 }
twisti@1568 562
twisti@1573 563
twisti@1573 564 // -----------------------------------------------------------------------------
twisti@1573 565 // MethodHandleWalker::change_argument
twisti@1573 566 //
twisti@1573 567 // This is messy because some kinds of arguments are paired with
twisti@1573 568 // companion slots containing an empty value.
twisti@1568 569 void MethodHandleWalker::change_argument(BasicType old_type, int slot, BasicType new_type,
twisti@1573 570 const ArgToken& new_arg) {
twisti@1568 571 int old_size = type2size[old_type];
twisti@1568 572 int new_size = type2size[new_type];
twisti@1568 573 if (old_size == new_size) {
twisti@1568 574 // simple case first
twisti@1568 575 _outgoing.at_put(slot, make_state(new_type, new_arg));
twisti@1568 576 } else if (old_size > new_size) {
twisti@1573 577 for (int i = old_size - 1; i >= new_size; i--) {
twisti@1568 578 assert((i != 0) == (_outgoing.at(slot + i)._type == T_VOID), "");
twisti@1568 579 _outgoing.remove_at(slot + i);
twisti@1568 580 }
twisti@1568 581 if (new_size > 0)
twisti@1568 582 _outgoing.at_put(slot, make_state(new_type, new_arg));
twisti@1568 583 else
twisti@1568 584 _outgoing_argc -= 1; // deleted a real argument
twisti@1568 585 } else {
twisti@1568 586 for (int i = old_size; i < new_size; i++) {
twisti@1573 587 _outgoing.insert_before(slot + i, make_state(T_VOID, ArgToken(tt_void)));
twisti@1568 588 }
twisti@1568 589 _outgoing.at_put(slot, make_state(new_type, new_arg));
twisti@1568 590 if (old_size == 0)
twisti@1568 591 _outgoing_argc += 1; // inserted a real argument
twisti@1568 592 }
twisti@1568 593 }
twisti@1568 594
twisti@1568 595
twisti@1568 596 #ifdef ASSERT
twisti@1568 597 int MethodHandleWalker::argument_count_slow() {
twisti@1568 598 int args_seen = 0;
twisti@1568 599 for (int i = _outgoing.length() - 1; i >= 0; i--) {
twisti@1568 600 if (_outgoing.at(i)._type != T_VOID) {
twisti@1568 601 ++args_seen;
twisti@1568 602 }
twisti@1568 603 }
twisti@1568 604 return args_seen;
twisti@1568 605 }
twisti@1568 606 #endif
twisti@1568 607
twisti@1568 608
twisti@1573 609 // -----------------------------------------------------------------------------
twisti@1573 610 // MethodHandleCompiler
twisti@1573 611
twisti@1573 612 MethodHandleCompiler::MethodHandleCompiler(Handle root, methodHandle callee, bool is_invokedynamic, TRAPS)
twisti@1573 613 : MethodHandleWalker(root, is_invokedynamic, THREAD),
twisti@1573 614 _callee(callee),
twisti@1573 615 _thread(THREAD),
twisti@1573 616 _bytecode(THREAD, 50),
twisti@1573 617 _constants(THREAD, 10),
twisti@1573 618 _cur_stack(0),
twisti@1573 619 _max_stack(0),
twisti@1573 620 _rtype(T_ILLEGAL)
twisti@1573 621 {
twisti@1573 622
twisti@1573 623 // Element zero is always the null constant.
twisti@1573 624 (void) _constants.append(NULL);
twisti@1573 625
twisti@1573 626 // Set name and signature index.
twisti@1573 627 _name_index = cpool_symbol_put(_callee->name());
twisti@1573 628 _signature_index = cpool_symbol_put(_callee->signature());
twisti@1573 629
twisti@1573 630 // Get return type klass.
twisti@1573 631 Handle first_mtype(THREAD, chain().method_type_oop());
twisti@1573 632 // _rklass is NULL for primitives.
jrose@2639 633 _rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(first_mtype()), &_rklass);
twisti@1587 634 if (_rtype == T_ARRAY) _rtype = T_OBJECT;
twisti@1573 635
twisti@1573 636 int params = _callee->size_of_parameters(); // Incoming arguments plus receiver.
twisti@1573 637 _num_params = for_invokedynamic() ? params - 1 : params; // XXX Check if callee is static?
twisti@1573 638 }
twisti@1573 639
twisti@1573 640
twisti@1573 641 // -----------------------------------------------------------------------------
twisti@1573 642 // MethodHandleCompiler::compile
twisti@1573 643 //
twisti@1573 644 // Compile this MethodHandle into a bytecode adapter and return a
twisti@1573 645 // methodOop.
twisti@1573 646 methodHandle MethodHandleCompiler::compile(TRAPS) {
twisti@1568 647 assert(_thread == THREAD, "must be same thread");
twisti@1573 648 methodHandle nullHandle;
twisti@1573 649 (void) walk(CHECK_(nullHandle));
twisti@1573 650 return get_method_oop(CHECK_(nullHandle));
twisti@1573 651 }
twisti@1568 652
twisti@1573 653
twisti@1573 654 void MethodHandleCompiler::emit_bc(Bytecodes::Code op, int index) {
twisti@1573 655 Bytecodes::check(op); // Are we legal?
twisti@1573 656
twisti@1573 657 switch (op) {
twisti@1573 658 // b
twisti@1573 659 case Bytecodes::_aconst_null:
twisti@1573 660 case Bytecodes::_iconst_m1:
twisti@1573 661 case Bytecodes::_iconst_0:
twisti@1573 662 case Bytecodes::_iconst_1:
twisti@1573 663 case Bytecodes::_iconst_2:
twisti@1573 664 case Bytecodes::_iconst_3:
twisti@1573 665 case Bytecodes::_iconst_4:
twisti@1573 666 case Bytecodes::_iconst_5:
twisti@1573 667 case Bytecodes::_lconst_0:
twisti@1573 668 case Bytecodes::_lconst_1:
twisti@1573 669 case Bytecodes::_fconst_0:
twisti@1573 670 case Bytecodes::_fconst_1:
twisti@1573 671 case Bytecodes::_fconst_2:
twisti@1573 672 case Bytecodes::_dconst_0:
twisti@1573 673 case Bytecodes::_dconst_1:
twisti@1573 674 case Bytecodes::_iload_0:
twisti@1573 675 case Bytecodes::_iload_1:
twisti@1573 676 case Bytecodes::_iload_2:
twisti@1573 677 case Bytecodes::_iload_3:
twisti@1573 678 case Bytecodes::_lload_0:
twisti@1573 679 case Bytecodes::_lload_1:
twisti@1573 680 case Bytecodes::_lload_2:
twisti@1573 681 case Bytecodes::_lload_3:
twisti@1573 682 case Bytecodes::_fload_0:
twisti@1573 683 case Bytecodes::_fload_1:
twisti@1573 684 case Bytecodes::_fload_2:
twisti@1573 685 case Bytecodes::_fload_3:
twisti@1573 686 case Bytecodes::_dload_0:
twisti@1573 687 case Bytecodes::_dload_1:
twisti@1573 688 case Bytecodes::_dload_2:
twisti@1573 689 case Bytecodes::_dload_3:
twisti@1573 690 case Bytecodes::_aload_0:
twisti@1573 691 case Bytecodes::_aload_1:
twisti@1573 692 case Bytecodes::_aload_2:
twisti@1573 693 case Bytecodes::_aload_3:
twisti@1573 694 case Bytecodes::_istore_0:
twisti@1573 695 case Bytecodes::_istore_1:
twisti@1573 696 case Bytecodes::_istore_2:
twisti@1573 697 case Bytecodes::_istore_3:
twisti@1573 698 case Bytecodes::_lstore_0:
twisti@1573 699 case Bytecodes::_lstore_1:
twisti@1573 700 case Bytecodes::_lstore_2:
twisti@1573 701 case Bytecodes::_lstore_3:
twisti@1573 702 case Bytecodes::_fstore_0:
twisti@1573 703 case Bytecodes::_fstore_1:
twisti@1573 704 case Bytecodes::_fstore_2:
twisti@1573 705 case Bytecodes::_fstore_3:
twisti@1573 706 case Bytecodes::_dstore_0:
twisti@1573 707 case Bytecodes::_dstore_1:
twisti@1573 708 case Bytecodes::_dstore_2:
twisti@1573 709 case Bytecodes::_dstore_3:
twisti@1573 710 case Bytecodes::_astore_0:
twisti@1573 711 case Bytecodes::_astore_1:
twisti@1573 712 case Bytecodes::_astore_2:
twisti@1573 713 case Bytecodes::_astore_3:
twisti@1573 714 case Bytecodes::_i2l:
twisti@1573 715 case Bytecodes::_i2f:
twisti@1573 716 case Bytecodes::_i2d:
twisti@1573 717 case Bytecodes::_i2b:
twisti@1573 718 case Bytecodes::_i2c:
twisti@1573 719 case Bytecodes::_i2s:
twisti@1573 720 case Bytecodes::_l2i:
twisti@1573 721 case Bytecodes::_l2f:
twisti@1573 722 case Bytecodes::_l2d:
twisti@1573 723 case Bytecodes::_f2i:
twisti@1573 724 case Bytecodes::_f2l:
twisti@1573 725 case Bytecodes::_f2d:
twisti@1573 726 case Bytecodes::_d2i:
twisti@1573 727 case Bytecodes::_d2l:
twisti@1573 728 case Bytecodes::_d2f:
twisti@1573 729 case Bytecodes::_ireturn:
twisti@1573 730 case Bytecodes::_lreturn:
twisti@1573 731 case Bytecodes::_freturn:
twisti@1573 732 case Bytecodes::_dreturn:
twisti@1573 733 case Bytecodes::_areturn:
twisti@1573 734 case Bytecodes::_return:
jrose@1920 735 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_b, "wrong bytecode format");
twisti@1573 736 _bytecode.push(op);
twisti@1573 737 break;
twisti@1573 738
twisti@1573 739 // bi
twisti@1573 740 case Bytecodes::_ldc:
jrose@2017 741 assert(Bytecodes::format_bits(op, false) == (Bytecodes::_fmt_b|Bytecodes::_fmt_has_k), "wrong bytecode format");
jrose@2017 742 assert((char) index == index, "index does not fit in 8-bit");
jrose@2017 743 _bytecode.push(op);
jrose@2017 744 _bytecode.push(index);
jrose@2017 745 break;
jrose@2017 746
twisti@1573 747 case Bytecodes::_iload:
twisti@1573 748 case Bytecodes::_lload:
twisti@1573 749 case Bytecodes::_fload:
twisti@1573 750 case Bytecodes::_dload:
twisti@1573 751 case Bytecodes::_aload:
twisti@1573 752 case Bytecodes::_istore:
twisti@1573 753 case Bytecodes::_lstore:
twisti@1573 754 case Bytecodes::_fstore:
twisti@1573 755 case Bytecodes::_dstore:
twisti@1573 756 case Bytecodes::_astore:
jrose@1920 757 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bi, "wrong bytecode format");
twisti@1573 758 assert((char) index == index, "index does not fit in 8-bit");
twisti@1573 759 _bytecode.push(op);
twisti@1573 760 _bytecode.push(index);
twisti@1573 761 break;
twisti@1573 762
jrose@2017 763 // bkk
jrose@2017 764 case Bytecodes::_ldc_w:
twisti@1573 765 case Bytecodes::_ldc2_w:
twisti@1573 766 case Bytecodes::_checkcast:
jrose@1920 767 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bkk, "wrong bytecode format");
twisti@1573 768 assert((short) index == index, "index does not fit in 16-bit");
twisti@1573 769 _bytecode.push(op);
twisti@1573 770 _bytecode.push(index >> 8);
twisti@1573 771 _bytecode.push(index);
twisti@1573 772 break;
twisti@1573 773
jrose@1920 774 // bJJ
twisti@1573 775 case Bytecodes::_invokestatic:
twisti@1573 776 case Bytecodes::_invokespecial:
twisti@1573 777 case Bytecodes::_invokevirtual:
jrose@1920 778 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
twisti@1573 779 assert((short) index == index, "index does not fit in 16-bit");
twisti@1573 780 _bytecode.push(op);
twisti@1573 781 _bytecode.push(index >> 8);
twisti@1573 782 _bytecode.push(index);
twisti@1573 783 break;
twisti@1573 784
twisti@1573 785 default:
twisti@1573 786 ShouldNotReachHere();
twisti@1568 787 }
twisti@1573 788 }
twisti@1568 789
twisti@1573 790
twisti@1573 791 void MethodHandleCompiler::emit_load(BasicType bt, int index) {
twisti@1573 792 if (index <= 3) {
twisti@1573 793 switch (bt) {
twisti@1573 794 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
twisti@1573 795 case T_INT: emit_bc(Bytecodes::cast(Bytecodes::_iload_0 + index)); break;
twisti@1573 796 case T_LONG: emit_bc(Bytecodes::cast(Bytecodes::_lload_0 + index)); break;
twisti@1573 797 case T_FLOAT: emit_bc(Bytecodes::cast(Bytecodes::_fload_0 + index)); break;
twisti@1573 798 case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dload_0 + index)); break;
twisti@1573 799 case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_aload_0 + index)); break;
twisti@1573 800 default:
twisti@1573 801 ShouldNotReachHere();
twisti@1573 802 }
twisti@1573 803 }
twisti@1573 804 else {
twisti@1573 805 switch (bt) {
twisti@1573 806 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
twisti@1573 807 case T_INT: emit_bc(Bytecodes::_iload, index); break;
twisti@1573 808 case T_LONG: emit_bc(Bytecodes::_lload, index); break;
twisti@1573 809 case T_FLOAT: emit_bc(Bytecodes::_fload, index); break;
twisti@1573 810 case T_DOUBLE: emit_bc(Bytecodes::_dload, index); break;
twisti@1573 811 case T_OBJECT: emit_bc(Bytecodes::_aload, index); break;
twisti@1573 812 default:
twisti@1573 813 ShouldNotReachHere();
twisti@1573 814 }
twisti@1573 815 }
twisti@1573 816 stack_push(bt);
twisti@1568 817 }
twisti@1568 818
twisti@1573 819 void MethodHandleCompiler::emit_store(BasicType bt, int index) {
twisti@1573 820 if (index <= 3) {
twisti@1573 821 switch (bt) {
twisti@1573 822 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
twisti@1573 823 case T_INT: emit_bc(Bytecodes::cast(Bytecodes::_istore_0 + index)); break;
twisti@1573 824 case T_LONG: emit_bc(Bytecodes::cast(Bytecodes::_lstore_0 + index)); break;
twisti@1573 825 case T_FLOAT: emit_bc(Bytecodes::cast(Bytecodes::_fstore_0 + index)); break;
twisti@1573 826 case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dstore_0 + index)); break;
twisti@1573 827 case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_astore_0 + index)); break;
twisti@1573 828 default:
twisti@1573 829 ShouldNotReachHere();
twisti@1573 830 }
twisti@1573 831 }
twisti@1573 832 else {
twisti@1573 833 switch (bt) {
twisti@1573 834 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
twisti@1573 835 case T_INT: emit_bc(Bytecodes::_istore, index); break;
twisti@1573 836 case T_LONG: emit_bc(Bytecodes::_lstore, index); break;
twisti@1573 837 case T_FLOAT: emit_bc(Bytecodes::_fstore, index); break;
twisti@1573 838 case T_DOUBLE: emit_bc(Bytecodes::_dstore, index); break;
twisti@1573 839 case T_OBJECT: emit_bc(Bytecodes::_astore, index); break;
twisti@1573 840 default:
twisti@1573 841 ShouldNotReachHere();
twisti@1573 842 }
twisti@1573 843 }
twisti@1573 844 stack_pop(bt);
twisti@1573 845 }
twisti@1573 846
twisti@1573 847
twisti@1573 848 void MethodHandleCompiler::emit_load_constant(ArgToken arg) {
twisti@1573 849 BasicType bt = arg.basic_type();
twisti@1573 850 switch (bt) {
twisti@1573 851 case T_INT: {
twisti@1573 852 jint value = arg.get_jint();
twisti@1573 853 if (-1 <= value && value <= 5)
twisti@1573 854 emit_bc(Bytecodes::cast(Bytecodes::_iconst_0 + value));
twisti@1573 855 else
twisti@1573 856 emit_bc(Bytecodes::_ldc, cpool_int_put(value));
twisti@1573 857 break;
twisti@1573 858 }
twisti@1573 859 case T_LONG: {
twisti@1573 860 jlong value = arg.get_jlong();
twisti@1573 861 if (0 <= value && value <= 1)
twisti@1573 862 emit_bc(Bytecodes::cast(Bytecodes::_lconst_0 + (int) value));
twisti@1573 863 else
twisti@1573 864 emit_bc(Bytecodes::_ldc2_w, cpool_long_put(value));
twisti@1573 865 break;
twisti@1573 866 }
twisti@1573 867 case T_FLOAT: {
twisti@1573 868 jfloat value = arg.get_jfloat();
twisti@1573 869 if (value == 0.0 || value == 1.0 || value == 2.0)
twisti@1573 870 emit_bc(Bytecodes::cast(Bytecodes::_fconst_0 + (int) value));
twisti@1573 871 else
twisti@1573 872 emit_bc(Bytecodes::_ldc, cpool_float_put(value));
twisti@1573 873 break;
twisti@1573 874 }
twisti@1573 875 case T_DOUBLE: {
twisti@1573 876 jdouble value = arg.get_jdouble();
twisti@1573 877 if (value == 0.0 || value == 1.0)
twisti@1573 878 emit_bc(Bytecodes::cast(Bytecodes::_dconst_0 + (int) value));
twisti@1573 879 else
twisti@1573 880 emit_bc(Bytecodes::_ldc2_w, cpool_double_put(value));
twisti@1573 881 break;
twisti@1573 882 }
twisti@1573 883 case T_OBJECT: {
twisti@1573 884 Handle value = arg.object();
twisti@1573 885 if (value.is_null())
twisti@1573 886 emit_bc(Bytecodes::_aconst_null);
twisti@1573 887 else
twisti@1573 888 emit_bc(Bytecodes::_ldc, cpool_object_put(value));
twisti@1573 889 break;
twisti@1573 890 }
twisti@1573 891 default:
twisti@1573 892 ShouldNotReachHere();
twisti@1573 893 }
twisti@1573 894 stack_push(bt);
twisti@1573 895 }
twisti@1573 896
twisti@1573 897
twisti@1568 898 MethodHandleWalker::ArgToken
twisti@1568 899 MethodHandleCompiler::make_conversion(BasicType type, klassOop tk, Bytecodes::Code op,
twisti@1573 900 const ArgToken& src, TRAPS) {
twisti@1573 901
twisti@1573 902 BasicType srctype = src.basic_type();
twisti@1573 903 int index = src.index();
twisti@1573 904
twisti@1573 905 switch (op) {
twisti@1573 906 case Bytecodes::_i2l:
twisti@1573 907 case Bytecodes::_i2f:
twisti@1573 908 case Bytecodes::_i2d:
twisti@1573 909 case Bytecodes::_i2b:
twisti@1573 910 case Bytecodes::_i2c:
twisti@1573 911 case Bytecodes::_i2s:
twisti@1573 912
twisti@1573 913 case Bytecodes::_l2i:
twisti@1573 914 case Bytecodes::_l2f:
twisti@1573 915 case Bytecodes::_l2d:
twisti@1573 916
twisti@1573 917 case Bytecodes::_f2i:
twisti@1573 918 case Bytecodes::_f2l:
twisti@1573 919 case Bytecodes::_f2d:
twisti@1573 920
twisti@1573 921 case Bytecodes::_d2i:
twisti@1573 922 case Bytecodes::_d2l:
twisti@1573 923 case Bytecodes::_d2f:
twisti@1573 924 emit_load(srctype, index);
twisti@1573 925 stack_pop(srctype); // pop the src type
twisti@1573 926 emit_bc(op);
twisti@1573 927 stack_push(type); // push the dest value
twisti@1573 928 if (srctype != type)
twisti@1573 929 index = new_local_index(type);
twisti@1573 930 emit_store(type, index);
twisti@1573 931 break;
twisti@1573 932
twisti@1573 933 case Bytecodes::_checkcast:
twisti@1573 934 emit_load(srctype, index);
twisti@1573 935 emit_bc(op, cpool_klass_put(tk));
twisti@1573 936 emit_store(srctype, index);
twisti@1573 937 break;
twisti@1573 938
twisti@1573 939 default:
twisti@1573 940 ShouldNotReachHere();
twisti@1573 941 }
twisti@1573 942
twisti@1573 943 return make_parameter(type, tk, index, THREAD);
twisti@1568 944 }
twisti@1568 945
twisti@1573 946
twisti@1573 947 // -----------------------------------------------------------------------------
twisti@1573 948 // MethodHandleCompiler
twisti@1573 949 //
twisti@1573 950
twisti@1573 951 static jvalue zero_jvalue;
twisti@1573 952
twisti@1573 953 // Emit bytecodes for the given invoke instruction.
twisti@1568 954 MethodHandleWalker::ArgToken
twisti@1568 955 MethodHandleCompiler::make_invoke(methodOop m, vmIntrinsics::ID iid,
twisti@1568 956 Bytecodes::Code op, bool tailcall,
twisti@1568 957 int argc, MethodHandleWalker::ArgToken* argv,
twisti@1568 958 TRAPS) {
twisti@1573 959 if (m == NULL) {
twisti@1573 960 // Get the intrinsic methodOop.
twisti@1573 961 m = vmIntrinsics::method_for(iid);
jrose@2638 962 if (m == NULL && iid == vmIntrinsics::_checkSpreadArgument && AllowTransitionalJSR292) {
jrose@2638 963 m = vmIntrinsics::method_for(vmIntrinsics::_checkSpreadArgument_TRANS);
jrose@2639 964 if (m == NULL)
jrose@2639 965 // sun.dyn.MethodHandleImpl not found, look for java.dyn.MethodHandleNatives:
jrose@2639 966 m = vmIntrinsics::method_for(vmIntrinsics::_checkSpreadArgument_TRANS2);
jrose@2638 967 }
jrose@2638 968 if (m == NULL) {
jrose@2638 969 ArgToken zero;
jrose@2638 970 lose(vmIntrinsics::name_at(iid), CHECK_(zero));
jrose@2638 971 }
twisti@1573 972 }
twisti@1573 973
coleenp@2497 974 klassOop klass = m->method_holder();
coleenp@2497 975 Symbol* name = m->name();
coleenp@2497 976 Symbol* signature = m->signature();
twisti@1573 977
twisti@1573 978 if (tailcall) {
twisti@1587 979 // Actually, in order to make these methods more recognizable,
twisti@2343 980 // let's put them in holder class MethodHandle. That way stack
twisti@2343 981 // walkers and compiler heuristics can recognize them.
twisti@2343 982 _target_klass = SystemDictionary::MethodHandle_klass();
twisti@1573 983 }
twisti@1573 984
twisti@1573 985 // Inline the method.
twisti@1573 986 InvocationCounter* ic = m->invocation_counter();
iveresov@2138 987 ic->set_carry_flag();
twisti@1573 988
twisti@1573 989 for (int i = 0; i < argc; i++) {
twisti@1573 990 ArgToken arg = argv[i];
twisti@1573 991 TokenType tt = arg.token_type();
twisti@1573 992 BasicType bt = arg.basic_type();
twisti@1573 993
twisti@1573 994 switch (tt) {
twisti@1573 995 case tt_parameter:
twisti@1573 996 case tt_temporary:
twisti@1573 997 emit_load(bt, arg.index());
twisti@1573 998 break;
twisti@1573 999 case tt_constant:
twisti@1573 1000 emit_load_constant(arg);
twisti@1573 1001 break;
twisti@1573 1002 case tt_illegal:
twisti@1573 1003 // Sentinel.
twisti@1573 1004 assert(i == (argc - 1), "sentinel must be last entry");
twisti@1573 1005 break;
twisti@1573 1006 case tt_void:
twisti@1573 1007 default:
twisti@1573 1008 ShouldNotReachHere();
twisti@1573 1009 }
twisti@1573 1010 }
twisti@1573 1011
twisti@1573 1012 // Populate constant pool.
twisti@1573 1013 int name_index = cpool_symbol_put(name);
twisti@1573 1014 int signature_index = cpool_symbol_put(signature);
twisti@1573 1015 int name_and_type_index = cpool_name_and_type_put(name_index, signature_index);
twisti@1573 1016 int klass_index = cpool_klass_put(klass);
twisti@1573 1017 int methodref_index = cpool_methodref_put(klass_index, name_and_type_index);
twisti@1573 1018
twisti@1573 1019 // Generate invoke.
twisti@1568 1020 switch (op) {
twisti@1573 1021 case Bytecodes::_invokestatic:
twisti@1573 1022 case Bytecodes::_invokespecial:
twisti@1568 1023 case Bytecodes::_invokevirtual:
twisti@1573 1024 emit_bc(op, methodref_index);
twisti@1573 1025 break;
twisti@1568 1026 case Bytecodes::_invokeinterface:
twisti@1573 1027 Unimplemented();
twisti@1568 1028 break;
twisti@1568 1029 default:
twisti@1568 1030 ShouldNotReachHere();
twisti@1568 1031 }
twisti@1568 1032
twisti@1573 1033 // If tailcall, we have walked all the way to a direct method handle.
twisti@1573 1034 // Otherwise, make a recursive call to some helper routine.
twisti@1573 1035 BasicType rbt = m->result_type();
twisti@1587 1036 if (rbt == T_ARRAY) rbt = T_OBJECT;
twisti@1573 1037 ArgToken ret;
twisti@1573 1038 if (tailcall) {
twisti@1573 1039 if (rbt != _rtype) {
twisti@1573 1040 if (rbt == T_VOID) {
twisti@1573 1041 // push a zero of the right sort
twisti@1573 1042 ArgToken zero;
twisti@1573 1043 if (_rtype == T_OBJECT) {
twisti@1573 1044 zero = make_oop_constant(NULL, CHECK_(zero));
twisti@1573 1045 } else {
twisti@1573 1046 zero = make_prim_constant(_rtype, &zero_jvalue, CHECK_(zero));
twisti@1573 1047 }
twisti@1573 1048 emit_load_constant(zero);
twisti@1573 1049 } else if (_rtype == T_VOID) {
twisti@1573 1050 // We'll emit a _return with something on the stack.
twisti@1573 1051 // It's OK to ignore what's on the stack.
twisti@1573 1052 } else {
twisti@1573 1053 tty->print_cr("*** rbt=%d != rtype=%d", rbt, _rtype);
twisti@1573 1054 assert(false, "IMPLEMENT ME");
twisti@1573 1055 }
twisti@1573 1056 }
twisti@1573 1057 switch (_rtype) {
twisti@1573 1058 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
twisti@1573 1059 case T_INT: emit_bc(Bytecodes::_ireturn); break;
twisti@1573 1060 case T_LONG: emit_bc(Bytecodes::_lreturn); break;
twisti@1573 1061 case T_FLOAT: emit_bc(Bytecodes::_freturn); break;
twisti@1573 1062 case T_DOUBLE: emit_bc(Bytecodes::_dreturn); break;
twisti@1573 1063 case T_VOID: emit_bc(Bytecodes::_return); break;
twisti@1573 1064 case T_OBJECT:
never@1577 1065 if (_rklass.not_null() && _rklass() != SystemDictionary::Object_klass())
twisti@1573 1066 emit_bc(Bytecodes::_checkcast, cpool_klass_put(_rklass()));
twisti@1573 1067 emit_bc(Bytecodes::_areturn);
twisti@1573 1068 break;
twisti@1573 1069 default: ShouldNotReachHere();
twisti@1573 1070 }
twisti@1573 1071 ret = ArgToken(); // Dummy return value.
twisti@1573 1072 }
twisti@1573 1073 else {
twisti@1573 1074 stack_push(rbt); // The return value is already pushed onto the stack.
twisti@1573 1075 int index = new_local_index(rbt);
twisti@1573 1076 switch (rbt) {
twisti@1573 1077 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
twisti@1573 1078 case T_INT: case T_LONG: case T_FLOAT: case T_DOUBLE:
twisti@1573 1079 case T_OBJECT:
twisti@1573 1080 emit_store(rbt, index);
twisti@1573 1081 ret = ArgToken(tt_temporary, rbt, index);
twisti@1573 1082 break;
twisti@1573 1083 case T_VOID:
twisti@1573 1084 ret = ArgToken(tt_void);
twisti@1573 1085 break;
twisti@1573 1086 default:
twisti@1573 1087 ShouldNotReachHere();
twisti@1573 1088 }
twisti@1573 1089 }
twisti@1573 1090
twisti@1573 1091 return ret;
twisti@1568 1092 }
twisti@1568 1093
twisti@1568 1094 MethodHandleWalker::ArgToken
twisti@1568 1095 MethodHandleCompiler::make_fetch(BasicType type, klassOop tk, Bytecodes::Code op,
twisti@1573 1096 const MethodHandleWalker::ArgToken& base,
twisti@1573 1097 const MethodHandleWalker::ArgToken& offset,
twisti@1568 1098 TRAPS) {
twisti@1568 1099 Unimplemented();
twisti@1573 1100 return ArgToken();
twisti@1568 1101 }
twisti@1568 1102
twisti@1568 1103
twisti@1573 1104 int MethodHandleCompiler::cpool_primitive_put(BasicType bt, jvalue* con) {
twisti@1568 1105 jvalue con_copy;
twisti@1568 1106 assert(bt < T_OBJECT, "");
twisti@1568 1107 if (type2aelembytes(bt) < jintSize) {
twisti@1568 1108 // widen to int
twisti@1568 1109 con_copy = (*con);
twisti@1568 1110 con = &con_copy;
twisti@1568 1111 switch (bt) {
twisti@1568 1112 case T_BOOLEAN: con->i = (con->z ? 1 : 0); break;
twisti@1568 1113 case T_BYTE: con->i = con->b; break;
twisti@1568 1114 case T_CHAR: con->i = con->c; break;
twisti@1568 1115 case T_SHORT: con->i = con->s; break;
twisti@1568 1116 default: ShouldNotReachHere();
twisti@1568 1117 }
twisti@1568 1118 bt = T_INT;
twisti@1568 1119 }
twisti@1573 1120
twisti@1573 1121 // for (int i = 1, imax = _constants.length(); i < imax; i++) {
twisti@1573 1122 // ConstantValue* con = _constants.at(i);
twisti@1573 1123 // if (con != NULL && con->is_primitive() && con->_type == bt) {
twisti@1573 1124 // bool match = false;
twisti@1573 1125 // switch (type2size[bt]) {
twisti@1573 1126 // case 1: if (pcon->_value.i == con->i) match = true; break;
twisti@1573 1127 // case 2: if (pcon->_value.j == con->j) match = true; break;
twisti@1573 1128 // }
twisti@1573 1129 // if (match)
twisti@1573 1130 // return i;
twisti@1573 1131 // }
twisti@1573 1132 // }
twisti@1573 1133 ConstantValue* cv = new ConstantValue(bt, *con);
twisti@1573 1134 int index = _constants.append(cv);
twisti@1573 1135
twisti@1573 1136 // long and double entries take 2 slots, we add another empty entry.
twisti@1573 1137 if (type2size[bt] == 2)
twisti@1573 1138 (void) _constants.append(NULL);
twisti@1573 1139
twisti@1573 1140 return index;
twisti@1573 1141 }
twisti@1573 1142
twisti@1573 1143
twisti@1573 1144 constantPoolHandle MethodHandleCompiler::get_constant_pool(TRAPS) const {
twisti@1573 1145 constantPoolHandle nullHandle;
ysr@2533 1146 constantPoolOop cpool_oop = oopFactory::new_constantPool(_constants.length(),
ysr@2533 1147 oopDesc::IsSafeConc,
ysr@2533 1148 CHECK_(nullHandle));
twisti@1573 1149 constantPoolHandle cpool(THREAD, cpool_oop);
twisti@1573 1150
twisti@1573 1151 // Fill the real constant pool skipping the zero element.
twisti@1573 1152 for (int i = 1; i < _constants.length(); i++) {
twisti@1573 1153 ConstantValue* cv = _constants.at(i);
twisti@1573 1154 switch (cv->tag()) {
coleenp@2497 1155 case JVM_CONSTANT_Utf8: cpool->symbol_at_put( i, cv->symbol() ); break;
twisti@1573 1156 case JVM_CONSTANT_Integer: cpool->int_at_put( i, cv->get_jint() ); break;
twisti@1573 1157 case JVM_CONSTANT_Float: cpool->float_at_put( i, cv->get_jfloat() ); break;
twisti@1573 1158 case JVM_CONSTANT_Long: cpool->long_at_put( i, cv->get_jlong() ); break;
twisti@1573 1159 case JVM_CONSTANT_Double: cpool->double_at_put( i, cv->get_jdouble() ); break;
twisti@1573 1160 case JVM_CONSTANT_Class: cpool->klass_at_put( i, cv->klass_oop() ); break;
twisti@1573 1161 case JVM_CONSTANT_Methodref: cpool->method_at_put( i, cv->first_index(), cv->second_index()); break;
twisti@1573 1162 case JVM_CONSTANT_NameAndType: cpool->name_and_type_at_put(i, cv->first_index(), cv->second_index()); break;
twisti@1573 1163 case JVM_CONSTANT_Object: cpool->object_at_put( i, cv->object_oop() ); break;
twisti@1573 1164 default: ShouldNotReachHere();
twisti@1573 1165 }
twisti@1573 1166
twisti@1573 1167 switch (cv->tag()) {
twisti@1573 1168 case JVM_CONSTANT_Long:
twisti@1573 1169 case JVM_CONSTANT_Double:
twisti@1573 1170 i++; // Skip empty entry.
twisti@1573 1171 assert(_constants.at(i) == NULL, "empty entry");
twisti@1573 1172 break;
twisti@1568 1173 }
twisti@1568 1174 }
twisti@1573 1175
twisti@1573 1176 // Set the constant pool holder to the target method's class.
twisti@1573 1177 cpool->set_pool_holder(_target_klass());
twisti@1573 1178
twisti@1573 1179 return cpool;
twisti@1573 1180 }
twisti@1573 1181
twisti@1573 1182
twisti@1573 1183 methodHandle MethodHandleCompiler::get_method_oop(TRAPS) const {
twisti@1573 1184 methodHandle nullHandle;
twisti@1573 1185 // Create a method that holds the generated bytecode. invokedynamic
twisti@1573 1186 // has no receiver, normal MH calls do.
twisti@1573 1187 int flags_bits;
twisti@1573 1188 if (for_invokedynamic())
jrose@1862 1189 flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC | JVM_ACC_STATIC);
twisti@1573 1190 else
jrose@1862 1191 flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC);
twisti@1573 1192
twisti@1573 1193 methodOop m_oop = oopFactory::new_method(bytecode_length(),
twisti@1573 1194 accessFlags_from(flags_bits),
ysr@2533 1195 0, 0, 0, oopDesc::IsSafeConc, CHECK_(nullHandle));
twisti@1573 1196 methodHandle m(THREAD, m_oop);
twisti@1573 1197 m_oop = NULL; // oop not GC safe
twisti@1573 1198
twisti@1573 1199 constantPoolHandle cpool = get_constant_pool(CHECK_(nullHandle));
twisti@1573 1200 m->set_constants(cpool());
twisti@1573 1201
twisti@1573 1202 m->set_name_index(_name_index);
twisti@1573 1203 m->set_signature_index(_signature_index);
twisti@1573 1204
twisti@1573 1205 m->set_code((address) bytecode());
twisti@1573 1206
twisti@1573 1207 m->set_max_stack(_max_stack);
twisti@1573 1208 m->set_max_locals(max_locals());
twisti@1573 1209 m->set_size_of_parameters(_num_params);
twisti@1573 1210
twisti@1573 1211 typeArrayHandle exception_handlers(THREAD, Universe::the_empty_int_array());
twisti@1573 1212 m->set_exception_table(exception_handlers());
twisti@1573 1213
twisti@1573 1214 // Set the carry bit of the invocation counter to force inlining of
twisti@1573 1215 // the adapter.
twisti@1573 1216 InvocationCounter* ic = m->invocation_counter();
iveresov@2138 1217 ic->set_carry_flag();
twisti@1573 1218
twisti@1573 1219 // Rewrite the method and set up the constant pool cache.
twisti@1573 1220 objArrayOop m_array = oopFactory::new_system_objArray(1, CHECK_(nullHandle));
twisti@1573 1221 objArrayHandle methods(THREAD, m_array);
twisti@1573 1222 methods->obj_at_put(0, m());
twisti@1573 1223 Rewriter::rewrite(_target_klass(), cpool, methods, CHECK_(nullHandle)); // Use fake class.
twisti@1573 1224
twisti@1573 1225 #ifndef PRODUCT
twisti@1573 1226 if (TraceMethodHandles) {
twisti@1573 1227 m->print();
twisti@1573 1228 m->print_codes();
twisti@1573 1229 }
twisti@1573 1230 #endif //PRODUCT
twisti@1573 1231
jrose@1862 1232 assert(m->is_method_handle_adapter(), "must be recognized as an adapter");
twisti@1573 1233 return m;
twisti@1568 1234 }
twisti@1568 1235
twisti@1568 1236
twisti@1568 1237 #ifndef PRODUCT
twisti@1568 1238
twisti@1573 1239 #if 0
twisti@1568 1240 // MH printer for debugging.
twisti@1568 1241
twisti@1568 1242 class MethodHandlePrinter : public MethodHandleWalker {
twisti@1568 1243 private:
twisti@1568 1244 outputStream* _out;
twisti@1568 1245 bool _verbose;
twisti@1568 1246 int _temp_num;
twisti@1568 1247 stringStream _strbuf;
twisti@1568 1248 const char* strbuf() {
twisti@1568 1249 const char* s = _strbuf.as_string();
twisti@1568 1250 _strbuf.reset();
twisti@1568 1251 return s;
twisti@1568 1252 }
twisti@1568 1253 ArgToken token(const char* str) {
twisti@1568 1254 return (ArgToken) str;
twisti@1568 1255 }
twisti@1568 1256 void start_params() {
twisti@1568 1257 _out->print("(");
twisti@1568 1258 }
twisti@1568 1259 void end_params() {
twisti@1568 1260 if (_verbose) _out->print("\n");
twisti@1568 1261 _out->print(") => {");
twisti@1568 1262 }
twisti@1568 1263 void put_type_name(BasicType type, klassOop tk, outputStream* s) {
twisti@1568 1264 const char* kname = NULL;
twisti@1568 1265 if (tk != NULL)
twisti@1568 1266 kname = Klass::cast(tk)->external_name();
twisti@1568 1267 s->print("%s", (kname != NULL) ? kname : type2name(type));
twisti@1568 1268 }
twisti@1568 1269 ArgToken maybe_make_temp(const char* statement_op, BasicType type, const char* temp_name) {
twisti@1568 1270 const char* value = strbuf();
twisti@1568 1271 if (!_verbose) return token(value);
twisti@1568 1272 // make an explicit binding for each separate value
twisti@1568 1273 _strbuf.print("%s%d", temp_name, ++_temp_num);
twisti@1568 1274 const char* temp = strbuf();
twisti@1568 1275 _out->print("\n %s %s %s = %s;", statement_op, type2name(type), temp, value);
twisti@1568 1276 return token(temp);
twisti@1568 1277 }
twisti@1568 1278
twisti@1568 1279 public:
twisti@1568 1280 MethodHandlePrinter(Handle root, bool verbose, outputStream* out, TRAPS)
twisti@1568 1281 : MethodHandleWalker(root, THREAD),
twisti@1568 1282 _out(out),
twisti@1568 1283 _verbose(verbose),
twisti@1568 1284 _temp_num(0)
twisti@1568 1285 {
twisti@1568 1286 start_params();
twisti@1568 1287 }
twisti@1568 1288 virtual ArgToken make_parameter(BasicType type, klassOop tk, int argnum, TRAPS) {
twisti@1568 1289 if (argnum < 0) {
twisti@1568 1290 end_params();
twisti@1568 1291 return NULL;
twisti@1568 1292 }
twisti@1568 1293 if (argnum == 0) {
twisti@1568 1294 _out->print(_verbose ? "\n " : "");
twisti@1568 1295 } else {
twisti@1568 1296 _out->print(_verbose ? ",\n " : ", ");
twisti@1568 1297 }
twisti@1568 1298 if (argnum >= _temp_num)
twisti@1568 1299 _temp_num = argnum;
twisti@1568 1300 // generate an argument name
twisti@1568 1301 _strbuf.print("a%d", argnum);
twisti@1568 1302 const char* arg = strbuf();
twisti@1568 1303 put_type_name(type, tk, _out);
twisti@1568 1304 _out->print(" %s", arg);
twisti@1568 1305 return token(arg);
twisti@1568 1306 }
twisti@1568 1307 virtual ArgToken make_oop_constant(oop con, TRAPS) {
twisti@1568 1308 if (con == NULL)
twisti@1568 1309 _strbuf.print("null");
twisti@1568 1310 else
twisti@1568 1311 con->print_value_on(&_strbuf);
twisti@1568 1312 if (_strbuf.size() == 0) { // yuck
twisti@1568 1313 _strbuf.print("(a ");
twisti@1568 1314 put_type_name(T_OBJECT, con->klass(), &_strbuf);
twisti@1568 1315 _strbuf.print(")");
twisti@1568 1316 }
twisti@1568 1317 return maybe_make_temp("constant", T_OBJECT, "k");
twisti@1568 1318 }
twisti@1568 1319 virtual ArgToken make_prim_constant(BasicType type, jvalue* con, TRAPS) {
twisti@1568 1320 java_lang_boxing_object::print(type, con, &_strbuf);
twisti@1568 1321 return maybe_make_temp("constant", type, "k");
twisti@1568 1322 }
twisti@1568 1323 virtual ArgToken make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, ArgToken src, TRAPS) {
twisti@1568 1324 _strbuf.print("%s(%s", Bytecodes::name(op), (const char*)src);
twisti@1568 1325 if (tk != NULL) {
twisti@1568 1326 _strbuf.print(", ");
twisti@1568 1327 put_type_name(type, tk, &_strbuf);
twisti@1568 1328 }
twisti@1568 1329 _strbuf.print(")");
twisti@1568 1330 return maybe_make_temp("convert", type, "v");
twisti@1568 1331 }
twisti@1568 1332 virtual ArgToken make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, ArgToken base, ArgToken offset, TRAPS) {
twisti@1568 1333 _strbuf.print("%s(%s, %s", Bytecodes::name(op), (const char*)base, (const char*)offset);
twisti@1568 1334 if (tk != NULL) {
twisti@1568 1335 _strbuf.print(", ");
twisti@1568 1336 put_type_name(type, tk, &_strbuf);
twisti@1568 1337 }
twisti@1568 1338 _strbuf.print(")");
twisti@1568 1339 return maybe_make_temp("fetch", type, "x");
twisti@1568 1340 }
twisti@1568 1341 virtual ArgToken make_invoke(methodOop m, vmIntrinsics::ID iid,
twisti@1568 1342 Bytecodes::Code op, bool tailcall,
twisti@1568 1343 int argc, ArgToken* argv, TRAPS) {
coleenp@2497 1344 Symbol* name, sig;
twisti@1568 1345 if (m != NULL) {
twisti@1568 1346 name = m->name();
twisti@1568 1347 sig = m->signature();
twisti@1568 1348 } else {
twisti@1568 1349 name = vmSymbols::symbol_at(vmIntrinsics::name_for(iid));
twisti@1568 1350 sig = vmSymbols::symbol_at(vmIntrinsics::signature_for(iid));
twisti@1568 1351 }
twisti@1568 1352 _strbuf.print("%s %s%s(", Bytecodes::name(op), name->as_C_string(), sig->as_C_string());
twisti@1568 1353 for (int i = 0; i < argc; i++) {
twisti@1568 1354 _strbuf.print("%s%s", (i > 0 ? ", " : ""), (const char*)argv[i]);
twisti@1568 1355 }
twisti@1568 1356 _strbuf.print(")");
twisti@1568 1357 if (!tailcall) {
twisti@1568 1358 BasicType rt = char2type(sig->byte_at(sig->utf8_length()-1));
twisti@1568 1359 if (rt == T_ILLEGAL) rt = T_OBJECT; // ';' at the end of '(...)L...;'
twisti@1568 1360 return maybe_make_temp("invoke", rt, "x");
twisti@1568 1361 } else {
twisti@1568 1362 const char* ret = strbuf();
twisti@1568 1363 _out->print(_verbose ? "\n return " : " ");
twisti@1568 1364 _out->print("%s", ret);
twisti@1568 1365 _out->print(_verbose ? "\n}\n" : " }");
twisti@1568 1366 }
twisti@1568 1367 return ArgToken();
twisti@1568 1368 }
twisti@1568 1369
twisti@1568 1370 virtual void set_method_handle(oop mh) {
twisti@1568 1371 if (WizardMode && Verbose) {
twisti@1568 1372 tty->print("\n--- next target: ");
twisti@1568 1373 mh->print();
twisti@1568 1374 }
twisti@1568 1375 }
twisti@1568 1376
twisti@1568 1377 static void print(Handle root, bool verbose, outputStream* out, TRAPS) {
twisti@1568 1378 ResourceMark rm;
twisti@1568 1379 MethodHandlePrinter printer(root, verbose, out, CHECK);
twisti@1568 1380 printer.walk(CHECK);
twisti@1568 1381 out->print("\n");
twisti@1568 1382 }
twisti@1568 1383 static void print(Handle root, bool verbose = Verbose, outputStream* out = tty) {
twisti@1568 1384 EXCEPTION_MARK;
twisti@1568 1385 ResourceMark rm;
twisti@1568 1386 MethodHandlePrinter printer(root, verbose, out, THREAD);
twisti@1568 1387 if (!HAS_PENDING_EXCEPTION)
twisti@1568 1388 printer.walk(THREAD);
twisti@1568 1389 if (HAS_PENDING_EXCEPTION) {
twisti@1568 1390 oop ex = PENDING_EXCEPTION;
twisti@1568 1391 CLEAR_PENDING_EXCEPTION;
twisti@1568 1392 out->print("\n*** ");
twisti@1568 1393 if (ex != Universe::virtual_machine_error_instance())
twisti@1568 1394 ex->print_on(out);
twisti@1568 1395 else
twisti@1568 1396 out->print("lose: %s", printer.lose_message());
twisti@1568 1397 out->print("\n}\n");
twisti@1568 1398 }
twisti@1568 1399 out->print("\n");
twisti@1568 1400 }
twisti@1568 1401 };
twisti@1573 1402 #endif // 0
twisti@1568 1403
twisti@1568 1404 extern "C"
twisti@1568 1405 void print_method_handle(oop mh) {
jrose@2148 1406 if (!mh->is_oop()) {
jrose@2148 1407 tty->print_cr("*** not a method handle: "INTPTR_FORMAT, (intptr_t)mh);
jrose@2639 1408 } else if (java_lang_invoke_MethodHandle::is_instance(mh)) {
twisti@1573 1409 //MethodHandlePrinter::print(mh);
twisti@1568 1410 } else {
twisti@1568 1411 tty->print("*** not a method handle: ");
twisti@1568 1412 mh->print();
twisti@1568 1413 }
twisti@1568 1414 }
twisti@1568 1415
twisti@1568 1416 #endif // PRODUCT

mercurial