blob: 10aaca4adbf17941e6367072b51e067e597c9b35 [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
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)
Logan Chien83426162011-12-09 09:29:50 +080052 // TODO: Add options to JNI_CreateJavaVM() and dex2oat, so that we don't
53 // have to hard-code the path.
Shih-wei Liaod1fec812012-02-13 09:51:10 -080054 WriteBitcodeToFile("/tmp/art_llvm_module.bc");
55#endif
56}
57
58
59void CompilerLLVM::WriteBitcodeToFile(std::string const &filepath) {
60 std::string error_msg;
61
62 // Write the translated bitcode
63 llvm::OwningPtr<llvm::tool_output_file>
64 out(new llvm::tool_output_file(filepath.c_str(), error_msg,
65 llvm::raw_fd_ostream::F_Binary));
66
67 if (!error_msg.empty()) {
68 LOG(FATAL) << "Unable to open file: " << error_msg;
69 return;
70 }
71
72 llvm::WriteBitcodeToFile(module_, out->os());
73 out->keep();
74
75 LOG(DEBUG) << "Bitcode Written At: " << filepath;
76}
77
78
79CompiledMethod*
80CompilerLLVM::CompileDexMethod(DexFile::CodeItem const* code_item,
81 uint32_t access_flags,
82 uint32_t method_idx,
83 ClassLoader const* class_loader,
84 DexFile const& dex_file) {
85
86 MutexLock GUARD(compiler_lock_);
87
88 ClassLinker *class_linker = Runtime::Current()->GetClassLinker();
89 DexCache *dex_cache = class_linker->FindDexCache(dex_file);
90
91 UniquePtr<MethodCompiler> method_compiler(
92 new MethodCompiler(insn_set_, compiler_, class_linker, class_loader,
93 &dex_file, dex_cache, code_item, method_idx,
94 access_flags));
95
96 return method_compiler->Compile();
97}
Logan Chien83426162011-12-09 09:29:50 +080098
99
100} // namespace compiler_llvm
101} // namespace art