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

25 26 27 28 29 30 31 32 33
#ifndef SHARE_VM_PRIMS_METHODHANDLES_HPP
#define SHARE_VM_PRIMS_METHODHANDLES_HPP

#include "classfile/javaClasses.hpp"
#include "classfile/vmSymbols.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/globals.hpp"
#include "runtime/interfaceSupport.hpp"

34 35 36 37 38
class MacroAssembler;
class Label;

class MethodHandles: AllStatic {
  // JVM support for MethodHandle, MethodType, and related types
39 40
  // in java.lang.invoke and sun.invoke.
  // See also  javaClasses for layouts java_lang_invoke_Method{Handle,Type,Type::Form}.
41 42 43 44 45 46 47
 public:
 public:
  static bool enabled()                         { return _enabled; }
  static void set_enabled(bool z);

 private:
  static bool _enabled;
48

49 50 51
  // Adapters.
  static MethodHandlesAdapterBlob* _adapter_code;

52 53 54 55
  // utility functions for reifying names and types
  static oop field_name_or_null(Symbol* s);
  static oop field_signature_type_or_null(Symbol* s);

56
 public:
57
  // working with member names
J
jrose 已提交
58
  static Handle resolve_MemberName(Handle mname, KlassHandle caller, TRAPS); // compute vmtarget/vmindex from name/type
59 60
  static void expand_MemberName(Handle mname, int suppress, TRAPS);  // expand defc/name/type if missing
  static Handle new_MemberName(TRAPS);  // must be followed by init_MemberName
61
  static oop init_MemberName(Handle mname_h, Handle target_h); // compute vmtarget/vmindex from target
62 63
  static oop init_field_MemberName(Handle mname_h, fieldDescriptor& fd, bool is_setter = false);
  static oop init_method_MemberName(Handle mname_h, CallInfo& info);
64
  static int method_ref_kind(Method* m, bool do_dispatch_if_possible = true);
65 66 67
  static int find_MemberNames(KlassHandle k, Symbol* name, Symbol* sig,
                              int mflags, KlassHandle caller,
                              int skip, objArrayHandle results);
68 69
  // bit values for suppress argument to expand_MemberName:
  enum { _suppress_defc = 1, _suppress_name = 2, _suppress_type = 4 };
70

71
  // Generate MethodHandles adapters.
72
                              static void generate_adapters();
73

74 75 76 77 78 79 80
  // Called from MethodHandlesAdapterGenerator.
  static address generate_method_handle_interpreter_entry(MacroAssembler* _masm, vmIntrinsics::ID iid);
  static void generate_method_handle_dispatch(MacroAssembler* _masm,
                                              vmIntrinsics::ID iid,
                                              Register receiver_reg,
                                              Register member_reg,
                                              bool for_compiler_entry);
81

82 83 84 85
  // Queries
  static bool is_signature_polymorphic(vmIntrinsics::ID iid) {
    return (iid >= vmIntrinsics::FIRST_MH_SIG_POLY &&
            iid <= vmIntrinsics::LAST_MH_SIG_POLY);
86 87
  }

88 89 90 91 92
  static bool is_signature_polymorphic_intrinsic(vmIntrinsics::ID iid) {
    assert(is_signature_polymorphic(iid), "");
    // Most sig-poly methods are intrinsics which do not require an
    // appeal to Java for adapter code.
    return (iid != vmIntrinsics::_invokeGeneric);
93 94
  }

95 96 97 98
  static bool is_signature_polymorphic_static(vmIntrinsics::ID iid) {
    assert(is_signature_polymorphic(iid), "");
    return (iid >= vmIntrinsics::FIRST_MH_STATIC &&
            iid <= vmIntrinsics::LAST_MH_SIG_POLY);
99 100
  }

101 102 103 104
  static bool has_member_arg(vmIntrinsics::ID iid) {
    assert(is_signature_polymorphic(iid), "");
    return (iid >= vmIntrinsics::_linkToVirtual &&
            iid <= vmIntrinsics::_linkToInterface);
105
  }
106 107 108 109 110
  static bool has_member_arg(Symbol* klass, Symbol* name) {
    if ((klass == vmSymbols::java_lang_invoke_MethodHandle()) &&
        is_signature_polymorphic_name(name)) {
      vmIntrinsics::ID iid = signature_polymorphic_name_id(name);
      return has_member_arg(iid);
T
twisti 已提交
111
    }
112
    return false;
T
twisti 已提交
113 114
  }

115 116
  static Symbol* signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid);
  static int signature_polymorphic_intrinsic_ref_kind(vmIntrinsics::ID iid);
117

118
  static vmIntrinsics::ID signature_polymorphic_name_id(Klass* klass, Symbol* name);
119 120 121
  static vmIntrinsics::ID signature_polymorphic_name_id(Symbol* name);
  static bool is_signature_polymorphic_name(Symbol* name) {
    return signature_polymorphic_name_id(name) != vmIntrinsics::_none;
122
  }
123 124
  static bool is_method_handle_invoke_name(Klass* klass, Symbol* name);
  static bool is_signature_polymorphic_name(Klass* klass, Symbol* name) {
125
    return signature_polymorphic_name_id(klass, name) != vmIntrinsics::_none;
126 127 128 129
  }

  enum {
    // format of query to getConstant:
130
    GC_COUNT_GWT = 4,
131
    GC_LAMBDA_SUPPORT = 5
132 133 134
  };
  static int get_named_constant(int which, Handle name_box, TRAPS);

135 136 137 138 139
public:
  static Symbol* lookup_signature(oop type_str, bool polymorphic, TRAPS);  // use TempNewSymbol
  static Symbol* lookup_basic_type_signature(Symbol* sig, bool keep_last_arg, TRAPS);  // use TempNewSymbol
  static Symbol* lookup_basic_type_signature(Symbol* sig, TRAPS) {
    return lookup_basic_type_signature(sig, false, THREAD);
140
  }
141
  static bool is_basic_type_signature(Symbol* sig);
142

143 144 145 146
  static Symbol* lookup_method_type(Symbol* msig, Handle mtype, TRAPS);

  static void print_as_method_type_on(outputStream* st, Symbol* sig) {
    print_as_basic_type_signature_on(st, sig, true, true);
147
  }
148 149 150 151 152 153
  static void print_as_basic_type_signature_on(outputStream* st, Symbol* sig, bool keep_arrays = false, bool keep_basic_names = false);

  // decoding CONSTANT_MethodHandle constants
  enum { JVM_REF_MIN = JVM_REF_getField, JVM_REF_MAX = JVM_REF_invokeInterface };
  static bool ref_kind_is_valid(int ref_kind) {
    return (ref_kind >= JVM_REF_MIN && ref_kind <= JVM_REF_MAX);
154
  }
155 156 157
  static bool ref_kind_is_field(int ref_kind) {
    assert(ref_kind_is_valid(ref_kind), "");
    return (ref_kind <= JVM_REF_putStatic);
158
  }
159 160 161
  static bool ref_kind_is_getter(int ref_kind) {
    assert(ref_kind_is_valid(ref_kind), "");
    return (ref_kind <= JVM_REF_getStatic);
162
  }
163 164
  static bool ref_kind_is_setter(int ref_kind) {
    return ref_kind_is_field(ref_kind) && !ref_kind_is_getter(ref_kind);
165
  }
166 167
  static bool ref_kind_is_method(int ref_kind) {
    return !ref_kind_is_field(ref_kind) && (ref_kind != JVM_REF_newInvokeSpecial);
168
  }
169 170 171 172 173 174 175 176 177 178
  static bool ref_kind_has_receiver(int ref_kind) {
    assert(ref_kind_is_valid(ref_kind), "");
    return (ref_kind & 1) != 0;
  }
  static bool ref_kind_is_static(int ref_kind) {
    return !ref_kind_has_receiver(ref_kind) && (ref_kind != JVM_REF_newInvokeSpecial);
  }
  static bool ref_kind_does_dispatch(int ref_kind) {
    return (ref_kind == JVM_REF_invokeVirtual ||
            ref_kind == JVM_REF_invokeInterface);
179 180
  }

181 182 183 184 185

#ifdef TARGET_ARCH_x86
# include "methodHandles_x86.hpp"
#endif
#ifdef TARGET_ARCH_sparc
186
# include "methodHandles_sparc.hpp"
187 188
#endif
#ifdef TARGET_ARCH_zero
189
# include "methodHandles_zero.hpp"
190 191
#endif
#ifdef TARGET_ARCH_arm
192
# include "methodHandles_arm.hpp"
193 194
#endif
#ifdef TARGET_ARCH_ppc
195
# include "methodHandles_ppc.hpp"
196
#endif
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  // Tracing
  static void trace_method_handle(MacroAssembler* _masm, const char* adaptername) PRODUCT_RETURN;
  static void trace_method_handle_interpreter_entry(MacroAssembler* _masm, vmIntrinsics::ID iid) {
    if (TraceMethodHandles) {
      const char* name = vmIntrinsics::name_at(iid);
      if (*name == '_')  name += 1;
      const size_t len = strlen(name) + 50;
      char* qname = NEW_C_HEAP_ARRAY(char, len, mtInternal);
      const char* suffix = "";
      if (is_signature_polymorphic(iid)) {
        if (is_signature_polymorphic_static(iid))
          suffix = "/static";
        else
          suffix = "/private";
      }
      jio_snprintf(qname, len, "MethodHandle::interpreter_entry::%s%s", name, suffix);
      trace_method_handle(_masm, qname);
      // Note:  Don't free the allocated char array because it's used
      // during runtime.
    }
  }
219 220
};

221 222 223 224 225
//------------------------------------------------------------------------------
// MethodHandlesAdapterGenerator
//
class MethodHandlesAdapterGenerator : public StubCodeGenerator {
public:
226
  MethodHandlesAdapterGenerator(CodeBuffer* code) : StubCodeGenerator(code, PrintMethodHandleStubs) {}
227

228
  void generate();
229
};
230

231 232 233
//------------------------------------------------------------------------------
// MemberNameTable
//
234

235 236
class MemberNameTable : public GrowableArray<jweak> {
 public:
237
  MemberNameTable(int methods_cnt);
238
  ~MemberNameTable();
239 240
  void add_member_name(int index, jweak mem_name_ref);
  oop  get_member_name(int index);
241 242 243 244 245 246 247 248 249 250 251 252 253

#if INCLUDE_JVMTI
 public:
  // RedefineClasses() API support:
  // If a MemberName refers to old_method then update it
  // to refer to new_method.
  void adjust_method_entries(Method** old_methods, Method** new_methods,
                             int methods_length, bool *trace_name_printed);
 private:
  oop find_member_name_by_method(Method* old_method);
#endif // INCLUDE_JVMTI
};

254
#endif // SHARE_VM_PRIMS_METHODHANDLES_HPP