src/share/vm/prims/methodHandleWalk.hpp

Mon, 04 Jan 2010 15:52:40 +0100

author
twisti
date
Mon, 04 Jan 2010 15:52:40 +0100
changeset 1568
aa62b9388fce
child 1573
dd57230ba8fe
permissions
-rw-r--r--

6894206: JVM needs a way to traverse method handle structures
Summary: We need a way to walk chained method handles in the JVM to call the right methods and to generate required bytecode adapters for the compilers.
Reviewed-by: kvn

     1 /*
     2  * Copyright 2008-2010 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 // Low-level parser for method handle chains.
    26 class MethodHandleChain : StackObj {
    27 public:
    28   typedef MethodHandles::EntryKind EntryKind;
    30 private:
    31   Handle        _root;          // original target
    32   Handle        _method_handle; // current target
    33   bool          _is_last;       // final guy in chain
    34   bool          _is_bound;      // has a bound argument
    35   BasicType     _arg_type;      // if is_bound, the bound argument type
    36   int           _arg_slot;      // if is_bound or is_adapter, affected argument slot
    37   jint          _conversion;    // conversion field of AMH or -1
    38   methodHandle  _last_method;   // if is_last, which method we target
    39   Bytecodes::Code _last_invoke; // if is_last, type of invoke
    40   const char*   _lose_message;  // saved argument to lose()
    42   void set_method_handle(Handle target, TRAPS);
    43   void set_last_method(oop target, TRAPS);
    44   static BasicType compute_bound_arg_type(oop target, methodOop m, int arg_slot, TRAPS);
    46   oop MethodHandle_type_oop()     { return java_dyn_MethodHandle::type(method_handle_oop()); }
    47   oop MethodHandle_vmtarget_oop() { return java_dyn_MethodHandle::vmtarget(method_handle_oop()); }
    48   int MethodHandle_vmslots()      { return java_dyn_MethodHandle::vmslots(method_handle_oop()); }
    49   int DirectMethodHandle_vmindex()     { return sun_dyn_DirectMethodHandle::vmindex(method_handle_oop()); }
    50   oop BoundMethodHandle_argument_oop() { return sun_dyn_BoundMethodHandle::argument(method_handle_oop()); }
    51   int BoundMethodHandle_vmargslot()    { return sun_dyn_BoundMethodHandle::vmargslot(method_handle_oop()); }
    52   int AdapterMethodHandle_conversion() { return sun_dyn_AdapterMethodHandle::conversion(method_handle_oop()); }
    54 public:
    55   MethodHandleChain(Handle root, TRAPS)
    56     : _root(root)
    57   { set_method_handle(root, THREAD); }
    59   bool is_adapter()             { return _conversion != -1; }
    60   bool is_bound()               { return _is_bound; }
    61   bool is_last()                { return _is_last; }
    63   void next(TRAPS) {
    64     assert(!is_last(), "");
    65     set_method_handle(MethodHandle_vmtarget_oop(), THREAD);
    66   }
    68   Handle method_handle()        { return _method_handle; }
    69   oop    method_handle_oop()    { return _method_handle(); }
    70   oop    method_type_oop()      { return MethodHandle_type_oop(); }
    72   jint adapter_conversion()     { assert(is_adapter(), ""); return _conversion; }
    73   int  adapter_conversion_op()  { return MethodHandles::adapter_conversion_op(adapter_conversion()); }
    74   BasicType adapter_conversion_src_type()
    75                                 { return MethodHandles::adapter_conversion_src_type(adapter_conversion()); }
    76   BasicType adapter_conversion_dest_type()
    77                                 { return MethodHandles::adapter_conversion_dest_type(adapter_conversion()); }
    78   int  adapter_conversion_stack_move()
    79                                 { return MethodHandles::adapter_conversion_stack_move(adapter_conversion()); }
    80   int  adapter_conversion_stack_pushes()
    81                                 { return adapter_conversion_stack_move() / MethodHandles::stack_move_unit(); }
    82   int  adapter_conversion_vminfo()
    83                                 { return MethodHandles::adapter_conversion_vminfo(adapter_conversion()); }
    84   int adapter_arg_slot()        { assert(is_adapter(), ""); return _arg_slot; }
    85   oop adapter_arg_oop()         { assert(is_adapter(), ""); return BoundMethodHandle_argument_oop(); }
    87   BasicType bound_arg_type()    { assert(is_bound(), ""); return _arg_type; }
    88   int       bound_arg_slot()    { assert(is_bound(), ""); return _arg_slot; }
    89   oop       bound_arg_oop()     { assert(is_bound(), ""); return BoundMethodHandle_argument_oop(); }
    91   methodOop last_method_oop()   { assert(is_last(), ""); return _last_method(); }
    92   Bytecodes::Code last_invoke_code() { assert(is_last(), ""); return _last_invoke; }
    94   void lose(const char* msg, TRAPS);
    95   const char* lose_message()    { return _lose_message; }
    96 };
    99 // Structure walker for method handles.
   100 // Does abstract interpretation on top of low-level parsing.
   101 // You supply the tokens shuffled by the abstract interpretation.
   102 class MethodHandleWalker : StackObj {
   103 public:
   104   struct _ArgToken { };  // dummy struct
   105   typedef _ArgToken* ArgToken;
   107   // Abstract interpretation state:
   108   struct SlotState {
   109     BasicType _type;
   110     ArgToken  _arg;
   111     SlotState() : _type(), _arg() {}
   112   };
   113   static SlotState make_state(BasicType type, ArgToken arg) {
   114     SlotState ss;
   115     ss._type = type; ss._arg = arg;
   116     return ss;
   117   }
   119 private:
   120   MethodHandleChain _chain;
   122   GrowableArray<SlotState> _outgoing;  // current outgoing parameter slots
   123   int                      _outgoing_argc;  // # non-empty outgoing slots
   125   // Replace a value of type old_type at slot (and maybe slot+1) with the new value.
   126   // If old_type != T_VOID, remove the old argument at that point.
   127   // If new_type != T_VOID, insert the new argument at that point.
   128   // Insert or delete a second empty slot as needed.
   129   void change_argument(BasicType old_type, int slot, BasicType new_type, ArgToken new_arg);
   131   SlotState* slot_state(int slot) {
   132     if (slot < 0 || slot >= _outgoing.length())
   133       return NULL;
   134     return _outgoing.adr_at(slot);
   135   }
   136   BasicType slot_type(int slot) {
   137     SlotState* ss = slot_state(slot);
   138     if (ss == NULL)
   139       return T_ILLEGAL;
   140     return ss->_type;
   141   }
   142   bool slot_has_argument(int slot) {
   143     return slot_type(slot) < T_VOID;
   144   }
   146 #ifdef ASSERT
   147   int argument_count_slow();
   148 #endif
   150   // Return a bytecode for converting src to dest, if one exists.
   151   Bytecodes::Code conversion_code(BasicType src, BasicType dest);
   153   void walk_incoming_state(TRAPS);
   155 public:
   156   MethodHandleWalker(Handle root, TRAPS)
   157     : _chain(root, THREAD),
   158       _outgoing(THREAD, 10),
   159       _outgoing_argc(0)
   160   { }
   162   MethodHandleChain& chain() { return _chain; }
   164   // plug-in abstract interpretation steps:
   165   virtual ArgToken make_parameter( BasicType type, klassOop tk, int argnum, TRAPS ) = 0;
   166   virtual ArgToken make_prim_constant( BasicType type, jvalue* con, TRAPS ) = 0;
   167   virtual ArgToken make_oop_constant( oop con, TRAPS ) = 0;
   168   virtual ArgToken make_conversion( BasicType type, klassOop tk, Bytecodes::Code op, ArgToken src, TRAPS ) = 0;
   169   virtual ArgToken make_fetch( BasicType type, klassOop tk, Bytecodes::Code op, ArgToken base, ArgToken offset, TRAPS ) = 0;
   170   virtual ArgToken make_invoke( methodOop m, vmIntrinsics::ID iid, Bytecodes::Code op, bool tailcall, int argc, ArgToken* argv, TRAPS ) = 0;
   172   // For make_invoke, the methodOop can be NULL if the intrinsic ID
   173   // is something other than vmIntrinsics::_none.
   175   // and in case anyone cares to related the previous actions to the chain:
   176   virtual void set_method_handle(oop mh) { }
   178   void lose(const char* msg, TRAPS) { chain().lose(msg, THREAD); }
   179   const char* lose_message()        { return chain().lose_message(); }
   181   ArgToken walk(TRAPS);
   182 };
   185 // An abstract interpreter for method handle chains.
   186 // Produces an account of the semantics of a chain, in terms of a static IR.
   187 // The IR happens to be JVM bytecodes.
   188 class MethodHandleCompiler : public MethodHandleWalker {
   189 private:
   190   Thread* _thread;
   192   struct PrimCon {
   193     BasicType _type;
   194     jvalue    _value;
   195   };
   197   // Accumulated compiler state:
   198   stringStream _bytes;
   199   GrowableArray<Handle>   _constant_oops;
   200   GrowableArray<PrimCon*> _constant_prims;
   201   int _max_stack;
   202   int _num_params;
   203   int _max_locals;
   204   int _name_index;
   205   int _signature_index;
   207   // Stack values:
   208   enum TokenType {
   209     tt_void,
   210     tt_parameter,
   211     tt_temporary,
   212     tt_constant
   213   };
   215   ArgToken make_stack_value(TokenType tt, BasicType type, int id) {
   216     return ArgToken( ((intptr_t)id << 8) | ((intptr_t)type << 4) | (intptr_t)tt );
   217   }
   219 public:
   220   virtual ArgToken make_parameter(BasicType type, klassOop tk, int argnum, TRAPS) {
   221     return make_stack_value(tt_parameter, type, argnum);
   222   }
   223   virtual ArgToken make_oop_constant(oop con, TRAPS) {
   224     return make_stack_value(tt_constant, T_OBJECT, find_oop_constant(con));
   225   }
   226   virtual ArgToken make_prim_constant(BasicType type, jvalue* con, TRAPS) {
   227     return make_stack_value(tt_constant, type, find_prim_constant(type, con));
   228   }
   229   virtual ArgToken make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, ArgToken src, TRAPS);
   230   virtual ArgToken make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, ArgToken base, ArgToken offset, TRAPS);
   231   virtual ArgToken make_invoke(methodOop m, vmIntrinsics::ID iid, Bytecodes::Code op, bool tailcall, int argc, ArgToken* argv, TRAPS);
   233   int find_oop_constant(oop con);
   234   int find_prim_constant(BasicType type, jvalue* con);
   236 public:
   237   MethodHandleCompiler(Handle root, TRAPS)
   238     : MethodHandleWalker(root, THREAD),
   239       _thread(THREAD),
   240       _bytes(50),
   241       _constant_oops(THREAD, 10),
   242       _constant_prims(THREAD, 10),
   243       _max_stack(0), _max_locals(0),
   244       _name_index(0), _signature_index(0)
   245   { }
   246   const char* bytes()         { return _bytes.as_string(); }
   247   int constant_length()       { return _constant_oops.length(); }
   248   int max_stack()             { return _max_stack; }
   249   int max_locals()            { return _max_locals; }
   250   int name_index()            { return _name_index; }
   251   int signature_index()       { return _signature_index; }
   252   symbolHandle name()         { return symbolHandle(_thread, (symbolOop)constant_oop_at(_name_index)()); }
   253   symbolHandle signature()    { return symbolHandle(_thread, (symbolOop)constant_oop_at(_signature_index)()); }
   255   bool constant_is_oop_at(int i) {
   256     return (_constant_prims.at(i) == NULL);
   257   }
   258   Handle constant_oop_at(int i) {
   259     assert(constant_is_oop_at(i), "");
   260     return _constant_oops.at(i);
   261   }
   262   PrimCon* constant_prim_at(int i) {
   263     assert(!constant_is_oop_at(i), "");
   264     return _constant_prims.at(i);
   265   }
   267   // Compile the given MH chain into bytecode.
   268   void compile(TRAPS);
   269 };

mercurial