hotspot/src/share/vm/prims/methodHandleWalk.hpp
author twisti
Tue, 05 Jan 2010 15:21:25 +0100
changeset 4567 7fc02fbe5c7a
parent 4562 5d93cb2d2090
child 4581 e89fbd1bcb3d
permissions -rw-r--r--
6893268: additional dynamic language related optimizations in C2 Summary: C2 needs some additional optimizations to be able to handle MethodHandle invokes and invokedynamic instructions at the best performance. Reviewed-by: kvn, never
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
     1
/*
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
     2
 * Copyright 2008-2010 Sun Microsystems, Inc.  All Rights Reserved.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
     4
 *
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
     8
 *
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    13
 * accompanied this code).
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    14
 *
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    18
 *
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    21
 * have any questions.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    22
 *
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    23
 */
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    24
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    25
// Low-level parser for method handle chains.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    26
class MethodHandleChain : StackObj {
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    27
public:
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    28
  typedef MethodHandles::EntryKind EntryKind;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    29
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    30
private:
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    31
  Handle        _root;          // original target
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    32
  Handle        _method_handle; // current target
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    33
  bool          _is_last;       // final guy in chain
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    34
  bool          _is_bound;      // has a bound argument
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    35
  BasicType     _arg_type;      // if is_bound, the bound argument type
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    36
  int           _arg_slot;      // if is_bound or is_adapter, affected argument slot
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    37
  jint          _conversion;    // conversion field of AMH or -1
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    38
  methodHandle  _last_method;   // if is_last, which method we target
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    39
  Bytecodes::Code _last_invoke; // if is_last, type of invoke
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    40
  const char*   _lose_message;  // saved argument to lose()
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    41
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    42
  void set_method_handle(Handle target, TRAPS);
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    43
  void set_last_method(oop target, TRAPS);
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    44
  static BasicType compute_bound_arg_type(oop target, methodOop m, int arg_slot, TRAPS);
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    45
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    46
  oop MethodHandle_type_oop()     { return java_dyn_MethodHandle::type(method_handle_oop()); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    47
  oop MethodHandle_vmtarget_oop() { return java_dyn_MethodHandle::vmtarget(method_handle_oop()); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    48
  int MethodHandle_vmslots()      { return java_dyn_MethodHandle::vmslots(method_handle_oop()); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    49
  int DirectMethodHandle_vmindex()     { return sun_dyn_DirectMethodHandle::vmindex(method_handle_oop()); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    50
  oop BoundMethodHandle_argument_oop() { return sun_dyn_BoundMethodHandle::argument(method_handle_oop()); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    51
  int BoundMethodHandle_vmargslot()    { return sun_dyn_BoundMethodHandle::vmargslot(method_handle_oop()); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    52
  int AdapterMethodHandle_conversion() { return sun_dyn_AdapterMethodHandle::conversion(method_handle_oop()); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    53
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    54
public:
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    55
  MethodHandleChain(Handle root, TRAPS)
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    56
    : _root(root)
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    57
  { set_method_handle(root, THREAD); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    58
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    59
  bool is_adapter()             { return _conversion != -1; }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    60
  bool is_bound()               { return _is_bound; }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    61
  bool is_last()                { return _is_last; }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    62
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    63
  void next(TRAPS) {
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    64
    assert(!is_last(), "");
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    65
    set_method_handle(MethodHandle_vmtarget_oop(), THREAD);
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    66
  }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    67
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    68
  Handle method_handle()        { return _method_handle; }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    69
  oop    method_handle_oop()    { return _method_handle(); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    70
  oop    method_type_oop()      { return MethodHandle_type_oop(); }
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
    71
  oop    vmtarget_oop()         { return MethodHandle_vmtarget_oop(); }
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    72
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    73
  jint adapter_conversion()     { assert(is_adapter(), ""); return _conversion; }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    74
  int  adapter_conversion_op()  { return MethodHandles::adapter_conversion_op(adapter_conversion()); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    75
  BasicType adapter_conversion_src_type()
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    76
                                { return MethodHandles::adapter_conversion_src_type(adapter_conversion()); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    77
  BasicType adapter_conversion_dest_type()
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    78
                                { return MethodHandles::adapter_conversion_dest_type(adapter_conversion()); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    79
  int  adapter_conversion_stack_move()
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    80
                                { return MethodHandles::adapter_conversion_stack_move(adapter_conversion()); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    81
  int  adapter_conversion_stack_pushes()
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    82
                                { return adapter_conversion_stack_move() / MethodHandles::stack_move_unit(); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    83
  int  adapter_conversion_vminfo()
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    84
                                { return MethodHandles::adapter_conversion_vminfo(adapter_conversion()); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    85
  int adapter_arg_slot()        { assert(is_adapter(), ""); return _arg_slot; }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    86
  oop adapter_arg_oop()         { assert(is_adapter(), ""); return BoundMethodHandle_argument_oop(); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    87
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    88
  BasicType bound_arg_type()    { assert(is_bound(), ""); return _arg_type; }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    89
  int       bound_arg_slot()    { assert(is_bound(), ""); return _arg_slot; }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    90
  oop       bound_arg_oop()     { assert(is_bound(), ""); return BoundMethodHandle_argument_oop(); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    91
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    92
  methodOop last_method_oop()   { assert(is_last(), ""); return _last_method(); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    93
  Bytecodes::Code last_invoke_code() { assert(is_last(), ""); return _last_invoke; }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    94
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    95
  void lose(const char* msg, TRAPS);
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    96
  const char* lose_message()    { return _lose_message; }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    97
};
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    98
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
    99
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   100
// Structure walker for method handles.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   101
// Does abstract interpretation on top of low-level parsing.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   102
// You supply the tokens shuffled by the abstract interpretation.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   103
class MethodHandleWalker : StackObj {
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   104
public:
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   105
  // Stack values:
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   106
  enum TokenType {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   107
    tt_void,
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   108
    tt_parameter,
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   109
    tt_temporary,
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   110
    tt_constant,
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   111
    tt_illegal
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   112
  };
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   113
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   114
  // Argument token:
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   115
  class ArgToken {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   116
  private:
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   117
    TokenType _tt;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   118
    BasicType _bt;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   119
    jvalue    _value;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   120
    Handle    _handle;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   121
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   122
  public:
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   123
    ArgToken(TokenType tt = tt_illegal) : _tt(tt) {}
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   124
    ArgToken(TokenType tt, BasicType bt, jvalue value) : _tt(tt), _bt(bt), _value(value) {}
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   125
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   126
    ArgToken(TokenType tt, BasicType bt, int index) : _tt(tt), _bt(bt) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   127
      _value.i = index;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   128
    }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   129
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   130
    ArgToken(TokenType tt, BasicType bt, Handle value) : _tt(tt), _bt(bt) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   131
      _handle = value;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   132
    }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   133
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   134
    TokenType token_type()  const { return _tt; }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   135
    BasicType basic_type()  const { return _bt; }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   136
    int       index()       const { return _value.i; }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   137
    Handle    object()      const { return _handle; }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   138
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   139
    jint      get_jint()    const { return _value.i; }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   140
    jlong     get_jlong()   const { return _value.j; }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   141
    jfloat    get_jfloat()  const { return _value.f; }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   142
    jdouble   get_jdouble() const { return _value.d; }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   143
  };
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   144
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   145
  // Abstract interpretation state:
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   146
  struct SlotState {
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   147
    BasicType _type;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   148
    ArgToken  _arg;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   149
    SlotState() : _type(), _arg() {}
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   150
  };
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   151
  static SlotState make_state(BasicType type, ArgToken arg) {
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   152
    SlotState ss;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   153
    ss._type = type; ss._arg = arg;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   154
    return ss;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   155
  }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   156
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   157
private:
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   158
  MethodHandleChain _chain;
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   159
  bool              _for_invokedynamic;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   160
  int               _local_index;
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   161
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   162
  GrowableArray<SlotState> _outgoing;       // current outgoing parameter slots
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   163
  int                      _outgoing_argc;  // # non-empty outgoing slots
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   164
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   165
  // Replace a value of type old_type at slot (and maybe slot+1) with the new value.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   166
  // If old_type != T_VOID, remove the old argument at that point.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   167
  // If new_type != T_VOID, insert the new argument at that point.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   168
  // Insert or delete a second empty slot as needed.
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   169
  void change_argument(BasicType old_type, int slot, BasicType new_type, const ArgToken& new_arg);
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   170
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   171
  SlotState* slot_state(int slot) {
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   172
    if (slot < 0 || slot >= _outgoing.length())
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   173
      return NULL;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   174
    return _outgoing.adr_at(slot);
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   175
  }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   176
  BasicType slot_type(int slot) {
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   177
    SlotState* ss = slot_state(slot);
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   178
    if (ss == NULL)
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   179
      return T_ILLEGAL;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   180
    return ss->_type;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   181
  }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   182
  bool slot_has_argument(int slot) {
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   183
    return slot_type(slot) < T_VOID;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   184
  }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   185
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   186
#ifdef ASSERT
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   187
  int argument_count_slow();
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   188
#endif
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   189
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   190
  // Return a bytecode for converting src to dest, if one exists.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   191
  Bytecodes::Code conversion_code(BasicType src, BasicType dest);
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   192
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   193
  void walk_incoming_state(TRAPS);
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   194
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   195
public:
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   196
  MethodHandleWalker(Handle root, bool for_invokedynamic, TRAPS)
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   197
    : _chain(root, THREAD),
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   198
      _for_invokedynamic(for_invokedynamic),
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   199
      _outgoing(THREAD, 10),
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   200
      _outgoing_argc(0)
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   201
  {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   202
    _local_index = for_invokedynamic ? 0 : 1;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   203
  }
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   204
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   205
  MethodHandleChain& chain() { return _chain; }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   206
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   207
  bool for_invokedynamic() const { return _for_invokedynamic; }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   208
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   209
  int new_local_index(BasicType bt) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   210
    //int index = _for_invokedynamic ? _local_index : _local_index - 1;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   211
    int index = _local_index;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   212
    _local_index += type2size[bt];
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   213
    return index;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   214
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   215
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   216
  int max_locals() const { return _local_index; }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   217
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   218
  // plug-in abstract interpretation steps:
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   219
  virtual ArgToken make_parameter( BasicType type, klassOop tk, int argnum, TRAPS ) = 0;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   220
  virtual ArgToken make_prim_constant( BasicType type, jvalue* con, TRAPS ) = 0;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   221
  virtual ArgToken make_oop_constant( oop con, TRAPS ) = 0;
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   222
  virtual ArgToken make_conversion( BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& src, TRAPS ) = 0;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   223
  virtual ArgToken make_fetch( BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& base, const ArgToken& offset, TRAPS ) = 0;
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   224
  virtual ArgToken make_invoke( methodOop m, vmIntrinsics::ID iid, Bytecodes::Code op, bool tailcall, int argc, ArgToken* argv, TRAPS ) = 0;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   225
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   226
  // For make_invoke, the methodOop can be NULL if the intrinsic ID
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   227
  // is something other than vmIntrinsics::_none.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   228
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   229
  // and in case anyone cares to related the previous actions to the chain:
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   230
  virtual void set_method_handle(oop mh) { }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   231
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   232
  void lose(const char* msg, TRAPS) { chain().lose(msg, THREAD); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   233
  const char* lose_message()        { return chain().lose_message(); }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   234
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   235
  ArgToken walk(TRAPS);
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   236
};
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   237
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   238
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   239
// An abstract interpreter for method handle chains.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   240
// Produces an account of the semantics of a chain, in terms of a static IR.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   241
// The IR happens to be JVM bytecodes.
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   242
class MethodHandleCompiler : public MethodHandleWalker {
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   243
private:
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   244
  methodHandle _callee;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   245
  KlassHandle  _rklass;        // Return type for casting.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   246
  BasicType    _rtype;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   247
  KlassHandle  _target_klass;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   248
  Thread*      _thread;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   249
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   250
  // Fake constant pool entry.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   251
  class ConstantValue {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   252
  private:
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   253
    int       _tag;   // Constant pool tag type.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   254
    JavaValue _value;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   255
    Handle    _handle;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   256
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   257
  public:
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   258
    // Constructor for oop types.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   259
    ConstantValue(int tag, Handle con) : _tag(tag), _handle(con) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   260
      assert(tag == JVM_CONSTANT_Utf8   ||
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   261
             tag == JVM_CONSTANT_Class  ||
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   262
             tag == JVM_CONSTANT_String ||
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   263
             tag == JVM_CONSTANT_Object, "must be oop type");
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   264
    }
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   265
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   266
    // Constructor for oop reference types.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   267
    ConstantValue(int tag, int index) : _tag(tag) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   268
      assert(JVM_CONSTANT_Fieldref <= tag && tag <= JVM_CONSTANT_NameAndType, "must be ref type");
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   269
      _value.set_jint(index);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   270
    }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   271
    ConstantValue(int tag, int first_index, int second_index) : _tag(tag) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   272
      assert(JVM_CONSTANT_Fieldref <= tag && tag <= JVM_CONSTANT_NameAndType, "must be ref type");
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   273
      _value.set_jint(first_index << 16 | second_index);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   274
    }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   275
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   276
    // Constructor for primitive types.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   277
    ConstantValue(BasicType bt, jvalue con) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   278
      _value.set_type(bt);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   279
      switch (bt) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   280
      case T_INT:    _tag = JVM_CONSTANT_Integer; _value.set_jint(   con.i); break;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   281
      case T_LONG:   _tag = JVM_CONSTANT_Long;    _value.set_jlong(  con.j); break;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   282
      case T_FLOAT:  _tag = JVM_CONSTANT_Float;   _value.set_jfloat( con.f); break;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   283
      case T_DOUBLE: _tag = JVM_CONSTANT_Double;  _value.set_jdouble(con.d); break;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   284
      default: ShouldNotReachHere();
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   285
      }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   286
    }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   287
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   288
    int       tag()          const { return _tag; }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   289
    symbolOop symbol_oop()   const { return (symbolOop) _handle(); }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   290
    klassOop  klass_oop()    const { return (klassOop)  _handle(); }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   291
    oop       object_oop()   const { return _handle(); }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   292
    int       index()        const { return _value.get_jint(); }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   293
    int       first_index()  const { return _value.get_jint() >> 16; }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   294
    int       second_index() const { return _value.get_jint() & 0x0000FFFF; }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   295
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   296
    bool      is_primitive() const { return is_java_primitive(_value.get_type()); }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   297
    jint      get_jint()     const { return _value.get_jint();    }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   298
    jlong     get_jlong()    const { return _value.get_jlong();   }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   299
    jfloat    get_jfloat()   const { return _value.get_jfloat();  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   300
    jdouble   get_jdouble()  const { return _value.get_jdouble(); }
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   301
  };
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   302
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   303
  // Fake constant pool.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   304
  GrowableArray<ConstantValue*> _constants;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   305
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   306
  // Accumulated compiler state:
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   307
  GrowableArray<unsigned char> _bytecode;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   308
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   309
  int _cur_stack;
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   310
  int _max_stack;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   311
  int _num_params;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   312
  int _name_index;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   313
  int _signature_index;
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   314
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   315
  void stack_push(BasicType bt) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   316
    _cur_stack += type2size[bt];
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   317
    if (_cur_stack > _max_stack) _max_stack = _cur_stack;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   318
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   319
  void stack_pop(BasicType bt) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   320
    _cur_stack -= type2size[bt];
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   321
    assert(_cur_stack >= 0, "sanity");
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   322
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   323
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   324
  unsigned char* bytecode()        const { return _bytecode.adr_at(0); }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   325
  int            bytecode_length() const { return _bytecode.length(); }
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   326
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   327
  // Fake constant pool.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   328
  int cpool_oop_put(int tag, Handle con) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   329
    if (con.is_null())  return 0;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   330
    ConstantValue* cv = new ConstantValue(tag, con);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   331
    return _constants.append(cv);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   332
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   333
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   334
  int cpool_oop_reference_put(int tag, int first_index, int second_index) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   335
    if (first_index == 0 && second_index == 0)  return 0;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   336
    assert(first_index != 0 && second_index != 0, "no zero indexes");
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   337
    ConstantValue* cv = new ConstantValue(tag, first_index, second_index);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   338
    return _constants.append(cv);
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   339
  }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   340
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   341
  int cpool_primitive_put(BasicType type, jvalue* con);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   342
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   343
  int cpool_int_put(jint value) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   344
    jvalue con; con.i = value;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   345
    return cpool_primitive_put(T_INT, &con);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   346
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   347
  int cpool_long_put(jlong value) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   348
    jvalue con; con.j = value;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   349
    return cpool_primitive_put(T_LONG, &con);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   350
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   351
  int cpool_float_put(jfloat value) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   352
    jvalue con; con.f = value;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   353
    return cpool_primitive_put(T_FLOAT, &con);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   354
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   355
  int cpool_double_put(jdouble value) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   356
    jvalue con; con.d = value;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   357
    return cpool_primitive_put(T_DOUBLE, &con);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   358
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   359
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   360
  int cpool_object_put(Handle obj) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   361
    return cpool_oop_put(JVM_CONSTANT_Object, obj);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   362
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   363
  int cpool_symbol_put(symbolOop sym) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   364
    return cpool_oop_put(JVM_CONSTANT_Utf8, sym);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   365
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   366
  int cpool_klass_put(klassOop klass) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   367
    return cpool_oop_put(JVM_CONSTANT_Class, klass);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   368
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   369
  int cpool_methodref_put(int class_index, int name_and_type_index) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   370
    return cpool_oop_reference_put(JVM_CONSTANT_Methodref, class_index, name_and_type_index);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   371
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   372
  int cpool_name_and_type_put(int name_index, int signature_index) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   373
    return cpool_oop_reference_put(JVM_CONSTANT_NameAndType, name_index, signature_index);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   374
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   375
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   376
  void emit_bc(Bytecodes::Code op, int index = 0);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   377
  void emit_load(BasicType bt, int index);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   378
  void emit_store(BasicType bt, int index);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   379
  void emit_load_constant(ArgToken arg);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   380
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   381
  virtual ArgToken make_parameter(BasicType type, klassOop tk, int argnum, TRAPS) {
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   382
    return ArgToken(tt_parameter, type, argnum);
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   383
  }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   384
  virtual ArgToken make_oop_constant(oop con, TRAPS) {
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   385
    Handle h(THREAD, con);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   386
    return ArgToken(tt_constant, T_OBJECT, h);
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   387
  }
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   388
  virtual ArgToken make_prim_constant(BasicType type, jvalue* con, TRAPS) {
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   389
    return ArgToken(tt_constant, type, *con);
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   390
  }
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   391
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   392
  virtual ArgToken make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& src, TRAPS);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   393
  virtual ArgToken make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& base, const ArgToken& offset, TRAPS);
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   394
  virtual ArgToken make_invoke(methodOop m, vmIntrinsics::ID iid, Bytecodes::Code op, bool tailcall, int argc, ArgToken* argv, TRAPS);
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   395
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   396
  // Get a real constant pool.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   397
  constantPoolHandle get_constant_pool(TRAPS) const;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   398
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   399
  // Get a real methodOop.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   400
  methodHandle get_method_oop(TRAPS) const;
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   401
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   402
public:
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   403
  MethodHandleCompiler(Handle root, methodHandle call_method, bool for_invokedynamic, TRAPS);
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   404
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   405
  // Compile the given MH chain into bytecode.
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 4562
diff changeset
   406
  methodHandle compile(TRAPS);
4562
5d93cb2d2090 6894206: JVM needs a way to traverse method handle structures
twisti
parents:
diff changeset
   407
};