blob: 8a9b3622e554c8d7b87056ba3b59d48fd67974c0 [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
Logan Chien83426162011-12-09 09:29:50 +080030namespace art {
31namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080032
33
Logan Chien42e0e152012-01-13 15:42:36 +080034namespace {
35using namespace llvm;
36#include "art_module.cc"
37}
38
39
Shih-wei Liaod1fec812012-02-13 09:51:10 -080040CompilerLLVM::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 Chien42e0e152012-01-13 15:42:36 +080046 makeLLVMModuleContents(module_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080047
48 // Create IRBuilder
49 irb_.reset(new IRBuilder(*context_, *module_));
50}
51
52
53CompilerLLVM::~CompilerLLVM() {
54}
55
56
57void CompilerLLVM::MaterializeLLVMModule() {
58#if !defined(NDEBUG)
Logan Chien83426162011-12-09 09:29:50 +080059 // TODO: Add options to JNI_CreateJavaVM() and dex2oat, so that we don't
60 // have to hard-code the path.
Shih-wei Liaod1fec812012-02-13 09:51:10 -080061 WriteBitcodeToFile("/tmp/art_llvm_module.bc");
62#endif
63}
64
65
66void 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
86CompiledMethod*
87CompilerLLVM::CompileDexMethod(DexFile::CodeItem const* code_item,
88 uint32_t access_flags,
89 uint32_t method_idx,
90 ClassLoader const* class_loader,
91 DexFile const& dex_file) {
92
93 MutexLock GUARD(compiler_lock_);
94
95 ClassLinker *class_linker = Runtime::Current()->GetClassLinker();
96 DexCache *dex_cache = class_linker->FindDexCache(dex_file);
97
98 UniquePtr<MethodCompiler> method_compiler(
99 new MethodCompiler(insn_set_, compiler_, class_linker, class_loader,
100 &dex_file, dex_cache, code_item, method_idx,
101 access_flags));
102
103 return method_compiler->Compile();
104}
Logan Chien83426162011-12-09 09:29:50 +0800105
106
107} // namespace compiler_llvm
108} // namespace art