src/share/vm/prims/methodHandleWalk.cpp

Tue, 14 Jun 2011 14:41:33 -0700

author
never
date
Tue, 14 Jun 2011 14:41:33 -0700
changeset 2954
f8c9417e3571
parent 2950
cba7b5c2d53f
child 2956
cfcf2ba8f3eb
permissions
-rw-r--r--

7052219: JSR 292: Crash in ~BufferBlob::MethodHandles adapters
Reviewed-by: twisti, kvn, jrose

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

mercurial