src/hotspot/share/prims/methodHandles.hpp
changeset 47216 71c04702a3d5
parent 46560 388aa8d67c80
child 47765 b7c7428eaab9
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2008, 2017, 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  */
       
    24 
       
    25 #ifndef SHARE_VM_PRIMS_METHODHANDLES_HPP
       
    26 #define SHARE_VM_PRIMS_METHODHANDLES_HPP
       
    27 
       
    28 #include "classfile/javaClasses.hpp"
       
    29 #include "classfile/vmSymbols.hpp"
       
    30 #include "prims/jvm.h"
       
    31 #include "runtime/frame.inline.hpp"
       
    32 #include "runtime/globals.hpp"
       
    33 #include "runtime/interfaceSupport.hpp"
       
    34 #include "utilities/macros.hpp"
       
    35 
       
    36 #ifdef ZERO
       
    37 # include "entry_zero.hpp"
       
    38 #endif
       
    39 
       
    40 
       
    41 class MacroAssembler;
       
    42 class Label;
       
    43 
       
    44 class MethodHandles: AllStatic {
       
    45   // JVM support for MethodHandle, MethodType, and related types
       
    46   // in java.lang.invoke and sun.invoke.
       
    47   // See also  javaClasses for layouts java_lang_invoke_Method{Handle,Type,Type::Form}.
       
    48  public:
       
    49  public:
       
    50   static bool enabled()                         { return _enabled; }
       
    51   static void set_enabled(bool z);
       
    52 
       
    53  private:
       
    54   static bool _enabled;
       
    55 
       
    56   // Adapters.
       
    57   static MethodHandlesAdapterBlob* _adapter_code;
       
    58 
       
    59   // utility functions for reifying names and types
       
    60   static oop field_name_or_null(Symbol* s);
       
    61   static oop field_signature_type_or_null(Symbol* s);
       
    62 
       
    63  public:
       
    64   // working with member names
       
    65   static Handle resolve_MemberName(Handle mname, Klass* caller, TRAPS); // compute vmtarget/vmindex from name/type
       
    66   static void expand_MemberName(Handle mname, int suppress, TRAPS);  // expand defc/name/type if missing
       
    67   static oop init_MemberName(Handle mname_h, Handle target_h, TRAPS); // 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(Klass* k, Symbol* name, Symbol* sig,
       
    72                               int mflags, Klass* caller,
       
    73                               int skip, objArrayHandle results, TRAPS);
       
    74   // bit values for suppress argument to expand_MemberName:
       
    75   enum { _suppress_defc = 1, _suppress_name = 2, _suppress_type = 4 };
       
    76 
       
    77   // CallSite support
       
    78   static void add_dependent_nmethod(oop call_site, nmethod* nm);
       
    79   static void remove_dependent_nmethod(oop call_site, nmethod* nm);
       
    80 
       
    81   static void flush_dependent_nmethods(Handle call_site, Handle target);
       
    82 
       
    83   // Generate MethodHandles adapters.
       
    84   static void generate_adapters();
       
    85 
       
    86   // Called from MethodHandlesAdapterGenerator.
       
    87   static address generate_method_handle_interpreter_entry(MacroAssembler* _masm, vmIntrinsics::ID iid);
       
    88   static void generate_method_handle_dispatch(MacroAssembler* _masm,
       
    89                                               vmIntrinsics::ID iid,
       
    90                                               Register receiver_reg,
       
    91                                               Register member_reg,
       
    92                                               bool for_compiler_entry);
       
    93 
       
    94   // Queries
       
    95   static bool is_signature_polymorphic(vmIntrinsics::ID iid) {
       
    96     return (iid >= vmIntrinsics::FIRST_MH_SIG_POLY &&
       
    97             iid <= vmIntrinsics::LAST_MH_SIG_POLY);
       
    98   }
       
    99 
       
   100   static bool is_signature_polymorphic_method(Method* m) {
       
   101     return is_signature_polymorphic(m->intrinsic_id());
       
   102   }
       
   103 
       
   104   static bool is_signature_polymorphic_intrinsic(vmIntrinsics::ID iid) {
       
   105     assert(is_signature_polymorphic(iid), "");
       
   106     // Most sig-poly methods are intrinsics which do not require an
       
   107     // appeal to Java for adapter code.
       
   108     return (iid != vmIntrinsics::_invokeGeneric);
       
   109   }
       
   110 
       
   111   static bool is_signature_polymorphic_static(vmIntrinsics::ID iid) {
       
   112     assert(is_signature_polymorphic(iid), "");
       
   113     return (iid >= vmIntrinsics::FIRST_MH_STATIC &&
       
   114             iid <= vmIntrinsics::LAST_MH_SIG_POLY);
       
   115   }
       
   116 
       
   117   static bool has_member_arg(vmIntrinsics::ID iid) {
       
   118     assert(is_signature_polymorphic(iid), "");
       
   119     return (iid >= vmIntrinsics::_linkToVirtual &&
       
   120             iid <= vmIntrinsics::_linkToInterface);
       
   121   }
       
   122   static bool has_member_arg(Symbol* klass, Symbol* name) {
       
   123     if ((klass == vmSymbols::java_lang_invoke_MethodHandle() ||
       
   124          klass == vmSymbols::java_lang_invoke_VarHandle()) &&
       
   125         is_signature_polymorphic_name(name)) {
       
   126       vmIntrinsics::ID iid = signature_polymorphic_name_id(name);
       
   127       return has_member_arg(iid);
       
   128     }
       
   129     return false;
       
   130   }
       
   131 
       
   132   static Symbol* signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid);
       
   133   static int signature_polymorphic_intrinsic_ref_kind(vmIntrinsics::ID iid);
       
   134 
       
   135   static vmIntrinsics::ID signature_polymorphic_name_id(Klass* klass, Symbol* name);
       
   136   static vmIntrinsics::ID signature_polymorphic_name_id(Symbol* name);
       
   137   static bool is_signature_polymorphic_name(Symbol* name) {
       
   138     return signature_polymorphic_name_id(name) != vmIntrinsics::_none;
       
   139   }
       
   140   static bool is_method_handle_invoke_name(Klass* klass, Symbol* name);
       
   141   static bool is_signature_polymorphic_name(Klass* klass, Symbol* name) {
       
   142     return signature_polymorphic_name_id(klass, name) != vmIntrinsics::_none;
       
   143   }
       
   144 
       
   145   static Bytecodes::Code signature_polymorphic_intrinsic_bytecode(vmIntrinsics::ID id);
       
   146 
       
   147   static int get_named_constant(int which, Handle name_box, TRAPS);
       
   148 
       
   149 public:
       
   150   static Symbol* lookup_signature(oop type_str, bool polymorphic, TRAPS);  // use TempNewSymbol
       
   151   static Symbol* lookup_basic_type_signature(Symbol* sig, bool keep_last_arg, TRAPS);  // use TempNewSymbol
       
   152   static Symbol* lookup_basic_type_signature(Symbol* sig, TRAPS) {
       
   153     return lookup_basic_type_signature(sig, false, THREAD);
       
   154   }
       
   155   static bool is_basic_type_signature(Symbol* sig);
       
   156 
       
   157   static Symbol* lookup_method_type(Symbol* msig, Handle mtype, TRAPS);
       
   158 
       
   159   static void print_as_method_type_on(outputStream* st, Symbol* sig) {
       
   160     print_as_basic_type_signature_on(st, sig, true, true);
       
   161   }
       
   162   static void print_as_basic_type_signature_on(outputStream* st, Symbol* sig, bool keep_arrays = false, bool keep_basic_names = false);
       
   163 
       
   164   // decoding CONSTANT_MethodHandle constants
       
   165   enum { JVM_REF_MIN = JVM_REF_getField, JVM_REF_MAX = JVM_REF_invokeInterface };
       
   166   static bool ref_kind_is_valid(int ref_kind) {
       
   167     return (ref_kind >= JVM_REF_MIN && ref_kind <= JVM_REF_MAX);
       
   168   }
       
   169   static bool ref_kind_is_field(int ref_kind) {
       
   170     assert(ref_kind_is_valid(ref_kind), "");
       
   171     return (ref_kind <= JVM_REF_putStatic);
       
   172   }
       
   173   static bool ref_kind_is_getter(int ref_kind) {
       
   174     assert(ref_kind_is_valid(ref_kind), "");
       
   175     return (ref_kind <= JVM_REF_getStatic);
       
   176   }
       
   177   static bool ref_kind_is_setter(int ref_kind) {
       
   178     return ref_kind_is_field(ref_kind) && !ref_kind_is_getter(ref_kind);
       
   179   }
       
   180   static bool ref_kind_is_method(int ref_kind) {
       
   181     return !ref_kind_is_field(ref_kind) && (ref_kind != JVM_REF_newInvokeSpecial);
       
   182   }
       
   183   static bool ref_kind_has_receiver(int ref_kind) {
       
   184     assert(ref_kind_is_valid(ref_kind), "");
       
   185     return (ref_kind & 1) != 0;
       
   186   }
       
   187   static bool ref_kind_is_static(int ref_kind) {
       
   188     return !ref_kind_has_receiver(ref_kind) && (ref_kind != JVM_REF_newInvokeSpecial);
       
   189   }
       
   190   static bool ref_kind_does_dispatch(int ref_kind) {
       
   191     return (ref_kind == JVM_REF_invokeVirtual ||
       
   192             ref_kind == JVM_REF_invokeInterface);
       
   193   }
       
   194 
       
   195 #include CPU_HEADER(methodHandles)
       
   196 
       
   197   // Tracing
       
   198   static void trace_method_handle(MacroAssembler* _masm, const char* adaptername) PRODUCT_RETURN;
       
   199   static void trace_method_handle_interpreter_entry(MacroAssembler* _masm, vmIntrinsics::ID iid) {
       
   200     if (TraceMethodHandles) {
       
   201       const char* name = vmIntrinsics::name_at(iid);
       
   202       if (*name == '_')  name += 1;
       
   203       const size_t len = strlen(name) + 50;
       
   204       char* qname = NEW_C_HEAP_ARRAY(char, len, mtInternal);
       
   205       const char* suffix = "";
       
   206       if (is_signature_polymorphic(iid)) {
       
   207         if (is_signature_polymorphic_static(iid))
       
   208           suffix = "/static";
       
   209         else
       
   210           suffix = "/private";
       
   211       }
       
   212       jio_snprintf(qname, len, "MethodHandle::interpreter_entry::%s%s", name, suffix);
       
   213       trace_method_handle(_masm, qname);
       
   214       // Note:  Don't free the allocated char array because it's used
       
   215       // during runtime.
       
   216     }
       
   217   }
       
   218 };
       
   219 
       
   220 //------------------------------------------------------------------------------
       
   221 // MethodHandlesAdapterGenerator
       
   222 //
       
   223 class MethodHandlesAdapterGenerator : public StubCodeGenerator {
       
   224 public:
       
   225   MethodHandlesAdapterGenerator(CodeBuffer* code) : StubCodeGenerator(code, PrintMethodHandleStubs) {}
       
   226 
       
   227   void generate();
       
   228 };
       
   229 
       
   230 #endif // SHARE_VM_PRIMS_METHODHANDLES_HPP