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