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