src/share/vm/prims/methodHandleWalk.hpp

Thu, 23 Jun 2011 17:14:06 -0700

author
jrose
date
Thu, 23 Jun 2011 17:14:06 -0700
changeset 2982
ddd894528dbc
parent 2950
cba7b5c2d53f
child 3105
c26de9aef2ed
permissions
-rw-r--r--

7056328: JSR 292 invocation sometimes fails in adapters for types not on boot class path
Reviewed-by: never

     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 #ifndef SHARE_VM_PRIMS_METHODHANDLEWALK_HPP
    26 #define SHARE_VM_PRIMS_METHODHANDLEWALK_HPP
    28 #include "prims/methodHandles.hpp"
    30 // Low-level parser for method handle chains.
    31 class MethodHandleChain : StackObj {
    32 public:
    33   typedef MethodHandles::EntryKind EntryKind;
    35 private:
    36   Handle        _root;          // original target
    37   Handle        _method_handle; // current target
    38   bool          _is_last;       // final guy in chain
    39   bool          _is_bound;      // has a bound argument
    40   BasicType     _arg_type;      // if is_bound, the bound argument type
    41   int           _arg_slot;      // if is_bound or is_adapter, affected argument slot
    42   jint          _conversion;    // conversion field of AMH or -1
    43   methodHandle  _last_method;   // if is_last, which method we target
    44   Bytecodes::Code _last_invoke; // if is_last, type of invoke
    45   const char*   _lose_message;  // saved argument to lose()
    47   void set_method_handle(Handle target, TRAPS);
    48   void set_last_method(oop target, TRAPS);
    49   static BasicType compute_bound_arg_type(oop target, methodOop m, int arg_slot, TRAPS);
    51   oop MethodHandle_type_oop()          { return java_lang_invoke_MethodHandle::type(method_handle_oop()); }
    52   oop MethodHandle_vmtarget_oop()      { return java_lang_invoke_MethodHandle::vmtarget(method_handle_oop()); }
    53   int MethodHandle_vmslots()           { return java_lang_invoke_MethodHandle::vmslots(method_handle_oop()); }
    54   int DirectMethodHandle_vmindex()     { return java_lang_invoke_DirectMethodHandle::vmindex(method_handle_oop()); }
    55   oop BoundMethodHandle_argument_oop() { return java_lang_invoke_BoundMethodHandle::argument(method_handle_oop()); }
    56   int BoundMethodHandle_vmargslot()    { return java_lang_invoke_BoundMethodHandle::vmargslot(method_handle_oop()); }
    57   int AdapterMethodHandle_conversion() { return java_lang_invoke_AdapterMethodHandle::conversion(method_handle_oop()); }
    59 #ifdef ASSERT
    60   void print_impl(TRAPS);
    61 #endif
    63 public:
    64   MethodHandleChain(Handle root, TRAPS)
    65     : _root(root)
    66   { set_method_handle(root, THREAD); }
    68   bool is_adapter()             { return _conversion != -1; }
    69   bool is_bound()               { return _is_bound; }
    70   bool is_last()                { return _is_last; }
    72   void next(TRAPS) {
    73     assert(!is_last(), "");
    74     set_method_handle(MethodHandle_vmtarget_oop(), THREAD);
    75   }
    77   Handle method_handle()        { return _method_handle; }
    78   oop    method_handle_oop()    { return _method_handle(); }
    79   oop    method_type_oop()      { return MethodHandle_type_oop(); }
    80   oop    vmtarget_oop()         { return MethodHandle_vmtarget_oop(); }
    82   jint adapter_conversion()     { assert(is_adapter(), ""); return _conversion; }
    83   int  adapter_conversion_op()  { return MethodHandles::adapter_conversion_op(adapter_conversion()); }
    84   BasicType adapter_conversion_src_type()
    85                                 { return MethodHandles::adapter_conversion_src_type(adapter_conversion()); }
    86   BasicType adapter_conversion_dest_type()
    87                                 { return MethodHandles::adapter_conversion_dest_type(adapter_conversion()); }
    88   int  adapter_conversion_stack_move()
    89                                 { return MethodHandles::adapter_conversion_stack_move(adapter_conversion()); }
    90   int  adapter_conversion_stack_pushes()
    91                                 { return adapter_conversion_stack_move() / MethodHandles::stack_move_unit(); }
    92   int  adapter_conversion_vminfo()
    93                                 { return MethodHandles::adapter_conversion_vminfo(adapter_conversion()); }
    94   int adapter_arg_slot()        { assert(is_adapter(), ""); return _arg_slot; }
    95   oop adapter_arg_oop()         { assert(is_adapter(), ""); return BoundMethodHandle_argument_oop(); }
    97   BasicType bound_arg_type()    { assert(is_bound(), ""); return _arg_type; }
    98   int       bound_arg_slot()    { assert(is_bound(), ""); return _arg_slot; }
    99   oop       bound_arg_oop()     { assert(is_bound(), ""); return BoundMethodHandle_argument_oop(); }
   101   methodHandle last_method()    { assert(is_last(), ""); return _last_method; }
   102   methodOop last_method_oop()   { assert(is_last(), ""); return _last_method(); }
   103   Bytecodes::Code last_invoke_code() { assert(is_last(), ""); return _last_invoke; }
   105   void lose(const char* msg, TRAPS);
   106   const char* lose_message()    { return _lose_message; }
   108 #ifdef ASSERT
   109   // Print a symbolic description of a method handle chain, including
   110   // the signature for each method.  The signatures are printed in
   111   // slot order to make it easier to understand.
   112   void print();
   113   static void print(Handle mh);
   114   static void print(oopDesc* mh);
   115 #endif
   116 };
   119 // Structure walker for method handles.
   120 // Does abstract interpretation on top of low-level parsing.
   121 // You supply the tokens shuffled by the abstract interpretation.
   122 class MethodHandleWalker : StackObj {
   123 public:
   124   // Stack values:
   125   enum TokenType {
   126     tt_void,
   127     tt_parameter,
   128     tt_temporary,
   129     tt_constant,
   130     tt_symbolic,
   131     tt_illegal
   132   };
   134   // Argument token:
   135   class ArgToken {
   136   private:
   137     TokenType _tt;
   138     BasicType _bt;
   139     jvalue    _value;
   140     Handle    _handle;
   142   public:
   143     ArgToken(TokenType tt = tt_illegal) : _tt(tt), _bt(tt == tt_void ? T_VOID : T_ILLEGAL) {
   144       assert(tt == tt_illegal || tt == tt_void, "invalid token type");
   145     }
   147     ArgToken(TokenType tt, BasicType bt, int index) : _tt(tt), _bt(bt) {
   148       assert(_tt == tt_parameter || _tt == tt_temporary, "must have index");
   149       _value.i = index;
   150     }
   152     ArgToken(BasicType bt, jvalue value) : _tt(tt_constant), _bt(bt), _value(value) { assert(_bt != T_OBJECT, "wrong constructor"); }
   153     ArgToken(Handle handle) : _tt(tt_constant), _bt(T_OBJECT), _handle(handle) {}
   156     ArgToken(const char* str, BasicType type) : _tt(tt_symbolic), _bt(type) {
   157       _value.j = (intptr_t)str;
   158     }
   160     TokenType token_type()  const { return _tt; }
   161     BasicType basic_type()  const { return _bt; }
   162     bool      has_index()   const { return _tt == tt_parameter || _tt == tt_temporary; }
   163     int       index()       const { assert(has_index(), "must have index");; return _value.i; }
   164     Handle    object()      const { assert(_bt == T_OBJECT, "wrong accessor"); assert(_tt == tt_constant, "value type"); return _handle; }
   165     const char* str()       const { assert(_tt == tt_symbolic, "string type"); return (const char*)(intptr_t)_value.j; }
   167     jint      get_jint()    const { assert(_bt == T_INT || is_subword_type(_bt), "wrong accessor"); assert(_tt == tt_constant, "value types"); return _value.i; }
   168     jlong     get_jlong()   const { assert(_bt == T_LONG, "wrong accessor");   assert(_tt == tt_constant, "value types"); return _value.j; }
   169     jfloat    get_jfloat()  const { assert(_bt == T_FLOAT, "wrong accessor");  assert(_tt == tt_constant, "value types"); return _value.f; }
   170     jdouble   get_jdouble() const { assert(_bt == T_DOUBLE, "wrong accessor"); assert(_tt == tt_constant, "value types"); return _value.d; }
   171   };
   173 private:
   174   MethodHandleChain _chain;
   175   bool              _for_invokedynamic;
   176   int               _local_index;
   178   // This array is kept in an unusual order, indexed by low-level "slot number".
   179   // TOS is always _outgoing.at(0), so simple pushes and pops shift the whole _outgoing array.
   180   // If there is a receiver in the current argument list, it is at _outgoing.at(_outgoing.length()-1).
   181   // If a value at _outgoing.at(n) is T_LONG or T_DOUBLE, the value at _outgoing.at(n+1) is T_VOID.
   182   GrowableArray<ArgToken>  _outgoing;       // current outgoing parameter slots
   183   int                      _outgoing_argc;  // # non-empty outgoing slots
   185   vmIntrinsics::ID _return_conv;            // Return conversion required by raw retypes.
   187   // Replace a value of type old_type at slot (and maybe slot+1) with the new value.
   188   // If old_type != T_VOID, remove the old argument at that point.
   189   // If new_type != T_VOID, insert the new argument at that point.
   190   // Insert or delete a second empty slot as needed.
   191   void change_argument(BasicType old_type, int slot, const ArgToken& new_arg);
   192   void change_argument(BasicType old_type, int slot, BasicType type, const ArgToken& new_arg) {
   193     assert(type == new_arg.basic_type(), "must agree");
   194     change_argument(old_type, slot, new_arg);
   195   }
   197   // Raw retype conversions for OP_RAW_RETYPE.
   198   void retype_raw_conversion(BasicType src, BasicType dst, bool for_return, int slot, TRAPS);
   199   void retype_raw_argument_type(BasicType src, BasicType dst, int slot, TRAPS) { retype_raw_conversion(src, dst, false, slot, CHECK); }
   200   void retype_raw_return_type(  BasicType src, BasicType dst,           TRAPS) { retype_raw_conversion(src, dst, true,  -1,   CHECK); }
   202   BasicType arg_type(int slot) {
   203     return _outgoing.at(slot).basic_type();
   204   }
   205   bool has_argument(int slot) {
   206     return arg_type(slot) < T_VOID;
   207   }
   209 #ifdef ASSERT
   210   int argument_count_slow();
   211 #endif
   213   // Return a bytecode for converting src to dest, if one exists.
   214   Bytecodes::Code conversion_code(BasicType src, BasicType dest);
   216   void walk_incoming_state(TRAPS);
   218   void verify_args_and_signature(TRAPS) NOT_DEBUG_RETURN;
   220 public:
   221   MethodHandleWalker(Handle root, bool for_invokedynamic, TRAPS)
   222     : _chain(root, THREAD),
   223       _for_invokedynamic(for_invokedynamic),
   224       _outgoing(THREAD, 10),
   225       _outgoing_argc(0),
   226       _return_conv(vmIntrinsics::_none)
   227   {
   228     _local_index = for_invokedynamic ? 0 : 1;
   229   }
   231   MethodHandleChain& chain() { return _chain; }
   233   bool for_invokedynamic() const { return _for_invokedynamic; }
   235   vmIntrinsics::ID return_conv() const { return _return_conv; }
   236   void set_return_conv(vmIntrinsics::ID c) { _return_conv = c; }
   237   static vmIntrinsics::ID zero_return_conv() { return vmIntrinsics::_min; }
   239   int new_local_index(BasicType bt) {
   240     //int index = _for_invokedynamic ? _local_index : _local_index - 1;
   241     int index = _local_index;
   242     _local_index += type2size[bt];
   243     return index;
   244   }
   246   int max_locals() const { return _local_index; }
   248   // plug-in abstract interpretation steps:
   249   virtual ArgToken make_parameter(BasicType type, klassOop tk, int argnum, TRAPS) = 0;
   250   virtual ArgToken make_prim_constant(BasicType type, jvalue* con, TRAPS) = 0;
   251   virtual ArgToken make_oop_constant(oop con, TRAPS) = 0;
   252   virtual ArgToken make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& src, TRAPS) = 0;
   253   virtual ArgToken make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& base, const ArgToken& offset, TRAPS) = 0;
   254   virtual ArgToken make_invoke(methodHandle m, vmIntrinsics::ID iid, Bytecodes::Code op, bool tailcall, int argc, ArgToken* argv, TRAPS) = 0;
   256   // For make_invoke, the methodHandle can be NULL if the intrinsic ID
   257   // is something other than vmIntrinsics::_none.
   259   // and in case anyone cares to related the previous actions to the chain:
   260   virtual void set_method_handle(oop mh) { }
   262   void lose(const char* msg, TRAPS) { chain().lose(msg, THREAD); }
   263   const char* lose_message()        { return chain().lose_message(); }
   265   ArgToken walk(TRAPS);
   266 };
   269 // An abstract interpreter for method handle chains.
   270 // Produces an account of the semantics of a chain, in terms of a static IR.
   271 // The IR happens to be JVM bytecodes.
   272 class MethodHandleCompiler : public MethodHandleWalker {
   273 private:
   274   int          _invoke_count;  // count the original call site has been executed
   275   KlassHandle  _rklass;        // Return type for casting.
   276   BasicType    _rtype;
   277   KlassHandle  _target_klass;
   278   Thread*      _thread;
   280   // Values used by the compiler.
   281   static jvalue zero_jvalue;
   282   static jvalue one_jvalue;
   284   // Fake constant pool entry.
   285   class ConstantValue : public ResourceObj {
   286   private:
   287     int       _tag;   // Constant pool tag type.
   288     JavaValue _value;
   289     Handle    _handle;
   290     Symbol*   _sym;
   291     methodHandle _method;  // pre-linkage
   293   public:
   294     // Constructor for oop types.
   295     ConstantValue(int tag, Handle con) : _tag(tag), _handle(con) {
   296       assert(tag == JVM_CONSTANT_Class  ||
   297              tag == JVM_CONSTANT_String ||
   298              tag == JVM_CONSTANT_Object, "must be oop type");
   299     }
   301     ConstantValue(int tag, Symbol* con) : _tag(tag), _sym(con) {
   302       assert(tag == JVM_CONSTANT_Utf8, "must be symbol type");
   303     }
   305     // Constructor for oop reference types.
   306     ConstantValue(int tag, int index) : _tag(tag) {
   307       assert(JVM_CONSTANT_Fieldref <= tag && tag <= JVM_CONSTANT_NameAndType, "must be ref type");
   308       _value.set_jint(index);
   309     }
   310     ConstantValue(int tag, int first_index, int second_index) : _tag(tag) {
   311       assert(JVM_CONSTANT_Fieldref <= tag && tag <= JVM_CONSTANT_NameAndType, "must be ref type");
   312       _value.set_jint(first_index << 16 | second_index);
   313     }
   315     // Constructor for primitive types.
   316     ConstantValue(BasicType bt, jvalue con) {
   317       _value.set_type(bt);
   318       switch (bt) {
   319       case T_INT:    _tag = JVM_CONSTANT_Integer; _value.set_jint(   con.i); break;
   320       case T_LONG:   _tag = JVM_CONSTANT_Long;    _value.set_jlong(  con.j); break;
   321       case T_FLOAT:  _tag = JVM_CONSTANT_Float;   _value.set_jfloat( con.f); break;
   322       case T_DOUBLE: _tag = JVM_CONSTANT_Double;  _value.set_jdouble(con.d); break;
   323       default: ShouldNotReachHere();
   324       }
   325     }
   327     int       tag()          const { return _tag; }
   328     Symbol*   symbol()       const { return _sym; }
   329     klassOop  klass_oop()    const { return (klassOop)  _handle(); }
   330     oop       object_oop()   const { return _handle(); }
   331     int       index()        const { return _value.get_jint(); }
   332     int       first_index()  const { return _value.get_jint() >> 16; }
   333     int       second_index() const { return _value.get_jint() & 0x0000FFFF; }
   335     bool      is_primitive() const { return is_java_primitive(_value.get_type()); }
   336     jint      get_jint()     const { return _value.get_jint();    }
   337     jlong     get_jlong()    const { return _value.get_jlong();   }
   338     jfloat    get_jfloat()   const { return _value.get_jfloat();  }
   339     jdouble   get_jdouble()  const { return _value.get_jdouble(); }
   341     void set_linkage(methodHandle method) {
   342       assert(_method.is_null(), "");
   343       _method = method;
   344     }
   345     bool     has_linkage()   const { return _method.not_null(); }
   346     methodHandle linkage()   const { return _method; }
   347   };
   349   // Fake constant pool.
   350   GrowableArray<ConstantValue*> _constants;
   352   // Non-BCP classes that appear in associated MethodTypes (require special handling).
   353   GrowableArray<KlassHandle> _non_bcp_klasses;
   355   // Accumulated compiler state:
   356   GrowableArray<unsigned char> _bytecode;
   358   int _cur_stack;
   359   int _max_stack;
   360   int _num_params;
   361   int _name_index;
   362   int _signature_index;
   364   void stack_push(BasicType bt) {
   365     _cur_stack += type2size[bt];
   366     if (_cur_stack > _max_stack) _max_stack = _cur_stack;
   367   }
   368   void stack_pop(BasicType bt) {
   369     _cur_stack -= type2size[bt];
   370     assert(_cur_stack >= 0, "sanity");
   371   }
   373   unsigned char* bytecode()        const { return _bytecode.adr_at(0); }
   374   int            bytecode_length() const { return _bytecode.length(); }
   376   // Fake constant pool.
   377   int cpool_oop_put(int tag, Handle con) {
   378     if (con.is_null())  return 0;
   379     ConstantValue* cv = new ConstantValue(tag, con);
   380     return _constants.append(cv);
   381   }
   383   int cpool_symbol_put(int tag, Symbol* con) {
   384     if (con == NULL)  return 0;
   385     ConstantValue* cv = new ConstantValue(tag, con);
   386     con->increment_refcount();
   387     return _constants.append(cv);
   388   }
   390   int cpool_oop_reference_put(int tag, int first_index, int second_index, methodHandle method) {
   391     if (first_index == 0 && second_index == 0)  return 0;
   392     assert(first_index != 0 && second_index != 0, "no zero indexes");
   393     ConstantValue* cv = new ConstantValue(tag, first_index, second_index);
   394     if (method.not_null())  cv->set_linkage(method);
   395     return _constants.append(cv);
   396   }
   398   int cpool_primitive_put(BasicType type, jvalue* con);
   400   bool check_non_bcp_klasses(Handle method_type, TRAPS);
   401   bool check_non_bcp_klass(klassOop klass, TRAPS);
   402   void record_non_bcp_klasses();
   404   int cpool_int_put(jint value) {
   405     jvalue con; con.i = value;
   406     return cpool_primitive_put(T_INT, &con);
   407   }
   408   int cpool_long_put(jlong value) {
   409     jvalue con; con.j = value;
   410     return cpool_primitive_put(T_LONG, &con);
   411   }
   412   int cpool_float_put(jfloat value) {
   413     jvalue con; con.f = value;
   414     return cpool_primitive_put(T_FLOAT, &con);
   415   }
   416   int cpool_double_put(jdouble value) {
   417     jvalue con; con.d = value;
   418     return cpool_primitive_put(T_DOUBLE, &con);
   419   }
   421   int cpool_object_put(Handle obj) {
   422     return cpool_oop_put(JVM_CONSTANT_Object, obj);
   423   }
   424   int cpool_symbol_put(Symbol* sym) {
   425     return cpool_symbol_put(JVM_CONSTANT_Utf8, sym);
   426   }
   427   int cpool_klass_put(klassOop klass) {
   428     return cpool_oop_put(JVM_CONSTANT_Class, klass);
   429   }
   430   int cpool_methodref_put(Bytecodes::Code op, int class_index, int name_and_type_index, methodHandle method) {
   431     int tag = (op == Bytecodes::_invokeinterface ? JVM_CONSTANT_InterfaceMethodref : JVM_CONSTANT_Methodref);
   432     return cpool_oop_reference_put(tag, class_index, name_and_type_index, method);
   433   }
   434   int cpool_name_and_type_put(int name_index, int signature_index) {
   435     return cpool_oop_reference_put(JVM_CONSTANT_NameAndType, name_index, signature_index, methodHandle());
   436   }
   438   void emit_bc(Bytecodes::Code op, int index = 0, int args_size = -1);
   439   void emit_load(BasicType bt, int index);
   440   void emit_store(BasicType bt, int index);
   441   void emit_load_constant(ArgToken arg);
   443   virtual ArgToken make_parameter(BasicType type, klassOop tk, int argnum, TRAPS) {
   444     return ArgToken(tt_parameter, type, argnum);
   445   }
   446   virtual ArgToken make_oop_constant(oop con, TRAPS) {
   447     Handle h(THREAD, con);
   448     return ArgToken(h);
   449   }
   450   virtual ArgToken make_prim_constant(BasicType type, jvalue* con, TRAPS) {
   451     return ArgToken(type, *con);
   452   }
   454   virtual ArgToken make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& src, TRAPS);
   455   virtual ArgToken make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& base, const ArgToken& offset, TRAPS);
   456   virtual ArgToken make_invoke(methodHandle m, vmIntrinsics::ID iid, Bytecodes::Code op, bool tailcall, int argc, ArgToken* argv, TRAPS);
   458   // Get a real constant pool.
   459   constantPoolHandle get_constant_pool(TRAPS) const;
   461   // Get a real methodOop.
   462   methodHandle get_method_oop(TRAPS) const;
   464 public:
   465   MethodHandleCompiler(Handle root, Symbol* name, Symbol* signature, int invoke_count, bool for_invokedynamic, TRAPS);
   467   // Compile the given MH chain into bytecode.
   468   methodHandle compile(TRAPS);
   470   // Tests if the given class is a MH adapter holder.
   471   static bool klass_is_method_handle_adapter_holder(klassOop klass) {
   472     return (klass == SystemDictionary::MethodHandle_klass());
   473   }
   474 };
   476 #endif // SHARE_VM_PRIMS_METHODHANDLEWALK_HPP

mercurial