Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "compiler_llvm.h" |
| 18 | |
| 19 | #include "compiler.h" |
| 20 | #include "ir_builder.h" |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame^] | 21 | #include "jni_compiler.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 22 | #include "method_compiler.h" |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 23 | #include "oat_compilation_unit.h" |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 24 | #include "upcall_compiler.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 25 | |
| 26 | #include <llvm/ADT/OwningPtr.h> |
| 27 | #include <llvm/Bitcode/ReaderWriter.h> |
| 28 | #include <llvm/DerivedTypes.h> |
| 29 | #include <llvm/LLVMContext.h> |
| 30 | #include <llvm/Module.h> |
| 31 | #include <llvm/Support/ToolOutputFile.h> |
| 32 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 33 | namespace art { |
| 34 | namespace compiler_llvm { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 35 | |
| 36 | |
Logan Chien | e75a8cc | 2012-02-24 12:26:43 +0800 | [diff] [blame] | 37 | llvm::Module* makeLLVMModuleContents(llvm::Module* module); |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 38 | |
| 39 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 40 | CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set) |
| 41 | : compiler_(compiler), compiler_lock_("llvm_compiler_lock"), |
| 42 | insn_set_(insn_set), context_(new llvm::LLVMContext()) { |
| 43 | |
| 44 | // Create the module and include the runtime function declaration |
| 45 | module_ = new llvm::Module("art", *context_); |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 46 | makeLLVMModuleContents(module_); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 47 | |
| 48 | // Create IRBuilder |
| 49 | irb_.reset(new IRBuilder(*context_, *module_)); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | CompilerLLVM::~CompilerLLVM() { |
| 54 | } |
| 55 | |
| 56 | |
| 57 | void CompilerLLVM::MaterializeLLVMModule() { |
| 58 | #if !defined(NDEBUG) |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 59 | // TODO: Add options to JNI_CreateJavaVM() and dex2oat, so that we don't |
| 60 | // have to hard-code the path. |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 61 | WriteBitcodeToFile("/tmp/art_llvm_module.bc"); |
| 62 | #endif |
| 63 | } |
| 64 | |
| 65 | |
| 66 | void CompilerLLVM::WriteBitcodeToFile(std::string const &filepath) { |
| 67 | std::string error_msg; |
| 68 | |
| 69 | // Write the translated bitcode |
| 70 | llvm::OwningPtr<llvm::tool_output_file> |
| 71 | out(new llvm::tool_output_file(filepath.c_str(), error_msg, |
| 72 | llvm::raw_fd_ostream::F_Binary)); |
| 73 | |
| 74 | if (!error_msg.empty()) { |
| 75 | LOG(FATAL) << "Unable to open file: " << error_msg; |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | llvm::WriteBitcodeToFile(module_, out->os()); |
| 80 | out->keep(); |
| 81 | |
| 82 | LOG(DEBUG) << "Bitcode Written At: " << filepath; |
| 83 | } |
| 84 | |
| 85 | |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 86 | CompiledMethod* CompilerLLVM::CompileDexMethod(OatCompilationUnit* oat_compilation_unit) { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 87 | MutexLock GUARD(compiler_lock_); |
| 88 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 89 | UniquePtr<MethodCompiler> method_compiler( |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 90 | new MethodCompiler(insn_set_, compiler_, oat_compilation_unit)); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 91 | |
| 92 | return method_compiler->Compile(); |
| 93 | } |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 94 | |
| 95 | |
Logan Chien | 88894ee | 2012-02-13 16:42:22 +0800 | [diff] [blame^] | 96 | CompiledMethod* CompilerLLVM::CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) { |
| 97 | MutexLock GUARD(compiler_lock_); |
| 98 | |
| 99 | UniquePtr<JniCompiler> jni_compiler( |
| 100 | new JniCompiler(insn_set_, *compiler_, oat_compilation_unit)); |
| 101 | |
| 102 | return jni_compiler->Compile(); |
| 103 | } |
| 104 | |
| 105 | |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 106 | CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static, |
| 107 | char const *shorty) { |
| 108 | |
| 109 | MutexLock GUARD(compiler_lock_); |
| 110 | |
| 111 | UniquePtr<UpcallCompiler> upcall_compiler( |
| 112 | new UpcallCompiler(insn_set_, *compiler_)); |
| 113 | |
| 114 | return upcall_compiler->CreateStub(is_static, shorty); |
| 115 | } |
| 116 | |
| 117 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 118 | } // namespace compiler_llvm |
| 119 | } // namespace art |