twisti@2047: /* stefank@2314: * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. twisti@2047: * Copyright 2008, 2009 Red Hat, Inc. twisti@2047: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. twisti@2047: * twisti@2047: * This code is free software; you can redistribute it and/or modify it twisti@2047: * under the terms of the GNU General Public License version 2 only, as twisti@2047: * published by the Free Software Foundation. twisti@2047: * twisti@2047: * This code is distributed in the hope that it will be useful, but WITHOUT twisti@2047: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or twisti@2047: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License twisti@2047: * version 2 for more details (a copy is included in the LICENSE file that twisti@2047: * accompanied this code). twisti@2047: * twisti@2047: * You should have received a copy of the GNU General Public License version twisti@2047: * 2 along with this work; if not, write to the Free Software Foundation, twisti@2047: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. twisti@2047: * twisti@2047: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA twisti@2047: * or visit www.oracle.com if you need additional information or have any twisti@2047: * questions. twisti@2047: * twisti@2047: */ twisti@2047: stefank@2314: #ifndef SHARE_VM_SHARK_SHARKCOMPILER_HPP stefank@2314: #define SHARE_VM_SHARK_SHARKCOMPILER_HPP stefank@2314: stefank@2314: #include "ci/ciEnv.hpp" stefank@2314: #include "ci/ciMethod.hpp" stefank@2314: #include "compiler/abstractCompiler.hpp" stefank@2314: #include "compiler/compileBroker.hpp" stefank@2314: #include "shark/llvmHeaders.hpp" stefank@2314: #include "shark/sharkMemoryManager.hpp" stefank@2314: twisti@2047: class SharkContext; twisti@2047: twisti@2047: class SharkCompiler : public AbstractCompiler { twisti@2047: public: twisti@2047: // Creation twisti@2047: SharkCompiler(); twisti@2047: twisti@2047: // Name of this compiler twisti@2047: const char *name() { return "shark"; } twisti@2047: twisti@2047: // Missing feature tests twisti@2047: bool supports_native() { return true; } twisti@2047: bool supports_osr() { return true; } twisti@2047: twisti@2047: // Customization twisti@2047: bool needs_adapters() { return false; } twisti@2047: bool needs_stubs() { return false; } twisti@2047: twisti@2047: // Initialization twisti@2047: void initialize(); twisti@2047: twisti@2047: // Compile a normal (bytecode) method and install it in the VM twisti@2047: void compile_method(ciEnv* env, ciMethod* target, int entry_bci); twisti@2047: twisti@2047: // Generate a wrapper for a native (JNI) method twisti@2047: nmethod* generate_native_wrapper(MacroAssembler* masm, twisti@2047: methodHandle target, twisti@2047: BasicType* arg_types, twisti@2047: BasicType return_type); twisti@2047: twisti@2047: // Free compiled methods (and native wrappers) twisti@2047: void free_compiled_method(address code); twisti@2047: twisti@2047: // Each thread generating IR needs its own context. The normal twisti@2047: // context is used for bytecode methods, and is protected from twisti@2047: // multiple simultaneous accesses by being restricted to the twisti@2047: // compiler thread. The native context is used for JNI methods, twisti@2047: // and is protected from multiple simultaneous accesses by the twisti@2047: // adapter handler library lock. twisti@2047: private: twisti@2047: SharkContext* _normal_context; twisti@2047: SharkContext* _native_context; twisti@2047: twisti@2047: public: twisti@2047: SharkContext* context() const { twisti@2047: if (JavaThread::current()->is_Compiler_thread()) { twisti@2047: return _normal_context; twisti@2047: } twisti@2047: else { twisti@2047: assert(AdapterHandlerLibrary_lock->owned_by_self(), "should be"); twisti@2047: return _native_context; twisti@2047: } twisti@2047: } twisti@2047: twisti@2047: // The LLVM execution engine is the JIT we use to generate native twisti@2047: // code. It is thread safe, but we need to protect it with a lock twisti@2047: // of our own because otherwise LLVM's lock and HotSpot's locks twisti@2047: // interleave and deadlock. The SharkMemoryManager is not thread twisti@2047: // safe, and is protected by the same lock as the execution engine. twisti@2047: private: twisti@2047: Monitor* _execution_engine_lock; twisti@2047: SharkMemoryManager* _memory_manager; twisti@2047: llvm::ExecutionEngine* _execution_engine; twisti@2047: twisti@2047: private: twisti@2047: Monitor* execution_engine_lock() const { twisti@2047: return _execution_engine_lock; twisti@2047: } twisti@2047: SharkMemoryManager* memory_manager() const { twisti@2047: assert(execution_engine_lock()->owned_by_self(), "should be"); twisti@2047: return _memory_manager; twisti@2047: } twisti@2047: llvm::ExecutionEngine* execution_engine() const { twisti@2047: assert(execution_engine_lock()->owned_by_self(), "should be"); twisti@2047: return _execution_engine; twisti@2047: } twisti@2047: twisti@2047: // Global access twisti@2047: public: twisti@2047: static SharkCompiler* compiler() { twisti@2200: AbstractCompiler *compiler = CompileBroker::compiler(CompLevel_simple); twisti@2047: assert(compiler->is_shark() && compiler->is_initialized(), "should be"); twisti@2047: return (SharkCompiler *) compiler; twisti@2047: } twisti@2047: twisti@2047: // Helpers twisti@2047: private: twisti@2047: static const char* methodname(const char* klass, const char* method); twisti@2047: void generate_native_code(SharkEntry* entry, twisti@2047: llvm::Function* function, twisti@2047: const char* name); twisti@2047: void free_queued_methods(); twisti@2047: }; stefank@2314: stefank@2314: #endif // SHARE_VM_SHARK_SHARKCOMPILER_HPP