src/share/vm/prims/methodHandleWalk.cpp

Wed, 15 Jun 2011 10:20:03 -0700

author
never
date
Wed, 15 Jun 2011 10:20:03 -0700
changeset 2956
cfcf2ba8f3eb
parent 2954
f8c9417e3571
parent 2947
96c891ebe56a
child 2982
ddd894528dbc
permissions
-rw-r--r--

Merge

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@2903 34 #ifdef PRODUCT
twisti@2903 35 #define print_method_handle(mh) {}
twisti@2903 36 #else //PRODUCT
twisti@2903 37 extern "C" void print_method_handle(oop mh);
twisti@2903 38 #endif //PRODUCT
twisti@1573 39
twisti@1573 40 // -----------------------------------------------------------------------------
twisti@1573 41 // MethodHandleChain
twisti@1573 42
twisti@1568 43 void MethodHandleChain::set_method_handle(Handle mh, TRAPS) {
jrose@2639 44 if (!java_lang_invoke_MethodHandle::is_instance(mh())) lose("bad method handle", CHECK);
twisti@1568 45
twisti@1568 46 // set current method handle and unpack partially
twisti@1568 47 _method_handle = mh;
twisti@1568 48 _is_last = false;
twisti@1568 49 _is_bound = false;
twisti@1568 50 _arg_slot = -1;
twisti@1568 51 _arg_type = T_VOID;
twisti@1568 52 _conversion = -1;
twisti@1568 53 _last_invoke = Bytecodes::_nop; //arbitrary non-garbage
twisti@1568 54
jrose@2639 55 if (java_lang_invoke_DirectMethodHandle::is_instance(mh())) {
twisti@1568 56 set_last_method(mh(), THREAD);
twisti@1568 57 return;
twisti@1568 58 }
jrose@2639 59 if (java_lang_invoke_AdapterMethodHandle::is_instance(mh())) {
twisti@1568 60 _conversion = AdapterMethodHandle_conversion();
twisti@1568 61 assert(_conversion != -1, "bad conv value");
jrose@2639 62 assert(java_lang_invoke_BoundMethodHandle::is_instance(mh()), "also BMH");
twisti@1568 63 }
jrose@2639 64 if (java_lang_invoke_BoundMethodHandle::is_instance(mh())) {
twisti@1568 65 if (!is_adapter()) // keep AMH and BMH separate in this model
twisti@1568 66 _is_bound = true;
twisti@1568 67 _arg_slot = BoundMethodHandle_vmargslot();
twisti@1568 68 oop target = MethodHandle_vmtarget_oop();
jrose@2639 69 if (!is_bound() || java_lang_invoke_MethodHandle::is_instance(target)) {
twisti@1568 70 _arg_type = compute_bound_arg_type(target, NULL, _arg_slot, CHECK);
twisti@1568 71 } else if (target != NULL && target->is_method()) {
twisti@1573 72 methodOop m = (methodOop) target;
twisti@1573 73 _arg_type = compute_bound_arg_type(NULL, m, _arg_slot, CHECK);
twisti@1568 74 set_last_method(mh(), CHECK);
twisti@1568 75 } else {
twisti@1568 76 _is_bound = false; // lose!
twisti@1568 77 }
twisti@1568 78 }
twisti@1568 79 if (is_bound() && _arg_type == T_VOID) {
twisti@1568 80 lose("bad vmargslot", CHECK);
twisti@1568 81 }
twisti@1568 82 if (!is_bound() && !is_adapter()) {
twisti@1568 83 lose("unrecognized MH type", CHECK);
twisti@1568 84 }
twisti@1568 85 }
twisti@1568 86
twisti@1573 87
twisti@1568 88 void MethodHandleChain::set_last_method(oop target, TRAPS) {
twisti@1568 89 _is_last = true;
twisti@2806 90 KlassHandle receiver_limit; int flags = 0;
twisti@2806 91 _last_method = MethodHandles::decode_method(target, receiver_limit, flags);
twisti@1568 92 if ((flags & MethodHandles::_dmf_has_receiver) == 0)
twisti@1568 93 _last_invoke = Bytecodes::_invokestatic;
twisti@1568 94 else if ((flags & MethodHandles::_dmf_does_dispatch) == 0)
twisti@1568 95 _last_invoke = Bytecodes::_invokespecial;
twisti@1568 96 else if ((flags & MethodHandles::_dmf_from_interface) != 0)
twisti@1568 97 _last_invoke = Bytecodes::_invokeinterface;
twisti@1568 98 else
twisti@1568 99 _last_invoke = Bytecodes::_invokevirtual;
twisti@1568 100 }
twisti@1568 101
twisti@1573 102
twisti@1568 103 BasicType MethodHandleChain::compute_bound_arg_type(oop target, methodOop m, int arg_slot, TRAPS) {
twisti@1568 104 // There is no direct indication of whether the argument is primitive or not.
twisti@1568 105 // It is implied by the _vmentry code, and by the MethodType of the target.
twisti@1568 106 BasicType arg_type = T_VOID;
twisti@1568 107 if (target != NULL) {
jrose@2639 108 oop mtype = java_lang_invoke_MethodHandle::type(target);
twisti@1568 109 int arg_num = MethodHandles::argument_slot_to_argnum(mtype, arg_slot);
twisti@1568 110 if (arg_num >= 0) {
jrose@2639 111 oop ptype = java_lang_invoke_MethodType::ptype(mtype, arg_num);
twisti@1568 112 arg_type = java_lang_Class::as_BasicType(ptype);
twisti@1568 113 }
twisti@1568 114 } else if (m != NULL) {
twisti@1568 115 // figure out the argument type from the slot
twisti@1568 116 // FIXME: make this explicit in the MH
twisti@1568 117 int cur_slot = m->size_of_parameters();
twisti@1568 118 if (arg_slot >= cur_slot)
twisti@1568 119 return T_VOID;
twisti@1568 120 if (!m->is_static()) {
twisti@1568 121 cur_slot -= type2size[T_OBJECT];
twisti@1568 122 if (cur_slot == arg_slot)
twisti@1568 123 return T_OBJECT;
twisti@1568 124 }
coleenp@2497 125 ResourceMark rm(THREAD);
twisti@1568 126 for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) {
twisti@1568 127 BasicType bt = ss.type();
twisti@1568 128 cur_slot -= type2size[bt];
twisti@1568 129 if (cur_slot <= arg_slot) {
twisti@1568 130 if (cur_slot == arg_slot)
twisti@1568 131 arg_type = bt;
twisti@1568 132 break;
twisti@1568 133 }
twisti@1568 134 }
twisti@1568 135 }
twisti@1568 136 if (arg_type == T_ARRAY)
twisti@1568 137 arg_type = T_OBJECT;
twisti@1568 138 return arg_type;
twisti@1568 139 }
twisti@1568 140
twisti@1573 141
twisti@1568 142 void MethodHandleChain::lose(const char* msg, TRAPS) {
twisti@1568 143 _lose_message = msg;
never@2937 144 #ifdef ASSERT
never@2937 145 if (Verbose) {
never@2937 146 tty->print_cr(INTPTR_FORMAT " lose: %s", _method_handle(), msg);
never@2937 147 print();
never@2937 148 }
never@2937 149 #endif
twisti@1568 150 if (!THREAD->is_Java_thread() || ((JavaThread*)THREAD)->thread_state() != _thread_in_vm) {
twisti@1568 151 // throw a preallocated exception
twisti@1568 152 THROW_OOP(Universe::virtual_machine_error_instance());
twisti@1568 153 }
twisti@1568 154 THROW_MSG(vmSymbols::java_lang_InternalError(), msg);
twisti@1568 155 }
twisti@1568 156
twisti@1573 157
never@2937 158 #ifdef ASSERT
never@2937 159 static const char* adapter_ops[] = {
never@2937 160 "retype_only" ,
never@2937 161 "retype_raw" ,
never@2937 162 "check_cast" ,
never@2937 163 "prim_to_prim" ,
never@2937 164 "ref_to_prim" ,
never@2937 165 "prim_to_ref" ,
never@2937 166 "swap_args" ,
never@2937 167 "rot_args" ,
never@2937 168 "dup_args" ,
never@2937 169 "drop_args" ,
never@2937 170 "collect_args" ,
never@2937 171 "spread_args" ,
never@2937 172 "fold_args"
never@2937 173 };
never@2937 174
never@2937 175 static const char* adapter_op_to_string(int op) {
never@2937 176 if (op >= 0 && op < (int)ARRAY_SIZE(adapter_ops))
never@2937 177 return adapter_ops[op];
never@2937 178 return "unknown_op";
never@2937 179 }
never@2937 180
never@2950 181 void MethodHandleChain::print(oopDesc* m) {
never@2950 182 HandleMark hm;
never@2950 183 ResourceMark rm;
never@2950 184 Handle mh(m);
never@2950 185 print(mh);
never@2950 186 }
never@2937 187
never@2937 188 void MethodHandleChain::print(Handle mh) {
never@2937 189 EXCEPTION_MARK;
never@2937 190 MethodHandleChain mhc(mh, THREAD);
never@2937 191 if (HAS_PENDING_EXCEPTION) {
never@2937 192 oop ex = THREAD->pending_exception();
never@2937 193 CLEAR_PENDING_EXCEPTION;
never@2937 194 ex->print();
never@2937 195 return;
never@2937 196 }
never@2937 197 mhc.print();
never@2937 198 }
never@2937 199
never@2937 200
never@2937 201 void MethodHandleChain::print() {
never@2937 202 EXCEPTION_MARK;
never@2937 203 print_impl(THREAD);
never@2937 204 if (HAS_PENDING_EXCEPTION) {
never@2937 205 oop ex = THREAD->pending_exception();
never@2937 206 CLEAR_PENDING_EXCEPTION;
never@2937 207 ex->print();
never@2937 208 }
never@2937 209 }
never@2937 210
never@2937 211 void MethodHandleChain::print_impl(TRAPS) {
never@2937 212 ResourceMark rm;
never@2937 213
never@2937 214 MethodHandleChain chain(_root, CHECK);
never@2937 215 for (;;) {
never@2937 216 tty->print(INTPTR_FORMAT ": ", chain.method_handle()());
never@2937 217 if (chain.is_bound()) {
never@2937 218 tty->print("bound: arg_type %s arg_slot %d",
never@2937 219 type2name(chain.bound_arg_type()),
never@2937 220 chain.bound_arg_slot());
never@2937 221 oop o = chain.bound_arg_oop();
never@2937 222 if (o != NULL) {
never@2937 223 if (o->is_instance()) {
never@2937 224 tty->print(" instance %s", o->klass()->klass_part()->internal_name());
never@2937 225 } else {
never@2937 226 o->print();
never@2937 227 }
never@2937 228 }
never@2937 229 } else if (chain.is_adapter()) {
never@2937 230 tty->print("adapter: arg_slot %d conversion op %s",
never@2937 231 chain.adapter_arg_slot(),
never@2937 232 adapter_op_to_string(chain.adapter_conversion_op()));
never@2937 233 switch (chain.adapter_conversion_op()) {
never@2937 234 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY:
never@2937 235 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW:
never@2937 236 case java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST:
never@2937 237 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM:
never@2937 238 case java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM:
never@2937 239 break;
never@2937 240
never@2954 241 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF: {
never@2954 242 tty->print(" src_type = %s", type2name(chain.adapter_conversion_src_type()));
never@2954 243 break;
never@2954 244 }
never@2954 245
never@2937 246 case java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS:
never@2937 247 case java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS: {
never@2937 248 int dest_arg_slot = chain.adapter_conversion_vminfo();
never@2937 249 tty->print(" dest_arg_slot %d type %s", dest_arg_slot, type2name(chain.adapter_conversion_src_type()));
never@2937 250 break;
never@2937 251 }
never@2937 252
never@2937 253 case java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS:
never@2937 254 case java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS: {
never@2937 255 int dup_slots = chain.adapter_conversion_stack_pushes();
never@2937 256 tty->print(" pushes %d", dup_slots);
never@2937 257 break;
never@2937 258 }
never@2937 259
never@2937 260 case java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS:
never@2937 261 case java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS: {
never@2937 262 int coll_slots = chain.MethodHandle_vmslots();
never@2937 263 tty->print(" coll_slots %d", coll_slots);
never@2937 264 break;
never@2937 265 }
never@2937 266
never@2937 267 case java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS: {
never@2937 268 // Check the required length.
never@2937 269 int spread_slots = 1 + chain.adapter_conversion_stack_pushes();
never@2937 270 tty->print(" spread_slots %d", spread_slots);
never@2937 271 break;
never@2937 272 }
never@2937 273
never@2937 274 default:
never@2937 275 tty->print_cr("bad adapter conversion");
never@2937 276 break;
never@2937 277 }
never@2937 278 } else {
never@2937 279 // DMH
never@2937 280 tty->print("direct: ");
never@2937 281 chain.last_method_oop()->print_short_name(tty);
never@2937 282 }
never@2937 283
never@2937 284 tty->print(" (");
never@2937 285 objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(chain.method_type_oop());
never@2937 286 for (int i = ptypes->length() - 1; i >= 0; i--) {
never@2937 287 BasicType t = java_lang_Class::as_BasicType(ptypes->obj_at(i));
never@2937 288 if (t == T_ARRAY) t = T_OBJECT;
never@2937 289 tty->print("%c", type2char(t));
never@2937 290 if (t == T_LONG || t == T_DOUBLE) tty->print("_");
never@2937 291 }
never@2937 292 tty->print(")");
never@2937 293 BasicType rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(chain.method_type_oop()));
never@2937 294 if (rtype == T_ARRAY) rtype = T_OBJECT;
never@2937 295 tty->print("%c", type2char(rtype));
never@2937 296 tty->cr();
never@2937 297 if (!chain.is_last()) {
never@2937 298 chain.next(CHECK);
never@2937 299 } else {
never@2937 300 break;
never@2937 301 }
never@2937 302 }
never@2937 303 }
never@2937 304 #endif
never@2937 305
never@2937 306
twisti@1573 307 // -----------------------------------------------------------------------------
twisti@1573 308 // MethodHandleWalker
twisti@1573 309
twisti@1568 310 Bytecodes::Code MethodHandleWalker::conversion_code(BasicType src, BasicType dest) {
twisti@1568 311 if (is_subword_type(src)) {
twisti@1568 312 src = T_INT; // all subword src types act like int
twisti@1568 313 }
twisti@1568 314 if (src == dest) {
twisti@1568 315 return Bytecodes::_nop;
twisti@1568 316 }
twisti@1568 317
twisti@1568 318 #define SRC_DEST(s,d) (((int)(s) << 4) + (int)(d))
twisti@1568 319 switch (SRC_DEST(src, dest)) {
twisti@1568 320 case SRC_DEST(T_INT, T_LONG): return Bytecodes::_i2l;
twisti@1568 321 case SRC_DEST(T_INT, T_FLOAT): return Bytecodes::_i2f;
twisti@1568 322 case SRC_DEST(T_INT, T_DOUBLE): return Bytecodes::_i2d;
twisti@1568 323 case SRC_DEST(T_INT, T_BYTE): return Bytecodes::_i2b;
twisti@1568 324 case SRC_DEST(T_INT, T_CHAR): return Bytecodes::_i2c;
twisti@1568 325 case SRC_DEST(T_INT, T_SHORT): return Bytecodes::_i2s;
twisti@1568 326
twisti@1568 327 case SRC_DEST(T_LONG, T_INT): return Bytecodes::_l2i;
twisti@1568 328 case SRC_DEST(T_LONG, T_FLOAT): return Bytecodes::_l2f;
twisti@1568 329 case SRC_DEST(T_LONG, T_DOUBLE): return Bytecodes::_l2d;
twisti@1568 330
twisti@1568 331 case SRC_DEST(T_FLOAT, T_INT): return Bytecodes::_f2i;
twisti@1568 332 case SRC_DEST(T_FLOAT, T_LONG): return Bytecodes::_f2l;
twisti@1568 333 case SRC_DEST(T_FLOAT, T_DOUBLE): return Bytecodes::_f2d;
twisti@1568 334
twisti@1568 335 case SRC_DEST(T_DOUBLE, T_INT): return Bytecodes::_d2i;
twisti@1568 336 case SRC_DEST(T_DOUBLE, T_LONG): return Bytecodes::_d2l;
twisti@1568 337 case SRC_DEST(T_DOUBLE, T_FLOAT): return Bytecodes::_d2f;
twisti@1568 338 }
twisti@1568 339 #undef SRC_DEST
twisti@1568 340
twisti@1568 341 // cannot do it in one step, or at all
twisti@1568 342 return Bytecodes::_illegal;
twisti@1568 343 }
twisti@1568 344
twisti@1573 345
twisti@1573 346 // -----------------------------------------------------------------------------
twisti@1573 347 // MethodHandleWalker::walk
twisti@1573 348 //
twisti@1568 349 MethodHandleWalker::ArgToken
twisti@1568 350 MethodHandleWalker::walk(TRAPS) {
twisti@1573 351 ArgToken empty = ArgToken(); // Empty return value.
twisti@1573 352
twisti@1573 353 walk_incoming_state(CHECK_(empty));
twisti@1568 354
twisti@1568 355 for (;;) {
twisti@1568 356 set_method_handle(chain().method_handle_oop());
twisti@1568 357
twisti@1568 358 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
twisti@1568 359
twisti@1568 360 if (chain().is_adapter()) {
twisti@1568 361 int conv_op = chain().adapter_conversion_op();
twisti@1568 362 int arg_slot = chain().adapter_arg_slot();
never@2937 363
never@2937 364 // Check that the arg_slot is valid. In most cases it must be
never@2937 365 // within range of the current arguments but there are some
never@2937 366 // exceptions. Those are sanity checked in their implemention
never@2937 367 // below.
never@2937 368 if ((arg_slot < 0 || arg_slot >= _outgoing.length()) &&
never@2937 369 conv_op > java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW &&
never@2937 370 conv_op != java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS &&
never@2937 371 conv_op != java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS) {
never@2937 372 lose(err_msg("bad argument index %d", arg_slot), CHECK_(empty));
twisti@1568 373 }
twisti@1568 374
twisti@2903 375 bool retain_original_args = false; // used by fold/collect logic
twisti@2903 376
twisti@1568 377 // perform the adapter action
twisti@2903 378 switch (conv_op) {
jrose@2639 379 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY:
twisti@1568 380 // No changes to arguments; pass the bits through.
twisti@1568 381 break;
twisti@1568 382
jrose@2639 383 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW: {
twisti@1573 384 // To keep the verifier happy, emit bitwise ("raw") conversions as needed.
twisti@1573 385 // See MethodHandles::same_basic_type_for_arguments for allowed conversions.
twisti@1573 386 Handle incoming_mtype(THREAD, chain().method_type_oop());
twisti@2903 387 Handle outgoing_mtype;
twisti@2903 388 {
twisti@2903 389 oop outgoing_mh_oop = chain().vmtarget_oop();
twisti@2903 390 if (!java_lang_invoke_MethodHandle::is_instance(outgoing_mh_oop))
twisti@2903 391 lose("outgoing target not a MethodHandle", CHECK_(empty));
twisti@2903 392 outgoing_mtype = Handle(THREAD, java_lang_invoke_MethodHandle::type(outgoing_mh_oop));
twisti@2903 393 }
twisti@1573 394
jrose@2639 395 int nptypes = java_lang_invoke_MethodType::ptype_count(outgoing_mtype());
jrose@2639 396 if (nptypes != java_lang_invoke_MethodType::ptype_count(incoming_mtype()))
twisti@1573 397 lose("incoming and outgoing parameter count do not agree", CHECK_(empty));
twisti@1573 398
twisti@2903 399 // Argument types.
twisti@1573 400 for (int i = 0, slot = _outgoing.length() - 1; slot >= 0; slot--) {
never@2937 401 if (arg_type(slot) == T_VOID) continue;
twisti@1573 402
twisti@2903 403 klassOop src_klass = NULL;
twisti@2903 404 klassOop dst_klass = NULL;
twisti@2903 405 BasicType src = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(incoming_mtype(), i), &src_klass);
twisti@2903 406 BasicType dst = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(outgoing_mtype(), i), &dst_klass);
twisti@2903 407 retype_raw_argument_type(src, dst, slot, CHECK_(empty));
twisti@1573 408 i++; // We need to skip void slots at the top of the loop.
twisti@1573 409 }
twisti@1573 410
twisti@2903 411 // Return type.
twisti@2903 412 {
twisti@2903 413 BasicType src = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(incoming_mtype()));
twisti@2903 414 BasicType dst = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(outgoing_mtype()));
twisti@2903 415 retype_raw_return_type(src, dst, CHECK_(empty));
twisti@1573 416 }
twisti@1573 417 break;
twisti@1573 418 }
twisti@1573 419
jrose@2639 420 case java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST: {
twisti@1568 421 // checkcast the Nth outgoing argument in place
twisti@1568 422 klassOop dest_klass = NULL;
twisti@1568 423 BasicType dest = java_lang_Class::as_BasicType(chain().adapter_arg_oop(), &dest_klass);
twisti@1568 424 assert(dest == T_OBJECT, "");
never@2937 425 ArgToken arg = _outgoing.at(arg_slot);
never@2937 426 assert(dest == arg.basic_type(), "");
never@2950 427 arg = make_conversion(T_OBJECT, dest_klass, Bytecodes::_checkcast, arg, CHECK_(empty));
twisti@1568 428 debug_only(dest_klass = (klassOop)badOop);
twisti@1568 429 break;
twisti@1568 430 }
twisti@1568 431
jrose@2639 432 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM: {
twisti@1568 433 // i2l, etc., on the Nth outgoing argument in place
twisti@1568 434 BasicType src = chain().adapter_conversion_src_type(),
twisti@1568 435 dest = chain().adapter_conversion_dest_type();
never@2937 436 ArgToken arg = _outgoing.at(arg_slot);
twisti@1568 437 Bytecodes::Code bc = conversion_code(src, dest);
twisti@1568 438 if (bc == Bytecodes::_nop) {
twisti@1568 439 break;
twisti@1568 440 } else if (bc != Bytecodes::_illegal) {
twisti@1573 441 arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
twisti@1568 442 } else if (is_subword_type(dest)) {
twisti@1568 443 bc = conversion_code(src, T_INT);
twisti@1568 444 if (bc != Bytecodes::_illegal) {
twisti@1573 445 arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
twisti@1568 446 bc = conversion_code(T_INT, dest);
twisti@1573 447 arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
twisti@1568 448 }
twisti@1568 449 }
twisti@1568 450 if (bc == Bytecodes::_illegal) {
never@2937 451 lose(err_msg("bad primitive conversion for %s -> %s", type2name(src), type2name(dest)), CHECK_(empty));
twisti@1568 452 }
twisti@1568 453 change_argument(src, arg_slot, dest, arg);
twisti@1568 454 break;
twisti@1568 455 }
twisti@1568 456
jrose@2639 457 case java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM: {
twisti@1568 458 // checkcast to wrapper type & call intValue, etc.
twisti@1568 459 BasicType dest = chain().adapter_conversion_dest_type();
never@2937 460 ArgToken arg = _outgoing.at(arg_slot);
twisti@1568 461 arg = make_conversion(T_OBJECT, SystemDictionary::box_klass(dest),
twisti@1573 462 Bytecodes::_checkcast, arg, CHECK_(empty));
twisti@1568 463 vmIntrinsics::ID unboxer = vmIntrinsics::for_unboxing(dest);
twisti@1568 464 if (unboxer == vmIntrinsics::_none) {
twisti@1573 465 lose("no unboxing method", CHECK_(empty));
twisti@1568 466 }
twisti@1568 467 ArgToken arglist[2];
twisti@1573 468 arglist[0] = arg; // outgoing 'this'
twisti@1573 469 arglist[1] = ArgToken(); // sentinel
twisti@1573 470 arg = make_invoke(NULL, unboxer, Bytecodes::_invokevirtual, false, 1, &arglist[0], CHECK_(empty));
twisti@1568 471 change_argument(T_OBJECT, arg_slot, dest, arg);
twisti@1568 472 break;
twisti@1568 473 }
twisti@1568 474
jrose@2639 475 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF: {
twisti@1568 476 // call wrapper type.valueOf
twisti@1568 477 BasicType src = chain().adapter_conversion_src_type();
twisti@1568 478 vmIntrinsics::ID boxer = vmIntrinsics::for_boxing(src);
twisti@1568 479 if (boxer == vmIntrinsics::_none) {
twisti@1573 480 lose("no boxing method", CHECK_(empty));
twisti@1568 481 }
never@2937 482 ArgToken arg = _outgoing.at(arg_slot);
twisti@1568 483 ArgToken arglist[2];
twisti@1573 484 arglist[0] = arg; // outgoing value
twisti@1573 485 arglist[1] = ArgToken(); // sentinel
twisti@2903 486 arg = make_invoke(NULL, boxer, Bytecodes::_invokestatic, false, 1, &arglist[0], CHECK_(empty));
twisti@1568 487 change_argument(src, arg_slot, T_OBJECT, arg);
twisti@1568 488 break;
twisti@1568 489 }
twisti@1568 490
jrose@2639 491 case java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS: {
twisti@1568 492 int dest_arg_slot = chain().adapter_conversion_vminfo();
never@2937 493 if (!has_argument(dest_arg_slot)) {
twisti@1573 494 lose("bad swap index", CHECK_(empty));
twisti@1568 495 }
twisti@1568 496 // a simple swap between two arguments
never@2937 497 if (arg_slot > dest_arg_slot) {
never@2937 498 int tmp = arg_slot;
never@2937 499 arg_slot = dest_arg_slot;
never@2937 500 dest_arg_slot = tmp;
never@2937 501 }
never@2937 502 ArgToken a1 = _outgoing.at(arg_slot);
never@2937 503 ArgToken a2 = _outgoing.at(dest_arg_slot);
never@2937 504 change_argument(a2.basic_type(), dest_arg_slot, a1);
never@2937 505 change_argument(a1.basic_type(), arg_slot, a2);
twisti@1568 506 break;
twisti@1568 507 }
twisti@1568 508
jrose@2639 509 case java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS: {
never@2954 510 int limit_raw = chain().adapter_conversion_vminfo();
never@2954 511 bool rot_down = (arg_slot < limit_raw);
never@2954 512 int limit_bias = (rot_down ? MethodHandles::OP_ROT_ARGS_DOWN_LIMIT_BIAS : 0);
never@2954 513 int limit_slot = limit_raw - limit_bias;
never@2954 514 if ((uint)limit_slot > (uint)_outgoing.length()) {
twisti@1573 515 lose("bad rotate index", CHECK_(empty));
twisti@1568 516 }
twisti@1568 517 // Rotate the source argument (plus following N slots) into the
twisti@1568 518 // position occupied by the dest argument (plus following N slots).
never@2937 519 int rotate_count = type2size[chain().adapter_conversion_src_type()];
twisti@1568 520 // (no other rotate counts are currently supported)
never@2954 521 if (rot_down) {
twisti@1568 522 for (int i = 0; i < rotate_count; i++) {
never@2937 523 ArgToken temp = _outgoing.at(arg_slot);
twisti@1568 524 _outgoing.remove_at(arg_slot);
never@2954 525 _outgoing.insert_before(limit_slot - 1, temp);
twisti@1568 526 }
never@2954 527 } else { // arg_slot > limit_slot => rotate_up
twisti@1568 528 for (int i = 0; i < rotate_count; i++) {
never@2937 529 ArgToken temp = _outgoing.at(arg_slot + rotate_count - 1);
twisti@1568 530 _outgoing.remove_at(arg_slot + rotate_count - 1);
never@2954 531 _outgoing.insert_before(limit_slot, temp);
twisti@1568 532 }
twisti@1568 533 }
never@2937 534 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
twisti@1568 535 break;
twisti@1568 536 }
twisti@1568 537
jrose@2639 538 case java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS: {
twisti@1568 539 int dup_slots = chain().adapter_conversion_stack_pushes();
twisti@1568 540 if (dup_slots <= 0) {
twisti@1573 541 lose("bad dup count", CHECK_(empty));
twisti@1568 542 }
twisti@1568 543 for (int i = 0; i < dup_slots; i++) {
never@2937 544 ArgToken dup = _outgoing.at(arg_slot + 2*i);
never@2937 545 if (dup.basic_type() != T_VOID) _outgoing_argc += 1;
never@2937 546 _outgoing.insert_before(i, dup);
twisti@1568 547 }
never@2937 548 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
twisti@1568 549 break;
twisti@1568 550 }
twisti@1568 551
jrose@2639 552 case java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS: {
twisti@1568 553 int drop_slots = -chain().adapter_conversion_stack_pushes();
twisti@1568 554 if (drop_slots <= 0) {
twisti@1573 555 lose("bad drop count", CHECK_(empty));
twisti@1568 556 }
twisti@1568 557 for (int i = 0; i < drop_slots; i++) {
never@2937 558 ArgToken drop = _outgoing.at(arg_slot);
never@2937 559 if (drop.basic_type() != T_VOID) _outgoing_argc -= 1;
twisti@1568 560 _outgoing.remove_at(arg_slot);
twisti@1568 561 }
never@2937 562 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
twisti@1568 563 break;
twisti@1568 564 }
twisti@1568 565
twisti@2903 566 case java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS:
twisti@2903 567 retain_original_args = true; // and fall through:
twisti@2903 568 case java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS: {
twisti@2903 569 // call argument MH recursively
twisti@2903 570 //{static int x; if (!x++) print_method_handle(chain().method_handle_oop()); --x;}
twisti@2903 571 Handle recursive_mh(THREAD, chain().adapter_arg_oop());
twisti@2903 572 if (!java_lang_invoke_MethodHandle::is_instance(recursive_mh())) {
twisti@2903 573 lose("recursive target not a MethodHandle", CHECK_(empty));
twisti@2903 574 }
twisti@2903 575 Handle recursive_mtype(THREAD, java_lang_invoke_MethodHandle::type(recursive_mh()));
twisti@2903 576 int argc = java_lang_invoke_MethodType::ptype_count(recursive_mtype());
twisti@2903 577 int coll_slots = java_lang_invoke_MethodHandle::vmslots(recursive_mh());
twisti@2903 578 BasicType rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(recursive_mtype()));
twisti@2903 579 ArgToken* arglist = NEW_RESOURCE_ARRAY(ArgToken, 1 + argc + 1); // 1+: mh, +1: sentinel
twisti@2903 580 arglist[0] = make_oop_constant(recursive_mh(), CHECK_(empty));
twisti@2903 581 if (arg_slot < 0 || coll_slots < 0 || arg_slot + coll_slots > _outgoing.length()) {
twisti@2903 582 lose("bad fold/collect arg slot", CHECK_(empty));
twisti@2903 583 }
twisti@2903 584 for (int i = 0, slot = arg_slot + coll_slots - 1; slot >= arg_slot; slot--) {
never@2937 585 ArgToken arg_state = _outgoing.at(slot);
never@2937 586 BasicType arg_type = arg_state.basic_type();
twisti@2903 587 if (arg_type == T_VOID) continue;
never@2937 588 ArgToken arg = _outgoing.at(slot);
twisti@2903 589 if (i >= argc) { lose("bad fold/collect arg", CHECK_(empty)); }
twisti@2903 590 arglist[1+i] = arg;
twisti@2903 591 if (!retain_original_args)
twisti@2903 592 change_argument(arg_type, slot, T_VOID, ArgToken(tt_void));
bdelsart@2917 593 i++;
twisti@2903 594 }
twisti@2903 595 arglist[1+argc] = ArgToken(); // sentinel
twisti@2903 596 oop invoker = java_lang_invoke_MethodTypeForm::vmlayout(
twisti@2903 597 java_lang_invoke_MethodType::form(recursive_mtype()) );
twisti@2903 598 if (invoker == NULL || !invoker->is_method()) {
twisti@2903 599 lose("bad vmlayout slot", CHECK_(empty));
twisti@2903 600 }
twisti@2903 601 // FIXME: consider inlining the invokee at the bytecode level
twisti@2903 602 ArgToken ret = make_invoke(methodOop(invoker), vmIntrinsics::_none,
twisti@2903 603 Bytecodes::_invokevirtual, false, 1+argc, &arglist[0], CHECK_(empty));
twisti@2903 604 DEBUG_ONLY(invoker = NULL);
twisti@2903 605 if (rtype == T_OBJECT) {
twisti@2903 606 klassOop rklass = java_lang_Class::as_klassOop( java_lang_invoke_MethodType::rtype(recursive_mtype()) );
twisti@2903 607 if (rklass != SystemDictionary::Object_klass() &&
twisti@2903 608 !Klass::cast(rklass)->is_interface()) {
twisti@2903 609 // preserve type safety
twisti@2903 610 ret = make_conversion(T_OBJECT, rklass, Bytecodes::_checkcast, ret, CHECK_(empty));
twisti@2903 611 }
twisti@2903 612 }
never@2920 613 if (rtype != T_VOID) {
never@2920 614 int ret_slot = arg_slot + (retain_original_args ? coll_slots : 0);
never@2920 615 change_argument(T_VOID, ret_slot, rtype, ret);
never@2920 616 }
never@2895 617 break;
never@2895 618 }
never@2895 619
jrose@2639 620 case java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS: {
twisti@1568 621 klassOop array_klass_oop = NULL;
twisti@1568 622 BasicType array_type = java_lang_Class::as_BasicType(chain().adapter_arg_oop(),
twisti@1568 623 &array_klass_oop);
twisti@1568 624 assert(array_type == T_OBJECT, "");
twisti@1568 625 assert(Klass::cast(array_klass_oop)->oop_is_array(), "");
twisti@1568 626 arrayKlassHandle array_klass(THREAD, array_klass_oop);
twisti@1568 627 debug_only(array_klass_oop = (klassOop)badOop);
twisti@1568 628
twisti@1568 629 klassOop element_klass_oop = NULL;
twisti@1568 630 BasicType element_type = java_lang_Class::as_BasicType(array_klass->component_mirror(),
twisti@1568 631 &element_klass_oop);
twisti@1568 632 KlassHandle element_klass(THREAD, element_klass_oop);
twisti@1568 633 debug_only(element_klass_oop = (klassOop)badOop);
twisti@1568 634
twisti@1568 635 // Fetch the argument, which we will cast to the required array type.
never@2937 636 ArgToken arg = _outgoing.at(arg_slot);
never@2937 637 assert(arg.basic_type() == T_OBJECT, "");
never@2937 638 ArgToken array_arg = arg;
twisti@1573 639 array_arg = make_conversion(T_OBJECT, array_klass(), Bytecodes::_checkcast, array_arg, CHECK_(empty));
twisti@1573 640 change_argument(T_OBJECT, arg_slot, T_VOID, ArgToken(tt_void));
twisti@1568 641
twisti@1568 642 // Check the required length.
twisti@1568 643 int spread_slots = 1 + chain().adapter_conversion_stack_pushes();
twisti@1568 644 int spread_length = spread_slots;
twisti@1568 645 if (type2size[element_type] == 2) {
twisti@1568 646 if (spread_slots % 2 != 0) spread_slots = -1; // force error
twisti@1568 647 spread_length = spread_slots / 2;
twisti@1568 648 }
twisti@1568 649 if (spread_slots < 0) {
twisti@1573 650 lose("bad spread length", CHECK_(empty));
twisti@1568 651 }
twisti@1568 652
twisti@1568 653 jvalue length_jvalue; length_jvalue.i = spread_length;
twisti@1573 654 ArgToken length_arg = make_prim_constant(T_INT, &length_jvalue, CHECK_(empty));
twisti@1568 655 // Call a built-in method known to the JVM to validate the length.
twisti@1568 656 ArgToken arglist[3];
twisti@1573 657 arglist[0] = array_arg; // value to check
twisti@1573 658 arglist[1] = length_arg; // length to check
twisti@1573 659 arglist[2] = ArgToken(); // sentinel
twisti@1568 660 make_invoke(NULL, vmIntrinsics::_checkSpreadArgument,
bdelsart@2917 661 Bytecodes::_invokestatic, false, 2, &arglist[0], CHECK_(empty));
twisti@1568 662
twisti@1568 663 // Spread out the array elements.
never@2895 664 Bytecodes::Code aload_op = Bytecodes::_nop;
never@2895 665 switch (element_type) {
never@2895 666 case T_INT: aload_op = Bytecodes::_iaload; break;
never@2895 667 case T_LONG: aload_op = Bytecodes::_laload; break;
never@2895 668 case T_FLOAT: aload_op = Bytecodes::_faload; break;
never@2895 669 case T_DOUBLE: aload_op = Bytecodes::_daload; break;
never@2895 670 case T_OBJECT: aload_op = Bytecodes::_aaload; break;
never@2895 671 case T_BOOLEAN: // fall through:
never@2895 672 case T_BYTE: aload_op = Bytecodes::_baload; break;
never@2895 673 case T_CHAR: aload_op = Bytecodes::_caload; break;
never@2895 674 case T_SHORT: aload_op = Bytecodes::_saload; break;
never@2895 675 default: lose("primitive array NYI", CHECK_(empty));
twisti@1568 676 }
twisti@1568 677 int ap = arg_slot;
twisti@1568 678 for (int i = 0; i < spread_length; i++) {
twisti@1568 679 jvalue offset_jvalue; offset_jvalue.i = i;
twisti@1573 680 ArgToken offset_arg = make_prim_constant(T_INT, &offset_jvalue, CHECK_(empty));
twisti@1573 681 ArgToken element_arg = make_fetch(element_type, element_klass(), aload_op, array_arg, offset_arg, CHECK_(empty));
twisti@1568 682 change_argument(T_VOID, ap, element_type, element_arg);
twisti@1568 683 ap += type2size[element_type];
twisti@1568 684 }
twisti@1568 685 break;
twisti@1568 686 }
twisti@1568 687
twisti@1568 688 default:
twisti@1573 689 lose("bad adapter conversion", CHECK_(empty));
twisti@1568 690 break;
twisti@1568 691 }
twisti@1568 692 }
twisti@1568 693
twisti@1568 694 if (chain().is_bound()) {
twisti@1568 695 // push a new argument
twisti@1568 696 BasicType arg_type = chain().bound_arg_type();
twisti@1568 697 jint arg_slot = chain().bound_arg_slot();
twisti@1568 698 oop arg_oop = chain().bound_arg_oop();
twisti@1573 699 ArgToken arg;
twisti@1568 700 if (arg_type == T_OBJECT) {
twisti@1573 701 arg = make_oop_constant(arg_oop, CHECK_(empty));
twisti@1568 702 } else {
twisti@1568 703 jvalue arg_value;
twisti@1568 704 BasicType bt = java_lang_boxing_object::get_value(arg_oop, &arg_value);
never@2937 705 if (bt == arg_type || (bt == T_INT && is_subword_type(arg_type))) {
twisti@1573 706 arg = make_prim_constant(arg_type, &arg_value, CHECK_(empty));
twisti@1568 707 } else {
never@2937 708 lose(err_msg("bad bound value: arg_type %s boxing %s", type2name(arg_type), type2name(bt)), CHECK_(empty));
twisti@1568 709 }
twisti@1568 710 }
twisti@2903 711 DEBUG_ONLY(arg_oop = badOop);
twisti@1568 712 change_argument(T_VOID, arg_slot, arg_type, arg);
twisti@1568 713 }
twisti@1568 714
twisti@1568 715 // this test must come after the body of the loop
twisti@1568 716 if (!chain().is_last()) {
twisti@1573 717 chain().next(CHECK_(empty));
twisti@1568 718 } else {
twisti@1568 719 break;
twisti@1568 720 }
twisti@1568 721 }
twisti@1568 722
twisti@1568 723 // finish the sequence with a tail-call to the ultimate target
twisti@1568 724 // parameters are passed in logical order (recv 1st), not slot order
twisti@1568 725 ArgToken* arglist = NEW_RESOURCE_ARRAY(ArgToken, _outgoing.length() + 1);
twisti@1568 726 int ap = 0;
twisti@1568 727 for (int i = _outgoing.length() - 1; i >= 0; i--) {
never@2937 728 ArgToken arg_state = _outgoing.at(i);
never@2937 729 if (arg_state.basic_type() == T_VOID) continue;
never@2937 730 arglist[ap++] = _outgoing.at(i);
twisti@1568 731 }
twisti@1568 732 assert(ap == _outgoing_argc, "");
twisti@1573 733 arglist[ap] = ArgToken(); // add a sentinel, for the sake of asserts
twisti@1568 734 return make_invoke(chain().last_method_oop(),
twisti@1568 735 vmIntrinsics::_none,
twisti@1568 736 chain().last_invoke_code(), true,
twisti@1568 737 ap, arglist, THREAD);
twisti@1568 738 }
twisti@1568 739
twisti@1573 740
twisti@1573 741 // -----------------------------------------------------------------------------
twisti@1573 742 // MethodHandleWalker::walk_incoming_state
twisti@1573 743 //
twisti@1568 744 void MethodHandleWalker::walk_incoming_state(TRAPS) {
twisti@1568 745 Handle mtype(THREAD, chain().method_type_oop());
jrose@2639 746 int nptypes = java_lang_invoke_MethodType::ptype_count(mtype());
twisti@1568 747 _outgoing_argc = nptypes;
twisti@1568 748 int argp = nptypes - 1;
twisti@1568 749 if (argp >= 0) {
never@2937 750 _outgoing.at_grow(argp, ArgToken(tt_void)); // presize
twisti@1568 751 }
twisti@1568 752 for (int i = 0; i < nptypes; i++) {
twisti@1568 753 klassOop arg_type_klass = NULL;
twisti@2903 754 BasicType arg_type = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(mtype(), i), &arg_type_klass);
twisti@1573 755 int index = new_local_index(arg_type);
twisti@1573 756 ArgToken arg = make_parameter(arg_type, arg_type_klass, index, CHECK);
twisti@2903 757 DEBUG_ONLY(arg_type_klass = (klassOop) NULL);
never@2937 758 _outgoing.at_put(argp, arg);
twisti@1568 759 if (type2size[arg_type] == 2) {
twisti@1568 760 // add the extra slot, so we can model the JVM stack
never@2937 761 _outgoing.insert_before(argp+1, ArgToken(tt_void));
twisti@1568 762 }
twisti@1568 763 --argp;
twisti@1568 764 }
twisti@1568 765 // call make_parameter at the end of the list for the return type
twisti@1568 766 klassOop ret_type_klass = NULL;
twisti@2903 767 BasicType ret_type = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(mtype()), &ret_type_klass);
twisti@1568 768 ArgToken ret = make_parameter(ret_type, ret_type_klass, -1, CHECK);
twisti@1568 769 // ignore ret; client can catch it if needed
never@2937 770
never@2937 771 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
never@2937 772
never@2937 773 verify_args_and_signature(CHECK);
twisti@1568 774 }
twisti@1568 775
twisti@1573 776
never@2937 777 #ifdef ASSERT
never@2937 778 void MethodHandleWalker::verify_args_and_signature(TRAPS) {
never@2937 779 int index = _outgoing.length() - 1;
never@2937 780 objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(chain().method_type_oop());
never@2937 781 for (int i = 0, limit = ptypes->length(); i < limit; i++) {
never@2937 782 BasicType t = java_lang_Class::as_BasicType(ptypes->obj_at(i));
never@2937 783 if (t == T_ARRAY) t = T_OBJECT;
never@2937 784 if (t == T_LONG || t == T_DOUBLE) {
never@2937 785 assert(T_VOID == _outgoing.at(index).basic_type(), "types must match");
never@2937 786 index--;
never@2937 787 }
never@2937 788 assert(t == _outgoing.at(index).basic_type(), "types must match");
never@2937 789 index--;
never@2937 790 }
never@2937 791 }
never@2937 792 #endif
never@2937 793
never@2937 794
twisti@1573 795 // -----------------------------------------------------------------------------
twisti@1573 796 // MethodHandleWalker::change_argument
twisti@1573 797 //
twisti@1573 798 // This is messy because some kinds of arguments are paired with
twisti@1573 799 // companion slots containing an empty value.
never@2937 800 void MethodHandleWalker::change_argument(BasicType old_type, int slot, const ArgToken& new_arg) {
never@2937 801 BasicType new_type = new_arg.basic_type();
twisti@1568 802 int old_size = type2size[old_type];
twisti@1568 803 int new_size = type2size[new_type];
twisti@1568 804 if (old_size == new_size) {
twisti@1568 805 // simple case first
never@2937 806 _outgoing.at_put(slot, new_arg);
twisti@1568 807 } else if (old_size > new_size) {
twisti@1573 808 for (int i = old_size - 1; i >= new_size; i--) {
never@2937 809 assert((i != 0) == (_outgoing.at(slot + i).basic_type() == T_VOID), "");
twisti@1568 810 _outgoing.remove_at(slot + i);
twisti@1568 811 }
twisti@1568 812 if (new_size > 0)
never@2937 813 _outgoing.at_put(slot, new_arg);
twisti@1568 814 else
twisti@1568 815 _outgoing_argc -= 1; // deleted a real argument
twisti@1568 816 } else {
twisti@1568 817 for (int i = old_size; i < new_size; i++) {
never@2937 818 _outgoing.insert_before(slot + i, ArgToken(tt_void));
twisti@1568 819 }
never@2937 820 _outgoing.at_put(slot, new_arg);
twisti@1568 821 if (old_size == 0)
twisti@1568 822 _outgoing_argc += 1; // inserted a real argument
twisti@1568 823 }
never@2937 824 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
twisti@1568 825 }
twisti@1568 826
twisti@1568 827
twisti@1568 828 #ifdef ASSERT
twisti@1568 829 int MethodHandleWalker::argument_count_slow() {
twisti@1568 830 int args_seen = 0;
twisti@1568 831 for (int i = _outgoing.length() - 1; i >= 0; i--) {
never@2937 832 if (_outgoing.at(i).basic_type() != T_VOID) {
twisti@1568 833 ++args_seen;
never@2937 834 if (_outgoing.at(i).basic_type() == T_LONG ||
never@2937 835 _outgoing.at(i).basic_type() == T_DOUBLE) {
never@2937 836 assert(_outgoing.at(i + 1).basic_type() == T_VOID, "should only follow two word");
never@2937 837 }
never@2937 838 } else {
never@2937 839 assert(_outgoing.at(i - 1).basic_type() == T_LONG ||
never@2937 840 _outgoing.at(i - 1).basic_type() == T_DOUBLE, "should only follow two word");
twisti@1568 841 }
twisti@1568 842 }
twisti@1568 843 return args_seen;
twisti@1568 844 }
twisti@1568 845 #endif
twisti@1568 846
twisti@1568 847
twisti@1573 848 // -----------------------------------------------------------------------------
twisti@2903 849 // MethodHandleWalker::retype_raw_conversion
twisti@2903 850 //
twisti@2903 851 // Do the raw retype conversions for OP_RETYPE_RAW.
twisti@2903 852 void MethodHandleWalker::retype_raw_conversion(BasicType src, BasicType dst, bool for_return, int slot, TRAPS) {
twisti@2903 853 if (src != dst) {
twisti@2903 854 if (MethodHandles::same_basic_type_for_returns(src, dst, /*raw*/ true)) {
twisti@2903 855 if (MethodHandles::is_float_fixed_reinterpretation_cast(src, dst)) {
twisti@2903 856 if (for_return) Untested("MHW return raw conversion"); // still untested
twisti@2903 857 vmIntrinsics::ID iid = vmIntrinsics::for_raw_conversion(src, dst);
twisti@2903 858 if (iid == vmIntrinsics::_none) {
twisti@2903 859 lose("no raw conversion method", CHECK);
twisti@2903 860 }
twisti@2903 861 ArgToken arglist[2];
twisti@2903 862 if (!for_return) {
twisti@2903 863 // argument type conversion
never@2937 864 ArgToken arg = _outgoing.at(slot);
twisti@2903 865 assert(arg.token_type() >= tt_symbolic || src == arg.basic_type(), "sanity");
twisti@2903 866 arglist[0] = arg; // outgoing 'this'
twisti@2903 867 arglist[1] = ArgToken(); // sentinel
twisti@2903 868 arg = make_invoke(NULL, iid, Bytecodes::_invokestatic, false, 1, &arglist[0], CHECK);
twisti@2903 869 change_argument(src, slot, dst, arg);
twisti@2903 870 } else {
twisti@2903 871 // return type conversion
twisti@2903 872 klassOop arg_klass = NULL;
twisti@2903 873 arglist[0] = make_parameter(src, arg_klass, -1, CHECK); // return value
twisti@2903 874 arglist[1] = ArgToken(); // sentinel
twisti@2903 875 (void) make_invoke(NULL, iid, Bytecodes::_invokestatic, false, 1, &arglist[0], CHECK);
twisti@2903 876 }
twisti@2903 877 } else {
twisti@2903 878 // Nothing to do.
twisti@2903 879 }
twisti@2903 880 } else if (src == T_OBJECT && is_java_primitive(dst)) {
twisti@2903 881 // ref-to-prim: discard ref, push zero
twisti@2903 882 lose("requested ref-to-prim conversion not expected", CHECK);
twisti@2903 883 } else {
never@2937 884 lose(err_msg("requested raw conversion not allowed: %s -> %s", type2name(src), type2name(dst)), CHECK);
twisti@2903 885 }
twisti@2903 886 }
twisti@2903 887 }
twisti@2903 888
twisti@2903 889
twisti@2903 890 // -----------------------------------------------------------------------------
twisti@1573 891 // MethodHandleCompiler
twisti@1573 892
never@2920 893 MethodHandleCompiler::MethodHandleCompiler(Handle root, Symbol* name, Symbol* signature, int invoke_count, bool is_invokedynamic, TRAPS)
twisti@1573 894 : MethodHandleWalker(root, is_invokedynamic, THREAD),
twisti@2898 895 _invoke_count(invoke_count),
twisti@1573 896 _thread(THREAD),
twisti@1573 897 _bytecode(THREAD, 50),
twisti@1573 898 _constants(THREAD, 10),
twisti@1573 899 _cur_stack(0),
twisti@1573 900 _max_stack(0),
twisti@1573 901 _rtype(T_ILLEGAL)
twisti@1573 902 {
twisti@1573 903
twisti@1573 904 // Element zero is always the null constant.
twisti@1573 905 (void) _constants.append(NULL);
twisti@1573 906
twisti@1573 907 // Set name and signature index.
never@2920 908 _name_index = cpool_symbol_put(name);
never@2920 909 _signature_index = cpool_symbol_put(signature);
twisti@1573 910
twisti@1573 911 // Get return type klass.
twisti@1573 912 Handle first_mtype(THREAD, chain().method_type_oop());
twisti@1573 913 // _rklass is NULL for primitives.
jrose@2639 914 _rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(first_mtype()), &_rklass);
twisti@1587 915 if (_rtype == T_ARRAY) _rtype = T_OBJECT;
twisti@1573 916
never@2920 917 ArgumentSizeComputer args(signature);
never@2920 918 int params = args.size() + 1; // Incoming arguments plus receiver.
twisti@1573 919 _num_params = for_invokedynamic() ? params - 1 : params; // XXX Check if callee is static?
twisti@1573 920 }
twisti@1573 921
twisti@1573 922
twisti@1573 923 // -----------------------------------------------------------------------------
twisti@1573 924 // MethodHandleCompiler::compile
twisti@1573 925 //
twisti@1573 926 // Compile this MethodHandle into a bytecode adapter and return a
twisti@1573 927 // methodOop.
twisti@1573 928 methodHandle MethodHandleCompiler::compile(TRAPS) {
twisti@1568 929 assert(_thread == THREAD, "must be same thread");
twisti@1573 930 methodHandle nullHandle;
twisti@1573 931 (void) walk(CHECK_(nullHandle));
twisti@1573 932 return get_method_oop(CHECK_(nullHandle));
twisti@1573 933 }
twisti@1568 934
twisti@1573 935
never@2920 936 void MethodHandleCompiler::emit_bc(Bytecodes::Code op, int index, int args_size) {
twisti@1573 937 Bytecodes::check(op); // Are we legal?
twisti@1573 938
twisti@1573 939 switch (op) {
twisti@1573 940 // b
twisti@1573 941 case Bytecodes::_aconst_null:
twisti@1573 942 case Bytecodes::_iconst_m1:
twisti@1573 943 case Bytecodes::_iconst_0:
twisti@1573 944 case Bytecodes::_iconst_1:
twisti@1573 945 case Bytecodes::_iconst_2:
twisti@1573 946 case Bytecodes::_iconst_3:
twisti@1573 947 case Bytecodes::_iconst_4:
twisti@1573 948 case Bytecodes::_iconst_5:
twisti@1573 949 case Bytecodes::_lconst_0:
twisti@1573 950 case Bytecodes::_lconst_1:
twisti@1573 951 case Bytecodes::_fconst_0:
twisti@1573 952 case Bytecodes::_fconst_1:
twisti@1573 953 case Bytecodes::_fconst_2:
twisti@1573 954 case Bytecodes::_dconst_0:
twisti@1573 955 case Bytecodes::_dconst_1:
twisti@1573 956 case Bytecodes::_iload_0:
twisti@1573 957 case Bytecodes::_iload_1:
twisti@1573 958 case Bytecodes::_iload_2:
twisti@1573 959 case Bytecodes::_iload_3:
twisti@1573 960 case Bytecodes::_lload_0:
twisti@1573 961 case Bytecodes::_lload_1:
twisti@1573 962 case Bytecodes::_lload_2:
twisti@1573 963 case Bytecodes::_lload_3:
twisti@1573 964 case Bytecodes::_fload_0:
twisti@1573 965 case Bytecodes::_fload_1:
twisti@1573 966 case Bytecodes::_fload_2:
twisti@1573 967 case Bytecodes::_fload_3:
twisti@1573 968 case Bytecodes::_dload_0:
twisti@1573 969 case Bytecodes::_dload_1:
twisti@1573 970 case Bytecodes::_dload_2:
twisti@1573 971 case Bytecodes::_dload_3:
twisti@1573 972 case Bytecodes::_aload_0:
twisti@1573 973 case Bytecodes::_aload_1:
twisti@1573 974 case Bytecodes::_aload_2:
twisti@1573 975 case Bytecodes::_aload_3:
twisti@1573 976 case Bytecodes::_istore_0:
twisti@1573 977 case Bytecodes::_istore_1:
twisti@1573 978 case Bytecodes::_istore_2:
twisti@1573 979 case Bytecodes::_istore_3:
twisti@1573 980 case Bytecodes::_lstore_0:
twisti@1573 981 case Bytecodes::_lstore_1:
twisti@1573 982 case Bytecodes::_lstore_2:
twisti@1573 983 case Bytecodes::_lstore_3:
twisti@1573 984 case Bytecodes::_fstore_0:
twisti@1573 985 case Bytecodes::_fstore_1:
twisti@1573 986 case Bytecodes::_fstore_2:
twisti@1573 987 case Bytecodes::_fstore_3:
twisti@1573 988 case Bytecodes::_dstore_0:
twisti@1573 989 case Bytecodes::_dstore_1:
twisti@1573 990 case Bytecodes::_dstore_2:
twisti@1573 991 case Bytecodes::_dstore_3:
twisti@1573 992 case Bytecodes::_astore_0:
twisti@1573 993 case Bytecodes::_astore_1:
twisti@1573 994 case Bytecodes::_astore_2:
twisti@1573 995 case Bytecodes::_astore_3:
twisti@2903 996 case Bytecodes::_iand:
twisti@1573 997 case Bytecodes::_i2l:
twisti@1573 998 case Bytecodes::_i2f:
twisti@1573 999 case Bytecodes::_i2d:
twisti@1573 1000 case Bytecodes::_i2b:
twisti@1573 1001 case Bytecodes::_i2c:
twisti@1573 1002 case Bytecodes::_i2s:
twisti@1573 1003 case Bytecodes::_l2i:
twisti@1573 1004 case Bytecodes::_l2f:
twisti@1573 1005 case Bytecodes::_l2d:
twisti@1573 1006 case Bytecodes::_f2i:
twisti@1573 1007 case Bytecodes::_f2l:
twisti@1573 1008 case Bytecodes::_f2d:
twisti@1573 1009 case Bytecodes::_d2i:
twisti@1573 1010 case Bytecodes::_d2l:
twisti@1573 1011 case Bytecodes::_d2f:
never@2920 1012 case Bytecodes::_iaload:
never@2920 1013 case Bytecodes::_laload:
never@2920 1014 case Bytecodes::_faload:
never@2920 1015 case Bytecodes::_daload:
never@2920 1016 case Bytecodes::_aaload:
never@2920 1017 case Bytecodes::_baload:
never@2920 1018 case Bytecodes::_caload:
never@2920 1019 case Bytecodes::_saload:
twisti@1573 1020 case Bytecodes::_ireturn:
twisti@1573 1021 case Bytecodes::_lreturn:
twisti@1573 1022 case Bytecodes::_freturn:
twisti@1573 1023 case Bytecodes::_dreturn:
twisti@1573 1024 case Bytecodes::_areturn:
twisti@1573 1025 case Bytecodes::_return:
jrose@1920 1026 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_b, "wrong bytecode format");
twisti@1573 1027 _bytecode.push(op);
twisti@1573 1028 break;
twisti@1573 1029
twisti@1573 1030 // bi
twisti@1573 1031 case Bytecodes::_ldc:
jrose@2017 1032 assert(Bytecodes::format_bits(op, false) == (Bytecodes::_fmt_b|Bytecodes::_fmt_has_k), "wrong bytecode format");
never@2920 1033 if (index == (index & 0xff)) {
never@2920 1034 _bytecode.push(op);
never@2920 1035 _bytecode.push(index);
never@2920 1036 } else {
never@2920 1037 _bytecode.push(Bytecodes::_ldc_w);
never@2920 1038 _bytecode.push(index >> 8);
never@2920 1039 _bytecode.push(index);
never@2920 1040 }
jrose@2017 1041 break;
jrose@2017 1042
twisti@1573 1043 case Bytecodes::_iload:
twisti@1573 1044 case Bytecodes::_lload:
twisti@1573 1045 case Bytecodes::_fload:
twisti@1573 1046 case Bytecodes::_dload:
twisti@1573 1047 case Bytecodes::_aload:
twisti@1573 1048 case Bytecodes::_istore:
twisti@1573 1049 case Bytecodes::_lstore:
twisti@1573 1050 case Bytecodes::_fstore:
twisti@1573 1051 case Bytecodes::_dstore:
twisti@1573 1052 case Bytecodes::_astore:
jrose@1920 1053 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bi, "wrong bytecode format");
never@2920 1054 if (index == (index & 0xff)) {
never@2920 1055 _bytecode.push(op);
never@2920 1056 _bytecode.push(index);
never@2920 1057 } else {
never@2920 1058 // doesn't fit in a u2
never@2920 1059 _bytecode.push(Bytecodes::_wide);
never@2920 1060 _bytecode.push(op);
never@2920 1061 _bytecode.push(index >> 8);
never@2920 1062 _bytecode.push(index);
never@2920 1063 }
twisti@1573 1064 break;
twisti@1573 1065
jrose@2017 1066 // bkk
jrose@2017 1067 case Bytecodes::_ldc_w:
twisti@1573 1068 case Bytecodes::_ldc2_w:
twisti@1573 1069 case Bytecodes::_checkcast:
jrose@1920 1070 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bkk, "wrong bytecode format");
never@2920 1071 assert((unsigned short) index == index, "index does not fit in 16-bit");
twisti@1573 1072 _bytecode.push(op);
twisti@1573 1073 _bytecode.push(index >> 8);
twisti@1573 1074 _bytecode.push(index);
twisti@1573 1075 break;
twisti@1573 1076
jrose@1920 1077 // bJJ
twisti@1573 1078 case Bytecodes::_invokestatic:
twisti@1573 1079 case Bytecodes::_invokespecial:
twisti@1573 1080 case Bytecodes::_invokevirtual:
jrose@1920 1081 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
never@2920 1082 assert((unsigned short) index == index, "index does not fit in 16-bit");
twisti@1573 1083 _bytecode.push(op);
twisti@1573 1084 _bytecode.push(index >> 8);
twisti@1573 1085 _bytecode.push(index);
twisti@1573 1086 break;
twisti@1573 1087
never@2920 1088 case Bytecodes::_invokeinterface:
never@2920 1089 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
never@2920 1090 assert((unsigned short) index == index, "index does not fit in 16-bit");
never@2920 1091 assert(args_size > 0, "valid args_size");
never@2920 1092 _bytecode.push(op);
never@2920 1093 _bytecode.push(index >> 8);
never@2920 1094 _bytecode.push(index);
never@2920 1095 _bytecode.push(args_size);
never@2920 1096 _bytecode.push(0);
never@2920 1097 break;
never@2920 1098
twisti@1573 1099 default:
twisti@1573 1100 ShouldNotReachHere();
twisti@1568 1101 }
twisti@1573 1102 }
twisti@1568 1103
twisti@1573 1104
twisti@1573 1105 void MethodHandleCompiler::emit_load(BasicType bt, int index) {
twisti@1573 1106 if (index <= 3) {
twisti@1573 1107 switch (bt) {
twisti@1573 1108 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
twisti@1573 1109 case T_INT: emit_bc(Bytecodes::cast(Bytecodes::_iload_0 + index)); break;
twisti@1573 1110 case T_LONG: emit_bc(Bytecodes::cast(Bytecodes::_lload_0 + index)); break;
twisti@1573 1111 case T_FLOAT: emit_bc(Bytecodes::cast(Bytecodes::_fload_0 + index)); break;
twisti@1573 1112 case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dload_0 + index)); break;
twisti@1573 1113 case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_aload_0 + index)); break;
twisti@1573 1114 default:
twisti@1573 1115 ShouldNotReachHere();
twisti@1573 1116 }
twisti@1573 1117 }
twisti@1573 1118 else {
twisti@1573 1119 switch (bt) {
twisti@1573 1120 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
twisti@1573 1121 case T_INT: emit_bc(Bytecodes::_iload, index); break;
twisti@1573 1122 case T_LONG: emit_bc(Bytecodes::_lload, index); break;
twisti@1573 1123 case T_FLOAT: emit_bc(Bytecodes::_fload, index); break;
twisti@1573 1124 case T_DOUBLE: emit_bc(Bytecodes::_dload, index); break;
twisti@1573 1125 case T_OBJECT: emit_bc(Bytecodes::_aload, index); break;
twisti@1573 1126 default:
twisti@1573 1127 ShouldNotReachHere();
twisti@1573 1128 }
twisti@1573 1129 }
twisti@1573 1130 stack_push(bt);
twisti@1568 1131 }
twisti@1568 1132
twisti@1573 1133 void MethodHandleCompiler::emit_store(BasicType bt, int index) {
twisti@1573 1134 if (index <= 3) {
twisti@1573 1135 switch (bt) {
twisti@1573 1136 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
twisti@1573 1137 case T_INT: emit_bc(Bytecodes::cast(Bytecodes::_istore_0 + index)); break;
twisti@1573 1138 case T_LONG: emit_bc(Bytecodes::cast(Bytecodes::_lstore_0 + index)); break;
twisti@1573 1139 case T_FLOAT: emit_bc(Bytecodes::cast(Bytecodes::_fstore_0 + index)); break;
twisti@1573 1140 case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dstore_0 + index)); break;
twisti@1573 1141 case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_astore_0 + index)); break;
twisti@1573 1142 default:
twisti@1573 1143 ShouldNotReachHere();
twisti@1573 1144 }
twisti@1573 1145 }
twisti@1573 1146 else {
twisti@1573 1147 switch (bt) {
twisti@1573 1148 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
twisti@1573 1149 case T_INT: emit_bc(Bytecodes::_istore, index); break;
twisti@1573 1150 case T_LONG: emit_bc(Bytecodes::_lstore, index); break;
twisti@1573 1151 case T_FLOAT: emit_bc(Bytecodes::_fstore, index); break;
twisti@1573 1152 case T_DOUBLE: emit_bc(Bytecodes::_dstore, index); break;
twisti@1573 1153 case T_OBJECT: emit_bc(Bytecodes::_astore, index); break;
twisti@1573 1154 default:
twisti@1573 1155 ShouldNotReachHere();
twisti@1573 1156 }
twisti@1573 1157 }
twisti@1573 1158 stack_pop(bt);
twisti@1573 1159 }
twisti@1573 1160
twisti@1573 1161
twisti@1573 1162 void MethodHandleCompiler::emit_load_constant(ArgToken arg) {
twisti@1573 1163 BasicType bt = arg.basic_type();
never@2937 1164 if (is_subword_type(bt)) bt = T_INT;
twisti@1573 1165 switch (bt) {
twisti@1573 1166 case T_INT: {
twisti@1573 1167 jint value = arg.get_jint();
twisti@1573 1168 if (-1 <= value && value <= 5)
twisti@1573 1169 emit_bc(Bytecodes::cast(Bytecodes::_iconst_0 + value));
twisti@1573 1170 else
twisti@1573 1171 emit_bc(Bytecodes::_ldc, cpool_int_put(value));
twisti@1573 1172 break;
twisti@1573 1173 }
twisti@1573 1174 case T_LONG: {
twisti@1573 1175 jlong value = arg.get_jlong();
twisti@1573 1176 if (0 <= value && value <= 1)
twisti@1573 1177 emit_bc(Bytecodes::cast(Bytecodes::_lconst_0 + (int) value));
twisti@1573 1178 else
twisti@1573 1179 emit_bc(Bytecodes::_ldc2_w, cpool_long_put(value));
twisti@1573 1180 break;
twisti@1573 1181 }
twisti@1573 1182 case T_FLOAT: {
twisti@1573 1183 jfloat value = arg.get_jfloat();
twisti@1573 1184 if (value == 0.0 || value == 1.0 || value == 2.0)
twisti@1573 1185 emit_bc(Bytecodes::cast(Bytecodes::_fconst_0 + (int) value));
twisti@1573 1186 else
twisti@1573 1187 emit_bc(Bytecodes::_ldc, cpool_float_put(value));
twisti@1573 1188 break;
twisti@1573 1189 }
twisti@1573 1190 case T_DOUBLE: {
twisti@1573 1191 jdouble value = arg.get_jdouble();
twisti@1573 1192 if (value == 0.0 || value == 1.0)
twisti@1573 1193 emit_bc(Bytecodes::cast(Bytecodes::_dconst_0 + (int) value));
twisti@1573 1194 else
twisti@1573 1195 emit_bc(Bytecodes::_ldc2_w, cpool_double_put(value));
twisti@1573 1196 break;
twisti@1573 1197 }
twisti@1573 1198 case T_OBJECT: {
twisti@1573 1199 Handle value = arg.object();
twisti@1573 1200 if (value.is_null())
twisti@1573 1201 emit_bc(Bytecodes::_aconst_null);
twisti@1573 1202 else
twisti@1573 1203 emit_bc(Bytecodes::_ldc, cpool_object_put(value));
twisti@1573 1204 break;
twisti@1573 1205 }
twisti@1573 1206 default:
twisti@1573 1207 ShouldNotReachHere();
twisti@1573 1208 }
twisti@1573 1209 stack_push(bt);
twisti@1573 1210 }
twisti@1573 1211
twisti@1573 1212
twisti@1568 1213 MethodHandleWalker::ArgToken
twisti@1568 1214 MethodHandleCompiler::make_conversion(BasicType type, klassOop tk, Bytecodes::Code op,
twisti@1573 1215 const ArgToken& src, TRAPS) {
twisti@1573 1216
twisti@1573 1217 BasicType srctype = src.basic_type();
never@2920 1218 TokenType tt = src.token_type();
never@2920 1219 int index = -1;
twisti@1573 1220
twisti@1573 1221 switch (op) {
twisti@1573 1222 case Bytecodes::_i2l:
twisti@1573 1223 case Bytecodes::_i2f:
twisti@1573 1224 case Bytecodes::_i2d:
twisti@1573 1225 case Bytecodes::_i2b:
twisti@1573 1226 case Bytecodes::_i2c:
twisti@1573 1227 case Bytecodes::_i2s:
twisti@1573 1228
twisti@1573 1229 case Bytecodes::_l2i:
twisti@1573 1230 case Bytecodes::_l2f:
twisti@1573 1231 case Bytecodes::_l2d:
twisti@1573 1232
twisti@1573 1233 case Bytecodes::_f2i:
twisti@1573 1234 case Bytecodes::_f2l:
twisti@1573 1235 case Bytecodes::_f2d:
twisti@1573 1236
twisti@1573 1237 case Bytecodes::_d2i:
twisti@1573 1238 case Bytecodes::_d2l:
twisti@1573 1239 case Bytecodes::_d2f:
never@2920 1240 if (tt == tt_constant) {
never@2920 1241 emit_load_constant(src);
never@2920 1242 } else {
never@2920 1243 emit_load(srctype, src.index());
never@2920 1244 }
twisti@1573 1245 stack_pop(srctype); // pop the src type
twisti@1573 1246 emit_bc(op);
twisti@1573 1247 stack_push(type); // push the dest value
never@2920 1248 if (tt != tt_constant)
never@2920 1249 index = src.index();
never@2920 1250 if (srctype != type || index == -1)
twisti@1573 1251 index = new_local_index(type);
twisti@1573 1252 emit_store(type, index);
twisti@1573 1253 break;
twisti@1573 1254
twisti@1573 1255 case Bytecodes::_checkcast:
never@2920 1256 if (tt == tt_constant) {
never@2920 1257 emit_load_constant(src);
never@2920 1258 } else {
never@2920 1259 emit_load(srctype, src.index());
never@2920 1260 index = src.index();
never@2920 1261 }
twisti@1573 1262 emit_bc(op, cpool_klass_put(tk));
never@2950 1263 // Allocate a new local for the type so that we don't hide the
never@2950 1264 // previous type from the verifier.
never@2950 1265 index = new_local_index(type);
twisti@1573 1266 emit_store(srctype, index);
twisti@1573 1267 break;
twisti@1573 1268
never@2937 1269 case Bytecodes::_nop:
never@2937 1270 // nothing to do
never@2937 1271 return src;
never@2937 1272
twisti@1573 1273 default:
twisti@2903 1274 if (op == Bytecodes::_illegal)
never@2937 1275 lose(err_msg("no such primitive conversion: %s -> %s", type2name(src.basic_type()), type2name(type)), THREAD);
twisti@2903 1276 else
never@2937 1277 lose(err_msg("bad primitive conversion op: %s", Bytecodes::name(op)), THREAD);
twisti@2903 1278 return make_prim_constant(type, &zero_jvalue, THREAD);
twisti@1573 1279 }
twisti@1573 1280
twisti@1573 1281 return make_parameter(type, tk, index, THREAD);
twisti@1568 1282 }
twisti@1568 1283
twisti@1573 1284
twisti@1573 1285 // -----------------------------------------------------------------------------
twisti@1573 1286 // MethodHandleCompiler
twisti@1573 1287 //
twisti@1573 1288
twisti@2903 1289 // Values used by the compiler.
twisti@2903 1290 jvalue MethodHandleCompiler::zero_jvalue = { 0 };
twisti@2903 1291 jvalue MethodHandleCompiler::one_jvalue = { 1 };
twisti@1573 1292
twisti@1573 1293 // Emit bytecodes for the given invoke instruction.
twisti@1568 1294 MethodHandleWalker::ArgToken
twisti@1568 1295 MethodHandleCompiler::make_invoke(methodOop m, vmIntrinsics::ID iid,
twisti@1568 1296 Bytecodes::Code op, bool tailcall,
twisti@1568 1297 int argc, MethodHandleWalker::ArgToken* argv,
twisti@1568 1298 TRAPS) {
twisti@2903 1299 ArgToken zero;
twisti@1573 1300 if (m == NULL) {
twisti@1573 1301 // Get the intrinsic methodOop.
twisti@1573 1302 m = vmIntrinsics::method_for(iid);
jrose@2638 1303 if (m == NULL) {
jrose@2638 1304 lose(vmIntrinsics::name_at(iid), CHECK_(zero));
jrose@2638 1305 }
twisti@1573 1306 }
twisti@1573 1307
twisti@2903 1308 klassOop klass = m->method_holder();
twisti@2903 1309 Symbol* name = m->name();
twisti@2903 1310 Symbol* signature = m->signature();
twisti@1573 1311
never@2920 1312 // Count the number of arguments, not the size
never@2920 1313 ArgumentCount asc(signature);
never@2920 1314 assert(argc == asc.size() + ((op == Bytecodes::_invokestatic || op == Bytecodes::_invokedynamic) ? 0 : 1),
never@2920 1315 "argc mismatch");
never@2920 1316
twisti@1573 1317 if (tailcall) {
twisti@1587 1318 // Actually, in order to make these methods more recognizable,
twisti@2343 1319 // let's put them in holder class MethodHandle. That way stack
twisti@2343 1320 // walkers and compiler heuristics can recognize them.
twisti@2343 1321 _target_klass = SystemDictionary::MethodHandle_klass();
twisti@1573 1322 }
twisti@1573 1323
twisti@1573 1324 // Inline the method.
twisti@1573 1325 InvocationCounter* ic = m->invocation_counter();
iveresov@2138 1326 ic->set_carry_flag();
twisti@1573 1327
twisti@1573 1328 for (int i = 0; i < argc; i++) {
twisti@1573 1329 ArgToken arg = argv[i];
twisti@1573 1330 TokenType tt = arg.token_type();
twisti@1573 1331 BasicType bt = arg.basic_type();
twisti@1573 1332
twisti@1573 1333 switch (tt) {
twisti@1573 1334 case tt_parameter:
twisti@1573 1335 case tt_temporary:
twisti@1573 1336 emit_load(bt, arg.index());
twisti@1573 1337 break;
twisti@1573 1338 case tt_constant:
twisti@1573 1339 emit_load_constant(arg);
twisti@1573 1340 break;
twisti@1573 1341 case tt_illegal:
twisti@1573 1342 // Sentinel.
twisti@1573 1343 assert(i == (argc - 1), "sentinel must be last entry");
twisti@1573 1344 break;
twisti@1573 1345 case tt_void:
twisti@1573 1346 default:
twisti@1573 1347 ShouldNotReachHere();
twisti@1573 1348 }
twisti@1573 1349 }
twisti@1573 1350
twisti@1573 1351 // Populate constant pool.
twisti@1573 1352 int name_index = cpool_symbol_put(name);
twisti@1573 1353 int signature_index = cpool_symbol_put(signature);
twisti@1573 1354 int name_and_type_index = cpool_name_and_type_put(name_index, signature_index);
twisti@1573 1355 int klass_index = cpool_klass_put(klass);
twisti@1573 1356 int methodref_index = cpool_methodref_put(klass_index, name_and_type_index);
twisti@1573 1357
twisti@1573 1358 // Generate invoke.
twisti@1568 1359 switch (op) {
twisti@1573 1360 case Bytecodes::_invokestatic:
twisti@1573 1361 case Bytecodes::_invokespecial:
twisti@1568 1362 case Bytecodes::_invokevirtual:
twisti@1573 1363 emit_bc(op, methodref_index);
twisti@1573 1364 break;
never@2920 1365
never@2920 1366 case Bytecodes::_invokeinterface: {
never@2920 1367 ArgumentSizeComputer asc(signature);
never@2920 1368 emit_bc(op, methodref_index, asc.size() + 1);
twisti@1568 1369 break;
never@2920 1370 }
never@2920 1371
twisti@1568 1372 default:
twisti@1568 1373 ShouldNotReachHere();
twisti@1568 1374 }
twisti@1568 1375
twisti@1573 1376 // If tailcall, we have walked all the way to a direct method handle.
twisti@1573 1377 // Otherwise, make a recursive call to some helper routine.
twisti@1573 1378 BasicType rbt = m->result_type();
twisti@1587 1379 if (rbt == T_ARRAY) rbt = T_OBJECT;
never@2920 1380 stack_push(rbt); // The return value is already pushed onto the stack.
twisti@1573 1381 ArgToken ret;
twisti@1573 1382 if (tailcall) {
twisti@1573 1383 if (rbt != _rtype) {
twisti@1573 1384 if (rbt == T_VOID) {
twisti@1573 1385 // push a zero of the right sort
twisti@1573 1386 if (_rtype == T_OBJECT) {
twisti@1573 1387 zero = make_oop_constant(NULL, CHECK_(zero));
twisti@1573 1388 } else {
twisti@1573 1389 zero = make_prim_constant(_rtype, &zero_jvalue, CHECK_(zero));
twisti@1573 1390 }
twisti@1573 1391 emit_load_constant(zero);
twisti@1573 1392 } else if (_rtype == T_VOID) {
twisti@1573 1393 // We'll emit a _return with something on the stack.
twisti@1573 1394 // It's OK to ignore what's on the stack.
twisti@2903 1395 } else if (rbt == T_INT && is_subword_type(_rtype)) {
twisti@2903 1396 // Convert value to match return type.
twisti@2903 1397 switch (_rtype) {
twisti@2903 1398 case T_BOOLEAN: {
twisti@2903 1399 // boolean is treated as a one-bit unsigned integer.
twisti@2903 1400 // Cf. API documentation: java/lang/invoke/MethodHandles.html#explicitCastArguments
twisti@2903 1401 ArgToken one = make_prim_constant(T_INT, &one_jvalue, CHECK_(zero));
twisti@2903 1402 emit_load_constant(one);
twisti@2903 1403 emit_bc(Bytecodes::_iand);
twisti@2903 1404 break;
twisti@2903 1405 }
twisti@2903 1406 case T_BYTE: emit_bc(Bytecodes::_i2b); break;
twisti@2903 1407 case T_CHAR: emit_bc(Bytecodes::_i2c); break;
twisti@2903 1408 case T_SHORT: emit_bc(Bytecodes::_i2s); break;
twisti@2903 1409 default: ShouldNotReachHere();
twisti@2903 1410 }
twisti@2903 1411 } else if (is_subword_type(rbt) && (is_subword_type(_rtype) || (_rtype == T_INT))) {
twisti@2903 1412 // The subword type was returned as an int and will be passed
twisti@2903 1413 // on as an int.
twisti@1573 1414 } else {
twisti@2903 1415 lose("unknown conversion", CHECK_(zero));
twisti@1573 1416 }
twisti@1573 1417 }
twisti@1573 1418 switch (_rtype) {
twisti@1573 1419 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
twisti@1573 1420 case T_INT: emit_bc(Bytecodes::_ireturn); break;
twisti@1573 1421 case T_LONG: emit_bc(Bytecodes::_lreturn); break;
twisti@1573 1422 case T_FLOAT: emit_bc(Bytecodes::_freturn); break;
twisti@1573 1423 case T_DOUBLE: emit_bc(Bytecodes::_dreturn); break;
twisti@1573 1424 case T_VOID: emit_bc(Bytecodes::_return); break;
twisti@1573 1425 case T_OBJECT:
never@2954 1426 if (_rklass.not_null() && _rklass() != SystemDictionary::Object_klass() && !Klass::cast(_rklass())->is_interface()) {
twisti@1573 1427 emit_bc(Bytecodes::_checkcast, cpool_klass_put(_rklass()));
never@2954 1428 }
twisti@1573 1429 emit_bc(Bytecodes::_areturn);
twisti@1573 1430 break;
twisti@1573 1431 default: ShouldNotReachHere();
twisti@1573 1432 }
twisti@1573 1433 ret = ArgToken(); // Dummy return value.
twisti@1573 1434 }
twisti@1573 1435 else {
twisti@1573 1436 int index = new_local_index(rbt);
twisti@1573 1437 switch (rbt) {
twisti@1573 1438 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
twisti@1573 1439 case T_INT: case T_LONG: case T_FLOAT: case T_DOUBLE:
twisti@1573 1440 case T_OBJECT:
twisti@1573 1441 emit_store(rbt, index);
twisti@1573 1442 ret = ArgToken(tt_temporary, rbt, index);
twisti@1573 1443 break;
twisti@1573 1444 case T_VOID:
twisti@1573 1445 ret = ArgToken(tt_void);
twisti@1573 1446 break;
twisti@1573 1447 default:
twisti@1573 1448 ShouldNotReachHere();
twisti@1573 1449 }
twisti@1573 1450 }
twisti@1573 1451
twisti@1573 1452 return ret;
twisti@1568 1453 }
twisti@1568 1454
twisti@1568 1455 MethodHandleWalker::ArgToken
twisti@1568 1456 MethodHandleCompiler::make_fetch(BasicType type, klassOop tk, Bytecodes::Code op,
twisti@1573 1457 const MethodHandleWalker::ArgToken& base,
twisti@1573 1458 const MethodHandleWalker::ArgToken& offset,
twisti@1568 1459 TRAPS) {
never@2920 1460 switch (base.token_type()) {
never@2920 1461 case tt_parameter:
never@2920 1462 case tt_temporary:
never@2920 1463 emit_load(base.basic_type(), base.index());
never@2920 1464 break;
never@2920 1465 case tt_constant:
never@2920 1466 emit_load_constant(base);
never@2920 1467 break;
never@2920 1468 default:
never@2920 1469 ShouldNotReachHere();
never@2920 1470 }
never@2920 1471 switch (offset.token_type()) {
never@2920 1472 case tt_parameter:
never@2920 1473 case tt_temporary:
never@2920 1474 emit_load(offset.basic_type(), offset.index());
never@2920 1475 break;
never@2920 1476 case tt_constant:
never@2920 1477 emit_load_constant(offset);
never@2920 1478 break;
never@2920 1479 default:
never@2920 1480 ShouldNotReachHere();
never@2920 1481 }
never@2920 1482 emit_bc(op);
never@2920 1483 int index = new_local_index(type);
never@2920 1484 emit_store(type, index);
never@2920 1485 return ArgToken(tt_temporary, type, index);
twisti@1568 1486 }
twisti@1568 1487
twisti@1568 1488
twisti@1573 1489 int MethodHandleCompiler::cpool_primitive_put(BasicType bt, jvalue* con) {
twisti@1568 1490 jvalue con_copy;
twisti@1568 1491 assert(bt < T_OBJECT, "");
twisti@1568 1492 if (type2aelembytes(bt) < jintSize) {
twisti@1568 1493 // widen to int
twisti@1568 1494 con_copy = (*con);
twisti@1568 1495 con = &con_copy;
twisti@1568 1496 switch (bt) {
twisti@1568 1497 case T_BOOLEAN: con->i = (con->z ? 1 : 0); break;
twisti@1568 1498 case T_BYTE: con->i = con->b; break;
twisti@1568 1499 case T_CHAR: con->i = con->c; break;
twisti@1568 1500 case T_SHORT: con->i = con->s; break;
twisti@1568 1501 default: ShouldNotReachHere();
twisti@1568 1502 }
twisti@1568 1503 bt = T_INT;
twisti@1568 1504 }
twisti@1573 1505
twisti@1573 1506 // for (int i = 1, imax = _constants.length(); i < imax; i++) {
twisti@1573 1507 // ConstantValue* con = _constants.at(i);
never@2937 1508 // if (con != NULL && con->is_primitive() && con.basic_type() == bt) {
twisti@1573 1509 // bool match = false;
twisti@1573 1510 // switch (type2size[bt]) {
twisti@1573 1511 // case 1: if (pcon->_value.i == con->i) match = true; break;
twisti@1573 1512 // case 2: if (pcon->_value.j == con->j) match = true; break;
twisti@1573 1513 // }
twisti@1573 1514 // if (match)
twisti@1573 1515 // return i;
twisti@1573 1516 // }
twisti@1573 1517 // }
twisti@1573 1518 ConstantValue* cv = new ConstantValue(bt, *con);
twisti@1573 1519 int index = _constants.append(cv);
twisti@1573 1520
twisti@1573 1521 // long and double entries take 2 slots, we add another empty entry.
twisti@1573 1522 if (type2size[bt] == 2)
twisti@1573 1523 (void) _constants.append(NULL);
twisti@1573 1524
twisti@1573 1525 return index;
twisti@1573 1526 }
twisti@1573 1527
twisti@1573 1528
twisti@1573 1529 constantPoolHandle MethodHandleCompiler::get_constant_pool(TRAPS) const {
twisti@1573 1530 constantPoolHandle nullHandle;
ysr@2533 1531 constantPoolOop cpool_oop = oopFactory::new_constantPool(_constants.length(),
ysr@2533 1532 oopDesc::IsSafeConc,
ysr@2533 1533 CHECK_(nullHandle));
twisti@1573 1534 constantPoolHandle cpool(THREAD, cpool_oop);
twisti@1573 1535
twisti@1573 1536 // Fill the real constant pool skipping the zero element.
twisti@1573 1537 for (int i = 1; i < _constants.length(); i++) {
twisti@1573 1538 ConstantValue* cv = _constants.at(i);
twisti@1573 1539 switch (cv->tag()) {
coleenp@2497 1540 case JVM_CONSTANT_Utf8: cpool->symbol_at_put( i, cv->symbol() ); break;
twisti@1573 1541 case JVM_CONSTANT_Integer: cpool->int_at_put( i, cv->get_jint() ); break;
twisti@1573 1542 case JVM_CONSTANT_Float: cpool->float_at_put( i, cv->get_jfloat() ); break;
twisti@1573 1543 case JVM_CONSTANT_Long: cpool->long_at_put( i, cv->get_jlong() ); break;
twisti@1573 1544 case JVM_CONSTANT_Double: cpool->double_at_put( i, cv->get_jdouble() ); break;
twisti@1573 1545 case JVM_CONSTANT_Class: cpool->klass_at_put( i, cv->klass_oop() ); break;
twisti@1573 1546 case JVM_CONSTANT_Methodref: cpool->method_at_put( i, cv->first_index(), cv->second_index()); break;
twisti@1573 1547 case JVM_CONSTANT_NameAndType: cpool->name_and_type_at_put(i, cv->first_index(), cv->second_index()); break;
twisti@1573 1548 case JVM_CONSTANT_Object: cpool->object_at_put( i, cv->object_oop() ); break;
twisti@1573 1549 default: ShouldNotReachHere();
twisti@1573 1550 }
twisti@1573 1551
twisti@1573 1552 switch (cv->tag()) {
twisti@1573 1553 case JVM_CONSTANT_Long:
twisti@1573 1554 case JVM_CONSTANT_Double:
twisti@1573 1555 i++; // Skip empty entry.
twisti@1573 1556 assert(_constants.at(i) == NULL, "empty entry");
twisti@1573 1557 break;
twisti@1568 1558 }
twisti@1568 1559 }
twisti@1573 1560
twisti@1573 1561 // Set the constant pool holder to the target method's class.
twisti@1573 1562 cpool->set_pool_holder(_target_klass());
twisti@1573 1563
twisti@1573 1564 return cpool;
twisti@1573 1565 }
twisti@1573 1566
twisti@1573 1567
twisti@1573 1568 methodHandle MethodHandleCompiler::get_method_oop(TRAPS) const {
twisti@2898 1569 methodHandle empty;
twisti@1573 1570 // Create a method that holds the generated bytecode. invokedynamic
twisti@1573 1571 // has no receiver, normal MH calls do.
twisti@1573 1572 int flags_bits;
twisti@1573 1573 if (for_invokedynamic())
jrose@1862 1574 flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC | JVM_ACC_STATIC);
twisti@1573 1575 else
jrose@1862 1576 flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC);
twisti@1573 1577
twisti@2898 1578 // Create a new method
twisti@2898 1579 methodHandle m;
twisti@2898 1580 {
twisti@2898 1581 methodOop m_oop = oopFactory::new_method(bytecode_length(),
twisti@2898 1582 accessFlags_from(flags_bits),
twisti@2898 1583 0, 0, 0, oopDesc::IsSafeConc, CHECK_(empty));
twisti@2898 1584 m = methodHandle(THREAD, m_oop);
twisti@2898 1585 }
twisti@1573 1586
twisti@2898 1587 constantPoolHandle cpool = get_constant_pool(CHECK_(empty));
twisti@1573 1588 m->set_constants(cpool());
twisti@1573 1589
twisti@1573 1590 m->set_name_index(_name_index);
twisti@1573 1591 m->set_signature_index(_signature_index);
twisti@1573 1592
twisti@1573 1593 m->set_code((address) bytecode());
twisti@1573 1594
twisti@1573 1595 m->set_max_stack(_max_stack);
twisti@1573 1596 m->set_max_locals(max_locals());
twisti@1573 1597 m->set_size_of_parameters(_num_params);
twisti@1573 1598
twisti@1573 1599 typeArrayHandle exception_handlers(THREAD, Universe::the_empty_int_array());
twisti@1573 1600 m->set_exception_table(exception_handlers());
twisti@1573 1601
twisti@1573 1602 // Rewrite the method and set up the constant pool cache.
twisti@2898 1603 objArrayOop m_array = oopFactory::new_system_objArray(1, CHECK_(empty));
twisti@1573 1604 objArrayHandle methods(THREAD, m_array);
twisti@1573 1605 methods->obj_at_put(0, m());
twisti@2898 1606 Rewriter::rewrite(_target_klass(), cpool, methods, CHECK_(empty)); // Use fake class.
coleenp@2945 1607 Rewriter::relocate_and_link(_target_klass(), methods, CHECK_(empty)); // Use fake class.
twisti@2898 1608
twisti@2903 1609 // Set the invocation counter's count to the invoke count of the
twisti@2903 1610 // original call site.
twisti@2903 1611 InvocationCounter* ic = m->invocation_counter();
twisti@2903 1612 ic->set(InvocationCounter::wait_for_compile, _invoke_count);
twisti@2903 1613
twisti@2898 1614 // Create a new MDO
twisti@2898 1615 {
twisti@2898 1616 methodDataOop mdo = oopFactory::new_methodData(m, CHECK_(empty));
twisti@2898 1617 assert(m->method_data() == NULL, "there should not be an MDO yet");
twisti@2898 1618 m->set_method_data(mdo);
twisti@2898 1619
twisti@2898 1620 // Iterate over all profile data and set the count of the counter
twisti@2898 1621 // data entries to the original call site counter.
twisti@2903 1622 for (ProfileData* profile_data = mdo->first_data();
twisti@2903 1623 mdo->is_valid(profile_data);
twisti@2903 1624 profile_data = mdo->next_data(profile_data)) {
twisti@2903 1625 if (profile_data->is_CounterData()) {
twisti@2903 1626 CounterData* counter_data = profile_data->as_CounterData();
twisti@2903 1627 counter_data->set_count(_invoke_count);
twisti@2898 1628 }
twisti@2898 1629 }
twisti@2898 1630 }
twisti@1573 1631
twisti@1573 1632 #ifndef PRODUCT
twisti@1573 1633 if (TraceMethodHandles) {
twisti@1573 1634 m->print();
twisti@1573 1635 m->print_codes();
twisti@1573 1636 }
twisti@1573 1637 #endif //PRODUCT
twisti@1573 1638
jrose@1862 1639 assert(m->is_method_handle_adapter(), "must be recognized as an adapter");
twisti@1573 1640 return m;
twisti@1568 1641 }
twisti@1568 1642
twisti@1568 1643
twisti@1568 1644 #ifndef PRODUCT
twisti@1568 1645
twisti@1568 1646 // MH printer for debugging.
twisti@1568 1647
twisti@1568 1648 class MethodHandlePrinter : public MethodHandleWalker {
twisti@1568 1649 private:
twisti@1568 1650 outputStream* _out;
twisti@1568 1651 bool _verbose;
twisti@1568 1652 int _temp_num;
twisti@2903 1653 int _param_state;
twisti@1568 1654 stringStream _strbuf;
twisti@1568 1655 const char* strbuf() {
twisti@1568 1656 const char* s = _strbuf.as_string();
twisti@1568 1657 _strbuf.reset();
twisti@1568 1658 return s;
twisti@1568 1659 }
never@2937 1660 ArgToken token(const char* str, BasicType type) {
never@2937 1661 return ArgToken(str, type);
twisti@2903 1662 }
twisti@2903 1663 const char* string(ArgToken token) {
never@2920 1664 return token.str();
twisti@1568 1665 }
twisti@1568 1666 void start_params() {
twisti@2903 1667 _param_state <<= 1;
twisti@1568 1668 _out->print("(");
twisti@1568 1669 }
twisti@1568 1670 void end_params() {
twisti@1568 1671 if (_verbose) _out->print("\n");
twisti@1568 1672 _out->print(") => {");
twisti@2903 1673 _param_state >>= 1;
twisti@1568 1674 }
twisti@1568 1675 void put_type_name(BasicType type, klassOop tk, outputStream* s) {
twisti@1568 1676 const char* kname = NULL;
twisti@1568 1677 if (tk != NULL)
twisti@1568 1678 kname = Klass::cast(tk)->external_name();
twisti@1568 1679 s->print("%s", (kname != NULL) ? kname : type2name(type));
twisti@1568 1680 }
twisti@1568 1681 ArgToken maybe_make_temp(const char* statement_op, BasicType type, const char* temp_name) {
twisti@1568 1682 const char* value = strbuf();
never@2937 1683 if (!_verbose) return token(value, type);
twisti@1568 1684 // make an explicit binding for each separate value
twisti@1568 1685 _strbuf.print("%s%d", temp_name, ++_temp_num);
twisti@1568 1686 const char* temp = strbuf();
twisti@1568 1687 _out->print("\n %s %s %s = %s;", statement_op, type2name(type), temp, value);
never@2937 1688 return token(temp, type);
twisti@1568 1689 }
twisti@1568 1690
twisti@1568 1691 public:
twisti@1568 1692 MethodHandlePrinter(Handle root, bool verbose, outputStream* out, TRAPS)
twisti@2903 1693 : MethodHandleWalker(root, false, THREAD),
twisti@1568 1694 _out(out),
twisti@1568 1695 _verbose(verbose),
twisti@2903 1696 _param_state(0),
twisti@1568 1697 _temp_num(0)
twisti@1568 1698 {
twisti@1568 1699 start_params();
twisti@1568 1700 }
twisti@1568 1701 virtual ArgToken make_parameter(BasicType type, klassOop tk, int argnum, TRAPS) {
twisti@1568 1702 if (argnum < 0) {
twisti@1568 1703 end_params();
never@2937 1704 return token("return", type);
twisti@1568 1705 }
twisti@2903 1706 if ((_param_state & 1) == 0) {
twisti@2903 1707 _param_state |= 1;
twisti@1568 1708 _out->print(_verbose ? "\n " : "");
twisti@1568 1709 } else {
twisti@1568 1710 _out->print(_verbose ? ",\n " : ", ");
twisti@1568 1711 }
twisti@1568 1712 if (argnum >= _temp_num)
twisti@1568 1713 _temp_num = argnum;
twisti@1568 1714 // generate an argument name
twisti@1568 1715 _strbuf.print("a%d", argnum);
twisti@1568 1716 const char* arg = strbuf();
twisti@1568 1717 put_type_name(type, tk, _out);
twisti@1568 1718 _out->print(" %s", arg);
never@2937 1719 return token(arg, type);
twisti@1568 1720 }
twisti@1568 1721 virtual ArgToken make_oop_constant(oop con, TRAPS) {
twisti@1568 1722 if (con == NULL)
twisti@1568 1723 _strbuf.print("null");
twisti@1568 1724 else
twisti@1568 1725 con->print_value_on(&_strbuf);
twisti@1568 1726 if (_strbuf.size() == 0) { // yuck
twisti@1568 1727 _strbuf.print("(a ");
twisti@1568 1728 put_type_name(T_OBJECT, con->klass(), &_strbuf);
twisti@1568 1729 _strbuf.print(")");
twisti@1568 1730 }
twisti@1568 1731 return maybe_make_temp("constant", T_OBJECT, "k");
twisti@1568 1732 }
twisti@1568 1733 virtual ArgToken make_prim_constant(BasicType type, jvalue* con, TRAPS) {
twisti@1568 1734 java_lang_boxing_object::print(type, con, &_strbuf);
twisti@1568 1735 return maybe_make_temp("constant", type, "k");
twisti@1568 1736 }
twisti@2903 1737 void print_bytecode_name(Bytecodes::Code op) {
twisti@2903 1738 if (Bytecodes::is_defined(op))
twisti@2903 1739 _strbuf.print("%s", Bytecodes::name(op));
twisti@2903 1740 else
twisti@2903 1741 _strbuf.print("bytecode_%d", (int) op);
twisti@2903 1742 }
twisti@2903 1743 virtual ArgToken make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& src, TRAPS) {
twisti@2903 1744 print_bytecode_name(op);
twisti@2903 1745 _strbuf.print("(%s", string(src));
twisti@1568 1746 if (tk != NULL) {
twisti@1568 1747 _strbuf.print(", ");
twisti@1568 1748 put_type_name(type, tk, &_strbuf);
twisti@1568 1749 }
twisti@1568 1750 _strbuf.print(")");
twisti@1568 1751 return maybe_make_temp("convert", type, "v");
twisti@1568 1752 }
twisti@2903 1753 virtual ArgToken make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& base, const ArgToken& offset, TRAPS) {
twisti@2903 1754 _strbuf.print("%s(%s, %s", Bytecodes::name(op), string(base), string(offset));
twisti@1568 1755 if (tk != NULL) {
twisti@1568 1756 _strbuf.print(", ");
twisti@1568 1757 put_type_name(type, tk, &_strbuf);
twisti@1568 1758 }
twisti@1568 1759 _strbuf.print(")");
twisti@1568 1760 return maybe_make_temp("fetch", type, "x");
twisti@1568 1761 }
twisti@1568 1762 virtual ArgToken make_invoke(methodOop m, vmIntrinsics::ID iid,
twisti@1568 1763 Bytecodes::Code op, bool tailcall,
twisti@1568 1764 int argc, ArgToken* argv, TRAPS) {
twisti@2903 1765 Symbol* name;
twisti@2903 1766 Symbol* sig;
twisti@1568 1767 if (m != NULL) {
twisti@1568 1768 name = m->name();
twisti@1568 1769 sig = m->signature();
twisti@1568 1770 } else {
twisti@1568 1771 name = vmSymbols::symbol_at(vmIntrinsics::name_for(iid));
twisti@1568 1772 sig = vmSymbols::symbol_at(vmIntrinsics::signature_for(iid));
twisti@1568 1773 }
twisti@1568 1774 _strbuf.print("%s %s%s(", Bytecodes::name(op), name->as_C_string(), sig->as_C_string());
twisti@1568 1775 for (int i = 0; i < argc; i++) {
twisti@2903 1776 _strbuf.print("%s%s", (i > 0 ? ", " : ""), string(argv[i]));
twisti@1568 1777 }
twisti@1568 1778 _strbuf.print(")");
twisti@1568 1779 if (!tailcall) {
twisti@1568 1780 BasicType rt = char2type(sig->byte_at(sig->utf8_length()-1));
twisti@1568 1781 if (rt == T_ILLEGAL) rt = T_OBJECT; // ';' at the end of '(...)L...;'
twisti@1568 1782 return maybe_make_temp("invoke", rt, "x");
twisti@1568 1783 } else {
twisti@1568 1784 const char* ret = strbuf();
twisti@1568 1785 _out->print(_verbose ? "\n return " : " ");
twisti@1568 1786 _out->print("%s", ret);
twisti@1568 1787 _out->print(_verbose ? "\n}\n" : " }");
twisti@1568 1788 }
twisti@1568 1789 return ArgToken();
twisti@1568 1790 }
twisti@1568 1791
twisti@1568 1792 virtual void set_method_handle(oop mh) {
twisti@1568 1793 if (WizardMode && Verbose) {
twisti@1568 1794 tty->print("\n--- next target: ");
twisti@1568 1795 mh->print();
twisti@1568 1796 }
twisti@1568 1797 }
twisti@1568 1798
twisti@1568 1799 static void print(Handle root, bool verbose, outputStream* out, TRAPS) {
twisti@1568 1800 ResourceMark rm;
twisti@1568 1801 MethodHandlePrinter printer(root, verbose, out, CHECK);
twisti@1568 1802 printer.walk(CHECK);
twisti@1568 1803 out->print("\n");
twisti@1568 1804 }
twisti@1568 1805 static void print(Handle root, bool verbose = Verbose, outputStream* out = tty) {
never@2937 1806 Thread* THREAD = Thread::current();
twisti@1568 1807 ResourceMark rm;
twisti@1568 1808 MethodHandlePrinter printer(root, verbose, out, THREAD);
twisti@1568 1809 if (!HAS_PENDING_EXCEPTION)
twisti@1568 1810 printer.walk(THREAD);
twisti@1568 1811 if (HAS_PENDING_EXCEPTION) {
twisti@1568 1812 oop ex = PENDING_EXCEPTION;
twisti@1568 1813 CLEAR_PENDING_EXCEPTION;
twisti@2903 1814 out->print(" *** ");
twisti@2903 1815 if (printer.lose_message() != NULL) out->print("%s ", printer.lose_message());
twisti@2903 1816 out->print("}");
twisti@1568 1817 }
twisti@1568 1818 out->print("\n");
twisti@1568 1819 }
twisti@1568 1820 };
twisti@1568 1821
twisti@1568 1822 extern "C"
twisti@1568 1823 void print_method_handle(oop mh) {
jrose@2148 1824 if (!mh->is_oop()) {
twisti@2903 1825 tty->print_cr("*** not a method handle: "PTR_FORMAT, (intptr_t)mh);
jrose@2639 1826 } else if (java_lang_invoke_MethodHandle::is_instance(mh)) {
twisti@2903 1827 MethodHandlePrinter::print(mh);
twisti@1568 1828 } else {
twisti@1568 1829 tty->print("*** not a method handle: ");
twisti@1568 1830 mh->print();
twisti@1568 1831 }
twisti@1568 1832 }
twisti@1568 1833
twisti@1568 1834 #endif // PRODUCT

mercurial