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 "shark/llvmHeaders.hpp"
  34 #include "shark/sharkMemoryManager.hpp"
  35 
  36 class SharkContext;
  37 
  38 class SharkCompiler : public AbstractCompiler {
  39  public:
  40   // Creation
  41   SharkCompiler();
  42 
  43   // Name of this compiler
  44   const char *name()     { return "shark"; }
  45 
  46   // Missing feature tests
  47   bool supports_native() { return true; }
  48   bool supports_osr()    { return true; }
  49   bool can_compile_method(methodHandle method)  {
  50     return ! (method->is_method_handle_intrinsic() || method->is_compiled_lambda_form());
  51   }
  52 
  53   // Initialization
  54   void initialize();
  55 
  56   // Compile a normal (bytecode) method and install it in the VM
  57   void compile_method(ciEnv* env, ciMethod* target, int entry_bci);
  58 
  59   // Print compilation timers and statistics
  60   void print_timers();
  61 
  62   // Generate a wrapper for a native (JNI) method
  63   nmethod* generate_native_wrapper(MacroAssembler* masm,
  64                                    methodHandle    target,
  65                                    int             compile_id,
  66                                    BasicType*      arg_types,
  67                                    BasicType       return_type);
  68 
  69   // Free compiled methods (and native wrappers)
  70   void free_compiled_method(address code);
  71 
  72   // Each thread generating IR needs its own context.  The normal
  73   // context is used for bytecode methods, and is protected from
  74   // multiple simultaneous accesses by being restricted to the
  75   // compiler thread.  The native context is used for JNI methods,
  76   // and is protected from multiple simultaneous accesses by the
  77   // adapter handler library lock.
  78  private:
  79   SharkContext* _normal_context;
  80   SharkContext* _native_context;
  81 
  82  public:
  83   SharkContext* context() const {
  84     if (JavaThread::current()->is_Compiler_thread()) {
  85       return _normal_context;
  86     }
  87     else {
  88       assert(AdapterHandlerLibrary_lock->owned_by_self(), "should be");
  89       return _native_context;
  90     }
  91   }
  92 
  93   // The LLVM execution engine is the JIT we use to generate native
  94   // code.  It is thread safe, but we need to protect it with a lock
  95   // of our own because otherwise LLVM's lock and HotSpot's locks
  96   // interleave and deadlock.  The SharkMemoryManager is not thread
  97   // safe, and is protected by the same lock as the execution engine.
  98  private:
  99   Monitor*               _execution_engine_lock;
 100   SharkMemoryManager*    _memory_manager;
 101   llvm::ExecutionEngine* _execution_engine;
 102 
 103  private:
 104   Monitor* execution_engine_lock() const {
 105     return _execution_engine_lock;
 106   }
 107   SharkMemoryManager* memory_manager() const {
 108     assert(execution_engine_lock()->owned_by_self(), "should be");
 109     return _memory_manager;
 110   }
 111   llvm::ExecutionEngine* execution_engine() const {
 112     assert(execution_engine_lock()->owned_by_self(), "should be");
 113     return _execution_engine;
 114   }
 115 
 116   // Global access
 117  public:
 118   static SharkCompiler* compiler() {
 119     AbstractCompiler *compiler =
 120       CompileBroker::compiler(CompLevel_full_optimization);
 121     assert(compiler->is_shark() && compiler->is_initialized(), "should be");
 122     return (SharkCompiler *) compiler;
 123   }
 124 
 125   // Helpers
 126  private:
 127   static const char* methodname(const char* klass, const char* method);
 128   void generate_native_code(SharkEntry*     entry,
 129                             llvm::Function* function,
 130                             const char*     name);
 131   void free_queued_methods();
 132 };
 133 
 134 #endif // SHARE_VM_SHARK_SHARKCOMPILER_HPP