1 /*
   2  * Copyright (c) 2012, 2015, 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 ParseClosure : public StackObj {
  44   int _lineNo;
  45   char* _filename;
  46   bool _abort;
  47 protected:
  48   void abort() { _abort = true; }
  49   void warn_and_abort(const char* message) {
  50     warn(message);
  51     abort();
  52   }
  53   void warn(const char* message) {
  54     warning("Error at line %d while parsing %s: %s", _lineNo, _filename == NULL ? "?" : _filename, message);
  55   }
  56  public:
  57   ParseClosure() : _lineNo(0), _filename(NULL), _abort(false) {}
  58   void parse_line(char* line) {
  59     _lineNo++;
  60     do_line(line);
  61   }
  62   virtual void do_line(char* line) = 0;
  63   int lineNo() { return _lineNo; }
  64   bool is_aborted() { return _abort; }
  65   void set_filename(char* path) {_filename = path; _lineNo = 0;}
  66 };
  67 
  68 class JVMCIRuntime: public AllStatic {
  69  private:
  70   static jobject _HotSpotJVMCIRuntime_instance;
  71   static bool _HotSpotJVMCIRuntime_initialized;
  72   static bool _well_known_classes_initialized;
  73   static const char* _compiler;
  74   static int _options_count;
  75   static SystemProperty** _options;
  76 
  77   static int _trivial_prefixes_count;
  78   static char** _trivial_prefixes;
  79 
  80   static bool _shutdown_called;
  81 
  82   /**
  83    * Instantiates a service object, calls its default constructor and returns it.
  84    *
  85    * @param name the name of a class implementing jdk.vm.ci.service.Service
  86    */
  87   static Handle create_Service(const char* name, TRAPS);
  88 
  89  public:
  90 
  91   /**
  92    * Parses *.properties files in jre/lib/jvmci/ and adds the properties to plist.
  93    */
  94   static void init_system_properties(SystemProperty** plist);
  95 
  96   /**
  97    * Saves the value of the "jvmci.compiler" system property for processing
  98    * when JVMCI is initialized.
  99    */
 100   static void save_compiler(const char* compiler);
 101 
 102   /**
 103    * Saves the value of the system properties starting with "jvmci.option." for processing
 104    * when JVMCI is initialized.
 105    *
 106    * @param props the head of the system property list
 107    */
 108   static void save_options(SystemProperty* props);
 109 
 110   /**
 111    * If either the PrintFlags or ShowFlags JVMCI option is present,
 112    * then JVMCI is initialized to show the help message.
 113    */
 114   static void maybe_print_flags(TRAPS);
 115 
 116   static bool is_HotSpotJVMCIRuntime_initialized() { return _HotSpotJVMCIRuntime_initialized; }
 117 
 118   /**
 119    * Gets the singleton HotSpotJVMCIRuntime instance, initializing it if necessary
 120    */
 121   static Handle get_HotSpotJVMCIRuntime(TRAPS) {
 122     initialize_JVMCI(CHECK_(Handle()));
 123     return Handle(JNIHandles::resolve_non_null(_HotSpotJVMCIRuntime_instance));
 124   }
 125 
 126   static jobject get_HotSpotJVMCIRuntime_jobject(TRAPS) {
 127     initialize_JVMCI(CHECK_NULL);
 128     assert(_HotSpotJVMCIRuntime_initialized, "must be");
 129     return _HotSpotJVMCIRuntime_instance;
 130   }
 131 
 132   static Handle callStatic(const char* className, const char* methodName, const char* returnType, JavaCallArguments* args, TRAPS);
 133 
 134   /**
 135    * Trigger initialization of HotSpotJVMCIRuntime through JVMCI.getRuntime()
 136    */
 137   static void initialize_JVMCI(TRAPS);
 138 
 139   /**
 140    * Explicitly initialize HotSpotJVMCIRuntime itself
 141    */
 142   static void initialize_HotSpotJVMCIRuntime(TRAPS);
 143 
 144   static void initialize_well_known_classes(TRAPS);
 145 
 146   static void metadata_do(void f(Metadata*));
 147 
 148   static void shutdown(TRAPS);
 149 
 150   static bool shutdown_called() {
 151     return _shutdown_called;
 152   }
 153 
 154   static bool treat_as_trivial(Method* method);
 155   static void parse_lines(char* path, ParseClosure* closure, bool warnStatFailure);
 156 
 157   static BasicType kindToBasicType(Handle kind, TRAPS);
 158 
 159   // The following routines are all called from compiled JVMCI code
 160 
 161   static void new_instance(JavaThread* thread, Klass* klass);
 162   static void new_array(JavaThread* thread, Klass* klass, jint length);
 163   static void new_multi_array(JavaThread* thread, Klass* klass, int rank, jint* dims);
 164   static void dynamic_new_array(JavaThread* thread, oopDesc* element_mirror, jint length);
 165   static void dynamic_new_instance(JavaThread* thread, oopDesc* type_mirror);
 166   static jboolean thread_is_interrupted(JavaThread* thread, oopDesc* obj, jboolean clear_interrupted);
 167   static void vm_message(jboolean vmError, jlong format, jlong v1, jlong v2, jlong v3);
 168   static jint identity_hash_code(JavaThread* thread, oopDesc* obj);
 169   static address exception_handler_for_pc(JavaThread* thread);
 170   static void monitorenter(JavaThread* thread, oopDesc* obj, BasicLock* lock);
 171   static void monitorexit (JavaThread* thread, oopDesc* obj, BasicLock* lock);
 172   static void create_null_exception(JavaThread* thread);
 173   static void create_out_of_bounds_exception(JavaThread* thread, jint index);
 174   static void vm_error(JavaThread* thread, jlong where, jlong format, jlong value);
 175   static oopDesc* load_and_clear_exception(JavaThread* thread);
 176   static void log_printf(JavaThread* thread, oopDesc* format, jlong v1, jlong v2, jlong v3);
 177   static void log_primitive(JavaThread* thread, jchar typeChar, jlong value, jboolean newline);
 178   // Print the passed in object, optionally followed by a newline.  If
 179   // as_string is true and the object is a java.lang.String then it
 180   // printed as a string, otherwise the type of the object is printed
 181   // followed by its address.
 182   static void log_object(JavaThread* thread, oopDesc* object, bool as_string, bool newline);
 183   static void write_barrier_pre(JavaThread* thread, oopDesc* obj);
 184   static void write_barrier_post(JavaThread* thread, void* card);
 185   static jboolean validate_object(JavaThread* thread, oopDesc* parent, oopDesc* child);
 186   static void new_store_pre_barrier(JavaThread* thread);
 187 
 188   // Test only function
 189   static int test_deoptimize_call_int(JavaThread* thread, int value);
 190 };
 191 
 192 // Tracing macros.
 193 
 194 #define IF_TRACE_jvmci_1 if (!(JVMCITraceLevel >= 1)) ; else
 195 #define IF_TRACE_jvmci_2 if (!(JVMCITraceLevel >= 2)) ; else
 196 #define IF_TRACE_jvmci_3 if (!(JVMCITraceLevel >= 3)) ; else
 197 #define IF_TRACE_jvmci_4 if (!(JVMCITraceLevel >= 4)) ; else
 198 #define IF_TRACE_jvmci_5 if (!(JVMCITraceLevel >= 5)) ; else
 199 
 200 #define TRACE_jvmci_1 if (!(JVMCITraceLevel >= 1 && (tty->print("JVMCITrace-1: "), true))) ; else tty->print_cr
 201 #define TRACE_jvmci_2 if (!(JVMCITraceLevel >= 2 && (tty->print("   JVMCITrace-2: "), true))) ; else tty->print_cr
 202 #define TRACE_jvmci_3 if (!(JVMCITraceLevel >= 3 && (tty->print("      JVMCITrace-3: "), true))) ; else tty->print_cr
 203 #define TRACE_jvmci_4 if (!(JVMCITraceLevel >= 4 && (tty->print("         JVMCITrace-4: "), true))) ; else tty->print_cr
 204 #define TRACE_jvmci_5 if (!(JVMCITraceLevel >= 5 && (tty->print("            JVMCITrace-5: "), true))) ; else tty->print_cr
 205 
 206 #endif // SHARE_VM_JVMCI_JVMCI_RUNTIME_HPP