1 /*
   2  * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2008, 2009, 2010, 2011 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #ifndef SHARE_VM_SHARK_SHARKCOMPILER_HPP
  27 #define SHARE_VM_SHARK_SHARKCOMPILER_HPP
  28 
  29 #include "ci/ciEnv.hpp"
  30 #include "ci/ciMethod.hpp"
  31 #include "compiler/abstractCompiler.hpp"
  32 #include "compiler/compileBroker.hpp"
  33 #include "compiler/compilerDirectives.hpp"
  34 #include "shark/llvmHeaders.hpp"
  35 #include "shark/sharkMemoryManager.hpp"
  36 
  37 class SharkContext;
  38 
  39 class SharkCompiler : public AbstractCompiler {
  40  public:
  41   // Creation
  42   SharkCompiler();
  43 
  44   // Name of this compiler
  45   const char *name()     { return "shark"; }
  46 
  47   // Missing feature tests
  48   bool supports_native() { return true; }
  49   bool supports_osr()    { return true; }
  50   bool can_compile_method(methodHandle method)  {
  51     return ! (method->is_method_handle_intrinsic() || method->is_compiled_lambda_form());
  52   }
  53 
  54   // Initialization
  55   void initialize();
  56 
  57   // Compile a normal (bytecode) method and install it in the VM
  58   void compile_method(ciEnv* env, ciMethod* target, int entry_bci, DirectiveSet* dirset);
  59 
  60   // Print compilation timers and statistics
  61   void print_timers();
  62 
  63   // Generate a wrapper for a native (JNI) method
  64   nmethod* generate_native_wrapper(MacroAssembler* masm,
  65                                    methodHandle    target,
  66                                    int             compile_id,
  67                                    BasicType*      arg_types,
  68                                    BasicType       return_type);
  69 
  70   // Free compiled methods (and native wrappers)
  71   void free_compiled_method(address code);
  72 
  73   // Each thread generating IR needs its own context.  The normal
  74   // context is used for bytecode methods, and is protected from
  75   // multiple simultaneous accesses by being restricted to the
  76   // compiler thread.  The native context is used for JNI methods,
  77   // and is protected from multiple simultaneous accesses by the
  78   // adapter handler library lock.
  79  private:
  80   SharkContext* _normal_context;
  81   SharkContext* _native_context;
  82 
  83  public:
  84   SharkContext* context() const {
  85     if (JavaThread::current()->is_Compiler_thread()) {
  86       return _normal_context;
  87     }
  88     else {
  89       assert(AdapterHandlerLibrary_lock->owned_by_self(), "should be");
  90       return _native_context;
  91     }
  92   }
  93 
  94   // The LLVM execution engine is the JIT we use to generate native
  95   // code.  It is thread safe, but we need to protect it with a lock
  96   // of our own because otherwise LLVM's lock and HotSpot's locks
  97   // interleave and deadlock.  The SharkMemoryManager is not thread
  98   // safe, and is protected by the same lock as the execution engine.
  99  private:
 100   Monitor*               _execution_engine_lock;
 101   SharkMemoryManager*    _memory_manager;
 102   llvm::ExecutionEngine* _execution_engine;
 103 
 104  private:
 105   Monitor* execution_engine_lock() const {
 106     return _execution_engine_lock;
 107   }
 108   SharkMemoryManager* memory_manager() const {
 109     assert(execution_engine_lock()->owned_by_self(), "should be");
 110     return _memory_manager;
 111   }
 112   llvm::ExecutionEngine* execution_engine() const {
 113     assert(execution_engine_lock()->owned_by_self(), "should be");
 114     return _execution_engine;
 115   }
 116 
 117   // Global access
 118  public:
 119   static SharkCompiler* compiler() {
 120     AbstractCompiler *compiler =
 121       CompileBroker::compiler(CompLevel_full_optimization);
 122     assert(compiler->is_shark() && compiler->is_initialized(), "should be");
 123     return (SharkCompiler *) compiler;
 124   }
 125 
 126   // Helpers
 127  private:
 128   static const char* methodname(const char* klass, const char* method);
 129   void generate_native_code(SharkEntry*     entry,
 130                             llvm::Function* function,
 131                             const char*     name);
 132   void free_queued_methods();
 133 };
 134 
 135 #endif // SHARE_VM_SHARK_SHARKCOMPILER_HPP