methodHandleWalk.cpp 48.6 KB
Newer Older
1
/*
2
 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24
 *
 */

25 26 27 28 29
#include "precompiled.hpp"
#include "interpreter/rewriter.hpp"
#include "memory/oopFactory.hpp"
#include "prims/methodHandleWalk.hpp"

30 31 32 33
/*
 * JSR 292 reference implementation: method handle structure analysis
 */

34 35 36 37

// -----------------------------------------------------------------------------
// MethodHandleChain

38
void MethodHandleChain::set_method_handle(Handle mh, TRAPS) {
39
  if (!java_lang_invoke_MethodHandle::is_instance(mh()))  lose("bad method handle", CHECK);
40 41 42 43 44 45 46 47 48 49

  // set current method handle and unpack partially
  _method_handle = mh;
  _is_last       = false;
  _is_bound      = false;
  _arg_slot      = -1;
  _arg_type      = T_VOID;
  _conversion    = -1;
  _last_invoke   = Bytecodes::_nop;  //arbitrary non-garbage

50
  if (java_lang_invoke_DirectMethodHandle::is_instance(mh())) {
51 52 53
    set_last_method(mh(), THREAD);
    return;
  }
54
  if (java_lang_invoke_AdapterMethodHandle::is_instance(mh())) {
55 56
    _conversion = AdapterMethodHandle_conversion();
    assert(_conversion != -1, "bad conv value");
57
    assert(java_lang_invoke_BoundMethodHandle::is_instance(mh()), "also BMH");
58
  }
59
  if (java_lang_invoke_BoundMethodHandle::is_instance(mh())) {
60 61 62 63
    if (!is_adapter())          // keep AMH and BMH separate in this model
      _is_bound = true;
    _arg_slot = BoundMethodHandle_vmargslot();
    oop target = MethodHandle_vmtarget_oop();
64
    if (!is_bound() || java_lang_invoke_MethodHandle::is_instance(target)) {
65 66
      _arg_type = compute_bound_arg_type(target, NULL, _arg_slot, CHECK);
    } else if (target != NULL && target->is_method()) {
67 68
      methodOop m = (methodOop) target;
      _arg_type = compute_bound_arg_type(NULL, m, _arg_slot, CHECK);
69 70 71 72 73 74 75 76 77 78 79 80 81
      set_last_method(mh(), CHECK);
    } else {
      _is_bound = false;  // lose!
    }
  }
  if (is_bound() && _arg_type == T_VOID) {
    lose("bad vmargslot", CHECK);
  }
  if (!is_bound() && !is_adapter()) {
    lose("unrecognized MH type", CHECK);
  }
}

82

83 84
void MethodHandleChain::set_last_method(oop target, TRAPS) {
  _is_last = true;
85 86
  KlassHandle receiver_limit; int flags = 0;
  _last_method = MethodHandles::decode_method(target, receiver_limit, flags);
87 88 89 90 91 92 93 94 95 96
  if ((flags & MethodHandles::_dmf_has_receiver) == 0)
    _last_invoke = Bytecodes::_invokestatic;
  else if ((flags & MethodHandles::_dmf_does_dispatch) == 0)
    _last_invoke = Bytecodes::_invokespecial;
  else if ((flags & MethodHandles::_dmf_from_interface) != 0)
    _last_invoke = Bytecodes::_invokeinterface;
  else
    _last_invoke = Bytecodes::_invokevirtual;
}

97

98 99 100 101 102
BasicType MethodHandleChain::compute_bound_arg_type(oop target, methodOop m, int arg_slot, TRAPS) {
  // There is no direct indication of whether the argument is primitive or not.
  // It is implied by the _vmentry code, and by the MethodType of the target.
  BasicType arg_type = T_VOID;
  if (target != NULL) {
103
    oop mtype = java_lang_invoke_MethodHandle::type(target);
104 105
    int arg_num = MethodHandles::argument_slot_to_argnum(mtype, arg_slot);
    if (arg_num >= 0) {
106
      oop ptype = java_lang_invoke_MethodType::ptype(mtype, arg_num);
107 108 109 110 111 112 113 114 115 116 117 118 119
      arg_type = java_lang_Class::as_BasicType(ptype);
    }
  } else if (m != NULL) {
    // figure out the argument type from the slot
    // FIXME: make this explicit in the MH
    int cur_slot = m->size_of_parameters();
    if (arg_slot >= cur_slot)
      return T_VOID;
    if (!m->is_static()) {
      cur_slot -= type2size[T_OBJECT];
      if (cur_slot == arg_slot)
        return T_OBJECT;
    }
120
    ResourceMark rm(THREAD);
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) {
      BasicType bt = ss.type();
      cur_slot -= type2size[bt];
      if (cur_slot <= arg_slot) {
        if (cur_slot == arg_slot)
          arg_type = bt;
        break;
      }
    }
  }
  if (arg_type == T_ARRAY)
    arg_type = T_OBJECT;
  return arg_type;
}

136

137 138 139 140 141 142 143 144 145
void MethodHandleChain::lose(const char* msg, TRAPS) {
  _lose_message = msg;
  if (!THREAD->is_Java_thread() || ((JavaThread*)THREAD)->thread_state() != _thread_in_vm) {
    // throw a preallocated exception
    THROW_OOP(Universe::virtual_machine_error_instance());
  }
  THROW_MSG(vmSymbols::java_lang_InternalError(), msg);
}

146 147 148 149

// -----------------------------------------------------------------------------
// MethodHandleWalker

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
Bytecodes::Code MethodHandleWalker::conversion_code(BasicType src, BasicType dest) {
  if (is_subword_type(src)) {
    src = T_INT;          // all subword src types act like int
  }
  if (src == dest) {
    return Bytecodes::_nop;
  }

#define SRC_DEST(s,d) (((int)(s) << 4) + (int)(d))
  switch (SRC_DEST(src, dest)) {
  case SRC_DEST(T_INT, T_LONG):           return Bytecodes::_i2l;
  case SRC_DEST(T_INT, T_FLOAT):          return Bytecodes::_i2f;
  case SRC_DEST(T_INT, T_DOUBLE):         return Bytecodes::_i2d;
  case SRC_DEST(T_INT, T_BYTE):           return Bytecodes::_i2b;
  case SRC_DEST(T_INT, T_CHAR):           return Bytecodes::_i2c;
  case SRC_DEST(T_INT, T_SHORT):          return Bytecodes::_i2s;

  case SRC_DEST(T_LONG, T_INT):           return Bytecodes::_l2i;
  case SRC_DEST(T_LONG, T_FLOAT):         return Bytecodes::_l2f;
  case SRC_DEST(T_LONG, T_DOUBLE):        return Bytecodes::_l2d;

  case SRC_DEST(T_FLOAT, T_INT):          return Bytecodes::_f2i;
  case SRC_DEST(T_FLOAT, T_LONG):         return Bytecodes::_f2l;
  case SRC_DEST(T_FLOAT, T_DOUBLE):       return Bytecodes::_f2d;

  case SRC_DEST(T_DOUBLE, T_INT):         return Bytecodes::_d2i;
  case SRC_DEST(T_DOUBLE, T_LONG):        return Bytecodes::_d2l;
  case SRC_DEST(T_DOUBLE, T_FLOAT):       return Bytecodes::_d2f;
  }
#undef SRC_DEST

  // cannot do it in one step, or at all
  return Bytecodes::_illegal;
}

185 186 187 188

// -----------------------------------------------------------------------------
// MethodHandleWalker::walk
//
189 190
MethodHandleWalker::ArgToken
MethodHandleWalker::walk(TRAPS) {
191 192 193
  ArgToken empty = ArgToken();  // Empty return value.

  walk_incoming_state(CHECK_(empty));
194 195 196 197 198 199 200 201 202 203 204

  for (;;) {
    set_method_handle(chain().method_handle_oop());

    assert(_outgoing_argc == argument_count_slow(), "empty slots under control");

    if (chain().is_adapter()) {
      int conv_op = chain().adapter_conversion_op();
      int arg_slot = chain().adapter_arg_slot();
      SlotState* arg_state = slot_state(arg_slot);
      if (arg_state == NULL
205
          && conv_op > java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW) {
206
        lose("bad argument index", CHECK_(empty));
207 208 209 210
      }

      // perform the adapter action
      switch (chain().adapter_conversion_op()) {
211
      case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY:
212 213 214
        // No changes to arguments; pass the bits through.
        break;

215
      case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW: {
216 217 218 219
        // To keep the verifier happy, emit bitwise ("raw") conversions as needed.
        // See MethodHandles::same_basic_type_for_arguments for allowed conversions.
        Handle incoming_mtype(THREAD, chain().method_type_oop());
        oop outgoing_mh_oop = chain().vmtarget_oop();
220
        if (!java_lang_invoke_MethodHandle::is_instance(outgoing_mh_oop))
221
          lose("outgoing target not a MethodHandle", CHECK_(empty));
222
        Handle outgoing_mtype(THREAD, java_lang_invoke_MethodHandle::type(outgoing_mh_oop));
223 224
        outgoing_mh_oop = NULL;  // GC safety

225 226
        int nptypes = java_lang_invoke_MethodType::ptype_count(outgoing_mtype());
        if (nptypes != java_lang_invoke_MethodType::ptype_count(incoming_mtype()))
227 228 229 230 231 232 233 234 235
          lose("incoming and outgoing parameter count do not agree", CHECK_(empty));

        for (int i = 0, slot = _outgoing.length() - 1; slot >= 0; slot--) {
          SlotState* arg_state = slot_state(slot);
          if (arg_state->_type == T_VOID)  continue;
          ArgToken arg = _outgoing.at(slot)._arg;

          klassOop  in_klass  = NULL;
          klassOop  out_klass = NULL;
236 237
          BasicType inpbt  = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(incoming_mtype(), i), &in_klass);
          BasicType outpbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(outgoing_mtype(), i), &out_klass);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
          assert(inpbt == arg.basic_type(), "sanity");

          if (inpbt != outpbt) {
            vmIntrinsics::ID iid = vmIntrinsics::for_raw_conversion(inpbt, outpbt);
            if (iid == vmIntrinsics::_none) {
              lose("no raw conversion method", CHECK_(empty));
            }
            ArgToken arglist[2];
            arglist[0] = arg;         // outgoing 'this'
            arglist[1] = ArgToken();  // sentinel
            arg = make_invoke(NULL, iid, Bytecodes::_invokestatic, false, 1, &arglist[0], CHECK_(empty));
            change_argument(inpbt, slot, outpbt, arg);
          }

          i++;  // We need to skip void slots at the top of the loop.
        }

255 256
        BasicType inrbt  = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(incoming_mtype()));
        BasicType outrbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(outgoing_mtype()));
257 258 259 260 261 262 263 264 265 266 267
        if (inrbt != outrbt) {
          if (inrbt == T_INT && outrbt == T_VOID) {
            // See comments in MethodHandles::same_basic_type_for_arguments.
          } else {
            assert(false, "IMPLEMENT ME");
            lose("no raw conversion method", CHECK_(empty));
          }
        }
        break;
      }

268
      case java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST: {
269 270 271 272 273
        // checkcast the Nth outgoing argument in place
        klassOop dest_klass = NULL;
        BasicType dest = java_lang_Class::as_BasicType(chain().adapter_arg_oop(), &dest_klass);
        assert(dest == T_OBJECT, "");
        assert(dest == arg_state->_type, "");
274 275 276
        ArgToken arg = arg_state->_arg;
        ArgToken new_arg = make_conversion(T_OBJECT, dest_klass, Bytecodes::_checkcast, arg, CHECK_(empty));
        assert(arg.index() == new_arg.index(), "should be the same index");
277 278 279 280
        debug_only(dest_klass = (klassOop)badOop);
        break;
      }

281
      case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM: {
282 283 284 285 286 287 288 289
        // i2l, etc., on the Nth outgoing argument in place
        BasicType src = chain().adapter_conversion_src_type(),
                  dest = chain().adapter_conversion_dest_type();
        Bytecodes::Code bc = conversion_code(src, dest);
        ArgToken arg = arg_state->_arg;
        if (bc == Bytecodes::_nop) {
          break;
        } else if (bc != Bytecodes::_illegal) {
290
          arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
291 292 293
        } else if (is_subword_type(dest)) {
          bc = conversion_code(src, T_INT);
          if (bc != Bytecodes::_illegal) {
294
            arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
295
            bc = conversion_code(T_INT, dest);
296
            arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
297 298 299
          }
        }
        if (bc == Bytecodes::_illegal) {
300
          lose("bad primitive conversion", CHECK_(empty));
301 302 303 304 305
        }
        change_argument(src, arg_slot, dest, arg);
        break;
      }

306
      case java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM: {
307 308 309 310
        // checkcast to wrapper type & call intValue, etc.
        BasicType dest = chain().adapter_conversion_dest_type();
        ArgToken arg = arg_state->_arg;
        arg = make_conversion(T_OBJECT, SystemDictionary::box_klass(dest),
311
                              Bytecodes::_checkcast, arg, CHECK_(empty));
312 313
        vmIntrinsics::ID unboxer = vmIntrinsics::for_unboxing(dest);
        if (unboxer == vmIntrinsics::_none) {
314
          lose("no unboxing method", CHECK_(empty));
315 316
        }
        ArgToken arglist[2];
317 318 319
        arglist[0] = arg;         // outgoing 'this'
        arglist[1] = ArgToken();  // sentinel
        arg = make_invoke(NULL, unboxer, Bytecodes::_invokevirtual, false, 1, &arglist[0], CHECK_(empty));
320 321 322 323
        change_argument(T_OBJECT, arg_slot, dest, arg);
        break;
      }

324
      case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF: {
325 326 327 328 329
        // call wrapper type.valueOf
        BasicType src = chain().adapter_conversion_src_type();
        ArgToken arg = arg_state->_arg;
        vmIntrinsics::ID boxer = vmIntrinsics::for_boxing(src);
        if (boxer == vmIntrinsics::_none) {
330
          lose("no boxing method", CHECK_(empty));
331 332
        }
        ArgToken arglist[2];
333 334
        arglist[0] = arg;         // outgoing value
        arglist[1] = ArgToken();  // sentinel
335
        arg = make_invoke(NULL, boxer, Bytecodes::_invokevirtual, false, 1, &arglist[0], CHECK_(empty));
336 337 338 339
        change_argument(src, arg_slot, T_OBJECT, arg);
        break;
      }

340
      case java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS: {
341 342
        int dest_arg_slot = chain().adapter_conversion_vminfo();
        if (!slot_has_argument(dest_arg_slot)) {
343
          lose("bad swap index", CHECK_(empty));
344 345 346 347 348 349 350 351 352
        }
        // a simple swap between two arguments
        SlotState* dest_arg_state = slot_state(dest_arg_slot);
        SlotState temp = (*dest_arg_state);
        (*dest_arg_state) = (*arg_state);
        (*arg_state) = temp;
        break;
      }

353
      case java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS: {
354 355
        int dest_arg_slot = chain().adapter_conversion_vminfo();
        if (!slot_has_argument(dest_arg_slot) || arg_slot == dest_arg_slot) {
356
          lose("bad rotate index", CHECK_(empty));
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
        }
        SlotState* dest_arg_state = slot_state(dest_arg_slot);
        // Rotate the source argument (plus following N slots) into the
        // position occupied by the dest argument (plus following N slots).
        int rotate_count = type2size[dest_arg_state->_type];
        // (no other rotate counts are currently supported)
        if (arg_slot < dest_arg_slot) {
          for (int i = 0; i < rotate_count; i++) {
            SlotState temp = _outgoing.at(arg_slot);
            _outgoing.remove_at(arg_slot);
            _outgoing.insert_before(dest_arg_slot + rotate_count - 1, temp);
          }
        } else { // arg_slot > dest_arg_slot
          for (int i = 0; i < rotate_count; i++) {
            SlotState temp = _outgoing.at(arg_slot + rotate_count - 1);
            _outgoing.remove_at(arg_slot + rotate_count - 1);
            _outgoing.insert_before(dest_arg_slot, temp);
          }
        }
        break;
      }

379
      case java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS: {
380 381
        int dup_slots = chain().adapter_conversion_stack_pushes();
        if (dup_slots <= 0) {
382
          lose("bad dup count", CHECK_(empty));
383 384 385 386 387 388 389 390 391 392
        }
        for (int i = 0; i < dup_slots; i++) {
          SlotState* dup = slot_state(arg_slot + 2*i);
          if (dup == NULL)              break;  // safety net
          if (dup->_type != T_VOID)     _outgoing_argc += 1;
          _outgoing.insert_before(i, (*dup));
        }
        break;
      }

393
      case java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS: {
394 395
        int drop_slots = -chain().adapter_conversion_stack_pushes();
        if (drop_slots <= 0) {
396
          lose("bad drop count", CHECK_(empty));
397 398 399 400 401 402 403 404 405 406
        }
        for (int i = 0; i < drop_slots; i++) {
          SlotState* drop = slot_state(arg_slot);
          if (drop == NULL)             break;  // safety net
          if (drop->_type != T_VOID)    _outgoing_argc -= 1;
          _outgoing.remove_at(arg_slot);
        }
        break;
      }

407
      case java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS: { //NYI, may GC
408
        lose("unimplemented", CHECK_(empty));
409 410 411
        break;
      }

412
      case java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS: {
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
        klassOop array_klass_oop = NULL;
        BasicType array_type = java_lang_Class::as_BasicType(chain().adapter_arg_oop(),
                                                             &array_klass_oop);
        assert(array_type == T_OBJECT, "");
        assert(Klass::cast(array_klass_oop)->oop_is_array(), "");
        arrayKlassHandle array_klass(THREAD, array_klass_oop);
        debug_only(array_klass_oop = (klassOop)badOop);

        klassOop element_klass_oop = NULL;
        BasicType element_type = java_lang_Class::as_BasicType(array_klass->component_mirror(),
                                                               &element_klass_oop);
        KlassHandle element_klass(THREAD, element_klass_oop);
        debug_only(element_klass_oop = (klassOop)badOop);

        // Fetch the argument, which we will cast to the required array type.
        assert(arg_state->_type == T_OBJECT, "");
        ArgToken array_arg = arg_state->_arg;
430 431
        array_arg = make_conversion(T_OBJECT, array_klass(), Bytecodes::_checkcast, array_arg, CHECK_(empty));
        change_argument(T_OBJECT, arg_slot, T_VOID, ArgToken(tt_void));
432 433 434 435 436 437 438 439 440

        // Check the required length.
        int spread_slots = 1 + chain().adapter_conversion_stack_pushes();
        int spread_length = spread_slots;
        if (type2size[element_type] == 2) {
          if (spread_slots % 2 != 0)  spread_slots = -1;  // force error
          spread_length = spread_slots / 2;
        }
        if (spread_slots < 0) {
441
          lose("bad spread length", CHECK_(empty));
442 443 444
        }

        jvalue   length_jvalue;  length_jvalue.i = spread_length;
445
        ArgToken length_arg = make_prim_constant(T_INT, &length_jvalue, CHECK_(empty));
446 447
        // Call a built-in method known to the JVM to validate the length.
        ArgToken arglist[3];
448 449 450
        arglist[0] = array_arg;   // value to check
        arglist[1] = length_arg;  // length to check
        arglist[2] = ArgToken();  // sentinel
451
        make_invoke(NULL, vmIntrinsics::_checkSpreadArgument,
452
                    Bytecodes::_invokestatic, false, 3, &arglist[0], CHECK_(empty));
453 454 455 456

        // Spread out the array elements.
        Bytecodes::Code aload_op = Bytecodes::_aaload;
        if (element_type != T_OBJECT) {
457
          lose("primitive array NYI", CHECK_(empty));
458 459 460 461
        }
        int ap = arg_slot;
        for (int i = 0; i < spread_length; i++) {
          jvalue   offset_jvalue;  offset_jvalue.i = i;
462 463
          ArgToken offset_arg = make_prim_constant(T_INT, &offset_jvalue, CHECK_(empty));
          ArgToken element_arg = make_fetch(element_type, element_klass(), aload_op, array_arg, offset_arg, CHECK_(empty));
464 465 466 467 468 469
          change_argument(T_VOID, ap, element_type, element_arg);
          ap += type2size[element_type];
        }
        break;
      }

470 471
      case java_lang_invoke_AdapterMethodHandle::OP_FLYBY: //NYI, runs Java code
      case java_lang_invoke_AdapterMethodHandle::OP_RICOCHET: //NYI, runs Java code
472
        lose("unimplemented", CHECK_(empty));
473 474 475
        break;

      default:
476
        lose("bad adapter conversion", CHECK_(empty));
477 478 479 480 481 482 483 484 485
        break;
      }
    }

    if (chain().is_bound()) {
      // push a new argument
      BasicType arg_type  = chain().bound_arg_type();
      jint      arg_slot  = chain().bound_arg_slot();
      oop       arg_oop   = chain().bound_arg_oop();
486
      ArgToken  arg;
487
      if (arg_type == T_OBJECT) {
488
        arg = make_oop_constant(arg_oop, CHECK_(empty));
489 490 491 492
      } else {
        jvalue arg_value;
        BasicType bt = java_lang_boxing_object::get_value(arg_oop, &arg_value);
        if (bt == arg_type) {
493
          arg = make_prim_constant(arg_type, &arg_value, CHECK_(empty));
494
        } else {
495
          lose("bad bound value", CHECK_(empty));
496 497 498 499 500 501 502 503
        }
      }
      debug_only(arg_oop = badOop);
      change_argument(T_VOID, arg_slot, arg_type, arg);
    }

    // this test must come after the body of the loop
    if (!chain().is_last()) {
504
      chain().next(CHECK_(empty));
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
    } else {
      break;
    }
  }

  // finish the sequence with a tail-call to the ultimate target
  // parameters are passed in logical order (recv 1st), not slot order
  ArgToken* arglist = NEW_RESOURCE_ARRAY(ArgToken, _outgoing.length() + 1);
  int ap = 0;
  for (int i = _outgoing.length() - 1; i >= 0; i--) {
    SlotState* arg_state = slot_state(i);
    if (arg_state->_type == T_VOID)  continue;
    arglist[ap++] = _outgoing.at(i)._arg;
  }
  assert(ap == _outgoing_argc, "");
520
  arglist[ap] = ArgToken();  // add a sentinel, for the sake of asserts
521 522 523 524 525 526
  return make_invoke(chain().last_method_oop(),
                     vmIntrinsics::_none,
                     chain().last_invoke_code(), true,
                     ap, arglist, THREAD);
}

527 528 529 530

// -----------------------------------------------------------------------------
// MethodHandleWalker::walk_incoming_state
//
531 532
void MethodHandleWalker::walk_incoming_state(TRAPS) {
  Handle mtype(THREAD, chain().method_type_oop());
533
  int nptypes = java_lang_invoke_MethodType::ptype_count(mtype());
534 535 536
  _outgoing_argc = nptypes;
  int argp = nptypes - 1;
  if (argp >= 0) {
537
    _outgoing.at_grow(argp, make_state(T_VOID, ArgToken(tt_void))); // presize
538 539 540 541
  }
  for (int i = 0; i < nptypes; i++) {
    klassOop  arg_type_klass = NULL;
    BasicType arg_type = java_lang_Class::as_BasicType(
542
                java_lang_invoke_MethodType::ptype(mtype(), i), &arg_type_klass);
543 544 545
    int index = new_local_index(arg_type);
    ArgToken arg = make_parameter(arg_type, arg_type_klass, index, CHECK);
    debug_only(arg_type_klass = (klassOop) NULL);
546 547 548
    _outgoing.at_put(argp, make_state(arg_type, arg));
    if (type2size[arg_type] == 2) {
      // add the extra slot, so we can model the JVM stack
549
      _outgoing.insert_before(argp+1, make_state(T_VOID, ArgToken(tt_void)));
550 551 552 553 554 555
    }
    --argp;
  }
  // call make_parameter at the end of the list for the return type
  klassOop  ret_type_klass = NULL;
  BasicType ret_type = java_lang_Class::as_BasicType(
556
              java_lang_invoke_MethodType::rtype(mtype()), &ret_type_klass);
557 558 559 560
  ArgToken  ret = make_parameter(ret_type, ret_type_klass, -1, CHECK);
  // ignore ret; client can catch it if needed
}

561 562 563 564 565 566

// -----------------------------------------------------------------------------
// MethodHandleWalker::change_argument
//
// This is messy because some kinds of arguments are paired with
// companion slots containing an empty value.
567
void MethodHandleWalker::change_argument(BasicType old_type, int slot, BasicType new_type,
568
                                         const ArgToken& new_arg) {
569 570 571 572 573 574
  int old_size = type2size[old_type];
  int new_size = type2size[new_type];
  if (old_size == new_size) {
    // simple case first
    _outgoing.at_put(slot, make_state(new_type, new_arg));
  } else if (old_size > new_size) {
575
    for (int i = old_size - 1; i >= new_size; i--) {
576 577 578 579 580 581 582 583 584
      assert((i != 0) == (_outgoing.at(slot + i)._type == T_VOID), "");
      _outgoing.remove_at(slot + i);
    }
    if (new_size > 0)
      _outgoing.at_put(slot, make_state(new_type, new_arg));
    else
      _outgoing_argc -= 1;      // deleted a real argument
  } else {
    for (int i = old_size; i < new_size; i++) {
585
      _outgoing.insert_before(slot + i, make_state(T_VOID, ArgToken(tt_void)));
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
    }
    _outgoing.at_put(slot, make_state(new_type, new_arg));
    if (old_size == 0)
      _outgoing_argc += 1;      // inserted a real argument
  }
}


#ifdef ASSERT
int MethodHandleWalker::argument_count_slow() {
  int args_seen = 0;
  for (int i = _outgoing.length() - 1; i >= 0; i--) {
    if (_outgoing.at(i)._type != T_VOID) {
      ++args_seen;
    }
  }
  return args_seen;
}
#endif


607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
// -----------------------------------------------------------------------------
// MethodHandleCompiler

MethodHandleCompiler::MethodHandleCompiler(Handle root, methodHandle callee, bool is_invokedynamic, TRAPS)
  : MethodHandleWalker(root, is_invokedynamic, THREAD),
    _callee(callee),
    _thread(THREAD),
    _bytecode(THREAD, 50),
    _constants(THREAD, 10),
    _cur_stack(0),
    _max_stack(0),
    _rtype(T_ILLEGAL)
{

  // Element zero is always the null constant.
  (void) _constants.append(NULL);

  // Set name and signature index.
  _name_index      = cpool_symbol_put(_callee->name());
  _signature_index = cpool_symbol_put(_callee->signature());

  // Get return type klass.
  Handle first_mtype(THREAD, chain().method_type_oop());
  // _rklass is NULL for primitives.
631
  _rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(first_mtype()), &_rklass);
632
  if (_rtype == T_ARRAY)  _rtype = T_OBJECT;
633 634 635 636 637 638 639 640 641 642 643 644

  int params = _callee->size_of_parameters();  // Incoming arguments plus receiver.
  _num_params = for_invokedynamic() ? params - 1 : params;  // XXX Check if callee is static?
}


// -----------------------------------------------------------------------------
// MethodHandleCompiler::compile
//
// Compile this MethodHandle into a bytecode adapter and return a
// methodOop.
methodHandle MethodHandleCompiler::compile(TRAPS) {
645
  assert(_thread == THREAD, "must be same thread");
646 647 648 649
  methodHandle nullHandle;
  (void) walk(CHECK_(nullHandle));
  return get_method_oop(CHECK_(nullHandle));
}
650

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732

void MethodHandleCompiler::emit_bc(Bytecodes::Code op, int index) {
  Bytecodes::check(op);  // Are we legal?

  switch (op) {
  // b
  case Bytecodes::_aconst_null:
  case Bytecodes::_iconst_m1:
  case Bytecodes::_iconst_0:
  case Bytecodes::_iconst_1:
  case Bytecodes::_iconst_2:
  case Bytecodes::_iconst_3:
  case Bytecodes::_iconst_4:
  case Bytecodes::_iconst_5:
  case Bytecodes::_lconst_0:
  case Bytecodes::_lconst_1:
  case Bytecodes::_fconst_0:
  case Bytecodes::_fconst_1:
  case Bytecodes::_fconst_2:
  case Bytecodes::_dconst_0:
  case Bytecodes::_dconst_1:
  case Bytecodes::_iload_0:
  case Bytecodes::_iload_1:
  case Bytecodes::_iload_2:
  case Bytecodes::_iload_3:
  case Bytecodes::_lload_0:
  case Bytecodes::_lload_1:
  case Bytecodes::_lload_2:
  case Bytecodes::_lload_3:
  case Bytecodes::_fload_0:
  case Bytecodes::_fload_1:
  case Bytecodes::_fload_2:
  case Bytecodes::_fload_3:
  case Bytecodes::_dload_0:
  case Bytecodes::_dload_1:
  case Bytecodes::_dload_2:
  case Bytecodes::_dload_3:
  case Bytecodes::_aload_0:
  case Bytecodes::_aload_1:
  case Bytecodes::_aload_2:
  case Bytecodes::_aload_3:
  case Bytecodes::_istore_0:
  case Bytecodes::_istore_1:
  case Bytecodes::_istore_2:
  case Bytecodes::_istore_3:
  case Bytecodes::_lstore_0:
  case Bytecodes::_lstore_1:
  case Bytecodes::_lstore_2:
  case Bytecodes::_lstore_3:
  case Bytecodes::_fstore_0:
  case Bytecodes::_fstore_1:
  case Bytecodes::_fstore_2:
  case Bytecodes::_fstore_3:
  case Bytecodes::_dstore_0:
  case Bytecodes::_dstore_1:
  case Bytecodes::_dstore_2:
  case Bytecodes::_dstore_3:
  case Bytecodes::_astore_0:
  case Bytecodes::_astore_1:
  case Bytecodes::_astore_2:
  case Bytecodes::_astore_3:
  case Bytecodes::_i2l:
  case Bytecodes::_i2f:
  case Bytecodes::_i2d:
  case Bytecodes::_i2b:
  case Bytecodes::_i2c:
  case Bytecodes::_i2s:
  case Bytecodes::_l2i:
  case Bytecodes::_l2f:
  case Bytecodes::_l2d:
  case Bytecodes::_f2i:
  case Bytecodes::_f2l:
  case Bytecodes::_f2d:
  case Bytecodes::_d2i:
  case Bytecodes::_d2l:
  case Bytecodes::_d2f:
  case Bytecodes::_ireturn:
  case Bytecodes::_lreturn:
  case Bytecodes::_freturn:
  case Bytecodes::_dreturn:
  case Bytecodes::_areturn:
  case Bytecodes::_return:
733
    assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_b, "wrong bytecode format");
734 735 736 737 738
    _bytecode.push(op);
    break;

  // bi
  case Bytecodes::_ldc:
739 740 741 742 743 744
    assert(Bytecodes::format_bits(op, false) == (Bytecodes::_fmt_b|Bytecodes::_fmt_has_k), "wrong bytecode format");
    assert((char) index == index, "index does not fit in 8-bit");
    _bytecode.push(op);
    _bytecode.push(index);
    break;

745 746 747 748 749 750 751 752 753 754
  case Bytecodes::_iload:
  case Bytecodes::_lload:
  case Bytecodes::_fload:
  case Bytecodes::_dload:
  case Bytecodes::_aload:
  case Bytecodes::_istore:
  case Bytecodes::_lstore:
  case Bytecodes::_fstore:
  case Bytecodes::_dstore:
  case Bytecodes::_astore:
755
    assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bi, "wrong bytecode format");
756 757 758 759 760
    assert((char) index == index, "index does not fit in 8-bit");
    _bytecode.push(op);
    _bytecode.push(index);
    break;

761 762
  // bkk
  case Bytecodes::_ldc_w:
763 764
  case Bytecodes::_ldc2_w:
  case Bytecodes::_checkcast:
765
    assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bkk, "wrong bytecode format");
766 767 768 769 770 771
    assert((short) index == index, "index does not fit in 16-bit");
    _bytecode.push(op);
    _bytecode.push(index >> 8);
    _bytecode.push(index);
    break;

772
  // bJJ
773 774 775
  case Bytecodes::_invokestatic:
  case Bytecodes::_invokespecial:
  case Bytecodes::_invokevirtual:
776
    assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
777 778 779 780 781 782 783 784
    assert((short) index == index, "index does not fit in 16-bit");
    _bytecode.push(op);
    _bytecode.push(index >> 8);
    _bytecode.push(index);
    break;

  default:
    ShouldNotReachHere();
785
  }
786 787
}

788

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
void MethodHandleCompiler::emit_load(BasicType bt, int index) {
  if (index <= 3) {
    switch (bt) {
    case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
    case T_INT:    emit_bc(Bytecodes::cast(Bytecodes::_iload_0 + index)); break;
    case T_LONG:   emit_bc(Bytecodes::cast(Bytecodes::_lload_0 + index)); break;
    case T_FLOAT:  emit_bc(Bytecodes::cast(Bytecodes::_fload_0 + index)); break;
    case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dload_0 + index)); break;
    case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_aload_0 + index)); break;
    default:
      ShouldNotReachHere();
    }
  }
  else {
    switch (bt) {
    case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
    case T_INT:    emit_bc(Bytecodes::_iload, index); break;
    case T_LONG:   emit_bc(Bytecodes::_lload, index); break;
    case T_FLOAT:  emit_bc(Bytecodes::_fload, index); break;
    case T_DOUBLE: emit_bc(Bytecodes::_dload, index); break;
    case T_OBJECT: emit_bc(Bytecodes::_aload, index); break;
    default:
      ShouldNotReachHere();
    }
  }
  stack_push(bt);
815 816
}

817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
void MethodHandleCompiler::emit_store(BasicType bt, int index) {
  if (index <= 3) {
    switch (bt) {
    case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
    case T_INT:    emit_bc(Bytecodes::cast(Bytecodes::_istore_0 + index)); break;
    case T_LONG:   emit_bc(Bytecodes::cast(Bytecodes::_lstore_0 + index)); break;
    case T_FLOAT:  emit_bc(Bytecodes::cast(Bytecodes::_fstore_0 + index)); break;
    case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dstore_0 + index)); break;
    case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_astore_0 + index)); break;
    default:
      ShouldNotReachHere();
    }
  }
  else {
    switch (bt) {
    case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
    case T_INT:    emit_bc(Bytecodes::_istore, index); break;
    case T_LONG:   emit_bc(Bytecodes::_lstore, index); break;
    case T_FLOAT:  emit_bc(Bytecodes::_fstore, index); break;
    case T_DOUBLE: emit_bc(Bytecodes::_dstore, index); break;
    case T_OBJECT: emit_bc(Bytecodes::_astore, index); break;
    default:
      ShouldNotReachHere();
    }
  }
  stack_pop(bt);
}


void MethodHandleCompiler::emit_load_constant(ArgToken arg) {
  BasicType bt = arg.basic_type();
  switch (bt) {
  case T_INT: {
    jint value = arg.get_jint();
    if (-1 <= value && value <= 5)
      emit_bc(Bytecodes::cast(Bytecodes::_iconst_0 + value));
    else
      emit_bc(Bytecodes::_ldc, cpool_int_put(value));
    break;
  }
  case T_LONG: {
    jlong value = arg.get_jlong();
    if (0 <= value && value <= 1)
      emit_bc(Bytecodes::cast(Bytecodes::_lconst_0 + (int) value));
    else
      emit_bc(Bytecodes::_ldc2_w, cpool_long_put(value));
    break;
  }
  case T_FLOAT: {
    jfloat value  = arg.get_jfloat();
    if (value == 0.0 || value == 1.0 || value == 2.0)
      emit_bc(Bytecodes::cast(Bytecodes::_fconst_0 + (int) value));
    else
      emit_bc(Bytecodes::_ldc, cpool_float_put(value));
    break;
  }
  case T_DOUBLE: {
    jdouble value = arg.get_jdouble();
    if (value == 0.0 || value == 1.0)
      emit_bc(Bytecodes::cast(Bytecodes::_dconst_0 + (int) value));
    else
      emit_bc(Bytecodes::_ldc2_w, cpool_double_put(value));
    break;
  }
  case T_OBJECT: {
    Handle value = arg.object();
    if (value.is_null())
      emit_bc(Bytecodes::_aconst_null);
    else
      emit_bc(Bytecodes::_ldc, cpool_object_put(value));
    break;
  }
  default:
    ShouldNotReachHere();
  }
  stack_push(bt);
}


896 897
MethodHandleWalker::ArgToken
MethodHandleCompiler::make_conversion(BasicType type, klassOop tk, Bytecodes::Code op,
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
                                      const ArgToken& src, TRAPS) {

  BasicType srctype = src.basic_type();
  int index = src.index();

  switch (op) {
  case Bytecodes::_i2l:
  case Bytecodes::_i2f:
  case Bytecodes::_i2d:
  case Bytecodes::_i2b:
  case Bytecodes::_i2c:
  case Bytecodes::_i2s:

  case Bytecodes::_l2i:
  case Bytecodes::_l2f:
  case Bytecodes::_l2d:

  case Bytecodes::_f2i:
  case Bytecodes::_f2l:
  case Bytecodes::_f2d:

  case Bytecodes::_d2i:
  case Bytecodes::_d2l:
  case Bytecodes::_d2f:
    emit_load(srctype, index);
    stack_pop(srctype);  // pop the src type
    emit_bc(op);
    stack_push(type);    // push the dest value
    if (srctype != type)
      index = new_local_index(type);
    emit_store(type, index);
    break;

  case Bytecodes::_checkcast:
    emit_load(srctype, index);
    emit_bc(op, cpool_klass_put(tk));
    emit_store(srctype, index);
    break;

  default:
    ShouldNotReachHere();
  }

  return make_parameter(type, tk, index, THREAD);
942 943
}

944 945 946 947 948 949 950 951

// -----------------------------------------------------------------------------
// MethodHandleCompiler
//

static jvalue zero_jvalue;

// Emit bytecodes for the given invoke instruction.
952 953 954 955 956
MethodHandleWalker::ArgToken
MethodHandleCompiler::make_invoke(methodOop m, vmIntrinsics::ID iid,
                                  Bytecodes::Code op, bool tailcall,
                                  int argc, MethodHandleWalker::ArgToken* argv,
                                  TRAPS) {
957 958 959
  if (m == NULL) {
    // Get the intrinsic methodOop.
    m = vmIntrinsics::method_for(iid);
960 961 962 963
    if (m == NULL) {
      ArgToken zero;
      lose(vmIntrinsics::name_at(iid), CHECK_(zero));
    }
964 965
  }

966 967 968
  klassOop  klass   = m->method_holder();
  Symbol* name      = m->name();
  Symbol* signature = m->signature();
969 970

  if (tailcall) {
971
    // Actually, in order to make these methods more recognizable,
972 973 974
    // let's put them in holder class MethodHandle.  That way stack
    // walkers and compiler heuristics can recognize them.
    _target_klass = SystemDictionary::MethodHandle_klass();
975 976 977 978
  }

  // Inline the method.
  InvocationCounter* ic = m->invocation_counter();
I
iveresov 已提交
979
  ic->set_carry_flag();
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

  for (int i = 0; i < argc; i++) {
    ArgToken arg = argv[i];
    TokenType tt = arg.token_type();
    BasicType bt = arg.basic_type();

    switch (tt) {
    case tt_parameter:
    case tt_temporary:
      emit_load(bt, arg.index());
      break;
    case tt_constant:
      emit_load_constant(arg);
      break;
    case tt_illegal:
      // Sentinel.
      assert(i == (argc - 1), "sentinel must be last entry");
      break;
    case tt_void:
    default:
      ShouldNotReachHere();
    }
  }

  // Populate constant pool.
  int name_index          = cpool_symbol_put(name);
  int signature_index     = cpool_symbol_put(signature);
  int name_and_type_index = cpool_name_and_type_put(name_index, signature_index);
  int klass_index         = cpool_klass_put(klass);
  int methodref_index     = cpool_methodref_put(klass_index, name_and_type_index);

  // Generate invoke.
1012 1013
  switch (op) {
  case Bytecodes::_invokestatic:
1014 1015 1016 1017
  case Bytecodes::_invokespecial:
  case Bytecodes::_invokevirtual:
    emit_bc(op, methodref_index);
    break;
1018
  case Bytecodes::_invokeinterface:
1019
    Unimplemented();
1020 1021 1022 1023 1024
    break;
  default:
    ShouldNotReachHere();
  }

1025 1026 1027
  // If tailcall, we have walked all the way to a direct method handle.
  // Otherwise, make a recursive call to some helper routine.
  BasicType rbt = m->result_type();
1028
  if (rbt == T_ARRAY)  rbt = T_OBJECT;
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
  ArgToken ret;
  if (tailcall) {
    if (rbt != _rtype) {
      if (rbt == T_VOID) {
        // push a zero of the right sort
        ArgToken zero;
        if (_rtype == T_OBJECT) {
          zero = make_oop_constant(NULL, CHECK_(zero));
        } else {
          zero = make_prim_constant(_rtype, &zero_jvalue, CHECK_(zero));
        }
        emit_load_constant(zero);
      } else if (_rtype == T_VOID) {
        // We'll emit a _return with something on the stack.
        // It's OK to ignore what's on the stack.
      } else {
        tty->print_cr("*** rbt=%d != rtype=%d", rbt, _rtype);
        assert(false, "IMPLEMENT ME");
      }
    }
    switch (_rtype) {
    case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
    case T_INT:    emit_bc(Bytecodes::_ireturn); break;
    case T_LONG:   emit_bc(Bytecodes::_lreturn); break;
    case T_FLOAT:  emit_bc(Bytecodes::_freturn); break;
    case T_DOUBLE: emit_bc(Bytecodes::_dreturn); break;
    case T_VOID:   emit_bc(Bytecodes::_return);  break;
    case T_OBJECT:
1057
      if (_rklass.not_null() && _rklass() != SystemDictionary::Object_klass())
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
        emit_bc(Bytecodes::_checkcast, cpool_klass_put(_rklass()));
      emit_bc(Bytecodes::_areturn);
      break;
    default: ShouldNotReachHere();
    }
    ret = ArgToken();  // Dummy return value.
  }
  else {
    stack_push(rbt);  // The return value is already pushed onto the stack.
    int index = new_local_index(rbt);
    switch (rbt) {
    case T_BOOLEAN: case T_BYTE: case T_CHAR:  case T_SHORT:
    case T_INT:     case T_LONG: case T_FLOAT: case T_DOUBLE:
    case T_OBJECT:
      emit_store(rbt, index);
      ret = ArgToken(tt_temporary, rbt, index);
      break;
    case T_VOID:
      ret = ArgToken(tt_void);
      break;
    default:
      ShouldNotReachHere();
    }
  }

  return ret;
1084 1085 1086 1087
}

MethodHandleWalker::ArgToken
MethodHandleCompiler::make_fetch(BasicType type, klassOop tk, Bytecodes::Code op,
1088 1089
                                 const MethodHandleWalker::ArgToken& base,
                                 const MethodHandleWalker::ArgToken& offset,
1090 1091
                                 TRAPS) {
  Unimplemented();
1092
  return ArgToken();
1093 1094 1095
}


1096
int MethodHandleCompiler::cpool_primitive_put(BasicType bt, jvalue* con) {
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
  jvalue con_copy;
  assert(bt < T_OBJECT, "");
  if (type2aelembytes(bt) < jintSize) {
    // widen to int
    con_copy = (*con);
    con = &con_copy;
    switch (bt) {
    case T_BOOLEAN: con->i = (con->z ? 1 : 0); break;
    case T_BYTE:    con->i = con->b;           break;
    case T_CHAR:    con->i = con->c;           break;
    case T_SHORT:   con->i = con->s;           break;
    default: ShouldNotReachHere();
    }
    bt = T_INT;
  }
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137

//   for (int i = 1, imax = _constants.length(); i < imax; i++) {
//     ConstantValue* con = _constants.at(i);
//     if (con != NULL && con->is_primitive() && con->_type == bt) {
//       bool match = false;
//       switch (type2size[bt]) {
//       case 1:  if (pcon->_value.i == con->i)  match = true;  break;
//       case 2:  if (pcon->_value.j == con->j)  match = true;  break;
//       }
//       if (match)
//         return i;
//     }
//   }
  ConstantValue* cv = new ConstantValue(bt, *con);
  int index = _constants.append(cv);

  // long and double entries take 2 slots, we add another empty entry.
  if (type2size[bt] == 2)
    (void) _constants.append(NULL);

  return index;
}


constantPoolHandle MethodHandleCompiler::get_constant_pool(TRAPS) const {
  constantPoolHandle nullHandle;
1138 1139 1140
  constantPoolOop cpool_oop = oopFactory::new_constantPool(_constants.length(),
                                                           oopDesc::IsSafeConc,
                                                           CHECK_(nullHandle));
1141 1142 1143 1144 1145 1146
  constantPoolHandle cpool(THREAD, cpool_oop);

  // Fill the real constant pool skipping the zero element.
  for (int i = 1; i < _constants.length(); i++) {
    ConstantValue* cv = _constants.at(i);
    switch (cv->tag()) {
1147
    case JVM_CONSTANT_Utf8:        cpool->symbol_at_put(       i, cv->symbol()                         ); break;
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
    case JVM_CONSTANT_Integer:     cpool->int_at_put(          i, cv->get_jint()                       ); break;
    case JVM_CONSTANT_Float:       cpool->float_at_put(        i, cv->get_jfloat()                     ); break;
    case JVM_CONSTANT_Long:        cpool->long_at_put(         i, cv->get_jlong()                      ); break;
    case JVM_CONSTANT_Double:      cpool->double_at_put(       i, cv->get_jdouble()                    ); break;
    case JVM_CONSTANT_Class:       cpool->klass_at_put(        i, cv->klass_oop()                      ); break;
    case JVM_CONSTANT_Methodref:   cpool->method_at_put(       i, cv->first_index(), cv->second_index()); break;
    case JVM_CONSTANT_NameAndType: cpool->name_and_type_at_put(i, cv->first_index(), cv->second_index()); break;
    case JVM_CONSTANT_Object:      cpool->object_at_put(       i, cv->object_oop()                     ); break;
    default: ShouldNotReachHere();
    }

    switch (cv->tag()) {
    case JVM_CONSTANT_Long:
    case JVM_CONSTANT_Double:
      i++;  // Skip empty entry.
      assert(_constants.at(i) == NULL, "empty entry");
      break;
1165 1166
    }
  }
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

  // Set the constant pool holder to the target method's class.
  cpool->set_pool_holder(_target_klass());

  return cpool;
}


methodHandle MethodHandleCompiler::get_method_oop(TRAPS) const {
  methodHandle nullHandle;
  // Create a method that holds the generated bytecode.  invokedynamic
  // has no receiver, normal MH calls do.
  int flags_bits;
  if (for_invokedynamic())
1181
    flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC | JVM_ACC_STATIC);
1182
  else
1183
    flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC);
1184 1185 1186

  methodOop m_oop = oopFactory::new_method(bytecode_length(),
                                           accessFlags_from(flags_bits),
1187
                                           0, 0, 0, oopDesc::IsSafeConc, CHECK_(nullHandle));
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
  methodHandle m(THREAD, m_oop);
  m_oop = NULL;  // oop not GC safe

  constantPoolHandle cpool = get_constant_pool(CHECK_(nullHandle));
  m->set_constants(cpool());

  m->set_name_index(_name_index);
  m->set_signature_index(_signature_index);

  m->set_code((address) bytecode());

  m->set_max_stack(_max_stack);
  m->set_max_locals(max_locals());
  m->set_size_of_parameters(_num_params);

  typeArrayHandle exception_handlers(THREAD, Universe::the_empty_int_array());
  m->set_exception_table(exception_handlers());

  // Set the carry bit of the invocation counter to force inlining of
  // the adapter.
  InvocationCounter* ic = m->invocation_counter();
I
iveresov 已提交
1209
  ic->set_carry_flag();
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223

  // Rewrite the method and set up the constant pool cache.
  objArrayOop m_array = oopFactory::new_system_objArray(1, CHECK_(nullHandle));
  objArrayHandle methods(THREAD, m_array);
  methods->obj_at_put(0, m());
  Rewriter::rewrite(_target_klass(), cpool, methods, CHECK_(nullHandle));  // Use fake class.

#ifndef PRODUCT
  if (TraceMethodHandles) {
    m->print();
    m->print_codes();
  }
#endif //PRODUCT

1224
  assert(m->is_method_handle_adapter(), "must be recognized as an adapter");
1225
  return m;
1226 1227 1228 1229 1230
}


#ifndef PRODUCT

1231
#if 0
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
// MH printer for debugging.

class MethodHandlePrinter : public MethodHandleWalker {
private:
  outputStream* _out;
  bool          _verbose;
  int           _temp_num;
  stringStream  _strbuf;
  const char* strbuf() {
    const char* s = _strbuf.as_string();
    _strbuf.reset();
    return s;
  }
  ArgToken token(const char* str) {
    return (ArgToken) str;
  }
  void start_params() {
    _out->print("(");
  }
  void end_params() {
    if (_verbose)  _out->print("\n");
    _out->print(") => {");
  }
  void put_type_name(BasicType type, klassOop tk, outputStream* s) {
    const char* kname = NULL;
    if (tk != NULL)
      kname = Klass::cast(tk)->external_name();
    s->print("%s", (kname != NULL) ? kname : type2name(type));
  }
  ArgToken maybe_make_temp(const char* statement_op, BasicType type, const char* temp_name) {
    const char* value = strbuf();
    if (!_verbose)  return token(value);
    // make an explicit binding for each separate value
    _strbuf.print("%s%d", temp_name, ++_temp_num);
    const char* temp = strbuf();
    _out->print("\n  %s %s %s = %s;", statement_op, type2name(type), temp, value);
    return token(temp);
  }

public:
  MethodHandlePrinter(Handle root, bool verbose, outputStream* out, TRAPS)
    : MethodHandleWalker(root, THREAD),
      _out(out),
      _verbose(verbose),
      _temp_num(0)
  {
    start_params();
  }
  virtual ArgToken make_parameter(BasicType type, klassOop tk, int argnum, TRAPS) {
    if (argnum < 0) {
      end_params();
      return NULL;
    }
    if (argnum == 0) {
      _out->print(_verbose ? "\n  " : "");
    } else {
      _out->print(_verbose ? ",\n  " : ", ");
    }
    if (argnum >= _temp_num)
      _temp_num = argnum;
    // generate an argument name
    _strbuf.print("a%d", argnum);
    const char* arg = strbuf();
    put_type_name(type, tk, _out);
    _out->print(" %s", arg);
    return token(arg);
  }
  virtual ArgToken make_oop_constant(oop con, TRAPS) {
    if (con == NULL)
      _strbuf.print("null");
    else
      con->print_value_on(&_strbuf);
    if (_strbuf.size() == 0) {  // yuck
      _strbuf.print("(a ");
      put_type_name(T_OBJECT, con->klass(), &_strbuf);
      _strbuf.print(")");
    }
    return maybe_make_temp("constant", T_OBJECT, "k");
  }
  virtual ArgToken make_prim_constant(BasicType type, jvalue* con, TRAPS) {
    java_lang_boxing_object::print(type, con, &_strbuf);
    return maybe_make_temp("constant", type, "k");
  }
  virtual ArgToken make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, ArgToken src, TRAPS) {
    _strbuf.print("%s(%s", Bytecodes::name(op), (const char*)src);
    if (tk != NULL) {
      _strbuf.print(", ");
      put_type_name(type, tk, &_strbuf);
    }
    _strbuf.print(")");
    return maybe_make_temp("convert", type, "v");
  }
  virtual ArgToken make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, ArgToken base, ArgToken offset, TRAPS) {
    _strbuf.print("%s(%s, %s", Bytecodes::name(op), (const char*)base, (const char*)offset);
    if (tk != NULL) {
      _strbuf.print(", ");
      put_type_name(type, tk, &_strbuf);
    }
    _strbuf.print(")");
    return maybe_make_temp("fetch", type, "x");
  }
  virtual ArgToken make_invoke(methodOop m, vmIntrinsics::ID iid,
                               Bytecodes::Code op, bool tailcall,
                               int argc, ArgToken* argv, TRAPS) {
1336
    Symbol* name, sig;
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
    if (m != NULL) {
      name = m->name();
      sig  = m->signature();
    } else {
      name = vmSymbols::symbol_at(vmIntrinsics::name_for(iid));
      sig  = vmSymbols::symbol_at(vmIntrinsics::signature_for(iid));
    }
    _strbuf.print("%s %s%s(", Bytecodes::name(op), name->as_C_string(), sig->as_C_string());
    for (int i = 0; i < argc; i++) {
      _strbuf.print("%s%s", (i > 0 ? ", " : ""), (const char*)argv[i]);
    }
    _strbuf.print(")");
    if (!tailcall) {
      BasicType rt = char2type(sig->byte_at(sig->utf8_length()-1));
      if (rt == T_ILLEGAL)  rt = T_OBJECT;  // ';' at the end of '(...)L...;'
      return maybe_make_temp("invoke", rt, "x");
    } else {
      const char* ret = strbuf();
      _out->print(_verbose ? "\n  return " : " ");
      _out->print("%s", ret);
      _out->print(_verbose ? "\n}\n" : " }");
    }
    return ArgToken();
  }

  virtual void set_method_handle(oop mh) {
    if (WizardMode && Verbose) {
      tty->print("\n--- next target: ");
      mh->print();
    }
  }

  static void print(Handle root, bool verbose, outputStream* out, TRAPS) {
    ResourceMark rm;
    MethodHandlePrinter printer(root, verbose, out, CHECK);
    printer.walk(CHECK);
    out->print("\n");
  }
  static void print(Handle root, bool verbose = Verbose, outputStream* out = tty) {
    EXCEPTION_MARK;
    ResourceMark rm;
    MethodHandlePrinter printer(root, verbose, out, THREAD);
    if (!HAS_PENDING_EXCEPTION)
      printer.walk(THREAD);
    if (HAS_PENDING_EXCEPTION) {
      oop ex = PENDING_EXCEPTION;
      CLEAR_PENDING_EXCEPTION;
      out->print("\n*** ");
      if (ex != Universe::virtual_machine_error_instance())
        ex->print_on(out);
      else
        out->print("lose: %s", printer.lose_message());
      out->print("\n}\n");
    }
    out->print("\n");
  }
};
1394
#endif // 0
1395 1396 1397

extern "C"
void print_method_handle(oop mh) {
1398 1399
  if (!mh->is_oop()) {
    tty->print_cr("*** not a method handle: "INTPTR_FORMAT, (intptr_t)mh);
1400
  } else if (java_lang_invoke_MethodHandle::is_instance(mh)) {
1401
    //MethodHandlePrinter::print(mh);
1402 1403 1404 1405 1406 1407 1408
  } else {
    tty->print("*** not a method handle: ");
    mh->print();
  }
}

#endif // PRODUCT