1 /*
   2  * Copyright (c) 2012, 2018, 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 #ifndef SHARE_VM_JVMCI_JVMCI_RUNTIME_HPP
  25 #define SHARE_VM_JVMCI_JVMCI_RUNTIME_HPP
  26 
  27 #include "interpreter/interpreter.hpp"
  28 #include "memory/allocation.hpp"
  29 #include "runtime/arguments.hpp"
  30 #include "runtime/deoptimization.hpp"
  31 
  32 #define JVMCI_ERROR(...)       \
  33   { Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::jdk_vm_ci_common_JVMCIError(), __VA_ARGS__); return; }
  34 
  35 #define JVMCI_ERROR_(ret, ...) \
  36   { Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::jdk_vm_ci_common_JVMCIError(), __VA_ARGS__); return ret; }
  37 
  38 #define JVMCI_ERROR_0(...)    JVMCI_ERROR_(0, __VA_ARGS__)
  39 #define JVMCI_ERROR_NULL(...) JVMCI_ERROR_(NULL, __VA_ARGS__)
  40 #define JVMCI_ERROR_OK(...)   JVMCI_ERROR_(JVMCIEnv::ok, __VA_ARGS__)
  41 #define CHECK_OK              CHECK_(JVMCIEnv::ok)
  42 
  43 class JVMCIRuntime: public AllStatic {
  44  public:
  45   // Constants describing whether JVMCI wants to be able to adjust the compilation
  46   // level selected for a method by the VM compilation policy and if so, based on
  47   // what information about the method being schedule for compilation.
  48   enum CompLevelAdjustment {
  49      none = 0,             // no adjustment
  50      by_holder = 1,        // adjust based on declaring class of method
  51      by_full_signature = 2 // adjust based on declaring class, name and signature of method
  52   };
  53 
  54  private:
  55   static jobject _HotSpotJVMCIRuntime_instance;
  56   static bool _HotSpotJVMCIRuntime_initialized;
  57   static bool _well_known_classes_initialized;
  58 
  59   static int _trivial_prefixes_count;
  60   static char** _trivial_prefixes;
  61 
  62   static CompLevelAdjustment _comp_level_adjustment;
  63 
  64   static bool _shutdown_called;
  65 
  66   static CompLevel adjust_comp_level_inner(const methodHandle& method, bool is_osr, CompLevel level, JavaThread* thread);
  67 
  68  public:
  69   static bool is_HotSpotJVMCIRuntime_initialized() {
  70     return _HotSpotJVMCIRuntime_initialized;
  71   }
  72 
  73   /**
  74    * Gets the singleton HotSpotJVMCIRuntime instance, initializing it if necessary
  75    */
  76   static Handle get_HotSpotJVMCIRuntime(TRAPS);
  77 
  78   static jobject get_HotSpotJVMCIRuntime_jobject(TRAPS) {
  79     initialize_JVMCI(CHECK_NULL);
  80     assert(_HotSpotJVMCIRuntime_initialized, "must be");
  81     return _HotSpotJVMCIRuntime_instance;
  82   }
  83 
  84   static Handle callStatic(const char* className, const char* methodName, const char* returnType, JavaCallArguments* args, TRAPS);
  85 
  86   /**
  87    * Determines if the VM is sufficiently booted to initialize JVMCI.
  88    */
  89   static bool can_initialize_JVMCI();
  90 
  91   /**
  92    * Trigger initialization of HotSpotJVMCIRuntime through JVMCI.getRuntime()
  93    */
  94   static void initialize_JVMCI(TRAPS);
  95 
  96   /**
  97    * Explicitly initialize HotSpotJVMCIRuntime itself
  98    */
  99   static void initialize_HotSpotJVMCIRuntime(TRAPS);
 100 
 101   static void initialize_well_known_classes(TRAPS);
 102 
 103   static void metadata_do(void f(Metadata*));
 104 
 105   static void shutdown(TRAPS);
 106 
 107   static void bootstrap_finished(TRAPS);
 108 
 109   static bool shutdown_called() {
 110     return _shutdown_called;
 111   }
 112 
 113   static bool treat_as_trivial(Method* method);
 114 
 115   /**
 116    * Lets JVMCI modify the compilation level currently selected for a method by
 117    * the VM compilation policy.
 118    *
 119    * @param method the method being scheduled for compilation
 120    * @param is_osr specifies if the compilation is an OSR compilation
 121    * @param level the compilation level currently selected by the VM compilation policy
 122    * @param thread the current thread
 123    * @return the compilation level to use for the compilation
 124    */
 125   static CompLevel adjust_comp_level(const methodHandle& method, bool is_osr, CompLevel level, JavaThread* thread);
 126 
 127   static BasicType kindToBasicType(Handle kind, TRAPS);
 128 
 129   // The following routines are all called from compiled JVMCI code
 130 
 131   static void new_instance(JavaThread* thread, Klass* klass);
 132   static void new_array(JavaThread* thread, Klass* klass, jint length);
 133   static void new_multi_array(JavaThread* thread, Klass* klass, int rank, jint* dims);
 134   static void dynamic_new_array(JavaThread* thread, oopDesc* element_mirror, jint length);
 135   static void dynamic_new_instance(JavaThread* thread, oopDesc* type_mirror);
 136   static jboolean thread_is_interrupted(JavaThread* thread, oopDesc* obj, jboolean clear_interrupted);
 137   static void vm_message(jboolean vmError, jlong format, jlong v1, jlong v2, jlong v3);
 138   static jint identity_hash_code(JavaThread* thread, oopDesc* obj);
 139   static address exception_handler_for_pc(JavaThread* thread);
 140   static void monitorenter(JavaThread* thread, oopDesc* obj, BasicLock* lock);
 141   static void monitorexit (JavaThread* thread, oopDesc* obj, BasicLock* lock);
 142   static jboolean object_notify(JavaThread* thread, oopDesc* obj);
 143   static jboolean object_notifyAll(JavaThread* thread, oopDesc* obj);
 144   static void vm_error(JavaThread* thread, jlong where, jlong format, jlong value);
 145   static oopDesc* load_and_clear_exception(JavaThread* thread);
 146   static void log_printf(JavaThread* thread, oopDesc* format, jlong v1, jlong v2, jlong v3);
 147   static void log_primitive(JavaThread* thread, jchar typeChar, jlong value, jboolean newline);
 148   // Print the passed in object, optionally followed by a newline.  If
 149   // as_string is true and the object is a java.lang.String then it
 150   // printed as a string, otherwise the type of the object is printed
 151   // followed by its address.
 152   static void log_object(JavaThread* thread, oopDesc* object, bool as_string, bool newline);
 153   static void write_barrier_pre(JavaThread* thread, oopDesc* obj);
 154   static void write_barrier_post(JavaThread* thread, void* card);
 155   static jboolean validate_object(JavaThread* thread, oopDesc* parent, oopDesc* child);
 156 
 157   // used to throw exceptions from compiled JVMCI code
 158   static void throw_and_post_jvmti_exception(JavaThread* thread, const char* exception, const char* message);
 159   // helper methods to throw exception with complex messages
 160   static void throw_klass_external_name_exception(JavaThread* thread, const char* exception, Klass* klass);
 161   static void throw_class_cast_exception(JavaThread* thread, const char* exception, Klass* caster_klass, Klass* target_klass);
 162 
 163   // Forces initialization of the JVMCI runtime.
 164   static void force_initialization(TRAPS);
 165 
 166   // Test only function
 167   static int test_deoptimize_call_int(JavaThread* thread, int value);
 168 };
 169 
 170 // Tracing macros.
 171 
 172 #define IF_TRACE_jvmci_1 if (!(JVMCITraceLevel >= 1)) ; else
 173 #define IF_TRACE_jvmci_2 if (!(JVMCITraceLevel >= 2)) ; else
 174 #define IF_TRACE_jvmci_3 if (!(JVMCITraceLevel >= 3)) ; else
 175 #define IF_TRACE_jvmci_4 if (!(JVMCITraceLevel >= 4)) ; else
 176 #define IF_TRACE_jvmci_5 if (!(JVMCITraceLevel >= 5)) ; else
 177 
 178 #define TRACE_jvmci_1 if (!(JVMCITraceLevel >= 1 && (tty->print("JVMCITrace-1: "), true))) ; else tty->print_cr
 179 #define TRACE_jvmci_2 if (!(JVMCITraceLevel >= 2 && (tty->print("   JVMCITrace-2: "), true))) ; else tty->print_cr
 180 #define TRACE_jvmci_3 if (!(JVMCITraceLevel >= 3 && (tty->print("      JVMCITrace-3: "), true))) ; else tty->print_cr
 181 #define TRACE_jvmci_4 if (!(JVMCITraceLevel >= 4 && (tty->print("         JVMCITrace-4: "), true))) ; else tty->print_cr
 182 #define TRACE_jvmci_5 if (!(JVMCITraceLevel >= 5 && (tty->print("            JVMCITrace-5: "), true))) ; else tty->print_cr
 183 
 184 #endif // SHARE_VM_JVMCI_JVMCI_RUNTIME_HPP