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" |
| 21 | #include "method_compiler.h" |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame^] | 22 | #include "upcall_compiler.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 23 | |
| 24 | #include <llvm/ADT/OwningPtr.h> |
| 25 | #include <llvm/Bitcode/ReaderWriter.h> |
| 26 | #include <llvm/DerivedTypes.h> |
| 27 | #include <llvm/LLVMContext.h> |
| 28 | #include <llvm/Module.h> |
| 29 | #include <llvm/Support/ToolOutputFile.h> |
| 30 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 31 | namespace art { |
| 32 | namespace compiler_llvm { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 33 | |
| 34 | |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 35 | namespace { |
| 36 | using namespace llvm; |
| 37 | #include "art_module.cc" |
| 38 | } |
| 39 | |
| 40 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 41 | CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set) |
| 42 | : compiler_(compiler), compiler_lock_("llvm_compiler_lock"), |
| 43 | insn_set_(insn_set), context_(new llvm::LLVMContext()) { |
| 44 | |
| 45 | // Create the module and include the runtime function declaration |
| 46 | module_ = new llvm::Module("art", *context_); |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 47 | makeLLVMModuleContents(module_); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 48 | |
| 49 | // Create IRBuilder |
| 50 | irb_.reset(new IRBuilder(*context_, *module_)); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | CompilerLLVM::~CompilerLLVM() { |
| 55 | } |
| 56 | |
| 57 | |
| 58 | void CompilerLLVM::MaterializeLLVMModule() { |
| 59 | #if !defined(NDEBUG) |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 60 | // TODO: Add options to JNI_CreateJavaVM() and dex2oat, so that we don't |
| 61 | // have to hard-code the path. |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 62 | WriteBitcodeToFile("/tmp/art_llvm_module.bc"); |
| 63 | #endif |
| 64 | } |
| 65 | |
| 66 | |
| 67 | void CompilerLLVM::WriteBitcodeToFile(std::string const &filepath) { |
| 68 | std::string error_msg; |
| 69 | |
| 70 | // Write the translated bitcode |
| 71 | llvm::OwningPtr<llvm::tool_output_file> |
| 72 | out(new llvm::tool_output_file(filepath.c_str(), error_msg, |
| 73 | llvm::raw_fd_ostream::F_Binary)); |
| 74 | |
| 75 | if (!error_msg.empty()) { |
| 76 | LOG(FATAL) << "Unable to open file: " << error_msg; |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | llvm::WriteBitcodeToFile(module_, out->os()); |
| 81 | out->keep(); |
| 82 | |
| 83 | LOG(DEBUG) << "Bitcode Written At: " << filepath; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | CompiledMethod* |
| 88 | CompilerLLVM::CompileDexMethod(DexFile::CodeItem const* code_item, |
| 89 | uint32_t access_flags, |
| 90 | uint32_t method_idx, |
| 91 | ClassLoader const* class_loader, |
| 92 | DexFile const& dex_file) { |
| 93 | |
| 94 | MutexLock GUARD(compiler_lock_); |
| 95 | |
| 96 | ClassLinker *class_linker = Runtime::Current()->GetClassLinker(); |
| 97 | DexCache *dex_cache = class_linker->FindDexCache(dex_file); |
| 98 | |
| 99 | UniquePtr<MethodCompiler> method_compiler( |
| 100 | new MethodCompiler(insn_set_, compiler_, class_linker, class_loader, |
| 101 | &dex_file, dex_cache, code_item, method_idx, |
| 102 | access_flags)); |
| 103 | |
| 104 | return method_compiler->Compile(); |
| 105 | } |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 106 | |
| 107 | |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame^] | 108 | CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static, |
| 109 | char const *shorty) { |
| 110 | |
| 111 | MutexLock GUARD(compiler_lock_); |
| 112 | |
| 113 | UniquePtr<UpcallCompiler> upcall_compiler( |
| 114 | new UpcallCompiler(insn_set_, *compiler_)); |
| 115 | |
| 116 | return upcall_compiler->CreateStub(is_static, shorty); |
| 117 | } |
| 118 | |
| 119 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 120 | } // namespace compiler_llvm |
| 121 | } // namespace art |