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