src/share/vm/prims/methodHandles.hpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7795
157895117ad5
parent 7535
7ae4e26cb1e0
child 8856
ac27a9c85bea
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2008, 2014, 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 /*
    26  * This file has been modified by Loongson Technology in 2015. These
    27  * modifications are Copyright (c) 2015 Loongson Technology, and are made
    28  * available on the same license terms set forth above.
    29  */
    31 #ifndef SHARE_VM_PRIMS_METHODHANDLES_HPP
    32 #define SHARE_VM_PRIMS_METHODHANDLES_HPP
    34 #include "classfile/javaClasses.hpp"
    35 #include "classfile/vmSymbols.hpp"
    36 #include "runtime/frame.inline.hpp"
    37 #include "runtime/globals.hpp"
    38 #include "runtime/interfaceSupport.hpp"
    40 class MacroAssembler;
    41 class Label;
    43 class MethodHandles: AllStatic {
    44   // JVM support for MethodHandle, MethodType, and related types
    45   // in java.lang.invoke and sun.invoke.
    46   // See also  javaClasses for layouts java_lang_invoke_Method{Handle,Type,Type::Form}.
    47  public:
    48  public:
    49   static bool enabled()                         { return _enabled; }
    50   static void set_enabled(bool z);
    52  private:
    53   static bool _enabled;
    55   // Adapters.
    56   static MethodHandlesAdapterBlob* _adapter_code;
    58   // utility functions for reifying names and types
    59   static oop field_name_or_null(Symbol* s);
    60   static oop field_signature_type_or_null(Symbol* s);
    62  public:
    63   // working with member names
    64   static Handle resolve_MemberName(Handle mname, KlassHandle caller, TRAPS); // compute vmtarget/vmindex from name/type
    65   static void expand_MemberName(Handle mname, int suppress, TRAPS);  // expand defc/name/type if missing
    66   static Handle new_MemberName(TRAPS);  // must be followed by init_MemberName
    67   static oop init_MemberName(Handle mname_h, Handle target_h); // compute vmtarget/vmindex from target
    68   static oop init_field_MemberName(Handle mname_h, fieldDescriptor& fd, bool is_setter = false);
    69   static oop init_method_MemberName(Handle mname_h, CallInfo& info);
    70   static int method_ref_kind(Method* m, bool do_dispatch_if_possible = true);
    71   static int find_MemberNames(KlassHandle k, Symbol* name, Symbol* sig,
    72                               int mflags, KlassHandle caller,
    73                               int skip, objArrayHandle results);
    74   // bit values for suppress argument to expand_MemberName:
    75   enum { _suppress_defc = 1, _suppress_name = 2, _suppress_type = 4 };
    77   // Generate MethodHandles adapters.
    78                               static void generate_adapters();
    80   // Called from MethodHandlesAdapterGenerator.
    81   static address generate_method_handle_interpreter_entry(MacroAssembler* _masm, vmIntrinsics::ID iid);
    82   static void generate_method_handle_dispatch(MacroAssembler* _masm,
    83                                               vmIntrinsics::ID iid,
    84                                               Register receiver_reg,
    85                                               Register member_reg,
    86                                               bool for_compiler_entry);
    88   // Queries
    89   static bool is_signature_polymorphic(vmIntrinsics::ID iid) {
    90     return (iid >= vmIntrinsics::FIRST_MH_SIG_POLY &&
    91             iid <= vmIntrinsics::LAST_MH_SIG_POLY);
    92   }
    94   static bool is_signature_polymorphic_intrinsic(vmIntrinsics::ID iid) {
    95     assert(is_signature_polymorphic(iid), "");
    96     // Most sig-poly methods are intrinsics which do not require an
    97     // appeal to Java for adapter code.
    98     return (iid != vmIntrinsics::_invokeGeneric);
    99   }
   101   static bool is_signature_polymorphic_static(vmIntrinsics::ID iid) {
   102     assert(is_signature_polymorphic(iid), "");
   103     return (iid >= vmIntrinsics::FIRST_MH_STATIC &&
   104             iid <= vmIntrinsics::LAST_MH_SIG_POLY);
   105   }
   107   static bool has_member_arg(vmIntrinsics::ID iid) {
   108     assert(is_signature_polymorphic(iid), "");
   109     return (iid >= vmIntrinsics::_linkToVirtual &&
   110             iid <= vmIntrinsics::_linkToInterface);
   111   }
   112   static bool has_member_arg(Symbol* klass, Symbol* name) {
   113     if ((klass == vmSymbols::java_lang_invoke_MethodHandle()) &&
   114         is_signature_polymorphic_name(name)) {
   115       vmIntrinsics::ID iid = signature_polymorphic_name_id(name);
   116       return has_member_arg(iid);
   117     }
   118     return false;
   119   }
   121   static Symbol* signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid);
   122   static int signature_polymorphic_intrinsic_ref_kind(vmIntrinsics::ID iid);
   124   static vmIntrinsics::ID signature_polymorphic_name_id(Klass* klass, Symbol* name);
   125   static vmIntrinsics::ID signature_polymorphic_name_id(Symbol* name);
   126   static bool is_signature_polymorphic_name(Symbol* name) {
   127     return signature_polymorphic_name_id(name) != vmIntrinsics::_none;
   128   }
   129   static bool is_method_handle_invoke_name(Klass* klass, Symbol* name);
   130   static bool is_signature_polymorphic_name(Klass* klass, Symbol* name) {
   131     return signature_polymorphic_name_id(klass, name) != vmIntrinsics::_none;
   132   }
   134   enum {
   135     // format of query to getConstant:
   136     GC_COUNT_GWT = 4,
   137     GC_LAMBDA_SUPPORT = 5
   138   };
   139   static int get_named_constant(int which, Handle name_box, TRAPS);
   141 public:
   142   static Symbol* lookup_signature(oop type_str, bool polymorphic, TRAPS);  // use TempNewSymbol
   143   static Symbol* lookup_basic_type_signature(Symbol* sig, bool keep_last_arg, TRAPS);  // use TempNewSymbol
   144   static Symbol* lookup_basic_type_signature(Symbol* sig, TRAPS) {
   145     return lookup_basic_type_signature(sig, false, THREAD);
   146   }
   147   static bool is_basic_type_signature(Symbol* sig);
   149   static Symbol* lookup_method_type(Symbol* msig, Handle mtype, TRAPS);
   151   static void print_as_method_type_on(outputStream* st, Symbol* sig) {
   152     print_as_basic_type_signature_on(st, sig, true, true);
   153   }
   154   static void print_as_basic_type_signature_on(outputStream* st, Symbol* sig, bool keep_arrays = false, bool keep_basic_names = false);
   156   // decoding CONSTANT_MethodHandle constants
   157   enum { JVM_REF_MIN = JVM_REF_getField, JVM_REF_MAX = JVM_REF_invokeInterface };
   158   static bool ref_kind_is_valid(int ref_kind) {
   159     return (ref_kind >= JVM_REF_MIN && ref_kind <= JVM_REF_MAX);
   160   }
   161   static bool ref_kind_is_field(int ref_kind) {
   162     assert(ref_kind_is_valid(ref_kind), "");
   163     return (ref_kind <= JVM_REF_putStatic);
   164   }
   165   static bool ref_kind_is_getter(int ref_kind) {
   166     assert(ref_kind_is_valid(ref_kind), "");
   167     return (ref_kind <= JVM_REF_getStatic);
   168   }
   169   static bool ref_kind_is_setter(int ref_kind) {
   170     return ref_kind_is_field(ref_kind) && !ref_kind_is_getter(ref_kind);
   171   }
   172   static bool ref_kind_is_method(int ref_kind) {
   173     return !ref_kind_is_field(ref_kind) && (ref_kind != JVM_REF_newInvokeSpecial);
   174   }
   175   static bool ref_kind_has_receiver(int ref_kind) {
   176     assert(ref_kind_is_valid(ref_kind), "");
   177     return (ref_kind & 1) != 0;
   178   }
   179   static bool ref_kind_is_static(int ref_kind) {
   180     return !ref_kind_has_receiver(ref_kind) && (ref_kind != JVM_REF_newInvokeSpecial);
   181   }
   182   static bool ref_kind_does_dispatch(int ref_kind) {
   183     return (ref_kind == JVM_REF_invokeVirtual ||
   184             ref_kind == JVM_REF_invokeInterface);
   185   }
   188 #ifdef TARGET_ARCH_x86
   189 # include "methodHandles_x86.hpp"
   190 #endif
   191 #ifdef TARGET_ARCH_sparc
   192 # include "methodHandles_sparc.hpp"
   193 #endif
   194 #ifdef TARGET_ARCH_zero
   195 # include "methodHandles_zero.hpp"
   196 #endif
   197 #ifdef TARGET_ARCH_arm
   198 # include "methodHandles_arm.hpp"
   199 #endif
   200 #ifdef TARGET_ARCH_ppc
   201 # include "methodHandles_ppc.hpp"
   202 #endif
   203 #ifdef TARGET_ARCH_mips
   204 # include "methodHandles_mips.hpp"
   205 #endif
   208   // Tracing
   209   static void trace_method_handle(MacroAssembler* _masm, const char* adaptername) PRODUCT_RETURN;
   210   static void trace_method_handle_interpreter_entry(MacroAssembler* _masm, vmIntrinsics::ID iid) {
   211     if (TraceMethodHandles) {
   212       const char* name = vmIntrinsics::name_at(iid);
   213       if (*name == '_')  name += 1;
   214       const size_t len = strlen(name) + 50;
   215       char* qname = NEW_C_HEAP_ARRAY(char, len, mtInternal);
   216       const char* suffix = "";
   217       if (is_signature_polymorphic(iid)) {
   218         if (is_signature_polymorphic_static(iid))
   219           suffix = "/static";
   220         else
   221           suffix = "/private";
   222       }
   223       jio_snprintf(qname, len, "MethodHandle::interpreter_entry::%s%s", name, suffix);
   224       trace_method_handle(_masm, qname);
   225       // Note:  Don't free the allocated char array because it's used
   226       // during runtime.
   227     }
   228   }
   229 };
   231 //------------------------------------------------------------------------------
   232 // MethodHandlesAdapterGenerator
   233 //
   234 class MethodHandlesAdapterGenerator : public StubCodeGenerator {
   235 public:
   236   MethodHandlesAdapterGenerator(CodeBuffer* code) : StubCodeGenerator(code, PrintMethodHandleStubs) {}
   238   void generate();
   239 };
   241 //------------------------------------------------------------------------------
   242 // MemberNameTable
   243 //
   245 class MemberNameTable : public GrowableArray<jweak> {
   246  public:
   247   MemberNameTable(int methods_cnt);
   248   ~MemberNameTable();
   249   void add_member_name(jweak mem_name_ref);
   251 #if INCLUDE_JVMTI
   252   // RedefineClasses() API support:
   253   // If a MemberName refers to old_method then update it to refer to new_method.
   254   void adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed);
   255 #endif // INCLUDE_JVMTI
   256 };
   258 #endif // SHARE_VM_PRIMS_METHODHANDLES_HPP

mercurial