blob: c0f58814384abd8bfdbc93a2e7bd9d9120d6c44d [file] [log] [blame]
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001/*
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"
22
23#include <llvm/ADT/OwningPtr.h>
24#include <llvm/Bitcode/ReaderWriter.h>
25#include <llvm/DerivedTypes.h>
26#include <llvm/LLVMContext.h>
27#include <llvm/Module.h>
28#include <llvm/Support/ToolOutputFile.h>
29
30using namespace art;
31using namespace art::compiler_llvm;
32
33
34CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
35: compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
36 insn_set_(insn_set), context_(new llvm::LLVMContext()) {
37
38 // Create the module and include the runtime function declaration
39 module_ = new llvm::Module("art", *context_);
40
41 // Create IRBuilder
42 irb_.reset(new IRBuilder(*context_, *module_));
43}
44
45
46CompilerLLVM::~CompilerLLVM() {
47}
48
49
50void CompilerLLVM::MaterializeLLVMModule() {
51#if !defined(NDEBUG)
52 // TODO: For device, need to use another temporary path.
53 WriteBitcodeToFile("/tmp/art_llvm_module.bc");
54#endif
55}
56
57
58void CompilerLLVM::WriteBitcodeToFile(std::string const &filepath) {
59 std::string error_msg;
60
61 // Write the translated bitcode
62 llvm::OwningPtr<llvm::tool_output_file>
63 out(new llvm::tool_output_file(filepath.c_str(), error_msg,
64 llvm::raw_fd_ostream::F_Binary));
65
66 if (!error_msg.empty()) {
67 LOG(FATAL) << "Unable to open file: " << error_msg;
68 return;
69 }
70
71 llvm::WriteBitcodeToFile(module_, out->os());
72 out->keep();
73
74 LOG(DEBUG) << "Bitcode Written At: " << filepath;
75}
76
77
78CompiledMethod*
79CompilerLLVM::CompileDexMethod(DexFile::CodeItem const* code_item,
80 uint32_t access_flags,
81 uint32_t method_idx,
82 ClassLoader const* class_loader,
83 DexFile const& dex_file) {
84
85 MutexLock GUARD(compiler_lock_);
86
87 ClassLinker *class_linker = Runtime::Current()->GetClassLinker();
88 DexCache *dex_cache = class_linker->FindDexCache(dex_file);
89
90 UniquePtr<MethodCompiler> method_compiler(
91 new MethodCompiler(insn_set_, compiler_, class_linker, class_loader,
92 &dex_file, dex_cache, code_item, method_idx,
93 access_flags));
94
95 return method_compiler->Compile();
96}